美文网首页
SpringCloud Feign

SpringCloud Feign

作者: 巨子联盟 | 来源:发表于2018-01-29 23:00 被阅读0次
    • 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
    

    注意和hystrixtimeoutInMilliseconds关系

    • Feign的用法

    1. 利用注解客户端
    
    @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 {
    
    • 日志的配置

    1. 设置全局的日志配置
    @EnableFeignClients
    @EnableDiscoveryClient
    @SpringBootApplication
    public class ConsumerApplication {
    
        @Bean
        Logger.Level feignLoggerLevel() {
            return Logger.Level.FULL;
        }
    
    1. 具体配置
    logging.level.com.didispace.web.HelloService=DEBUG
    
    • 各种坑

    1. 只要参数是复杂对象,即使指定了GET方法,feign依然会以post方法进行发送请求
    2. 不支持多层继承,报 Only single-level inheritance supported
    3. 奇葩的FeignClient问题
    4. @PathVariable not working with feign ,要写全 @PathVariable 里面的param值
    5. eureka里面的/info 端口必须连接的上,否则会报连接超时等错误.
    6. ClientAbortException: java.io.IOException: APR error: -730053

    相关文章

      网友评论

          本文标题:SpringCloud Feign

          本文链接:https://www.haomeiwen.com/subject/jbpxzxtx.html