一 什么是Feign
Feign使我们写Java的HTTP客户端更加容易,我们常说的Feign其实更多的是与SpringCloud结合的Spring Cloud OpenFeign。这里的快速上手,也是结合SpringCloud的示例。
二 写一个provider服务
用SpringBoot快速写一个provider服务,并暴露rest接口,如下:
项目可以到 https://start.spring.io/ 快速生成。
写ProviderController
如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| package com.qicoder.feign.provider.controller;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController;
@RestController @RequestMapping("/provider") public class ProviderController {
@GetMapping("test1") public String test1() { return "测试"; }
@GetMapping("test2") public String test2(@RequestParam String name) { return "hello," + name; } }
|
application.yaml
如下
1 2 3 4 5 6
| server: port: 8081
spring: application: name: provider-server
|
三 写一个Consumer服务
需要额外引入依赖:
1 2 3 4
| <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
|
需要@EnableFeignClients
注解,如果定义的FeignClien在FeignConsumerApplication
类所在包或者子包下,则不需要额外指定包路径。
1 2 3 4 5 6 7 8 9 10 11 12 13
| package com.qicoder.feign.consumer;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.openfeign.EnableFeignClients;
@EnableFeignClients @SpringBootApplication public class FeignConsumerApplication { public static void main(String[] args) { SpringApplication.run(FeignConsumerApplication.class, args); } }
|
写FeignClient如下(由于我们这里演示未使用eureka等注册中心,故需要指定url):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| package com.qicoder.feign.consumer.feign;
import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam;
@FeignClient(contextId = "ProviderService", name = "provider-server", url = "http://localhost:8081") @RequestMapping("/provider") public interface ProviderService {
@GetMapping("test1") String test1();
@GetMapping("test2") String test2(@RequestParam String name); }
|
写ConsumerController
如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| package com.qicoder.feign.consumer.controller;
import com.qicoder.feign.consumer.feign.ProviderService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController;
@RestController @RequestMapping("/consumer") public class ConsumerController { @Autowired private ProviderService providerService;
@GetMapping("test1") public String test1() { return "provider的返回:" + providerService.test1(); }
@GetMapping("test2") public String test2(@RequestParam String name) { return "provider的返回:" + providerService.test2(name); } }
|
四 结果
provider两个链接:
http://localhost:8081/provider/test1
返回:
http://localhost:8081/provider/test2?name=zhangsan
返回:
consumer两个链接:
http://localhost:8082/consumer/test1
返回:
http://localhost:8082/consumer/test2?name=zhangsan
返回:
1
| provider的返回:hello,zhangsan
|