您好,登錄后才能下訂單哦!
這篇文章主要介紹“SpringCloud如何使用Feign實(shí)現(xiàn)動(dòng)態(tài)路由操作”,在日常操作中,相信很多人在SpringCloud如何使用Feign實(shí)現(xiàn)動(dòng)態(tài)路由操作問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”SpringCloud如何使用Feign實(shí)現(xiàn)動(dòng)態(tài)路由操作”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!
Feign
基于接口 + 注解的方式,一個(gè)http請(qǐng)求調(diào)用的輕量級(jí)框架
Feign是Netflix開(kāi)發(fā)的聲明式、模板化的HTTP客戶端, Feign可以幫助我們更快捷、優(yōu)雅地調(diào)用HTTP API。
Feign是一種聲明式、模板化的HTTP客戶端(僅在Application Client中使用)。聲明式調(diào)用是指,就像調(diào)用本地方法一樣調(diào)用遠(yuǎn)程方法,無(wú)需感知操作遠(yuǎn)程http請(qǐng)求
創(chuàng)建Springboot基礎(chǔ)項(xiàng)目
在注冊(cè)中心(Eureka)配置的基礎(chǔ)上,進(jìn)行配置Feign
基礎(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分頁(yè)--> <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,開(kāi)啟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)類添加完成之后,在指定需要訪問(wèn)的service注冊(cè)使用,
見(jiàn)其他博主講解:
在通過(guò)Feign來(lái)實(shí)現(xiàn)遠(yuǎn)程服務(wù)調(diào)用時(shí),需要提供一個(gè)本地接口來(lái)繼承服務(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)過(guò)程類似通過(guò)代碼調(diào)用LoadBalancerClient實(shí)現(xiàn)的Rest遠(yuǎn)程訪問(wè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); }
到此,關(guān)于“SpringCloud如何使用Feign實(shí)現(xiàn)動(dòng)態(tài)路由操作”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。