1、概述
-
OpenFeign是什么
Feign是一个声明式的Web服务客户端,让编写Web服务客户端变得非常容易,只需 创建一个接口并在接口上添加注解即可
Feign和OpenFeign两者区别
2、OpenFeign使用步骤
-
接口+注解
微服务调用接口+@FeignClient
-
新建cloud-consumer-feign-order80
Feign在消费端使用
-
POM
<dependencies> <!--openfeign--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <!--eureka client--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependency> <groupId>com.atguigu.springcloud</groupId> <artifactId>cloud-api-common</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--监控--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!--热部署--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
-
YML
server: port: 80 eureka: client: register-with-eureka: false # 检索服务选项,当设置为True(默认值)时,会进行服务检索,注册中心不负责检索服务。 # fetch-registry: true service-url: defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
-
主启动
@SpringBootApplication //@EnableEurekaClient @EnableFeignClients public class OrderFeignMain80 { public static void main(String[] args) { SpringApplication.run(OrderFeignMain80.class, args); } }
-
业务类
1、业务逻辑接口+@FeignClient配置调用provider服务
新建PaymentFeignService接口并新增注解@FeignClient
@Component @FeignClient(value = "CLOUD-PROVIDER-SERVICE") public interface PaymentFeignService { @PostMapping(value = "/payment/create") CommonResult<Payment> create(@RequestBody Payment payment); @GetMapping(value = "/payment/get/{id}") CommonResult<Payment> getPaymentById(@PathVariable("id") Long id); }
2、控制层Controller
@RestController @Slf4j public class OrderFeignController { @Resource private PaymentFeignService paymentFeignService; @PostMapping(value = "/consumer/payment/create") public CommonResult<Payment> create(@RequestBody Payment payment){ return paymentFeignService.create(payment); } @GetMapping(value = "/consumer/payment/get/{id}") public CommonResult getPaymentById(@PathVariable("id") Long id) { return paymentFeignService.getPaymentById(id); } }
-
测试
Feign自带负载均衡配置项
- 小结
3、OpenFeign超时控制
-
超时设置,故意设置超时演示出错情况
1、服务提供方8001故意写暂停程序
PaymentController
@GetMapping(value = "/payment/feign/timeout") public String paymentFeignTimeout(){ try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); } return serverPort; }
2、服务消费方80添加超时方法
PaymentFeignService
@GetMapping(value = "/payment/feign/timeout") String paymentFeignTimeout();
3、服务消费方80添加超时方法
OrderFeignController
@GetMapping(value = "/consumer/payment/feign/timeout") public String paymentFeignTimeout(){ return paymentFeignService.paymentFeignTimeout(); }
4、报错
http://localhost/consumer/payment/feign/timeout 延迟访问
http://localhost:8001/payment/feign/timeout报错
- OpenFeign默认等待1秒钟,超过后报错
OpenFeign默认支持Ribbon
YML文件里需要开启OpenFeign客户端超时控制
ribbon:
# 指的是建立连接所用的时间,适用于网络状态正常的情况下,两端连接所用的时间
ReadTimeout: 5000
# 指的是建立连接后从服务器读取到可用资源所用的时间
ConnectTimeout: 5000
4、OpenFeign日志打印功能
- 日志级别
-
配置日志been
@Configuration public class FeignConfig { /** * feignClient配置日志级别 * * @return */ @Bean public Logger.Level feignLoggerLevel() { // 请求和响应的头信息,请求和响应的正文及元数据 return Logger.Level.FULL; } }
-
YML文件里需要开启日志的Feign客户端
logging:
level:
# feign日志以什么级别监控哪个接口
com.atguigu.springcloud.service.PaymentFeignService: debug
- 后台日志查看