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

1.2原理

二、Feign搭建實(shí)現(xiàn)步驟
- 創(chuàng)建Springboot基礎(chǔ)項(xiàng)目
- 在注冊(cè)中心(Eureka)配置的基礎(chǔ)上,進(jìn)行配置Feign
三、配置文件(pom.xml)
基礎(chǔ)配置:
<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> <!--驗(yàn)證碼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權(quán)限管理--> <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>
四、程序代碼
在啟動(dòng)類上加上@EnableFeignClients,開啟Feign的應(yīng)用
@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已啟動(dòng)========================");
}
}啟動(dòng)類添加完成之后,在指定需要訪問的service注冊(cè)使用,
見其他博主講解:
在通過Feign來實(shí)現(xiàn)遠(yuǎn)程服務(wù)調(diào)用時(shí),需要提供一個(gè)本地接口來繼承服務(wù)標(biāo)準(zhǔn)工程提供的服務(wù)接口。這個(gè)本地接口不需要給予任何實(shí)現(xiàn),在底層Spring容器會(huì)為這個(gè)接口提供一個(gè)基于JDK實(shí)現(xiàn)的代理對(duì)象,這個(gè)代理對(duì)象由Feign技術(shù)提供具體的HandlerInterceptor邏輯,實(shí)現(xiàn)遠(yuǎn)程的調(diào)用。實(shí)現(xiàn)過程類似通過代碼調(diào)用LoadBalancerClient實(shí)現(xiàn)的Rest遠(yuǎn)程訪問。
而本地接口繼承服務(wù)標(biāo)準(zhǔn)接口后,需要提供注解@FeignClient,注解的屬性name代表當(dāng)前接口要調(diào)用的遠(yuǎn)程服務(wù)的應(yīng)用命名。

@RestController
@Api(tags = "平臺(tái)基本信息管理")
@RequestMapping("/v1/pserver/platform/manager")
public class PlatformController {
@Autowired
private PPlatformService platformService;
@ApiOperation(value = "獲取平臺(tái)基本信息", notes = "獲取平臺(tái)基本信息", 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 {
/***
* 獲取平臺(tái)基本信息
* @param platformId
* @return
*/
@RequestMapping(value="/findPlatformInfo",method = RequestMethod.GET)
PPlatform findOnePlatformById(@RequestParam(value="platformId") String platformId);
}五、結(jié)果演示


到此這篇關(guān)于SpringCloud使用Feign實(shí)現(xiàn)動(dòng)態(tài)路由操作的文章就介紹到這了,更多相關(guān)Feign動(dòng)態(tài)路由內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringCloud使用Feign實(shí)現(xiàn)遠(yuǎn)程調(diào)用流程詳細(xì)介紹
- springcloud使用feign調(diào)用服務(wù)時(shí)參數(shù)內(nèi)容過大問題
- SpringCloud之@FeignClient()注解的使用方式
- SpringCloud分布式鏈路追蹤組件Sleuth配置詳解
- SpringCloud?分布式微服務(wù)架構(gòu)操作步驟
- SpringCloud?分布式鎖的多種實(shí)現(xiàn)
- SpringCloudAlibaba分布式組件詳解
- SpringCloud分布式項(xiàng)目下feign的使用示例詳解
相關(guān)文章
Java項(xiàng)目實(shí)現(xiàn)定時(shí)任務(wù)的三種方法
Java開發(fā)過程中經(jīng)常會(huì)遇到使用定時(shí)任務(wù)的情況,比如在某個(gè)活動(dòng)結(jié)束時(shí),自動(dòng)生成獲獎(jiǎng)名單,導(dǎo)出excel等,下面這篇文章主要給大家介紹了關(guān)于Java項(xiàng)目實(shí)現(xiàn)定時(shí)任務(wù)的三種方法,需要的朋友可以參考下2022-06-06
詳解Java分布式系統(tǒng)中session一致性問題
這篇文章主要介紹了Java分布式系統(tǒng)中session一致性問題,對(duì)分布式系統(tǒng)感興趣的同學(xué),要仔細(xì)看一下2021-04-04
spring mvc常用注解_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了spring mvc常用注解,詳細(xì)的介紹了@RequestMapping, @RequestParam, @ModelAttribute等等這樣類似的注解,有興趣的可以了解一下2017-08-08
手把手帶你實(shí)現(xiàn)第一個(gè)Mybatis程序
這篇文章主要介紹了mybatis實(shí)現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2021-07-07
關(guān)于logback日志級(jí)別動(dòng)態(tài)切換的四種方式
這篇文章主要介紹了關(guān)于logback日志級(jí)別動(dòng)態(tài)切換的四種方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08
java lambda 表達(dá)式中的雙冒號(hào)的用法說明 ::
這篇文章主要介紹了java lambda 表達(dá)式中的雙冒號(hào)的用法說明 ::具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09
詳解Java中字符串緩沖區(qū)StringBuffer類的使用
StringBuffer與String類似,只不過StringBuffer在進(jìn)行字符串處理時(shí)不生成新的對(duì)象,下面我們就來詳解Java中字符串緩沖區(qū)StringBuffer類的使用:2016-06-06

