首頁 > 軟體

SpringCloud使用Feign實現動態路由操作

2022-06-02 22:00:12

一、理解及原理

1.1理解

Feign
基於介面 + 註解的方式,一個http請求呼叫的輕量級框架

Feign是Netflix開發的宣告式、模板化的HTTP使用者端, Feign可以幫助我們更快捷、優雅地呼叫HTTP API。

Feign是一種宣告式、模板化的HTTP使用者端(僅在Application Client中使用)。宣告式呼叫是指,就像呼叫本地方法一樣呼叫遠端方法,無需感知操作遠端http請求

1.2原理

二、Feign搭建實現步驟

  • 建立Springboot基礎專案
  • 在註冊中心(Eureka)設定的基礎上,進行設定Feign

三、組態檔(pom.xml)

基礎設定:

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

整體:

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.30</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<!-- Swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.8.7</version>
</dependency>
<!--新增lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>20.0</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>19.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--pagehelper分頁-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.11</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.11</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>

<!--驗證碼https://blog.csdn.net/qq_41853447/article/details/105893567-->
<dependency>
<groupId>com.github.whvcse</groupId>
<artifactId>easy-captcha</artifactId>
<version>1.6.2</version>
</dependency>
<!--security許可權管理-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

</dependencies>

四、程式程式碼

在啟動類上加上@EnableFeignClients,開啟Feign的應用

@EnableEurekaServer
@EnableSwagger2
@SpringBootApplication
@EnableFeignClients(basePackages = "com.personal.pserver")
public class PserverApplication {

public static void main(String[] args) {
SpringApplication.run(PserverApplication.class, args);
System.out.println("========================person-server已啟動========================");
}

}

啟動類新增完成之後,在指定需要存取的service註冊使用,

見其他博主講解:

在通過Feign來實現遠端服務呼叫時,需要提供一個本地介面來繼承服務標準工程提供的服務介面。這個本地介面不需要給予任何實現,在底層Spring容器會為這個介面提供一個基於JDK實現的代理物件,這個代理物件由Feign技術提供具體的HandlerInterceptor邏輯,實現遠端的呼叫。實現過程類似通過程式碼呼叫LoadBalancerClient實現的Rest遠端存取。
  而本地介面繼承服務標準介面後,需要提供註解@FeignClient,註解的屬性name代表當前介面要呼叫的遠端服務的應用命名。

@RestController
@Api(tags = "平臺基本資訊管理")
@RequestMapping("/v1/pserver/platform/manager")
public class PlatformController {

@Autowired
private PPlatformService platformService;

@ApiOperation(value = "獲取平臺基本資訊", notes = "獲取平臺基本資訊", httpMethod = "GET")
@RequestMapping(value = "/findPlatformInfo",method = RequestMethod.GET)
public PPlatform findPlatformInfo(@RequestParam("platformId") String platformId) {
PPlatform pPlatform = platformService.findOnePlatformById(platformId);
return pPlatform;
}
}
@FeignClient(name="p-platform-service")
public interface PPlatformService {

/***
* 獲取平臺基本資訊
* @param platformId
* @return
*/
@RequestMapping(value="/findPlatformInfo",method = RequestMethod.GET)
PPlatform findOnePlatformById(@RequestParam(value="platformId") String platformId);

}

五、結果演示

到此這篇關於SpringCloud使用Feign實現動態路由操作的文章就介紹到這了,更多相關Feign動態路由內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com