Spring Cloud Eureka 服务发现与消费
服务发现与消费,其服务发现的任务是由Eureka的客户端完成,而服务的消费任务由Ribbon、JerseyClient等完成,Ribbon是一个基于HTTP和TCP的客户端负载均衡器;使用JerseyClient是一个基于HTTP的客户端调用组件,需要需要增加负载均衡器,需要自己来实现,示例代码如下:
-
创建一个Spring Boot 的基础工程来实现服务消费者,并在pom.xml 中引入必须依赖项 spring-cloud-starter-eureka,引入 spring-cloud-starter-web 来实现Spring MVC 的REST服务进行测试
<?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>
<groupId>org.drsoft.consumer</groupId>
<artifactId>consumer-helloservice</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>consumer-helloservice</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Dalston.SR2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</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>
<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>
-
创建应用主类 ConsumerHelloserviceApplication ,通过 @EanbleDiscoveryClient 注解将应用注册为Eureka 客户端应用,以获取服务发现的能力,代码如下:
@EnableDiscoveryClient
@SpringBootApplication
public class ConsumerHelloserviceApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerHelloserviceApplication.class, args);
}
}
-
创建 ConsumerController 类,并实现 /consumer 接口,在接口中对服务的调用,这里使用的是服务的名而不是一个具体的地址,服务消费Java代码:
@RestController
public class ConsumerController {
@Autowired
private DiscoveryClient discoveryClient;
@RequestMapping (value = "/consumer", method = RequestMethod.GET)
public String helloConsumer() {
List<ServiceInstance> serviceInstanceList = discoveryClient.getInstances("org.drsoft.webservice.helloservice");
StringBuilder stringBuilder = new StringBuilder();
URI uri = null;
for (ServiceInstance instance : serviceInstanceList) {
stringBuilder.append("host=" + instance.getHost());
stringBuilder.append("port=" + instance.getPort());
stringBuilder.append("serviceId=" + instance.getServiceId());
stringBuilder.append("uri=" + instance.getUri());
stringBuilder.append("\n");
uri = instance.getUri();
}
String uriString = uri.toString() + "/hello/get";
stringBuilder.append("response body=" + new RestTemplate().getForEntity(uriString, String.class).getBody());
return stringBuilder.toString();
}
}
-
在 application.properties 中配置Eureka 服务注册中心的位置,需要和服务提供者的一致,否则是无法发现服务的,同时设置该消费者的端口号,代码如下:
server.port=8090
spring.application.name=consumer-helloservice
eureka.client.service-url.defaultZone=http://eurekaserver1:1111/eureka/,http://eurekaserver2:1112/eureka
-
启动consumer-helloservice应用后,我们可以在Eureka信息面板中看到,我们实现的 CONSUMER-HELLOSERVICE 服务,访问地址 http://localhost:8090/consumer 可以看到请求返回内容:
host=192.168.196.187port=8085serviceId=ORG.DRSOFT.WEBSERVICE.HELLOSERVICEuri=http://192.168.196.187:8085 response body=get ok