-
Feign配置
#超时重试配置
HELLO-SERVICE.ribbon.ConnectTimeout=250
HELLO-SERVICE.ribbon.ReadTimeout=1000
HELLO-SERVICE.ribbon.OkToRetryOnAllOperations=true
HELLO-SERVICE.ribbon.MaxAutoRetriesNextServer=2
HELLO-SERVICE.ribbon.MaxAutoRetries=1
#压缩配置
#spring-configuration-metadata.json 在这个文件中查看默认的配置值
feign.compression.request.enabled=true
feign.compression.request.min-request-size=2048
feign.compression.request.mime-types=text/xml,application/xml,application/json
#是否开启feign中的hystrix熔断功能
feign.hystrix.enabled=false
#关闭熔断功能
hystrix.command.default.execution.timeout.enabled=true
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=1000
hystrix.command.hello.execution.isolation.thread.timeoutInMilliseconds=5000
注意和hystrix
的timeoutInMilliseconds
关系
-
Feign的用法
- 利用注解客户端
@FeignClient(name="HELLO-SERVICE", fallback = HelloServiceFallback.class)
public interface HelloService {
@RequestMapping("/hello")
String hello();
@RequestMapping(value = "/hello1", method = RequestMethod.GET)
String hello(@RequestParam("name") String name) ;
@RequestMapping(value = "/hello2", method = RequestMethod.GET)
User hello(@RequestHeader("name") String name, @RequestHeader("age") Integer age);
@RequestMapping(value = "/hello3", method = RequestMethod.POST)
String hello(@RequestBody User user);
}
其中HelloServiceFallback
为服务降级类
@Component
public class HelloServiceFallback implements HelloService {
@Override
public String hello() {
return "error";
}
@Override
public String hello(@RequestParam("name") String name) {
return "error";
}
@Override
public User hello(@RequestHeader("name") String name, @RequestHeader("age") Integer age) {
return new User("未知", 0);
}
@Override
public String hello(@RequestBody User user) {
return "error";
}
}
另:Feign有继承的特性
@RestController
public class RefactorHelloController implements HelloService {
-
日志的配置
- 设置全局的日志配置
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class ConsumerApplication {
@Bean
Logger.Level feignLoggerLevel() {
return Logger.Level.FULL;
}
- 具体配置
logging.level.com.didispace.web.HelloService=DEBUG
-
各种坑
- 只要参数是复杂对象,即使指定了GET方法,feign依然会以post方法进行发送请求
- 不支持多层继承,报
Only single-level inheritance supported
- 奇葩的FeignClient问题
- @PathVariable not working with feign ,要写全 @PathVariable 里面的param值
- eureka里面的/info 端口必须连接的上,否则会报连接超时等错误.
ClientAbortException: java.io.IOException: APR error: -730053
网友评论