SpringCloud使用Feign實(shí)現(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)用遠(yuǎn)程方法,無需感知操作遠(yuǎn)程http請求
1.2原理
二、Feign搭建實(shí)現(xiàn)步驟
- 創(chuàng)建Springboot基礎(chǔ)項目
- 在注冊中心(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>
四、程序代碼
在啟動類上加上@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已啟動========================"); } }
啟動類添加完成之后,在指定需要訪問的service注冊使用,
見其他博主講解:
在通過Feign來實(shí)現(xiàn)遠(yuǎn)程服務(wù)調(diào)用時,需要提供一個本地接口來繼承服務(wù)標(biāo)準(zhǔn)工程提供的服務(wù)接口。這個本地接口不需要給予任何實(shí)現(xiàn),在底層Spring容器會為這個接口提供一個基于JDK實(shí)現(xiàn)的代理對象,這個代理對象由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 = "平臺基本信息管理") @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); }
五、結(jié)果演示
到此這篇關(guān)于SpringCloud使用Feign實(shí)現(xiàn)動態(tài)路由操作的文章就介紹到這了,更多相關(guān)Feign動態(tài)路由內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解Java中字典樹(Trie樹)的圖解與實(shí)現(xiàn)
Trie又稱為前綴樹或字典樹,是一種有序樹,它是一種專門用來處理串匹配的數(shù)據(jù)結(jié)構(gòu)。本文將利用圖解詳細(xì)講解Trie樹的實(shí)現(xiàn),需要的可以參考一下2022-05-05SpringBoot整合Pulsar的實(shí)現(xiàn)示例
本文主要介紹了SpringBoot整合Pulsar的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07Java 實(shí)現(xiàn)repalceAll只替換第二個匹配到的字符串
這篇文章主要介紹了Java 實(shí)現(xiàn)repalceAll只替換第二個匹配到的字符串,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12