文章目录
环境描述
启动一个 Eureka 服务端和 2 个服务客户端。
通过配置文件将2个客户端配置到服务注册上去,也就是通常所说的服务注册。
然后,通过 FeignClient 注解来进行服务间调用。
服务调用的一个解释:
引入 Feign 的依赖,在接口上使用 @FeignClient
注解,设置name为你要调用的服务的名字(即注册在Eureka上的那个服务名)。
然后,操作就和Spring MVC 的用法差不多了。直接进行 Rest 接口调用即可。
项目
在 Idea 中,先创建一个空的项目,然后依次创建以上图中的三个项目。
register-server
这个项目主要用于注册服务。
pom.xml文件
<?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>2.2.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>org.feng</groupId>
<artifactId>register-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>register-server</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR3</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</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-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</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.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
</project>
代码与配置
application.yml
# eureka 端口
server:
port: 8888
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
RegisterServerApplication
package org.feng;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class RegisterServerApplication {
public static void main(String[] args) {
SpringApplication.run(RegisterServerApplication.class, args);
}
}
eureka-client1
这个项目就是上面的图中的 Clinet1,属于服务的调用者。它将调用 Client2 中的一个服务。
pom.xml 文件
<?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>2.2.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>org.feng</groupId>
<artifactId>eureka-client-1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-client-1</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR3</spring-cloud.version>
</properties>
<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-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</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.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
代码与配置
application.yml
eureka:
client:
serviceUrl:
# eureka的注册中心地址
defaultZone: http://localhost:8888/eureka/
server:
# 此项目端口号
port: 8889
spring:
application:
# 注册进eureka的名字
name: client-1
service
package org.feng.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
/**
* 调用 client-2中的 /hello
* @see FeignClient 中的name是注册的服务名
*/
@FeignClient(name = "client-2")
@Service
public interface MyService {
@GetMapping("/hello")
String myService();
}
MyController
package org.feng.controller;
import org.feng.service.MyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyController {
private final MyService service;
@Autowired
public MyController(MyService service) {
this.service = service;
}
@GetMapping("/hello")
public String hello(){
return service.myService();
}
}
EurekaClient1Application
package org.feng;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@EnableFeignClients //表示需要 Feign
@EnableEurekaClient// 表示是一个 Eureka 的客户端
@SpringBootApplication
public class EurekaClient1Application {
public static void main(String[] args) {
SpringApplication.run(EurekaClient1Application.class, args);
}
}
eureka-client2
pom.xml 文件
文件内容与 eureka-client2 中的 pom.xml 的 依赖一致。
代码与配置
application.yml
指定服务名为 client-2
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8888/eureka/
server:
port: 8899
spring:
application:
name: client-2
MyController
内容和原先使用 SpringBoot 和 Spring MVC 时是一致的。这里强调的是写法一致。
package org.feng.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyController {
@GetMapping("/hello")
public String hello(){
return "你好!eureka-client-1,我是 client2!";
}
}
EurekaClient2Application
package org.feng;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@EnableFeignClients
@EnableEurekaClient
@SpringBootApplication
public class EurekaClient2Application {
public static void main(String[] args) {
SpringApplication.run(EurekaClient2Application.class, args);
}
}
运行结果
首先确认服务已经注册成功,可以在 UI 页面查看。
进入 UI 的 url 是http://localhost:8888,会这样去访问的原因是,我的注册服务器的yml
文件中的配置是指定了主机和端口号的。
下面地结果是,我使用客户端1 访问客户端2的 /hello
之后页面上出现的内容。
附加:Feign中使用熔断器
这个实际操作也挺少的。
因为 Feign 是已经将熔断器整合了,不过现在的版本是默认关闭的。
原因参见: https://github.com/spring-cloud/spring-cloud-netflix/issues/1277
配置开启熔断器
在原先的 yml基础上增加以下内容:
以 client1为例:
feign:
hystrix:
enabled: true
注解与代码的修改
MyService
package org.feng.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
/**
* 使用熔断器时,必须加上 fallback属性,其中的类是必须实现 MyService 本接口的实现类
* @see FeignClient 中的name是注册的服务名
*/
@FeignClient(name = "client-2", fallback = MyFallbackService.class)
@Service
public interface MyService {
@GetMapping("/hello")
String myService();
}
MyService 的实现类
注意需要将其放入容器,可以使用 Service
注解或 Component
package org.feng.service;
import org.springframework.stereotype.Component;
@Component
public class MyFallbackService implements MyService {
@Override
public String myService() {
return "被熔断器弄断了";
}
}
测试运行
略