【SpringCloud】OpenFeign服務調用(八)


OpenFeign介紹

  前面在使用Ribbon+RestTemplate時,利用RestTemplate對http請求的封裝處理,形成了一套模版化的調用方法。但是在實際開發中,對於服務依賴的調用可能不止一處,往往一個接口會被多處調用。所有Feign在此基礎上做了進一步封裝,由他來幫助我們定義和實現依賴服務接口的定義。在Feign的實現下,我們只需創建一個接口並使用注解的方式來配置它(以前是Dao接口上面標注Mapper注解,現在是一個微服務接口上面標注一個Feign注解即可),集可完成對服務提供方的接口綁定,簡化了使用Spring Cloud Ribbon時,自動封裝服務調用客戶端的開發量。

  Feign旨在式編寫Java Http客戶端變得更容易。

  Feign集成了Ribbon,利用Ribbon維護了服務列表信息,並且通過輪詢實現了客戶端的負載均衡。而與Ribbon不同的是,通過feign只需要定義服務綁定接口且以聲明式的方法,優雅而簡單的實現了服務調用

  git地址:https://github.com/spring-cloud/spring-cloud-openfeign

  Feign與OpenFeign的區別

  1)Feign是Spring Cloud組件中一個輕量級RESTful的HTTP服務客戶端,Feign內置了Ribbon,用來做客戶端負載均衡,去調用服務注冊中心的服務。Feign的使用方式是:使用Feign的注解定義接口,調用接口,就可以調用服務注冊中心的服務

    Feign的依賴

1 <dependency>
2     <groupId>org.springframework.cloud</groupId>
3     <artifactId>spring-cloud-starter-feign</artifactId>
4 </dependency>

  2)OpenFeign是Spring Cloud在Feign的基礎上支持了SpringMVC的注解,如@RequestMapping等等。OpenFeign的@FeignClient可以解析SpringMVC的@RequestMapping注解下的接口,並通過動態代理的方式產生實現類,實現類中

    OpenFeign的依賴

1 <dependency>
2     <groupId>org.springframework.cloud</groupId>
3     <artifactId>spring-cloud-starter-openfeign</artifactId>
4 </dependency>  

OpenFeign使用

  項目架構如下:

  1個Eureka注冊中心,2個服務提供者,1個服務消費者

  

  1、新建項目,參考【SpringCloud】服務提供者集群與服務發現Discovery(三) 

  2、新建一個服務消費者模塊(test-springcloud-order7997)的maven工程

  3、修改pom文件,引入OpenFeign依賴,完整pom文件如下:

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

    Feign集成了Ribbon:

    

  4、修改配置文件,application.yml

 1 # 端口
 2 server:
 3   port: 7997
 4 
 5 spring:
 6   application:
 7     name: cloud-order
 8 
 9 eureka:
10   client:
11     # 設置與Eureka Server交互的地址查詢服務和注冊服務都需要依賴這個地址
12     service-url:
13       defaultZone: http://localhost:8761/eureka
14   instance:
15     #  instance:
16     instance-id: ${spring.cloud.client.ip-address}:${server.port}
17     # 訪問路徑可以顯示IP地址
18     prefer-ip-address: true

  4、編寫SpringBoot啟動類,並使用@EnableFeignClients注解,開始Feign客戶端

1 // 開啟Feign客戶端
2 @EnableFeignClients
3 @SpringBootApplication
4 public class OrderMain7997 {
5     public static void main(String[] args) {
6         SpringApplication.run(OrderMain7997.class, args);
7     }
8 }

  5、編寫接口,使用@FeignClient("CLOUD-PAYMENT-SERVICE")注解,表名此接口做為一個feign客戶端,並調用CLOUD-PAYMENT-SERVICE服務。

1 @Component
2 // Feign客戶端
3 @FeignClient("CLOUD-PAYMENT-SERVICE")
4 public interface PaymentFeignService {
5     // 獲取
6     @GetMapping("/payment/get/{id}")
7     public CommonResult getPaymentById(@PathVariable("id") Long id)
8

  6、編寫Controller,使用接口調用服務

 1 @RestController
 2 @Slf4j
 3 public class OrderFeignController {
 4 
 5     @Autowired
 6     private PaymentFeignService paymentFeignService;
 7 
 8     @GetMapping("/consumer/payment/get/{id}")
 9     public CommonResult getPaymentById(@PathVariable("id") Long id) {
10         return paymentFeignService.getPaymentById(id);
11     }
12 }

  7、啟動項目,測試

    訪問地址:http://localhost:7997/consumer/payment/get/1

    結果:

      正常遠程調用服務提供者的服務,且達到負載均衡的目的

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM