Spring Cloud Feign高级应用实例详解
作者:yaominghui 发布时间:2021-04-23 07:12:36
这篇文章主要介绍了Spring Cloud Feign高级应用实例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
1.使用feign进行服务间的调用
Spring boot2X Consul如何使用Feign实现服务调用
2.开启gzip压缩
Feign支持对请求与响应的压缩,以提高通信效率,需要在服务消费者配置文件开启压缩支持和压缩文件的类型
添加配置
feign.compression.request.enabled=true
feign.compression.response.enabled=true
feign.compression.request.mime-types=text/xml,application/xml,application/json
feign.compression.request.min-request-size=2048
3.开启日志
配置
logging.level.com.xyz.comsumer.feign.RemoteHelloService=debug
添加FeignLogConfig类
package com.xyz.comsumer.configure;
import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FeignLogConfig {
@Bean
Logger.Level feignLogger(){
return Logger.Level.FULL;
}
}
输出
2019-12-07 23:23:03.630 DEBUG 2668 --- [vice-provider-1] c.xyz.comsumer.feign.RemoteHelloService : [RemoteHelloService#hello] <--- HTTP/1.1 200 (671ms)
2019-12-07 23:23:03.631 DEBUG 2668 --- [vice-provider-1] c.xyz.comsumer.feign.RemoteHelloService : [RemoteHelloService#hello] content-length: 14
2019-12-07 23:23:03.631 DEBUG 2668 --- [vice-provider-1] c.xyz.comsumer.feign.RemoteHelloService : [RemoteHelloService#hello] content-type: text/plain;charset=UTF-8
2019-12-07 23:23:03.631 DEBUG 2668 --- [vice-provider-1] c.xyz.comsumer.feign.RemoteHelloService : [RemoteHelloService#hello] date: Sat, 07 Dec 2019 15:23:03 GMT
2019-12-07 23:23:03.631 DEBUG 2668 --- [vice-provider-1] c.xyz.comsumer.feign.RemoteHelloService : [RemoteHelloService#hello] vary: Origin
2019-12-07 23:23:03.631 DEBUG 2668 --- [vice-provider-1] c.xyz.comsumer.feign.RemoteHelloService : [RemoteHelloService#hello] vary: Access-Control-Request-Method
2019-12-07 23:23:03.631 DEBUG 2668 --- [vice-provider-1] c.xyz.comsumer.feign.RemoteHelloService : [RemoteHelloService#hello] vary: Access-Control-Request-Headers
2019-12-07 23:23:03.631 DEBUG 2668 --- [vice-provider-1] c.xyz.comsumer.feign.RemoteHelloService : [RemoteHelloService#hello]
2019-12-07 23:23:03.632 DEBUG 2668 --- [vice-provider-1] c.xyz.comsumer.feign.RemoteHelloService : [RemoteHelloService#hello] hello,provider
2019-12-07 23:23:03.632 DEBUG 2668 --- [vice-provider-1] c.xyz.comsumer.feign.RemoteHelloService : [RemoteHelloService#hello] <--- END HTTP (14-byte body)
说明:
Feign日志记录只能响应DEBUG日志级别
对每一个Feign客户端,可以配置一个Logger.Level对象,通过该对象控制日志输出内容。
Logger.Level有如下几种选择:
NONE, 不记录日志 (默认)。
BASIC, 只记录请求方法和URL以及响应状态代码和执行时间。
HEADERS, 记录请求和应答的头的基本信息。
FULL, 记录请求和响应的头信息,正文和元数据。
4.替换JDK默认的URLConnection为okhttp
在默认情况下 spring cloud feign在进行各个子服务之间的调用时,http组件使用的是jdk的HttpURLConnection
服务之间调用使用的HttpURLConnection,效率非常低
为了提高效率,可以通过连接池提高效率
使用okhttp,能提高qps,因为okhttp有连接池和超时时间进行调优
添加依赖
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-okhttp</artifactId>
</dependency>
修改配置
禁用默认的http,启用okhttp
feign.okhttp.enabled=true
feign.httpclient.enabled=false
添加FeignOkHttpConfig类
package com.xyz.comsumer.configure;
import feign.Feign;
import okhttp3.ConnectionPool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.cloud.openfeign.FeignAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.concurrent.TimeUnit;
@Configuration
@ConditionalOnClass(Feign.class)
@AutoConfigureBefore(FeignAutoConfiguration.class)
public class FeignOkHttpConfig {
@Bean
public okhttp3.OkHttpClient okHttpClient(){
return new okhttp3.OkHttpClient.Builder()
.readTimeout(10, TimeUnit.SECONDS)
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(20, TimeUnit.SECONDS)
.retryOnConnectionFailure(true)
.connectionPool(new ConnectionPool())
.build();
}
}
5.超时设置
Feign调用服务的默认时长是1秒钟
hystrix的超时时间
hystrix.command.default.execution.timeout.enabled=true
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=10000
说明:
hystrix.command.default.execution.timeout.enabled 执行是否启用超时,默认启用true
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds 命令执行超时时间,默认1000ms
常用的配置还有
hystrix.command.default.execution.isolation.strategy 隔离策略,默认是Thread, 可选THREAD|SEMAPHORE,建议选择SEMAPHORE
Feign 的负载均衡底层用的就是 Ribbon
ribbon的超时时间
ribbon.ReadTimeout=10000
ribbon.ConnectTimeout=10000
6.使用hystrix进行熔断、降级处理
feign启用hystrix,才能熔断、降级
feign.hystrix.enabled=true
hystrix服务降级处理
RemoteHelloServiceFallbackImpl
package com.xyz.comsumer.feign.fallback;
import com.xyz.comsumer.feign.RemoteHelloService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
@Component
public class RemoteHelloServiceFallbackImpl implements RemoteHelloService {
private final Logger logger = LoggerFactory.getLogger(RemoteHelloServiceFallbackImpl.class);
@Override
public String hello() {
logger.error("feign 查询信息失败:{}");
return null;
}
}
来源:https://www.cnblogs.com/baby123/p/12000316.html


猜你喜欢
- 代码如下:<% '/* 函数名称:Zxj_ReplaceHtmlClearHtml '/
- ImageDataGenerator的参数自己看文档from keras.preprocessing import imageimport
- 在这种配置下我们要实现关键词不区分大小写搜索并高亮显示要借助ASP的正则处理了,请看下面代码:<% Function&nbs
- 有一个比较有意思的传参方式:比如在 demo1.py 中指定 action='store_true'的时候:parser.a
- 我看见朋友可以把数据库的记录输出到页面表格上去,觉得很有用。这是怎么做的啊?见下:dbtable.asp<html><he
- python-docx库可用于创建和编辑Microsoft Word(.docx)文件。官方文档:链接地址备注:doc是微软的专有的文件格式
- 目录什么是预处理?那么预处理有啥好处?Go实现 MySQL 的事务sqlx使用gin + mysql + rest full api&nbs
- 昨天对其配置了一天,其配置为Jena 2.4.0,MySQL数据库版本为5.1.42-community,JDK版本为1.6.0,MySQL
- 前言:👉对于新手来说,库的安装是遇到的第一个挑战,我也入了很多坑,所以想出一期安装库的步骤,由于博主水平限制,博客难免会有错误和不准之处,我
- 通过学习借鉴朋友的实现方法进行整理,实现了PHP版的微信公共平台消息主动推送,分享给大家供大家参考,具体内容如下此方法是通过模拟登录微信公共
- 阅读对象:知道什么是restful,有了解swagger或者openAPI更佳。1.什么是restfulRepresentional Sta
- 1.需求说明记录一下项目对用户 UGC 文本进行字数限制的具体实现。不同的产品,出于种种原因,一般都会对用户输入的文本内容做字数限制。出于产
- 并发是一个很酷的话题,一旦你掌握了它,就会成为一笔巨大的财富。说实话,我一开始很害怕写这篇文章,因为我自己直到最近才对并发性不太适应。我已经
- 我曾以为,写脚本是很难的,直到我遇到了Python前言随着国内版权意识的跟进,很多影视音乐资源开始收费,而且度盘又经常随意封杀各种资源,所以
- 本文以修改用户名密码单元为案例,编写测试脚本。完成修改用户名密码模块单元测试。(ps.这个demo中登陆密码为“admin”)1. 打开浏览
- 一般来说,我们为了得到更完整的结果,我们需要从两个或更多的表中获取结果,我一般都是用select xxx,xxx from 表1,表2 wh
- 说明:通过随机产生密码,然后将密码EMail给注册用户,你可以确认用户的EMail填写是否正确。自动产生的密码往往安全性更高,同时,你可以过
- selenium操作chrome浏览器需要有ChromeDriver驱动来协助。webdriver中关浏览器关闭有两个方法,一个叫quit,
- 前言今天学习Django框架,用ajax向后台发送post请求,直接报了403错误,说CSRF验证失败;先前用模板的话都是在里面加一个 {%
- 通过无能的baidu逛了一圈,发现有两三段能用的代码,不过参考之下,发现还有不足的:不能拷贝有合并格式的sheet、没有拷贝cell的相关格