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

一、理解及原理

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動態路由內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: