05.SpringCloud OpenFeign (服务接口调用)


1.概述

OpenFeign是什么

    Feign是一个声明式的web服务客户端,让编写web服务客户端变得容易,只需创建一个接口并在接口上添加注解即可 https://github.com/spring-cloud/spring-cloud-openfeign  

能干嘛

 

Feign和OpenFeign两者区别

   

2.OpenFeign使用步骤

核心:接口+注解 微服务调用接口+@FeignClient   新建cloud-consumer-feign-order80 Feign在消费端使用   pom
<?xml version="1.0" encoding="UTF-8"?>

    
        springcloud2020
        com.chl.springcloud
        1.0-SNAPSHOT
    
    4.0.0

    cloud-consumer-feign-order80

    
    
        
        
            org.springframework.cloud
            spring-cloud-starter-openfeign
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
            com.chl.springcloud
            cloud-api-commons
            1.0-SNAPSHOT
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-actuator
        

        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        

        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    


yml
server:
  port: 80
eureka:
  client:
    register-with-eureka: false
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka, http://eureka7002.com:7002/eureka
主启动类
@SpringBootApplication
@EnableFeignClients //开启openfeign的功能
public class OrderFeignMain80 {
    public static void main(String[] args) {
        SpringApplication.run(OrderFeignMain80.class,args);
    }
}
业务类 业务逻辑接口+@FeignClient配置调用provider服务 新建PaymentFeignService接口并新增注解@FeignClient
@Component
@FeignClient("CLOUD-PAYMENT-SERVICE") //对应的提供方的服务名
public interface PaymentFeignService {

    //要调用提供方的对应接口url  用调用本地的service方式调用 远端的服务
    @GetMapping("/payment/get/{id}")
    public CommonResult get(@PathVariable("id")Long id);
}
控制层Controller
@RestController
@Slf4j
public class OrderFeignController {

    @Autowired
    private PaymentFeignService paymentFeignService;

    @GetMapping("/consumer/payment/get/{id}")
    public CommonResult get(@PathVariable("id")Long id){
       return paymentFeignService.get(id);
    }
}
测试 先启动2个eureka集群7001/7002 再启动2个微服务8001/8002 启动OpenFeign order80启动 http://localhost/consumer/payment/get/1   Feign自带负载均衡配置项   小总结  

3.OpenFeign超时控制

超时设置,故意设置超时演示出错情况

1.服务提供方8001故意写暂停程序 PaymentController新加测试方法
  /**
     * 故意写的服务提供方吃力业务要3秒,用于测试feign的超时问题
     * @return
     */
    @GetMapping(value = "/payment/feign/timeout")
    public String paymentFeignTimeout(){
        try { TimeUnit.SECONDS.sleep(3); }catch (Exception e) {e.printStackTrace();}
        return serverPort;
    }
2.服务消费方80添加超时方法PaymentFeignService
//测试feigin的超时问题
    @GetMapping(value = "/payment/feign/timeout")
    public String paymentFeignTimeout();
  3.服务消费方80添加超时方法OrderFeignController
/**
     * 测试feigin的超时问题
     * @return
     */
    @GetMapping(value = "/consumer/payment/feign/timeout")
    public String paymentFeignTimeout(){
        return paymentFeignService.paymentFeignTimeout();
    }
4.测试 http://localhost/consumer/payment/feign/timeout

原因说明

OpenFeign默认等待一秒钟,超过后报错、  

OpenFeign默认支持Ribbon

 

超时设置

ribbon:
  ReadTimeout:  5000 #请求连接的超时时间 5秒
  ConnectTimeout: 5000 #请求处理的超时时间 5秒
 

4.OpenFeign日志打印功能

日志打印功能

是什么

日志级别

配置日志bean

在消费端添加配置bean
@Configuration
public class FeignConfig {

    @Bean
    Logger.Level feignLoggerLevel(){
        return Logger.Level.FULL;
    }
}

YML文件里需要开启日志的Feign客户端

logging:
  level:
    com.chl.springcloud.service.PaymentFeignService: debug
  测试 先启动2个eureka集群7001/7002 再启动2个微服务8001/8002 启动OpenFeign order80启动 http://localhost/consumer/payment/get/1   测试结果  



来自为知笔记(Wiz)