SpringCloud之Feign示例詳解
Feign簡(jiǎn)介
Feign 是一個(gè)聲明web服務(wù)客戶端,這便得編寫web服務(wù)客戶端更容易,使用Feign 創(chuàng)建一個(gè)接口并對(duì)它進(jìn)行注解,它具有可插拔的注解支持包括Feign注解與JAX-RS注解,F(xiàn)eign還支持可插拔的編碼器與解碼器,Spring Cloud 增加了對(duì) Spring MVC的注解,Spring Web 默認(rèn)使用了HttpMessageConverters, Spring Cloud 集成 Ribbon 和 Eureka 提供的負(fù)載均衡的HTTP客戶端 Feign.
聲明式REST客戶端:Feign
先要啟動(dòng)eureka_register_service工程(注冊(cè)中心)和biz-service-0工程(服務(wù)生產(chǎn)者)
創(chuàng)建一個(gè)maven工程eureka_feign_client
pom.xml
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <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> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Brixton.SR5</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
在應(yīng)用主類中通過@EnableFeignClients注解開啟Feign功能
啟動(dòng)文件FeignApplication.java
@SpringBootApplication @EnableDiscoveryClient @EnableFeignClients public class FeignApplication { public static void main(String[] args) { SpringApplication.run(FeignApplication.class, args); } }
定義服務(wù)接口類UserClient.java
使用@FeignClient("biz-service-0")注解來綁定該接口對(duì)應(yīng)biz-service-0服務(wù)
@FeignClient("biz-service-0") public interface UserClient { @RequestMapping(method = RequestMethod.GET, value = "/getuser") public User getuserinfo(); @RequestMapping(method = RequestMethod.GET, value = "/getuser") public String getuserinfostr(); @RequestMapping(method = RequestMethod.GET, value = "/info") public String info(); }
在web層中調(diào)用上面定義的UserController,具體如下
@RestController public class UserController { @Autowired UserClient userClient; @RequestMapping(value = "/getuserinfo", method = RequestMethod.GET) public User getuserinfo() { return userClient.getuserinfo(); } @RequestMapping(value = "/getuserinfostr", method = RequestMethod.GET) public String getuserinfostr() { return userClient.getuserinfostr(); } @RequestMapping(value = "/info", method = RequestMethod.GET) public String info() { return userClient.info(); } }
application.properties配置變量
spring.application.name=feign-consumer server.port=8004 eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
訪問 http://127.0.0.1:8004/getuserinfo
總結(jié):
其實(shí)通過Feign封裝了HTTP調(diào)用服務(wù)方法,使得客戶端像調(diào)用本地方法那樣直接調(diào)用方法,類似Dubbo中暴露遠(yuǎn)程服務(wù)的方式,區(qū)別在于Dubbo是基于私有二進(jìn)制協(xié)議,而Feign本質(zhì)上還是個(gè)HTTP客戶端
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
K均值聚類算法的Java版實(shí)現(xiàn)代碼示例
這篇文章主要介紹了K均值聚類算法的Java版實(shí)現(xiàn)代碼示例,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12Java實(shí)現(xiàn)線程插隊(duì)的示例代碼
在編寫多線程的業(yè)務(wù)時(shí),會(huì)遇到讓一個(gè)線程優(yōu)先于其他線程運(yùn)行的情況,除了可以設(shè)置線程的優(yōu)先級(jí)高于其他線程,還有更直接的方式:線程插隊(duì)。本文將用Java實(shí)現(xiàn)線程插隊(duì),需要的可以參考一下2022-08-08Java實(shí)現(xiàn)按比抽獎(jiǎng)功能
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)按比抽獎(jiǎng)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-01-01SpringCloud Gateway的路由,過濾器和限流解讀
這篇文章主要介紹了SpringCloud Gateway的路由,過濾器和限流解讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02springboot druid數(shù)據(jù)庫連接池連接失敗后一直重連的解決方法
本文主要介紹了springboot druid數(shù)據(jù)庫連接池連接失敗后一直重連的解決方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04