SpringCloud使用Feign實現(xiàn)服務(wù)調(diào)用
Spring Cloud Feign簡介
Spring Cloud Feign也是一個基礎(chǔ)工具類,它整合了Spring Cloud Ribbon和Spring Cloud Hystrix,除了提供這兩者的強大功能以外,它還提供了一種聲明式的Web服務(wù)客戶端定義方式。使用它可以進行服務(wù)的消費,但是它的客戶端負(fù)載平衡仍是通過Ribbon實現(xiàn)的
使用Spring Cloud Feign
創(chuàng)建一個SpringBoot工程,作為服務(wù)調(diào)用方
1.pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
spring-cloud-starter-feign加入了feign的依賴
2.啟動類
@EnableFeignClients @EnableDiscoveryClient @SpringBootApplication public class FeignConsumerApplication { public static void main(String[] args) { SpringApplication.run(FeignConsumerApplication.class, args); } }
@EnableFeignClients:開啟Spring Cloud Feign的支持功能
3.服務(wù)層
@FeignClient("hello-service") public interface HelloService { @RequestMapping(value = "/hello", method = RequestMethod.GET) String hello(); }
@FeignClient(“hello-service”):制定服務(wù)名來綁定服務(wù)
注:服務(wù)名不區(qū)分大小寫
@RequestMapping:指定調(diào)用服務(wù)中的具體方法
4.Controller層
@Controller public class ConsumerController { @Autowired private HelloService helloService; @RequestMapping(value = "/feign-consumer", method = RequestMethod.GET) @ResponseBody public String helloConsumer() { return helloService.hello(); } }
在方法中調(diào)用這個綁定了hello-service服務(wù)接口的客戶端向該服務(wù)發(fā)起/hello接口的調(diào)用
5.配置類
server.port=9001 spring.application.name=feign-consumer eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
啟動程序以后,在瀏覽器中輸入http://localhost:9001/feign-consumer出現(xiàn)下圖:
- 提升使用,帶參數(shù)的請求
在具體業(yè)務(wù)中的接口會比之前程序的復(fù)雜得多,這里介紹一下Feign對集中不同形式參數(shù)的綁定方法。有@RequestParam、@RequestHeader、@RequestBody
1.改造服務(wù)提供類的Service層,添加幾個方法
@RequestMapping(value = "/hello1", method = RequestMethod.GET) @ResponseBody public String hello(@RequestParam String name) { return "hello " + name; } @RequestMapping(value = "/hello2", method = RequestMethod.GET) @ResponseBody public User hello(@RequestHeader String name, @RequestHeader Integer age) { return new User(name, age); } @RequestMapping(value = "/hello3", method = RequestMethod.POST) @ResponseBody public String hello(@RequestBody User user) { return "Hello " + user.getName() + ", " + user.getAge(); }
2.添加一個javabean
public class User { private String name; private Integer age; 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; } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
3.修改服務(wù)調(diào)用類的接口
@RequestMapping(value = "/hello1", 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);
這里在定義各參數(shù)綁定時@RequestParam、@RequestHeader等可以指定參數(shù)名稱的注解,但它們的value不能少,否則會報錯,這和SpringMVC中有一點不同
4.在服務(wù)調(diào)用類Controller層添加一個測試的接口
@RequestMapping(value = "/feign-consumer2", method = RequestMethod.GET) @ResponseBody public String helloConsumer2() { String s2 = helloService.hello("dayday"); return s2; }
啟動以后訪問瀏覽器:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- springcloud使用feign調(diào)用服務(wù)時參數(shù)內(nèi)容過大問題
- springcloud?feign服務(wù)之間調(diào)用,date類型轉(zhuǎn)換錯誤的問題
- SpringCloud中的Feign遠(yuǎn)程調(diào)用接口傳參失敗問題
- springcloud本地調(diào)試feign調(diào)用出現(xiàn)的詭異404問題及解決
- SpringCloud 服務(wù)負(fù)載均衡和調(diào)用 Ribbon、OpenFeign的方法
- SpringCloud Feign 服務(wù)調(diào)用的實現(xiàn)
- SpringCloud服務(wù)之間Feign調(diào)用不會帶上請求頭header的解決方法
相關(guān)文章
Spring?Security?基于URL的權(quán)限判斷源碼解析
這篇文章主要介紹了Spring?Security?基于URL的權(quán)限判斷問題,我們想要實現(xiàn)自己的基于請求Url的授權(quán)只需自定義一個?AccessDecisionManager即可,接下來跟隨小編一起看看實現(xiàn)代碼吧2021-12-12Spring中使用騰訊云發(fā)送短信驗證碼的實現(xiàn)示例
本文主要介紹了Spring?中?使用騰訊云發(fā)送短信驗證碼,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03java查找字符串中的包含子字符串的個數(shù)實現(xiàn)代碼
下面小編就為大家?guī)硪黄猨ava查找字符串中的包含子字符串的個數(shù)實現(xiàn)代碼。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-06-06SpringSecurity框架下實現(xiàn)CSRF跨站攻擊防御的方法
CSRF是一種網(wǎng)絡(luò)攻擊方式,也可以說是一種安全漏洞,這種安全漏洞在web開發(fā)中廣泛存在。這篇文章主要介紹了SpringSecurity框架下實現(xiàn)CSRF跨站攻擊防御,需要的朋友可以參考下2019-12-12ServletWebServerApplicationContext創(chuàng)建Web容器Tomcat示例
這篇文章主要為大家介紹了ServletWebServerApplicationContext創(chuàng)建Web容器Tomcat示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-03-03Java使用elasticsearch基礎(chǔ)API使用案例講解
這篇文章主要介紹了Java使用elasticsearch基礎(chǔ)API使用案例講解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08