Feign的配置

Ribbon的配置

1 全部配置

ribbon.<key>=<value>

ribbon.ConnectTimeout=500  //客户端的连接超时设置为500毫秒

ribbon.ReadTimeout=500  //客户端的读取超时设置为500毫秒

 

2 指定服务配置,client是@FeignClient(name="hello-service")的hello-service,使用@FeignClient来创建Feign客户端的时候,同时也创建了一个名为hello-service的ribbon的客户端。

<client>.ribbon.<key>=<value>

hello-service.ribbon.ConnectTimeout=500

3 Feign默认实现了重试机制,重试的配置 <client>.ribbon.<key>=<value>  配置如下

hello-service:
  ribbon:
    #配置单个节点重试最大值
    MaxAutoRetries: 1
    #配置更换节点数最大值
    MaxAutoRetriesNextServer: 2
    #链接超时时间
    ConnectTimeout: 500
    #请求处理时间
    ReadTimeout: 2000
    #每个操作都开启重试机制
    OkToRetryOnAllOperations: true
 

注意:Ribbon的超时和Hystrix的超时是两个概念,Hystrix超时应该大于Ribbon的超时,否则Hystrix超时后,该命令直接熔断,重试机制没任何意义。

Hystrix的配置

全局配置,比如设置全局的超时时间5000毫秒

 hystrix.command.defalut.execution.isolation.thread.timeoutInMilliseconds=5000

 

指定命令配置

hystrix.command.<commandKey>作为前缀,默认是采用Feign的客户端的方法名字作为标识

hystrix.command.hello.execution.isolation.thread.timeoutInMilliseconds=5000

 

服务降级配置

 

1 降级的实现逻辑只需要为Feign客户端编写一个具体的接口实现类

 

@Component
public class HelloServiceFallback implements HelloService {

    @Override
    public String hello() {
         
        return "error hello()";
    }

    @Override
    public String  hello3(@RequestParam("name") String name )  {
         
        return "error hello3(String name ) ";
    }

    @Override
    public User  hello5(@RequestHeader("name") String name,@RequestHeader("age") int  age ){
        return new User("hello5",0);
    }

    @Override
    public String  hello6(@RequestBody User user ){
         
        return "error hello6(  User user )";
    }

    @Override
    public User  hello7(@PathVariable("name") String name,@PathVariable("age") int age){
         
              return new User("hello7",0);
    }

}

 

 2 在服务绑定接口HelloService中,通过@FeignClient注解的fallback属性来指定对应的服务降级实现类

@FeignClient(name="hello-service",fallback=HelloServiceFallback.class)
public interface HelloService {
    
@RequestMapping("/hello")
String hello();

@RequestMapping("/hello3")
public String  hello3(@RequestParam("name") String name )  ;

@RequestMapping("/hello5")
public User  hello5(@RequestHeader("name") String name,@RequestHeader("age") int  age )  ;

@RequestMapping("/hello6")
public String  hello6(@RequestBody User user )  ;

@RequestMapping("/hello7/{name}/{age}")
public User  hello7(@PathVariable("name") String name,@PathVariable("age") int age) ;
}

3 验证

  关于服务端的应用,访问http://127.0.0.1:9001/feign-consumer地址,会直接触发服务降级,返回服务降级实现类的内容。

日志配置

Spring  Cloud Feign在构建@FeignClient注解修饰的服务客户端的时候,会为每一个客户端都创建feign.Logger 实例,我们可以利用该日志对象DEBUG模式来帮助分析Feign的请求细节。

1 <FeignClient>的参数配置格式来开启指定Feign客户端的DEBUG日志,FeignClient是Feign客户端定义的接口完整路径

logging:
  level:
       feign.consumer.feignconsumer: DEBUG

2 实现配置类,来指定Feign客户端来实现不同的日志级别

@Configuration
public class FeignLogConfiguration {
  @Bean
  Logger.Level feignLoggerLevel() {
    return Logger.Level.FULL;
  }
}

也可以针对全局的日志级别,所有Feign客户端都用这个日志级别

@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class FeignConsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run(FeignConsumerApplication.class, args);
    }

      @Bean
      Logger.Level feignLoggerLevel() {
        return Logger.Level.FULL;
      }
}
 

 

3 Feign客户端引用该日志配置

@FeignClient(name="hello-service",fallback=HelloServiceFallback.class,configuration=FeignLogConfiguration.class)
public interface HelloService {
    
@RequestMapping("/hello")
String hello();

。。。。。

}

 

 

4 验证,访问http://127.0.0.1:9001/feign-consumer,可以看到控制台打印结果如下,有请求的详细日志

 

Feign的Looger级别有下面4类

1 None:不记录任何信息

2 BASIC:仅记录请求方法,URL,响应状态码和执行时间

3 HEADERS:除了记录BASIC级别的信息之外,还会记录头信息

4 FULL:记录请求和相应的所有明细

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用Feign进行服务之间的调用时,可以通过配置开启Hystrix的熔断功能。如果服务不可用或者超过访问时间,就会触发Hystrix的fallback回调函数。要开启熔断配置,需要在服务消费者的pom文件中添加Hystrix的依赖。然后创建Feign的实现类,实现Feign中的方法,并在Feign接口的@FeignClient注解中加上fallback属性,值是Feign实现类的字节码文件。在主启动类上加上@EnableHystrix注解来允许Hystrix的使用。在配置文件中设置开启熔断功能,可以通过feign.hystrix.enabled=true来开启Feign的熔断功能。\[1\] Feign中的Hystrix配置如下: ``` feign: hystrix: enabled: true okhttp: enabled: true httpclient: enabled: false client: config: default: # 超时时间配置 connectTimeout: 10000 readTimeout: 10000 compression: request: enabled: true response: enabled: true # Hystrix配置 hystrix: command: default: execution: isolation: strategy: SEMAPHORE thread: timeoutInMilliseconds: 60000 shareSecurityContext: true ``` 以上是Feign中Hystrix的配置,可以根据实际需求进行相应的调整。\[3\] #### 引用[.reference_title] - *1* *3* [Spring Cloud Feign熔断配置](https://blog.csdn.net/Diandikongji/article/details/112747687)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [feign的熔断](https://blog.csdn.net/weixin_45893072/article/details/122972939)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值