Feign 是一个声明式的伪RPC的REST客户端,它用了基于接口的注解方式,很方便的客户端配置。Spring Cloud 给 Feign 添加了支持Spring MVC注解,并整合Ribbon及Eureka进行支持负载均衡。
Feign的使用很简单,有以下几步:
- 添加依赖和配置文件
- 启动类添加 @EnableFeignClients 注解支持
- 建立Client接口,并在接口中定义需调用的服务方法
- 使用Client接口。
1.添加依赖和配置文件
添加 spring-cloud-starter-openfeign 依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
完整的 pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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-netflix-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
在引入这些依赖之后,在工程的配置文件application.properties做相关的配置,包括配置程序名为eureka-feign-client,端口号为8764,Eureka服务注册地址是http://127.0.0.1:8761/eureka/,代码如下:
server.port=8764
spring.application.name=eureka-feign-client
#Eureka的注册中心
eureka.client.serviceUrl.defaultZone=http://127.0.0.1:8761/eureka/
2.启动类添加 @EnableFeignClients 注解支持
在程序的启动类FeigndemoApplication 上加上@EnableEurekaClient注解开启Eureka Client的功能,通过注解@EnableFeignClients开启Feign Client的功能,代码如下:
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class FeigndemoApplication {
public static void main(String[] args) {
SpringApplication.run(FeigndemoApplication.class, args);
}
}
3.建立Client接口,并在接口中定义需调用的服务方法
通过上面步骤,程序就具备了Feign功能,现在实现一个简单的Feign Client。新建一个EurekaFeignClient接口,在接口上加@FeignClient注解来声明一个Feign Client,其中value值为远程调用其他服务的服务名,FeignConfig.class为Feign Client的配置类,在EurekaFeignClient接口内部有一个index()方法,该方法通过Feign来调用eureka-client1服务的“/hello”的API接口,代码如下:
@FeignClient(name = "eureka-client1",configuration = FeignConfig.class)
public interface EurekaFeignClient {
@GetMapping("/hello/{name}")
public String index(@PathVariable String name);
}
在FeignClient类上加上@Configuration注解,表明该类是一个配置类,并注入了一个BeanName为feignRetryer的Retryer的Bean。注入该Bean之后,Feign在远程调用失败后会进行重试。代码如下:
@Configuration
public class FeignConfig {
@Bean
public Retryer feignRetryer()
{
return new Retryer.Default(100, TimeUnit.SECONDS.toMillis(1),5);
}
}
4.使用Client接口
在Controller层的TestController注入EurekaFeignClient 的Bean,通过EurekaFeignClient 去调用index()方法,代码如下:
@Controller
public class TestController {
@Autowired
public EurekaFeignClient eurekaFeignClient;
@GetMapping("/test/{name}")
@ResponseBody
public String test(@PathVariable String name)
{
return eurekaFeignClient.index(name);
}
}
启动工程,端口号是8764;启动两个EurekaClient工程,端口号分别是8762和8763,将这两个工程注册以eureka-client1服务名称注册到端口号是8761的Eureka注册服务上。
在浏览器里多次访问http://127.0.0.1:8764/test/yangyayun,浏览器会轮询展示以下内容:
hello!yangyayun======端口号:8763
hello!yangyayun======端口号:8762