Spring Cloud Feign簡(jiǎn)單使用詳解
概述
在Spring Cloud EureKa Ribbon 服務(wù)注冊(cè)-發(fā)現(xiàn)-調(diào)用一文中簡(jiǎn)單的介紹了在Spring Cloud中如何使用EureKa和Ribbon。文章中使用了RestTemplate去訪問(wèn)其他的restful微服務(wù)接口。其實(shí)在Spring Cloud還可以使用Feign來(lái)訪問(wèn)其他的restful微服務(wù)接口。使用起來(lái)更加的簡(jiǎn)潔明了。
集成Feign
修改一下Spring Cloud EureKa Ribbon 服務(wù)注冊(cè)-發(fā)現(xiàn)-調(diào)用中order service的pom配置,把Fegin引入進(jìn)來(lái)即可。
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency>
修改OrderApplication類(lèi),刪除如下代碼:
@Bean @LoadBalanced RestTemplate restTemplate() { return new RestTemplate(); }
并加上@EnableFeignClients注解。完整代碼如下:
package com.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.feign.EnableFeignClients; @EnableDiscoveryClient @EnableFeignClients @SpringBootApplication public class OrderApplication { public static void main(String[] args) { SpringApplication.run(OrderApplication.class, args); } }
新增接口UserService,并使用@FeignClient注解。
package com.springboot; import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; @FeignClient(name="user") public interface UserService { @GetMapping(value="/getUser") String getUser(); }
這里的@FeignClient(name="user")中的name=user表示要訪問(wèn)user這個(gè)微服務(wù)。由于order這個(gè)微服務(wù)已經(jīng)集成了Eureka和Ribbon。那么使用@FeignClient(name="user")訪問(wèn)user微服務(wù)的時(shí)候,已經(jīng)自動(dòng)支持客戶端路由了。并且會(huì)從注冊(cè)中心中找到user這個(gè)微服務(wù)。
修改OrderController,注入U(xiǎn)serService。
package com.springboot; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class OrderController { @Autowired private UserService userService; @GetMapping("/getOrderUser") public String getOrderUser() { return userService.getUser(); } }
這樣就無(wú)需使用
restTemplate.getForEntity("http://user/getUser",String.class).getBody();
來(lái)調(diào)用user服務(wù)中的getUser接口了。而是直接使用userService.getUser()就可以了。
啟動(dòng)注冊(cè)中心以及user和order這兩個(gè)微服務(wù)。使用http://localhost:8883/getOrderUser
訪問(wèn)一下。是可以返回
I am user list.
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java實(shí)現(xiàn)畫(huà)線、矩形、橢圓、字符串功能
本篇文章主要介紹了Java實(shí)現(xiàn)畫(huà)線、矩形、橢圓、字符串功能的實(shí)例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05深入了解Java atomic原子類(lèi)的使用方法和原理
這篇文章主要介紹了深入了解Java atomic原子類(lèi)的使用方法和原理,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,,需要的朋友可以參考下2019-06-06SpringBoot項(xiàng)目中如何訪問(wèn)HTML頁(yè)面
這篇文章主要介紹了SpringBoot項(xiàng)目中如何訪問(wèn)HTML頁(yè)面,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11Jenkins一鍵打包部署SpringBoot應(yīng)用的方法步驟
本文主要介紹了使用Jenkins一鍵打包部署SpringBoot應(yīng)用的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12SpringBoot多數(shù)據(jù)源配置方式以及報(bào)錯(cuò)問(wèn)題的解決
這篇文章主要介紹了SpringBoot多數(shù)據(jù)源配置方式以及報(bào)錯(cuò)問(wèn)題的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07Java GUI編程之貪吃蛇游戲簡(jiǎn)單實(shí)現(xiàn)方法【附demo源碼下載】
這篇文章主要介紹了Java GUI編程之貪吃蛇游戲簡(jiǎn)單實(shí)現(xiàn)方法,詳細(xì)分析了貪吃蛇游戲的具體實(shí)現(xiàn)步驟與相關(guān)注意事項(xiàng),并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2017-09-09