SpringCloud使用Feign實現(xiàn)動態(tài)路由操作
一、理解及原理
1.1理解
Feign
基于接口 + 注解的方式,一個http請求調(diào)用的輕量級框架
Feign是Netflix開發(fā)的聲明式、模板化的HTTP客戶端, Feign可以幫助我們更快捷、優(yōu)雅地調(diào)用HTTP API。
Feign是一種聲明式、模板化的HTTP客戶端(僅在Application Client中使用)。聲明式調(diào)用是指,就像調(diào)用本地方法一樣調(diào)用遠程方法,無需感知操作遠程http請求

1.2原理

二、Feign搭建實現(xiàn)步驟
- 創(chuàng)建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來實現(xiàn)遠程服務調(diào)用時,需要提供一個本地接口來繼承服務標準工程提供的服務接口。這個本地接口不需要給予任何實現(xiàn),在底層Spring容器會為這個接口提供一個基于JDK實現(xiàn)的代理對象,這個代理對象由Feign技術提供具體的HandlerInterceptor邏輯,實現(xiàn)遠程的調(diào)用。實現(xiàn)過程類似通過代碼調(diào)用LoadBalancerClient實現(xiàn)的Rest遠程訪問。
而本地接口繼承服務標準接口后,需要提供注解@FeignClient,注解的屬性name代表當前接口要調(diào)用的遠程服務的應用命名。

@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實現(xiàn)動態(tài)路由操作的文章就介紹到這了,更多相關Feign動態(tài)路由內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
詳解Java分布式系統(tǒng)中session一致性問題
這篇文章主要介紹了Java分布式系統(tǒng)中session一致性問題,對分布式系統(tǒng)感興趣的同學,要仔細看一下2021-04-04
spring mvc常用注解_動力節(jié)點Java學院整理
這篇文章主要介紹了spring mvc常用注解,詳細的介紹了@RequestMapping, @RequestParam, @ModelAttribute等等這樣類似的注解,有興趣的可以了解一下2017-08-08
詳解Java中字符串緩沖區(qū)StringBuffer類的使用
StringBuffer與String類似,只不過StringBuffer在進行字符串處理時不生成新的對象,下面我們就來詳解Java中字符串緩沖區(qū)StringBuffer類的使用:2016-06-06

