Feign
Feign是一个声明式的Web Service客户端,它使得编写Web Serivce客户端变得更加简单。我们只需要使用Feign来创建一个接口并用注解来配置它既可完成。它具备可插拔的注解支持,包括Feign注解和JAX-RS注解。Feign也支持可插拔的编码器和解码器。Spring Cloud为Feign增加了对Spring MVC注解的支持,还整合了Ribbon和Eureka来提供均衡负载的HTTP客户端实现。
Spring Cloud Netflix 的微服务都是以 HTTP 接口的形式暴露的,所以可以用 Apache 的 HttpClient 或 Spring 的 RestTemplate 去调用,而 Feign 是一个使用起来更加方便的 HTTP 客戶端,使用起来就像是调用自身工程的方法,而感觉不到是调用远程方法
代码实现
结尾有Github地址的代码示例
一、使用SpringMVC注解
1、添加依赖
1 2 3 4
| <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency>
|
2、加注解
在启动类上加注解@EnableFeignClients
1 2 3 4 5 6 7 8 9 10
| @SpringBootApplication @EnableEurekaClient @EnableFeignClients public class CustomerFeignApplication { public static void main( String[] args ){ SpringApplication.run(CustomerFeignApplication.class, args); } }
|
3、创建服务接口类
定义一个 producer
服务的接口类
1 2 3 4 5 6 7 8 9 10
| @FeignClient(name="producer") public interface FeignClientService {
@RequestMapping(value="/item/{id}",method=RequestMethod.GET) public Object detai(@PathVariable("id") String id); @RequestMapping(value="/add",method=RequestMethod.POST) public Object add(Item item); }
|
4、控制层调用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| @RestController public class TestController { @Autowired private FeignClientService feignClientService; @GetMapping("/provider/{id}") public Object test(@PathVariable String id) { return feignClientService.detai(id); } @GetMapping("/add") public Object test(Item item) { return feignClientService.add(item); } }
|
5、配置文件
其实配置文件application.yml
没什么特殊的
1 2 3 4 5 6 7 8 9 10 11 12
| server: port: 8903 eureka: client: serviceUrl: defaultZone: http://rstyro:rstyropwd@localhost:8761/eureka instance: prefer-ip-address: true spring: application: name: customer-feign
|
启动eureka 和两个生产者与feign,查看结果,还是实现了负载均衡



上面我们用的是SpringMVC
的注解,下面我们用feign 默认的注解,查看Github的地址 里面介绍了基本用法
二、使用默认的注解
1、自定义一个配置类FeignConfig
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| @Configuration public class FeignConfig {
@Bean public Contract feignContract() { return new feign.Contract.Default(); }
@Bean Logger.Level feignLoggerLevel() { return Logger.Level.FULL; }
}
|
2、自定义一个服务接口类FeignClientService
1 2 3 4 5 6 7 8 9 10 11
| @FeignClient(name="producer",configuration=FeignConfig.class) public interface FeignClientService {
@RequestLine("GET /item/{id}") public Object detai(@Param("id") String id); @RequestLine("POST /add") public Object add(Item item); }
|
3、调用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| @RestController public class TestController { @Autowired private FeignClientService feignClientService; @GetMapping("/provider/{id}") public Object test(@PathVariable String id) { return feignClientService.detai(id); } @GetMapping("/add") public Object test(Item item) { return feignClientService.add(item); } }
|
4、配置文件
如果机子的性能比较差什么的,第一次请求会报一个请求超时异常,解决方案看下面配置文件
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
| server: port: 8904 eureka: client: serviceUrl: defaultZone: http://rstyro:rstyropwd@localhost:8761/eureka instance: prefer-ip-address: true spring: application: name: customer-feign-default
logging: level: top.lrshuai.cloud.springcloud.feign.FeignClientService: DEBUG # 解决第一次请求报超时异常的方案: hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 5000 # 或者: # hystrix.command.default.execution.timeout.enabled: false # 或者: # feign.hystrix.enabled: false ## 索性禁用feign的hystrix
|