Feign is a declarative web service client. It makes writing web service clients easier. To use Feign create an interface and annotate it. It has pluggable annotation support including Feign annotations and JAX-RS annotations. Feign also supports pluggable encoders and decoders. Spring Cloud adds support for Spring MVC annotations and for using the same HttpMessageConverters used by default in Spring Web. Spring Cloud integrates Ribbon and Eureka to provide a load balanced http client when using Feign. [1]
Feign是一个声明式Web Service客户端。它可以让编写Web Service客户端更加简单。
不使用Feign:
使用Feign以后:
注册中心,服务提供者如何搭建请参考这篇文章:
五雨:Spring Cloud学习笔记--负载均衡(Ribbon)zhuanlan.zhihu.com我们来改造一下服务调用者:
首先,pom文件需要增加spring-cloud-starter-openfeign的引用。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
<version>1.3.0.RC1</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
服务调用者的yml文件:
server:
#服务调用者端口号
port: 50002
spring:
application:
name: ServerClient
eureka:
client:
service-url:
#将当前服务也注册到注册中心
defaultZone: http://eurekaserver1:30001/eureka/
主启动文件增加EnableFeignClients注解:
定义一个ClientTestService接口,代码如下:
package com.springclouddemo.springcloudclient.Service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
@FeignClient(value = "ServerProvide")
public interface ClientTestService {
@GetMapping("/ServerTest")
String ClientTestServiceMethod();
}
其中通过@FeignClient(value = "ServerProvide") 来绑定服务名,当前我绑定的是ServerProvide的服务。
Controller代码如下:
package com.springclouddemo.springcloudclient.controller;
import com.springclouddemo.springcloudclient.Service.ClientTestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@Configuration
public class FeignController {
@Autowired
ClientTestService clientTestService;
@GetMapping("/getTestMsgByFeign")
public String findById(){
return clientTestService.ClientTestServiceMethod();
}
}
启动运行测试一下。
咦,为什么没有配置ribbon,依旧还有负载均衡的策略?
因为feign是对ribbon的封装,当前例子采用了默认的负载均衡策略(轮询)。
feign对HyStrix的支持也非常友好,我们看下怎么做。
首先在yml中开启断路器。
yml配置文件如下:
server:
#服务调用者端口号
port: 50002
spring:
application:
name: ServerClient
eureka:
client:
service-url:
#将当前服务也注册到注册中心
defaultZone: http://eurekaserver1:30001/eureka/
feign:
hystrix:
enabled: true
创建ClientTestService的实现类,代码如下:
package com.springclouddemo.springcloudclient.Service;
import org.springframework.stereotype.Component;
@Component
public class ClientTestServiceFallback implements ClientTestService{
@Override
public String ClientTestServiceMethod() {
return "网络繁忙,请稍后再试。(这里是服务调用者使用Feign的快速失败给出的提示)";
}
}
修改ClientTestService,代码如下:
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
@FeignClient(value = "ServerProvide",fallback = ClientTestServiceFallback.class)
public interface ClientTestService {
@GetMapping("/ServerTest")
String ClientTestServiceMethod();
}
运行一下。
我把端口号为40001的服务停止,模拟宕机
可以看出,HyStrix已经起了作用。
参考
- ^Declarative REST Client(官方文档) https://cloud.spring.io/spring-cloud-netflix/multi/multi_spring-cloud-feign.html