本文是SpringCloud实战(三)-服务间调用(Feign),若要关注前文,请点击传送门:
SpringCloud实战(二)-服务间调用(ribbon+restTemplate)
前文我们基于ribbon+restTemplate实现了服务间调用,本文我们基于Feign来实现相同的效果,这两者其实没有什么区别,Feign也是基于ribbon来进行的负载均衡。
一、Feign简介
Feign是一个声明似的web服务客户端,它使得编写web服务客户端变得更加容易。使用Feign创建一个接口并对它进行注解。它具有可插拔的注解支持包括Feign注解与JAX-RS注解,Feign还支持可插拔的编码器与解码器,Spring Cloud 增加了对 Spring MVC的注解,Spring Web 默认使用了HttpMessageConverters, Spring Cloud 集成 Ribbon 和 Eureka 提供的负载均衡的HTTP客户端 Feign。
二、准备工作
我们需要基于上一篇文章中的Eureka服务端,启动Eureka服务端集群,启动service-hi工程,它的端口为8762,将service-hi的配置文件的端口改为8763,再次启动,此时我们有可以在服务注册列表中看到service-hi的两个实例,图示如下:
此时我们已经启动了5个节点,三个Eureka Server,两个Eureka Client,图示如下:
三、创建服务消费者
重新新建一个spring-boot工程,取名为:service-feign,在它的pom.xml文件分别引入起步依赖spring-cloud-starter-netflix-eureka-client、spring-cloud-starter-openfeig、spring-boot-starter-web。
3.1 maven依赖
<?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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.oal.microservice</groupId>
<artifactId>openAiLab</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.oal.microservice</groupId>
<artifactId>service-feign</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>service-feign</name>
<description>feign</description>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<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.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.2 配置文件(application.yml)
eureka:
client:
serviceUrl:
defaultZone: http://eureka-serve-01:8761/eureka/
server:
port: 8765
spring:
application:
name: service-feign
指定服务的注册中心地址为http://eureka-serve-01:8761/eureka/,程序名称为 service-feign,程序端口为8765。
3.3 启动类(ServiceFeignApplication)
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class ServiceFeignApplication {
public static void main(String[] args) {
SpringApplication.run( ServiceFeignApplication.class, args);
}
}
通过@EnableFeignClients启动Feign组件。
3.4 Controller层
@RestController
public class HelloController {
@Resource
ScheduleServiceHi scheduleServiceHi;
@RequestMapping(value = "/hi")
public String hi(@RequestParam String name){
return "service-feign:" + scheduleServiceHi.sayHiFromClientOne(name);
}
}
3.5 Service层
@FeignClient(value = "service-hi")
public interface ScheduleServiceHi {
@RequestMapping(value = "/hi",method = RequestMethod.GET)
String sayHiFromClientOne(@RequestParam(value = "name") String name);
}
启动service-feign,并访问 http://localhost:8765/hi?name=zzx,浏览器交替显示:
service-feign:hi zzx ,i am from port:8762
service-feign:hi zzx ,i am from port:8763
这说明当我们Feign已经做了负载均衡,访问了不同的端口的服务实例。