SpringCloud實(shí)戰(zhàn)之Feign聲明式服務(wù)調(diào)用
在前面的文章中可以發(fā)現(xiàn)當(dāng)我們通過RestTemplate調(diào)用其它服務(wù)的API時(shí),所需要的參數(shù)須在請求的URL中進(jìn)行拼接,如果參數(shù)少的話或許我們還可以忍受,一旦有多個(gè)參數(shù)的話,這時(shí)拼接請求字符串就會(huì)效率低下,并且顯得好傻。
那么有沒有更好的解決方案呢?答案是確定的有,Netflix已經(jīng)為我們提供了一個(gè)框架:Feign。
Feign是一個(gè)聲明式的Web Service客戶端,它的目的就是讓W(xué)eb Service調(diào)用更加簡單。Feign提供了HTTP請求的模板,通過編寫簡單的接口和插入注解,就可以定義好HTTP請求的參數(shù)、格式、地址等信息。
而Feign則會(huì)完全代理HTTP請求,我們只需要像調(diào)用方法一樣調(diào)用它就可以完成服務(wù)請求及相關(guān)處理。Feign整合了Ribbon和Hystrix(關(guān)于Hystrix我們后面再講),可以讓我們不再需要顯式地使用這兩個(gè)組件。
總起來說,F(xiàn)eign具有如下特性:
- 可插拔的注解支持,包括Feign注解和JAX-RS注解;
- 支持可插拔的HTTP編碼器和解碼器;
- 支持Hystrix和它的Fallback;
- 支持Ribbon的負(fù)載均衡;
- 支持HTTP請求和響應(yīng)的壓縮。
這看起來有點(diǎn)像我們springmvc模式的Controller層的RequestMapping映射。這種模式是我們非常喜歡的。Feign是用@FeignClient來映射服務(wù)的。
首先第一步,在原來的基礎(chǔ)上新建一個(gè)Feign模塊,接著引入相關(guān)依賴,引入Feign依賴,會(huì)自動(dòng)引入Hystrix依賴的,如下:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> <version>1.3.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> <version>1.4.0.RELEASE</version> </dependency>
application.yml配置如下:
server: port: 8083 spring: application: name: feign-consumer eureka: client: service-url: defaultZone: http://localhost:8888/eureka/,http://localhost:8889/eureka/
接著在前面文章中的的的兩個(gè)provider1和provider2兩個(gè)模塊的服務(wù)新增幾個(gè)方法,如下代碼所示:
/** * Created by cong on 2018/5/8. */ @RestController public class HelloController { @RequestMapping("/hello") public String hello(){ System.out.println("訪問來1了......"); return "hello1"; } @RequestMapping("/hjcs") public List<String> laowangs(String ids){ List<String> list = new ArrayList<>(); list.add("laowang1"); list.add("laowang2"); list.add("laowang3"); return list; } //新增的方法 @RequestMapping(value = "/hellol", method= RequestMethod.GET) public String hello(@RequestParam String name) { return "Hello " + name; } @RequestMapping(value = "/hello2", method= RequestMethod.GET) public User hello(@RequestHeader String name, @RequestHeader Integer age) { return new User(name, age); } @RequestMapping(value = "/hello3", method = RequestMethod.POST) public String hello (@RequestBody User user) { return "Hello "+ user. getName () + ", " + user. getAge (); } }
接著是上面代碼所需用到的User類,代碼如下:
/** * Created by cong 2017/12/2. */ public class User { private String name; private Integer age; //序列化傳輸?shù)臅r(shí)候必須要有空構(gòu)造方法,不然會(huì)出錯(cuò) public User() { } public User(String name, Integer age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
接下來用Feign的@FeignClient(“服務(wù)名稱”)映射服務(wù)調(diào)用。代碼如下:
package hjc; import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.web.bind.annotation.*; /** * Created by cong on 2018/5/17. */ //configuration = xxx.class 這個(gè)類配置Hystrix的一些精確屬性 //value=“你用到的服務(wù)名稱” @FeignClient(value = "hello-service",fallback = FeignFallBack.class) public interface FeignService { //服務(wù)中方法的映射路徑 @RequestMapping("/hello") String hello(); @RequestMapping(value = "/hellol", method= RequestMethod.GET) String hello(@RequestParam("name") String name) ; @RequestMapping(value = "/hello2", method= RequestMethod.GET) User hello(@RequestHeader("name") String name, @RequestHeader("age") Integer age); @RequestMapping(value = "/hello3", method= RequestMethod.POST) String hello(@RequestBody User user); }
接著在Controller層注入FeiService這個(gè)接口,進(jìn)行遠(yuǎn)程服務(wù)調(diào)用,代碼如下:
/** * Created by cong on 2018/5/17. */ @RestController public class ConsumerController { @Autowired FeignService feignService; @RequestMapping("/consumer") public String helloConsumer(){ return feignService.hello(); } @RequestMapping("/consumer2") public String helloConsumer2(){ String r1 = feignService.hello("hjc"); String r2 = feignService.hello("hjc", 23).toString(); String r3 = feignService.hello(new User("hjc", 23)); return r1 + "-----" + r2 + "----" + r3; } }
接著在Feign模塊的啟動(dòng)類哪里打上Eureka客戶端的注解@EnableDiscoveryClient Feign客戶端的注解
@EnableFeignClients,代碼如下: @SpringBootApplication @EnableDiscoveryClient @EnableFeignClients public class FeignApplication { public static void main(String[] args) { SpringApplication.run(FeignApplication.class, args); } }
接著啟動(dòng)啟動(dòng)類,瀏覽器上輸入localhost:8083/consumer 運(yùn)行結(jié)果如下:
可以看到負(fù)載均衡輪詢出現(xiàn)hello1,hello2。
接著繼續(xù)在瀏覽器上輸入localhost:8083/consumer2,運(yùn)行結(jié)果如下:
接下來我們進(jìn)行Feign聲明式調(diào)用服務(wù)下的,服務(wù)降級(jí)的使用,那么我們就必須新建一個(gè)FeignFallBack類來繼承FeiService,代碼如下:
package hjc; import org.springframework.stereotype.Component; /** * Created by cong on 2018/5/17. */ @Component public class FeignFallBack implements FeignService{ //實(shí)現(xiàn)的方法是服務(wù)調(diào)用的降級(jí)方法 @Override public String hello() { return "error"; } @Override public String hello(String name) { return "error"; } @Override public User hello(String name, Integer age) { return new User(); } @Override public String hello(User user) { return "error"; } }
接著我們再把那兩個(gè)服務(wù)提供模塊provider1,provider2模塊進(jìn)行停止,運(yùn)行結(jié)果如下所示:
可以看到我們這幾個(gè)調(diào)用,都進(jìn)行了服務(wù)降級(jí)了。
那么如果我們想精確的控制一下Hystrix的參數(shù)也是可以的,比方說跟Hystrix結(jié)合的參數(shù),那么可以在FeignClient注解里面配置一個(gè)Configuration=XXX類.class屬性,在哪個(gè)類里面精確的指定一下屬性。
或者在application.yml里面配置,如下:
hystrix: command: default: execution: isolation: thread: timeoutinMilliseconds: 5000 ribbon: connectTimeout: 500 #如果想對(duì)單獨(dú)的某個(gè)服務(wù)進(jìn)行詳細(xì)配置,如下 hello-service: ribbon: connectTimeout: 500
這里滿足了我們大部分場景的調(diào)用,但是有寫精細(xì)場景,還是要用原生的Hystrix,跟我們之前的Hystrix用法一下,不要走Feign客戶端調(diào)用就行了,如下:
/** * Created by cong on 2018/5/17. */ public class HjcCommand extends HystrixCommand { protected HjcCommand(HystrixCommandGroupKey group) { super(group); } @Override protected Object run() throws Exception { return null; } }
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java 中JFinal getModel方法和數(shù)據(jù)庫使用出現(xiàn)問題解決辦法
這篇文章主要介紹了java 中JFinal getModel方法和數(shù)據(jù)庫使用出現(xiàn)問題解決辦法的相關(guān)資料,需要的朋友可以參考下2017-04-04java:程序包org.apache.ibatis.annotations不存在報(bào)錯(cuò)解決
這篇文章主要給大家介紹了關(guān)于java:程序包org.apache.ibatis.annotations不存在報(bào)錯(cuò)的解決方法,這個(gè)錯(cuò)誤是我在直接導(dǎo)入springboot項(xiàng)目的時(shí)候報(bào)錯(cuò)的,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04SpringBoot整合flyway實(shí)現(xiàn)自動(dòng)創(chuàng)建表的方法
這篇文章主要介紹了SpringBoot整合flyway實(shí)現(xiàn)自動(dòng)創(chuàng)建表的方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03spring security4 添加驗(yàn)證碼的示例代碼
本篇文章主要介紹了spring security4 添加驗(yàn)證碼的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-02-02關(guān)于SpringMVC中數(shù)據(jù)綁定@ModelAttribute注解的使用
這篇文章主要介紹了關(guān)于SpringMVC中數(shù)據(jù)綁定@ModelAttribute注解的使用,SpringMVC是一個(gè)基于Spring框架的Web框架,它提供了一種簡單、靈活的方式來開發(fā)Web應(yīng)用程序,在開發(fā)Web應(yīng)用程序時(shí),我們需要將用戶提交的數(shù)據(jù)綁定到我們的Java對(duì)象上,需要的朋友可以參考下2023-07-07@Configuration與@Component作為配置類的區(qū)別詳解
這篇文章主要介紹了@Configuration與@Component作為配置類的區(qū)別詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06Java實(shí)現(xiàn)圖書館借閱系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)圖書館借閱系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03