SpringCloud微服务 之Feign(二-Customize)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012437781/article/details/83474887

前言

上一节 我们学习了如何在SpringCloud微服务架构需下使用Feign(所有Feign的功能使用的是默认配置)来完成一个典型的服务消费者与服务提供者间的通信(基于Http Protocol)。同时我们也需要了解一下几点,SpringCloud对Fegin的封装后

  • Feign使用的默认配置

    • Decoder feignDecoder:ResponseEntityDecoder(其中包含SpringDecoder。
    • Encoder feignEncoder:SpringEncoder。
    • Logger feignLogger:Slf4jLogger。
    • Contract feignContract:SpringMvcContract:MVC模型的Contract,上一小节我们就是用的是Feign的SpringMvcContract,这是SpringCloud对Feign封装之后的扩展,其实Feign有自己的MVC Contract。
    • Feign.Builder feignBuilder:HystrixFeign.Builder。
    • Client feignClient:如果Ribbon启用,则为LoadBalancerFeignClient,否则将使用默认的feign客户端。
  • Feign的非默认配置(但仍能从Spring application Context中获取)

    • Logger.Level:服务节点的日志级别。
    • Retryer:服务调用是的重试机制。
    • ErrorDecoder:服务调用出现Error时的编码解释与机制。
    • Request.Options
    • Collection < RequestInterceptor>
    • SetterFactory

下面我们将通过一个较为简单的案例来学习一下如何自定义FeignClient。

案例

  • Eureka Server端编写:(参考前例)。
    在这里插入图片描述

  • Eureka Client端服务提供方编写(参考前例)。

    • 项目结构
      在这里插入图片描述

    • CoreCode

      @SpringBootApplication
      @EnableDiscoveryClient
      @EnableFeignClients //开启FeignClient注解
      public class MicroserviceDealBrokerFeignCustomizedApplication {
      
      	public static void main(String[] args) {
      		SpringApplication.run(MicroserviceDealBrokerFeignCustomizedApplication.class,
      				args);
      	}
      }
      
      
      @Configuration
      public class FeignConfiguration {
      	/**
      	 * Feign默认使用的是SpringMVC的contract并支持所有SpringMVC contract支持的注解
      	 * Feign默认使用的是Feign自己封装的feignContract
      	 * (MVCcontract)若是用Feign的Contract则自定义的Feign interface中需要使用Feign自己的mvc
      	 * contract
      	 * 
      	 * @return
      	 */
      	@Bean
      	public Contract feignContract() {
      		return new feign.Contract.Default();
      	}
      	
      	/**
      	 * Feign的Logger输出级别
      	 *@return
      	 */
      	@Bean
      	Logger.Level feignLoggerLevel() {
      		return Logger.Level.FULL;
      	}
      }
      
      @FeignClient(name = "microservice-deal-cloud",configuration=FeignConfiguration.class)
      public interface FeignClientInterface {
      	
      	/**
      	 * 使用Feign自己的mvc Contract,它与SpringMVC Contract有一些不一样,参考下例子
      	 * 1、对Http请求的注解有增强封装:@RequestLine VS @RequestMap/@Getmapping
      	 * 2、@Param VS @Pathvariable
      	 * 3、使用Feign自己的 mvc contract我们可以自定义对Http增强的处理,比如使用@Headers声明Content-Type为 json
      	 * 更多参考:https://github.com/OpenFeign/feign
      	 * @param id
      	 * @return
      	 */
      	@Headers("Content-Type: application/json")
      	@RequestLine("GET /deal/{id}")
      	public Deal findById(@Param(value="id") Long id);
      }
      
      
      @RestController
      public class BrokerController {
      	
      	@Autowired
      	private FeignClientInterface feignClientInterface;
      	
      	@GetMapping("/deal/{id}")
      	public Deal findById(@PathVariable(value="id") Long id) {
      		return this.feignClientInterface.findById(id);
      	}
      }
      
      server:
        port: 8082
      spring:
        application:
          name: microservice-deal-broker-cloud-feign
      eureka:
        client:
          serviceUrl:
            defaultZone: http://localhost:8080/eureka/
        instance:
          prefer-ip-address: true
      
      #使用Feign时必须添加以下两项配置
      ribbon:
        eureka:
          enabled: true
      
      #设置feign的hystrix响应超时时间(必须)
      hystrix:
        command:
            default:
              execution:
                isolation:
                  thread:
                    timeoutInMilliseconds: 5000
                    
      logging:
        level:
          com.example.deal.client.feign.FeignClientInterface: DEBUG #pringCloud封装的Feign目前只支持DEBUG级别的日志输出
      
  • Eureka Client端服务提消费编写(单实例) (参考前例)。

  • 访问:http://localhost:8082/deal/2:
    在这里插入图片描述

小结

  • Feign有自己的源生的MVC Contract(参考本节服务消费者中的FeignClientInterface.java中的注解),经过SpringCloud的高度封装之后,在SpringCloud微服务架构下Feign的默认MVC contract是SpringMVC的contract。

  • 自定义FeignClient我们可以对Feign的默认配置和扩展功能做自定义,如本小节的案例中对Feign的default contract做了自定义,同理我们也可以自定义其他默认配置和非默认配置,参考这里。 在自定义的同时也要在FeignClientInterface中声明自定义的声明类,参考FeignClientInterface.java

  • 对于一些业务场景个人建议使用Feign自己源生的配置和扩展功能以避免出现各种坑的现象。比如在MVC Contract的定义上建议使用Feign源生的Contract。

  • Feign Logger的自定义需要在Feign的自定义配置配中对输出日志做声明:Logger.Level feignLoggerLevel(),同时在配置文件中配置logging:level:com.example.deal.client.feign.FeignClientInterface: DEBUG。

  • 本小节用到的案例:microservice-deal-eureka、microservice-deal-cloud、microservice-deal-broker-cloud-feign-cusomized。

猜你喜欢

转载自blog.csdn.net/u012437781/article/details/83474887