SpringCloud使用Feign實(shí)現(xiàn)服務(wù)調(diào)用
Spring Cloud Feign簡(jiǎn)介
Spring Cloud Feign也是一個(gè)基礎(chǔ)工具類(lèi),它整合了Spring Cloud Ribbon和Spring Cloud Hystrix,除了提供這兩者的強(qiáng)大功能以外,它還提供了一種聲明式的Web服務(wù)客戶(hù)端定義方式。使用它可以進(jìn)行服務(wù)的消費(fèi),但是它的客戶(hù)端負(fù)載平衡仍是通過(guò)Ribbon實(shí)現(xiàn)的
使用Spring Cloud Feign
創(chuàng)建一個(gè)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的依賴(lài)
2.啟動(dòng)類(lèi)
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class FeignConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(FeignConsumerApplication.class, args);
}
}
@EnableFeignClients:開(kāi)啟Spring Cloud Feign的支持功能
3.服務(wù)層
@FeignClient("hello-service")
public interface HelloService {
@RequestMapping(value = "/hello", method = RequestMethod.GET)
String hello();
}
@FeignClient(“hello-service”):制定服務(wù)名來(lái)綁定服務(wù)
注:服務(wù)名不區(qū)分大小寫(xiě)
@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)用這個(gè)綁定了hello-service服務(wù)接口的客戶(hù)端向該服務(wù)發(fā)起/hello接口的調(diào)用
5.配置類(lèi)
server.port=9001 spring.application.name=feign-consumer eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
啟動(dòng)程序以后,在瀏覽器中輸入http://localhost:9001/feign-consumer出現(xiàn)下圖:

- 提升使用,帶參數(shù)的請(qǐng)求
在具體業(yè)務(wù)中的接口會(huì)比之前程序的復(fù)雜得多,這里介紹一下Feign對(duì)集中不同形式參數(shù)的綁定方法。有@RequestParam、@RequestHeader、@RequestBody
1.改造服務(wù)提供類(lèi)的Service層,添加幾個(gè)方法
@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.添加一個(gè)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)用類(lèi)的接口
@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ù)綁定時(shí)@RequestParam、@RequestHeader等可以指定參數(shù)名稱(chēng)的注解,但它們的value不能少,否則會(huì)報(bào)錯(cuò),這和SpringMVC中有一點(diǎn)不同
4.在服務(wù)調(diào)用類(lèi)Controller層添加一個(gè)測(cè)試的接口
@RequestMapping(value = "/feign-consumer2", method = RequestMethod.GET)
@ResponseBody
public String helloConsumer2() {
String s2 = helloService.hello("dayday");
return s2;
}
啟動(dòng)以后訪問(wèn)瀏覽器:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- springcloud使用feign調(diào)用服務(wù)時(shí)參數(shù)內(nèi)容過(guò)大問(wèn)題
- springcloud?feign服務(wù)之間調(diào)用,date類(lèi)型轉(zhuǎn)換錯(cuò)誤的問(wèn)題
- SpringCloud中的Feign遠(yuǎn)程調(diào)用接口傳參失敗問(wèn)題
- springcloud本地調(diào)試feign調(diào)用出現(xiàn)的詭異404問(wèn)題及解決
- SpringCloud 服務(wù)負(fù)載均衡和調(diào)用 Ribbon、OpenFeign的方法
- SpringCloud Feign 服務(wù)調(diào)用的實(shí)現(xiàn)
- SpringCloud服務(wù)之間Feign調(diào)用不會(huì)帶上請(qǐng)求頭header的解決方法
相關(guān)文章
Spring?Security?基于URL的權(quán)限判斷源碼解析
這篇文章主要介紹了Spring?Security?基于URL的權(quán)限判斷問(wèn)題,我們想要實(shí)現(xiàn)自己的基于請(qǐng)求Url的授權(quán)只需自定義一個(gè)?AccessDecisionManager即可,接下來(lái)跟隨小編一起看看實(shí)現(xiàn)代碼吧2021-12-12
Spring中使用騰訊云發(fā)送短信驗(yàn)證碼的實(shí)現(xiàn)示例
本文主要介紹了Spring?中?使用騰訊云發(fā)送短信驗(yàn)證碼,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
java查找字符串中的包含子字符串的個(gè)數(shù)實(shí)現(xiàn)代碼
下面小編就為大家?guī)?lái)一篇java查找字符串中的包含子字符串的個(gè)數(shù)實(shí)現(xiàn)代碼。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-06-06
SpringSecurity框架下實(shí)現(xiàn)CSRF跨站攻擊防御的方法
CSRF是一種網(wǎng)絡(luò)攻擊方式,也可以說(shuō)是一種安全漏洞,這種安全漏洞在web開(kāi)發(fā)中廣泛存在。這篇文章主要介紹了SpringSecurity框架下實(shí)現(xiàn)CSRF跨站攻擊防御,需要的朋友可以參考下2019-12-12
ServletWebServerApplicationContext創(chuàng)建Web容器Tomcat示例
這篇文章主要為大家介紹了ServletWebServerApplicationContext創(chuàng)建Web容器Tomcat示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
Java使用elasticsearch基礎(chǔ)API使用案例講解
這篇文章主要介紹了Java使用elasticsearch基礎(chǔ)API使用案例講解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08

