zoukankan      html  css  js  c++  java
  • Spring Cloud:Open Feign基础知识

    一.Open Feign

    Feign是一个声明式Webservice客户端,使用Feign能让编写Web Service更加简单。
    它的使用方法是定义一个服务接口然后在上面添加注解。Feign也可以支持可拔插式的编码器和解码器。Spring Cloud对Feign进行了封装,使其支持Spring MVC标准注解和HttpMessageConverter。Feign可以与Eureka和Ribbon组合使用以支持负载均衡。
    Feign与Open Feign的区别:

    二.Open Feign的基本使用

    2.1 引入依赖(部分)

            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-openfeign</artifactId>
            </dependency>
    

    我们能观察到,OpenFeign引入了Ribbon,所以OpenFeign本身就自带了Ribbon的负载均衡功能。

    2.2 yml配置

    server:
      port: 80
    
    eureka:
      client:
        #是否将自己注册到Eureka Server 默认为true
        register-with-eureka: false
        #是否从EurekaServer抓取已有的注册信息,默认为true,单节点无所谓,集群必须设置true才能配合ribbon做负载均衡
        service-url:
          #设置eureka server交互的地址查询服务和注册服务都需要依赖这个地址
          #defaultZone: http://localhost:7001/eureka
          defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
    

    2.3 springboot主启动类

    添加@EnableFeignClients注解

    @EnableFeignClients
    @SpringBootApplication
    public class OrderFeignMain80 {
        public static void main(String[] args) {
            SpringApplication.run(OrderFeignMain80.class,args);
        }
    }
    

    2.4 业务类

    2.4.1 业务逻辑接口

    添加一个Service接口,接口标注@FeignClient,值为需要调用的微服务名称。

    @Component
    @FeignClient(value = "CLOUD-PAYMENT-SERVICE")
    public interface PaymentFeignService {
    
        @GetMapping("/payment/get/{id}")
        public CommonResult<Payment> getPaymentById(@PathVariable Long id);
    }
    

    2.4.2 控制层

    @RestController
    public class OrderFeignController {
        @Autowired
        private PaymentFeignService paymentFeignService;
    
        @GetMapping("/payment/get/{id}")
        public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id){
            return  paymentFeignService.getPaymentById(id);
    
        }
    }
    

    调用流程:

    三.Open Feign的超时控制

    Open Feign的客户端默认只等一秒钟。
    这里同一个CLOUD-PAYMENT-SERVICE微服务有两个服务提供方,一个占用8001,一个占用8002,现在先给8001的服务睡上三秒。

    @RestController
    @Slf4j
    public class PaymentController {
    
        @Resource
        private PaymentService paymentService;
    
        @Value("${server.port}")
        private String serverPort;
    
        @GetMapping("/payment/get/{id}")
        public CommonResult<Payment> getPaymentById(@PathVariable Long id){
            Payment payment = paymentService.getPaymentById(id);
            timeout();
            if(payment!=null){
                return new CommonResult<>(200,"查询成功+"+serverPort,payment);
            }else{
                return new CommonResult<>(444,"查询失败+"+serverPort,null);
            }
    
        }
    
        private void timeout(){
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    
    }
    

    然后由客户端调用该微服务,发现返回的数据一直由8002提供。
    然后我们进行配置ribbon的超时时间:

    ribbon:
      #连接建立所用超时时间
      ReadTimeout: 5000
      #建立连接后,读取到可用资源的超时时间
      ConnectTimeout: 5000
    

    再次调用微服务,发现8001,8002轮流提供服务。

    四 Open Feign日志增强

    Feign提供了日志打印功能,我们可以通过配置,来调整日志级别,从而了解Feign中http请求细节。
    日志级别:

    • NONE 默认,不显示任何日志
    • BASIC 仅记录请求方法、URL、响应码及响应时间
    • HEADERS 除了BASIC定义的信息外,还有请求和响应的头信息。
    • FULL 除了HEADERS中的信息外,还有请求和相应的正文及元数据。
      配置类:
    @Configuration
    public class FeignConfig {
    
        @Bean
        public Logger.Level feignLoggerLevel(){
            return Logger.Level.FULL;
        }
    }
    

    配置yml

    logging:
      level:
        #feign日志以什么日志级别监控哪个接口
        com.wj.springcloud.service.PaymentFeignService: debug
    

    运行效果:

  • 相关阅读:
    【题解】[USACO08DEC-Gold] Trick or Treat on the Farm
    【题解】[NOIP2015-TG] 信息传递
    【题解】[JLOI2011] 飞行路线
    平衡树
    斜率优化 dp 总结
    题解【P1833 樱花】
    题解【CodeForces 910A The Way to Home】
    三角恒等变换公式
    题解【洛谷 P1246 编码】
    生成函数(母函数)详解
  • 原文地址:https://www.cnblogs.com/wwjj4811/p/13619245.html
Copyright © 2011-2022 走看看