Spring Cloud Feign高级应用

摘要:
Logger.Level有如下几种选择:NONE,不记录日志(默认)。BASIC,只记录请求方法和URL以及响应状态代码和执行时间。FULL,记录请求和响应的头信息,正文和元数据。

1.使用feign进行服务间的调用

spring boot2X整合nacos一使用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
public classFeignLogConfig {
    @Bean
    Logger.Level feignLogger(){
        returnLogger.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 classFeignOkHttpConfig {
    @Bean
    publicokhttp3.OkHttpClient okHttpClient(){
        return newokhttp3.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
public class RemoteHelloServiceFallbackImpl implementsRemoteHelloService {
    private final Logger logger = LoggerFactory.getLogger(RemoteHelloServiceFallbackImpl.class);
    @Override
    publicString hello() {
        logger.error("feign 查询信息失败:{}");
        return null;
    }
}

免责声明:文章转载自《Spring Cloud Feign高级应用》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇生成 (web): 找不到目标 .NET Framework 版本的引用程序集;请确保已安装这些程序集或选择有效的目标版本。用LaTeX写线性规划下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Spring cloud 超时及重试配置【ribbon及其它http client】

开启重试在某些情况下是有问题的,比如当压力过大,一个实例停止响应时,路由将流量转到另一个实例,很有可能导致最终所有的实例全被压垮。说到底,断路器的其中一个作用就是防止故障或者压力扩散。用了retry,断路器就只有在该服务的所有实例都无法运作的情况下才能起作用。这种时候,断路器的形式更像是提供一种友好的错误信息,或者假装服务正常运行的假象给使用者。 不用re...

服务熔断Hystrix的替换方案Sentinel

1 服务熔断Hystrix的替换方案 1.1 概述 2018年底Netflix公司宣布Hystrix已经足够稳定,不再积极开发Hystrix,该项目处于维护模式。就目前来看Hystrix是比较稳定的,并且Hystrix只是停止开发新的版本,并不是完全停止维护,Bug什么的依然会维护。因此,短期内,Hystrix依然是能继续使用的。但是从长远看,Hystri...

网关的超时层级

网关的超时层级 zuul zuul: max: host: connections: 500 host: socket-timeout-millis: 60000 connect-timeout-millis: 60000 ribbon ribbon: ReadTimeout: 10000 Connect...

详解Spring Cloud中Hystrix 线程隔离导致ThreadLocal数据丢失

在Spring Cloud中我们用Hystrix来实现断路器,Zuul中默认是用信号量(Hystrix默认是线程)来进行隔离的,我们可以通过配置使用线程方式隔离。 在使用线程隔离的时候,有个问题是必须要解决的,那就是在某些业务场景下通过ThreadLocal来在线程里传递数据,用信号量是没问题的,从请求进来,但后续的流程都是通一个线程。 当隔离模式为线程时...

SpringCloud Feign 之 Fallback初体验

SpringCloud Feign 之 Fallback初体验 在微服务框架SpringCloud中,Feign是其中非常重要且常用的组件。Feign是声明式,模板化的HTTP客户端,可以帮助我们更方便快捷调用HTTP API。本文主要针对Feign的熔断机制Fallback进行简单介绍。Fallback主要是用来解决依赖的服务不可用或者调用服务失败或超时...

SpringCloud学习笔记(5):Hystrix Dashboard可视化监控数据

简介 上篇文章中讲了使用Hystrix实现容错,除此之外,Hystrix还提供了近乎实时的监控。本文将介绍如何进行服务监控以及使用Hystrix Dashboard来让监控数据图形化。 项目介绍 sc-parent,父模块(请参照SpringCloud学习笔记(1):Eureka注册中心) sc-eureka,注册中心(请参照SpringCloud学习笔...