美文网首页
spring cloud之feign

spring cloud之feign

作者: dancer4code | 来源:发表于2020-10-24 10:43 被阅读0次

    1.引入依赖

     <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
            </dependency>
            <!--这个依赖 -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-openfeign</artifactId>
            </dependency>
             <dependency>
                 <groupId>org.springframework.retry</groupId>
                 <artifactId>spring-retry</artifactId>
             </dependency>
             <dependency>
                 <groupId>org.springframework.cloud</groupId>
                 <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
             </dependency>
        
    

    2.配置

    server:
      port: 6063
    
    spring:
      application:
        name: consumer-feign
      cloud:
        loadbalancer:
          retry:
            enabled: true # 开启Spring Cloud的重试功能
      main:
        allow-bean-definition-overriding: true
    logging:
      level:
        com.d4c: debug
    
    eureka:
      instance:
        prefer-ip-address: true
      client:
        register-with-eureka: true
        fetch-registry: true
        service-url:
          defaultZone: http://peer1:6001/eureka/,http://peer2:6002/eureka/
    
    feign:
      hystrix:
        enabled: true # 开启Feign的熔断功能
      compression:
        request:
          enabled: true # 开启请求压缩
          mime-types: text/html,application/xml,application/json # 设置压缩的数据类型
          min-request-size: 2048 # 设置触发压缩的大小下限
        response:
          enabled: true # 开启响应压缩
    
    ribbon:
      ConnectTimeout: 250 # Ribbon的连接超时时间
      ReadTimeout: 1000 # Ribbon的数据读取超时时间
      OkToRetryOnAllOperations: true # 是否对所有操作都进行重试
      MaxAutoRetriesNextServer: 1 # 切换实例的重试次数
      MaxAutoRetries: 1 # 对当前实例的重试次数
    
    hystrix:
      command:
        default:
          execution:
            isolation:
              thread:
                timeoutInMilliseconds: 6000 # 设置hystrix的超时时间为6000ms
    

    3.相关类

    启动类

    @SpringBootApplication
    @EnableDiscoveryClient
    @EnableHystrix//或者@EnableCircuitBreaker
    @EnableFeignClients
    public class ConsumerFeignApplication {
        public static void main(String[] args) {
            SpringApplication.run(ConsumerFeignApplication.class, args);
        }
    }
    
    

    FeignClient接口

    @FeignClient(value = "account-demo",fallback = AccountClientFallback.class)
    public interface AccountClient {
    
        @GetMapping("account/get/server/{id}")
        String queryAccountById(@PathVariable Long id);
    
        @GetMapping("account/get/{id}")
        String queryAccountByIdTwo(@PathVariable Long id);
    
    }
    

    FeignClient接口实现类

    @Component
    public class AccountClientFallback implements AccountClient {
        @Override
        public String queryAccountById(Long id) {
            return "I am fallback!";
        }
    
        @Override
        public String queryAccountByIdTwo(Long id) {
            return "I am fallback two!";
        }
    }
    

    service类

    @Service
    public class AccountConsumerService {
    
    
        @Autowired
        private AccountClient accountClient;
    
    
        public String queryAccountById(Long id) {
            String s = accountClient.queryAccountById(id);
            return s;
        }
    
        public String queryAccountByIdTwo(Long id) {
            String s = accountClient.queryAccountByIdTwo(id);
            return s;
        }
    }
    

    controller类

    @RestController
    @RequestMapping("account")
    public class AccountConsumerController {
    
        @Resource
        private AccountConsumerService accountConsumerService;
    
        @RequestMapping("/{id}")
        public String queryAccountById(@PathVariable Long id){
            return accountConsumerService.queryAccountById(id);
        }
    
        @RequestMapping("/two/{id}")
        public String queryAccountByIdTwo(@PathVariable Long id){
            return accountConsumerService.queryAccountByIdTwo(id);
        }
    
    }
    

    feign自定的日志类

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

    这里指定的Level级别是FULL,Feign支持4种级别:

    • NONE:不记录任何日志信息,这是默认值。

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

    • HEADERS:在BASIC的基础上,额外记录了请求和响应的头信息

    • FULL:记录所有请求和响应的明细,包括头信息、请求体、元数据。

    相关文章

      网友评论

          本文标题:spring cloud之feign

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