一、Feign简介
Feign是一个声明式的伪Http客户端,它使得写Http客户端变得更简单。使用Feign,只需要创建一个接口
并注解。它具有可插拔
的注解特性,可使用Feign 注解和JAX-RS注解。Feign支持可插拔的编码器和解码器
。Feign默认集成了Ribbon,并和Eureka结合,默认实现了负载均衡
的效果。
简而言之:
-
Feign 采用的是基于接口的注解
-
Feign 整合了ribbon
二、准备工作
1.搭建并启动eureka服务
-
新建springboot项目
-
名称随意
-
添加依赖,这里只需
eureka server
即可
-
选择项目保存位置,可以按需修改
-
建好后先在命令行打包检查项目依赖是否有问题
-
在resource目录下新建配置文件
application.yml
,内容如下:server: port: 8761 spring: application: name: eureka-server eureka: client: registerWithEureka: false fetchRegistry: false serviceUrl: defaultZone: http://127.0.0.1:${server.port}/eureka/
-
在启动类上添加注解
@EnableEurekaServer
-
启动项目,访问 http://127.0.0.1:8761
至此eureka服务搭建完成。
2.创建一个Feign服务
此处取名为feign-load-balance
,对应的pom.xml
如下:
需要注意的是pom中的Springboot和SpringCloud之间版本对应关系
可以移步至此查看对应版本:https://start.spring.io/actuator/info
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.feign.load.balance</groupId>
<artifactId>feign-load-balance</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>feign-load-balance</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Dalston.SR1</spring-cloud.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</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-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
在配置文件application.yml中指定程序名端口号和eureka注册中心地址
spring:
application:
name: feign-load-balance
server:
port: 9091
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:8761/eureka/
在程序启动类上加上相关注解:
定义一个feign接口,通过@ FeignClient(“服务名”),来指定调用哪个服务。比如在代码中调用了service-hi服务的“/hi”接口,代码如下:
/**
* @Author YanQiQi
* @Date 2020-03-10 16:33
**/
@FeignClient(value = "service-hi")
public interface ScheduleServiceHi {
/**
* sayHiFromClientOne
*
* @param name name
* @return String
*/
@GetMapping(value = "/hi")
String sayHiFromClientOne(@RequestParam(value = "name") String name);
}
在Web层的controller层,对外暴露一个"/hi"的API接口,通过上面定义的Feign客户端SchedualServiceHi 来消费服务。代码如下:
/**
* @Author YanQiQi
* @Date 2020-03-10 16:28
**/
@RestController
public class HiController {
@Autowired
private ScheduleServiceHi scheduleServiceHi;
@GetMapping("/hi")
public String sayHello(@RequestParam String name) {
return scheduleServiceHi.sayHiFromClientOne(name);
}
}
-
启动feign-load-balance项目,访问eureka注册中心,可以看到服务注册成功,Feign服务即创建启动完成。
3.创建service-hi(示例服务),使用不同的端口启动,用于测试负载均衡
-
service-hi
服务的搭建与上面类似,对应的pom.xml
如下:
同样也需要注意Springboot和SpringCloud版本的对应
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com</groupId>
<artifactId>service-hi</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>service-hi</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Dalston.SR1</spring-cloud.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</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-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- 在配置文件application.yml中指定程序名端口号和eureka注册中心地址
spring:
application:
name: service-hi
server:
port: 9093
# [9092,9093...]可以多个端口分别启动进行负载均衡
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:8761/eureka/
-
启动类添加相关注解
@SpringBootApplication @EnableEurekaClient @EnableFeignClients public class ServiceHiApplication { public static void main(String[] args) { SpringApplication.run(ServiceHiApplication.class, args); } }
-
Controller定义Feign服务要调用的接口,此处为
/hi
/** * @Author YanQiQi * @Date 2020-03-10 17:14 **/ @RestController public class HiController { @Autowired private MyConfig myConfig; @GetMapping("/hi") public String sayHi(@RequestParam String name) { return "hi " + name + "your ip.port is " + myConfig.getPort(); } }
-
分别用
9092
和9093
端口启动服务单个服务多个端口启动时需要在启动时允许并行运行
-
启动服务后,eureka注册中心如下:
其中service-hi
在两个端口启动,Feign服务端调用时会负载均衡到不同的服务上
三、测试结果
多次访问Feign服务端端口可以发现已经实现负载均衡的效果
至此,使用Feign消费服务实现负载均衡完成。
项目源码已经上传至Github
https://github.com/StarryNightBlue/feign-load-balance-demo.git
四、参考资料
https://projects.spring.io/spring-cloud/spring-cloud.html#spring-cloud-feign
网友评论