一、Feign简介
Feign是一个声明式的伪Http客户端,它使得写Http客户端变得更简单。使用Feign,只需要创建一个接口并注解。Feign默认集成了Ribbon,与注册中心结合,默认实现了负载均衡的效果。
简而言之:
1、Feign 采用的是基于接口的注解
2、Feign 整合了ribbon,具有负载均衡的能力
3、整合了Hystrix,具有熔断的能力
二、准备工作
继续第一章的内容,启动eureka-server,端口8000。修改其中的提供者eureka-client-a。增加一个接口用于后面测试使用,代码如下:
@RestController
public class IndexController {
@Value("${server.port}")
private String port;
@Value("${spring.application.name}")
private String applicationName;
@GetMapping("/hello")
public String hello() {
return "applicationName is: " + applicationName + ", port is: " + port;
}
}
增加完成后,启动两个eureka-client-a项目,端口8001和8003,这为了后面测试负载均衡。
三、引入FeignClient
创建一个springboot项目,取名为eureka-client-b(具体创建方法,参考第一张创建eureka-client的步骤),在他的pom文件中引入feign的依赖,代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-cloud-demo</artifactId>
<groupId>com.cxlsky.www</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>eureka-client-b</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
</project>
修改application.yml配置文件,如下:
server:
port: 8002
spring:
application:
name: eureka-client-b
eureka:
client:
serviceUrl:
defaultZone: http://10.1.81.33:8000/eureka/
在主类上增加注解@EnableFeignClients
,并将其表明为服务提供者,注册到注册中心。代码如下:
@EnableDiscoveryClient
@SpringBootApplication(scanBasePackages = {"com.cxlsky"})
@EnableFeignClients(basePackages = {"com.cxlsky"})
public class EurekaClientBApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientBApplication.class, args);
}
}
定义一个feign接口,来调用我们其他的服务,这里我们调用eureka-client-a服务的/hello接口。代码如下:
@FeignClient(value = "eureka-client-a")
public interface ClientAFeignClient {
/**
* 调用eureka-client-a的/hello接口
*
* @return
*/
@GetMapping("/hello")
String hello();
}
然后在controller层,对外暴露一个"/hello/from/a"的API接口,通过上面定义的Feign客户端ClientAFeignClient来调用服务。代码如下:
@RestController
public class IndexController {
@Autowired
private ClientAFeignClient clientAFeignClient;
@Value("${server.port}")
private String port;
@Value("${spring.application.name}")
private String applicationName;
@GetMapping("/hello/from/a")
public String helloFromA() {
String hello = clientAFeignClient.hello();
return "this is " + applicationName + ", port: " + port + " and receive hello from: " + hello;
}
}
四、测试结果
到此为止,代码写完了,启动项目,然后通过postman调用localhost:8002/hello/from/a接口进行测试。
可以看到交替进行的结果:
this is eureka-client-b, port: 8002 and receive hello from: applicationName is: eureka-client-a, port is: 8001
this is eureka-client-b, port: 8002 and receive hello from: applicationName is: eureka-client-a, port is: 8003
可以看出feign自带了负载均衡的效果。