SpringCloud使用Feign實現(xiàn)服務調用
Spring Cloud Feign簡介
Spring Cloud Feign也是一個基礎工具類,它整合了Spring Cloud Ribbon和Spring Cloud Hystrix,除了提供這兩者的強大功能以外,它還提供了一種聲明式的Web服務客戶端定義方式。使用它可以進行服務的消費,但是它的客戶端負載平衡仍是通過Ribbon實現(xiàn)的
使用Spring Cloud Feign
創(chuàng)建一個SpringBoot工程,作為服務調用方
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.服務層
@FeignClient("hello-service")
public interface HelloService {
@RequestMapping(value = "/hello", method = RequestMethod.GET)
String hello();
}
@FeignClient(“hello-service”):制定服務名來綁定服務
注:服務名不區(qū)分大小寫
@RequestMapping:指定調用服務中的具體方法
4.Controller層
@Controller
public class ConsumerController {
@Autowired
private HelloService helloService;
@RequestMapping(value = "/feign-consumer", method = RequestMethod.GET)
@ResponseBody
public String helloConsumer() {
return helloService.hello();
}
}
在方法中調用這個綁定了hello-service服務接口的客戶端向該服務發(fā)起/hello接口的調用
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è)務中的接口會比之前程序的復雜得多,這里介紹一下Feign對集中不同形式參數(shù)的綁定方法。有@RequestParam、@RequestHeader、@RequestBody
1.改造服務提供類的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.修改服務調用類的接口
@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.在服務調用類Controller層添加一個測試的接口
@RequestMapping(value = "/feign-consumer2", method = RequestMethod.GET)
@ResponseBody
public String helloConsumer2() {
String s2 = helloService.hello("dayday");
return s2;
}
啟動以后訪問瀏覽器:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Spring?Security?基于URL的權限判斷源碼解析
這篇文章主要介紹了Spring?Security?基于URL的權限判斷問題,我們想要實現(xiàn)自己的基于請求Url的授權只需自定義一個?AccessDecisionManager即可,接下來跟隨小編一起看看實現(xiàn)代碼吧2021-12-12
Spring中使用騰訊云發(fā)送短信驗證碼的實現(xiàn)示例
本文主要介紹了Spring?中?使用騰訊云發(fā)送短信驗證碼,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03
java查找字符串中的包含子字符串的個數(shù)實現(xiàn)代碼
下面小編就為大家?guī)硪黄猨ava查找字符串中的包含子字符串的個數(shù)實現(xiàn)代碼。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-06-06
SpringSecurity框架下實現(xiàn)CSRF跨站攻擊防御的方法
CSRF是一種網(wǎng)絡攻擊方式,也可以說是一種安全漏洞,這種安全漏洞在web開發(fā)中廣泛存在。這篇文章主要介紹了SpringSecurity框架下實現(xiàn)CSRF跨站攻擊防御,需要的朋友可以參考下2019-12-12
ServletWebServerApplicationContext創(chuàng)建Web容器Tomcat示例
這篇文章主要為大家介紹了ServletWebServerApplicationContext創(chuàng)建Web容器Tomcat示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-03-03
Java使用elasticsearch基礎API使用案例講解
這篇文章主要介紹了Java使用elasticsearch基礎API使用案例講解,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下2021-08-08

