SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(4)-- OpenFeign服务接口调用

男娘i 2022-12-29 04:45 224阅读 0赞

上一篇:SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(3)— Ribbon负载均衡服务调用

一、概述

1、OpenFeign是什么

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

2、能干嘛

在这里插入图片描述

3、Feign和OpenFeign两者区别

在这里插入图片描述

二、OpenFeign使用步骤

接口+注解:微服务调用接口+@FeignClient

1、新建cloud-consumer-feign-order80

Feign在消费端使用
在这里插入图片描述

2、POM

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <parent>
  4. <artifactId>cloud2020</artifactId>
  5. <groupId>com.atguigu.springcloud</groupId>
  6. <version>1.0-SNAPSHOT</version>
  7. </parent>
  8. <modelVersion>4.0.0</modelVersion>
  9. <artifactId>cloud-consumer-feign-order80</artifactId>
  10. <!--openfeign-->
  11. <dependencies>
  12. <dependency>
  13. <groupId>org.springframework.cloud</groupId>
  14. <artifactId>spring-cloud-starter-openfeign</artifactId>
  15. </dependency>
  16. <dependency>
  17. <groupId>org.springframework.cloud</groupId>
  18. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  19. </dependency>
  20. <dependency>
  21. <groupId>com.atguigu.springcloud</groupId>
  22. <artifactId>cloud-api-common</artifactId>
  23. <version>${project.version}</version>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-starter-web</artifactId>
  28. </dependency>
  29. <dependency>
  30. <groupId>org.springframework.boot</groupId>
  31. <artifactId>spring-boot-starter-actuator</artifactId>
  32. </dependency>
  33. <dependency>
  34. <groupId>org.springframework.boot</groupId>
  35. <artifactId>spring-boot-devtools</artifactId>
  36. <scope>runtime</scope>
  37. <optional>true</optional>
  38. </dependency>
  39. <dependency>
  40. <groupId>org.projectlombok</groupId>
  41. <artifactId>lombok</artifactId>
  42. <optional>true</optional>
  43. </dependency>
  44. <dependency>
  45. <groupId>org.springframework.boot</groupId>
  46. <artifactId>spring-boot-starter-test</artifactId>
  47. <scope>test</scope>
  48. </dependency>
  49. </dependencies>
  50. </project>

3、YAML

  1. server:
  2. port: 80
  3. eureka:
  4. client:
  5. register-with-eureka: false
  6. service-url:
  7. defaultZone: http://eureka7001.com:7001/eureka, http://eureka7002.com:7002/eureka

4、主启动类

  1. package com.atguigu.springcloud;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.openfeign.EnableFeignClients;
  5. @SpringBootApplication
  6. @EnableFeignClients
  7. public class OrderFeignMain80 {
  8. public static void main(String[] args) {
  9. SpringApplication.run(OrderFeignMain80.class,args);
  10. }
  11. }

5、业务类

①、业务逻辑接口+@FeignClient配置调用provider服务
②、新建PaymentFeignService接口并新增注解@FeignClient
  1. package com.atguigu.springcloud.service;
  2. import com.atguigu.springcloud.entities.CommonResult;
  3. import com.atguigu.springcloud.entities.Payment;
  4. import feign.Param;
  5. import org.springframework.cloud.openfeign.FeignClient;
  6. import org.springframework.stereotype.Component;
  7. import org.springframework.web.bind.annotation.GetMapping;
  8. import org.springframework.web.bind.annotation.PathVariable;
  9. @Component
  10. @FeignClient(value = "CLOUD-PAYMENT-SERVICE")
  11. public interface PaymentFeignService {
  12. @GetMapping(value = "/payment/get/{id}")
  13. public CommonResult getPaymentById(@PathVariable("id") Long id);
  14. }
③、控制层Controller
  1. package com.atguigu.springcloud.controller;
  2. import com.atguigu.springcloud.entities.CommonResult;
  3. import com.atguigu.springcloud.entities.Payment;
  4. import com.atguigu.springcloud.service.PaymentFeignService;
  5. import org.springframework.web.bind.annotation.GetMapping;
  6. import org.springframework.web.bind.annotation.PathVariable;
  7. import org.springframework.web.bind.annotation.RestController;
  8. import javax.annotation.Resource;
  9. @RestController
  10. public class OrderFeignController {
  11. @Resource
  12. private PaymentFeignService paymentFeignService;
  13. @GetMapping(value = "/consumer/payment/get/{id}")
  14. public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id){
  15. return paymentFeignService.getPaymentById(id);
  16. }
  17. }

6、测试

在这里插入图片描述
在这里插入图片描述

7、小总结

在这里插入图片描述

三、OpenFeign超时控制

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

①、服务提供方8001故意写暂停程序
  1. @GetMapping(value = "/payment/feign/timeout")
  2. public String paymentFeignTimeout(){
  3. try { TimeUnit.SECONDS.sleep(3); }catch (Exception e) { e.printStackTrace();}
  4. return serverPort;
  5. }
②、服务消费方80添加超时方法PaymentFeignService
  1. @GetMapping(value = "/payment/feign/timeout")
  2. public String paymentFeignTimeout();
③、服务消费方80添加超时方法OrderFeignController
  1. @GetMapping(value = "/consumer/payment/feign/timeout")
  2. public String paymentFeignTimeout(){
  3. return paymentFeignService.paymentFeignTimeout();
  4. }
④、测试

http://localhost/consumer/payment/feign/timeout
在这里插入图片描述

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

3、是什么

在这里插入图片描述
OpenFeign默认支持Ribbon
在这里插入图片描述

4、YML文件里需要开启OpenFeign客户端超时控制

  1. ribbon:
  2. ReadTimeout: 5000
  3. ConnectTimeout: 5000

四、OpenFeign日志打印功能

1、是什么

在这里插入图片描述

2、日志级别

在这里插入图片描述

3、配置日志bean

  1. package com.atguigu.springcloud.config;
  2. import feign.Logger;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. @Configuration
  6. public class FeignConfig {
  7. @Bean
  8. Logger.Level feignLoggerLevel(){
  9. return Logger.Level.FULL;
  10. }
  11. }

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

  1. logging:
  2. level:
  3. com.atguigu.springcloud.service.PaymentFeignService: debug

5、后台日志查看

下一篇:SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(5)— Gateway新一代网关

发表评论

表情:
评论列表 (有 0 条评论,224人围观)

还没有评论,来说两句吧...

相关阅读