Spring Cloud Feign高级应用实例详解
这篇文章主要介绍了SpringCloudFeign高级应用实例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
1.使用feign进行服务间的调用
Springboot2XConsul如何使用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类
packagecom.xyz.comsumer.configure; importfeign.Logger; importorg.springframework.context.annotation.Bean; importorg.springframework.context.annotation.Configuration; @Configuration publicclassFeignLogConfig{ @Bean Logger.LevelfeignLogger(){ returnLogger.Level.FULL; } }
输出
2019-12-0723:23:03.630DEBUG2668---[vice-provider-1]c.xyz.comsumer.feign.RemoteHelloService:[RemoteHelloService#hello]<---HTTP/1.1200(671ms) 2019-12-0723:23:03.631DEBUG2668---[vice-provider-1]c.xyz.comsumer.feign.RemoteHelloService:[RemoteHelloService#hello]content-length:14 2019-12-0723:23:03.631DEBUG2668---[vice-provider-1]c.xyz.comsumer.feign.RemoteHelloService:[RemoteHelloService#hello]content-type:text/plain;charset=UTF-8 2019-12-0723:23:03.631DEBUG2668---[vice-provider-1]c.xyz.comsumer.feign.RemoteHelloService:[RemoteHelloService#hello]date:Sat,07Dec201915:23:03GMT 2019-12-0723:23:03.631DEBUG2668---[vice-provider-1]c.xyz.comsumer.feign.RemoteHelloService:[RemoteHelloService#hello]vary:Origin 2019-12-0723:23:03.631DEBUG2668---[vice-provider-1]c.xyz.comsumer.feign.RemoteHelloService:[RemoteHelloService#hello]vary:Access-Control-Request-Method 2019-12-0723:23:03.631DEBUG2668---[vice-provider-1]c.xyz.comsumer.feign.RemoteHelloService:[RemoteHelloService#hello]vary:Access-Control-Request-Headers 2019-12-0723:23:03.631DEBUG2668---[vice-provider-1]c.xyz.comsumer.feign.RemoteHelloService:[RemoteHelloService#hello] 2019-12-0723:23:03.632DEBUG2668---[vice-provider-1]c.xyz.comsumer.feign.RemoteHelloService:[RemoteHelloService#hello]hello,provider 2019-12-0723:23:03.632DEBUG2668---[vice-provider-1]c.xyz.comsumer.feign.RemoteHelloService:[RemoteHelloService#hello]<---ENDHTTP(14-bytebody)
说明:
Feign日志记录只能响应DEBUG日志级别
对每一个Feign客户端,可以配置一个Logger.Level对象,通过该对象控制日志输出内容。
Logger.Level有如下几种选择:
NONE,不记录日志(默认)。
BASIC,只记录请求方法和URL以及响应状态代码和执行时间。
HEADERS,记录请求和应答的头的基本信息。
FULL,记录请求和响应的头信息,正文和元数据。
4.替换JDK默认的URLConnection为okhttp
在默认情况下springcloudfeign在进行各个子服务之间的调用时,http组件使用的是jdk的HttpURLConnection
服务之间调用使用的HttpURLConnection,效率非常低
为了提高效率,可以通过连接池提高效率
使用okhttp,能提高qps,因为okhttp有连接池和超时时间进行调优
添加依赖
io.github.openfeign feign-okhttp
修改配置
禁用默认的http,启用okhttp
feign.okhttp.enabled=true feign.httpclient.enabled=false
添加FeignOkHttpConfig类
packagecom.xyz.comsumer.configure; importfeign.Feign; importokhttp3.ConnectionPool; importorg.springframework.beans.factory.annotation.Autowired; importorg.springframework.boot.autoconfigure.AutoConfigureBefore; importorg.springframework.boot.autoconfigure.condition.ConditionalOnClass; importorg.springframework.cloud.openfeign.FeignAutoConfiguration; importorg.springframework.context.annotation.Bean; importorg.springframework.context.annotation.Configuration; importjava.util.concurrent.TimeUnit; @Configuration @ConditionalOnClass(Feign.class) @AutoConfigureBefore(FeignAutoConfiguration.class) publicclassFeignOkHttpConfig{ @Bean publicokhttp3.OkHttpClientokHttpClient(){ returnnewokhttp3.OkHttpClient.Builder() .readTimeout(10,TimeUnit.SECONDS) .connectTimeout(10,TimeUnit.SECONDS) .writeTimeout(20,TimeUnit.SECONDS) .retryOnConnectionFailure(true) .connectionPool(newConnectionPool()) .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
packagecom.xyz.comsumer.feign.fallback; importcom.xyz.comsumer.feign.RemoteHelloService; importorg.slf4j.Logger; importorg.slf4j.LoggerFactory; importorg.springframework.stereotype.Component; @Component publicclassRemoteHelloServiceFallbackImplimplementsRemoteHelloService{ privatefinalLoggerlogger=LoggerFactory.getLogger(RemoteHelloServiceFallbackImpl.class); @Override publicStringhello(){ logger.error("feign查询信息失败:{}"); returnnull; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。