SpringCloud Feign使用二

本文介绍了SpringCloud Feign的高级配置方法,包括如何使用Feign原生注解、配置日志级别、实现负载均衡、启用请求响应压缩、自定义Eureka请求配置等。此外还探讨了Hystrix的支持配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SpringCloud Feign使用二

更多干货


一使用原生Feign注解

  • Feign默认使用的是 SpringMvc的注解,如果要使用Feign原生注解配置如下
@Configuration
public class Configuration1 {
  @Bean
  public Contract feignContract() {
    return new feign.Contract.Default();
  }


  //改变Feign 日志级别
  @Bean
  Logger.Level feignLoggerLevel() {
    return Logger.Level.FULL;
  }
}
  • 指定使用配置Configuration1
import feign.Param;
import feign.RequestLine;

@FeignClient(name = "microservice-provider-user", configuration = Configuration1.class)
public interface UserFeignClient {
  @RequestLine("GET /simple/{id}")
  public User findById(@Param("id") Long id);
}

编写接口查询eureka接口信息

  • 指定eureka 的帐号信息
@Configuration
public class Configuration2 {
  @Bean
  public BasicAuthRequestInterceptor basicAuthRequestInterceptor() {
    return new BasicAuthRequestInterceptor("user", "password123");
  }
}

  • 可以指定请求的eureka 的 url
  • 指定配置文件 Configuration2
@FeignClient(name = "xxxx", url = "http://localhost:8761/", configuration = Configuration2.class)
public interface FeignClient2 {
  @RequestMapping(value = "/eureka/apps/{serviceName}")
  public String findServiceInfoFromEurekaByServiceName(@PathVariable("serviceName") String serviceName);
}

日志级别

logging:
  level:
    com.itmuch.cloud.feign.UserFeignClient: DEBUG

Feign 加入Ribbon 负载均衡

加入以下配置

microservice-provider-user:
  ribbon:
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule

Feign支持请求和响应的压缩

  • 增加gzip配置
#请求和响应GZIP压缩支持
feign.compression.request.enabled=true
feign.compression.response.enabled=true

histrix

如果没有用到histrix则关闭

#Hystrix支持,如果为true,hystrix库必须在classpath中
feign.hystrix.enabled=false

可修改融短器的超时时间

  • 修改长超时时间
  • timeout设置为false
  • 禁用掉hystrix
# hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 5000
# 或者:
# hystrix.command.default.execution.timeout.enabled: false
# 或者:
feign.hystrix.enabled: false ## 索性禁用feign的hystrix支持

# 超时的issue:https://github.com/spring-cloud/spring-cloud-netflix/issues/768
# 超时的解决方案: http://stackoverflow.com/questions/27375557/hystrix-command-fails-with-timed-out-and-no-fallback-available
# hystrix配置: https://github.com/Netflix/Hystrix/wiki/Configuration#execution.isolation.thread.timeoutInMilliseconds
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值