您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關怎么在SpringCloud中使用Feign調用服務,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
使用Spring Cloud Feign
創建一個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”):制定服務名來綁定服務
注:服務名不區分大小寫
@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服務接口的客戶端向該服務發起/hello接口的調用
5.配置類
server.port=9001 spring.application.name=feign-consumer eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
啟動程序以后,在瀏覽器中輸入http://localhost:9001/feign-consumer出現下圖:
- 提升使用,帶參數的請求
在具體業務中的接口會比之前程序的復雜得多,這里介紹一下Feign對集中不同形式參數的綁定方法。有@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);
這里在定義各參數綁定時@RequestParam、@RequestHeader等可以指定參數名稱的注解,但它們的value不能少,否則會報錯,這和SpringMVC中有一點不同
4.在服務調用類Controller層添加一個測試的接口
@RequestMapping(value = "/feign-consumer2", method = RequestMethod.GET) @ResponseBody public String helloConsumer2() { String s2 = helloService.hello("dayday"); return s2; }
啟動以后訪問瀏覽器:
以上就是怎么在SpringCloud中使用Feign調用服務,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。