1.Spring Cloud Feign簡介
(1).Fegin簡介
官方檔案:http://projects.spring.io/spring-cloud/spring-cloud.html#spring-cloud-feign

Feign是一個宣告式WebService客戶端,使用Feign能讓撰寫Web Service客戶端更加簡單, 它的使用方法是定義一個介面,然后在上面添加注解,同時也支持JAX-RS標準的注解,Feign也支持可拔插式的編碼器和解碼器,Spring Cloud對Feign進行了封裝,使其支持了Spring MVC標準注解和HttpMessageConverters,Feign可以與Eureka和Ribbon組合使用以支持負載均衡,
Feign是一個宣告式的Web服務客戶端,使得撰寫Web服務客戶端變得非常容易,
只需要創建一個介面,然后在上面添加注解即可,
官方檔案:https://github.com/OpenFeign/feign
(2).Feign作用
Feign旨在使撰寫Java Http客戶端變得更容易,前面在使用Ribbon+RestTemplate時,利用RestTemplate對http請求的封裝處理,形成了一套模版化的呼叫方法,但是在實際開發中,由于對服務依賴的呼叫可能不止一處,往往一個介面會被多處呼叫,所以通常都會針對每個微服務自行封裝一些客戶端類來包裝這些依賴服務的呼叫,所以,Feign在此基礎上做了進一步封裝,由他來幫助我們定義和實作依賴服務介面的定義,在Feign的實作下,我們只需創建一個介面并使用注解的方式來配置它(以前是Dao介面上面標注Mapper注解,現在是一個微服務介面上面標注一個Feign注解即可),即可完成對服務提供方的介面系結,簡化了使用Spring cloud Ribbon時,自動封裝服務呼叫客戶端的開發量,
(3).Feign集成Ribbon
利用Ribbon維護了MicroServiceCloud-Dept的服務串列資訊,并且通過輪詢實作了客戶端的負載均衡,而與Ribbon不同的是,通過feign只需要定義服務系結介面且以宣告式的方法,優雅而簡單的實作了服務呼叫,
2.Feign實作步驟

(1).創建工程
[1].新建工程feign
新建microservicecloud-consumer-dept-feign


將consumer里的com包和application.yml組態檔復制到新創建的feign里

修改主啟動類名

[2].pom檔案
配置microservicecloud-consumer-dept-feign的pom檔案
|
<?xml version="1.0" encoding="UTF-8"?> <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"> <parent> <artifactId>microservicecloud</artifactId> <groupId>com.hosystem</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion>
<artifactId>microservicecloud-consumer-dept-feign</artifactId>
<dependencies> <dependency><!-- 自己定義的api --> <!--注意:groupId我們需要由com.hosystem.springcloud 變成com.hosystem就不會出現'Dependency 'com.hosystem.springcloud:microservicecloud-api:1.0-SNAPSHOT' not found'錯誤--> <groupId>com.hosystem</groupId> <artifactId>microservicecloud-api</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 修改后立即生效,熱部署 --> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency>
<!-- Ribbon相關 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency>
<!-- feign相關 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency> </dependencies>
</project> |
(2).修改microservicecloud-api工程

[1].修改pom檔案
新增部分:
|
<!--feign相關--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency> </dependencies> |
完整部分:
|
<?xml version="1.0" encoding="UTF-8"?> <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"> <!-- 子類里面顯示宣告才能有明確的繼承表現,無意外就是父類的默認版本否則自己定義 --> <parent> <artifactId>microservicecloud</artifactId> <groupId>com.hosystem</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion>
<!--當前Module叫什么名字 --> <artifactId>microservicecloud-api</artifactId>
<!-- 當前Module需要用到的jar包,按自己需求添加,如果父類已經包含了,可以不用寫版本號 --> <dependencies> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency>
<!--feign相關--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency> </dependencies>
</project> |
[2].創建介面
新建DeptClientService介面并新增注解@FeignClient,
|
package com.hosystem.springcloud.service;
import com.hosystem.springcloud.entities.Dept; import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod;
import java.util.List;
@FeignClient(value = "https://www.cnblogs.com/HOsystem/archive/2020/12/09/MICROSERVICECLOUD-DEPT") public interface DeptClientService { @RequestMapping(value = "https://www.cnblogs.com/dept/get/{id}",method = RequestMethod.GET) public Dept get(@PathVariable("id") long id);
@RequestMapping(value = "https://www.cnblogs.com/dept/list",method = RequestMethod.GET) public List<Dept> list();
@RequestMapping(value = "https://www.cnblogs.com/dept/add",method = RequestMethod.POST) public boolean add(Dept dept); } |
[3].mvn clean和mvn install

(3).修改microservicecloud-consumer-dept-feign
microservicecloud-consumer-dept-feign工程修改Controller,添加上一步新建的DeptClientService介面
|
package com.hosystem.springcloud.controller;
import com.hosystem.springcloud.entities.Dept; import com.hosystem.springcloud.service.DeptClientService; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource; import java.util.List;
@RestController //注:這里一定不能忘記注解@RestController 否則出現404; public class DeptController_Consumer {
//注:如果出現could not autowired no beans of 'DeptClientService' type found. 我們只需要將@autowired改成@Resource即可 @Resource private DeptClientService service;
@RequestMapping(value = "https://www.cnblogs.com/consumer/dept/get/{id}") public Dept get(@PathVariable("id") Long id) { return this.service.get(id); }
@RequestMapping(value = "https://www.cnblogs.com/consumer/dept/list") public List<Dept> list() { return this.service.list(); }
@RequestMapping(value = "https://www.cnblogs.com/consumer/dept/add") public Object add(Dept dept) { return this.service.add(dept); } } |
(4).修改主啟動類
修改microservicecloud-consumer-dept-feign主啟動類
|
package com.hosystem.springcloud;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.feign.EnableFeignClients; import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication @EnableEurekaClient @EnableFeignClients(basePackages= {"com.hosystem.springcloud"}) @ComponentScan("com.hosystem.springcloud") public class DeptConsumer80_Feign_App { public static void main(String[] args) { SpringApplication.run(DeptConsumer80_Feign_App.class, args); } } |
(5).測驗
[1].啟動eureka集群
[2].啟動provider8001、provider8002、provider8003
[3].啟動Feign
[4].訪問頁面
Feign自帶負載均衡配置項
|
http://localhost/consumer/dept/list |

(6).總結
Feign通過介面的方法呼叫Rest服務(之前是Ribbon+RestTemplate),該請求發送給Eureka服務器(http://MICROSERVICECLOUD-DEPT/dept/list),通過Feign直接找到服務介面,由于在進行服務呼叫的時候融合了Ribbon技術,所以也支持負載均衡作用,
參考檔案:
http://projects.spring.io/spring-cloud/spring-cloud.html#spring-cloud-feign
https://github.com/OpenFeign/feign
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/231787.html
標籤:其他
上一篇:Java-原生IO通覽
下一篇:BCB6.0呼叫vc元件



