Service Registration and Discovery
该教程将带你学习Netflix Eureka 服务注册创建和使用
What You Will Build
在本教程中,将会建立一个Netflix Eureka服务注册中心,并创建一个客户端服务,它会向Eureka中注册它自身并使用它来解析自己的host。服务注册中心让客户端侧负载均衡以及将服务提供者 服务消费者解耦成为可能。
What You Need
- 大约 15 分钟
- 文本编辑器或者IDE
- java1.8及以后
- Gradle 7.5+或Maven3.5+
- 你还可以将本教程代码直接导入到你的IDE中
- Spring Tool Suite(STS)
- IntelliJ IDEA
- VSCode
How to complete this guide
和其他Spring开始教程一样,你可以跟随教程从零开始的学习,也可以跳过哪些你已经熟悉的项目初始化内容。
要从零开始,请移步 Starting with Spring Initializr。
要跳过基础内容,按照下面去做
- 下载本教程的源代码,
git clone https://github.com/spring-guides/gs-service-registration-and-discovery.git。 - 进入文件夹
gs-service-registration-and-discovery/initial - 进入章节Start a Eureka Service Registry学习
Starting with Spring Initializr
Spring Initializr可以让你快速地添加你需要的依赖,并且添加了很多的设置。
本教程会创建两个应用,一个服务端应用,只需要添加 Eureka Server 依赖,一个客户端应用,需要添加 Eureka Server 和 Eureka Discovery Client 依赖。
为了方便,我们提供了构建文件(
pom.xml和build.gradle),他们位于项目的顶层文件夹(service和client文件夹的父层级)。我们还在项目中添加了Maven and Gradle wrappers。
你可以使用 [预定义好的服务端项目](start.spring.io/#!type=mave… project for Spring Boot&packageName=com.example.service-registration-and-discovery&dependencies=cloud-eureka-server) 或者使用 [预定义好的客户端项目](start.spring.io/#!type=mave… project for Spring Boot&packageName=com.example.service-registration-and-discovery-client&dependencies=cloud-eureka-server,cloud-eureka),点击其Generate来下载zip包。
如果要手动初始化项目,可按照下面步骤
- 点击start.spring.io
- 选择Gradle或Maven以及你想要使用的编程语言,本教程使用的是Java
- 点击Dependencies并对于服务端应用选择Eureka Server,对于客户端应用选择Eureka Server和Eureka Discovery Client。
- 点击Generate
- 下载所生成的项目Zip文件。
一些IDE集成了Spring Initialzr,你也可以在IDE中完成这部分操作。
你还可以在Github中fork这个项目,并在文本编辑器或IDE中打开它、
Start a Eureka Service Registry
首先创建Eureka服务端,你可以使用SpringCloud的@EnableEurekaServer注解来建立一个注册中心。
package com.example.serviceregistrationanddiscoveryservice;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class ServiceRegistrationAndDiscoveryServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceRegistrationAndDiscoveryServiceApplication.class, args);
}
}
当注册中心启动时,它会打印相关堆栈,没有副本节点可以连接。当在正式环境时,你会想要设立多个副本。但在教程中为了简单,把这相关的打印关掉即可。
默认情况下,注册中心会注册自身,所以你需要把该行为禁用掉。
在eureka-service/src/main/resources/application.properties中添加一些配置项来满足目前的需求,如下所示
server.port=8761
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
logging.level.com.netflix.eureka=OFF
logging.level.com.netflix.discovery=OFF
Talking to the Registry
现在你有了一个标准的服务注册中心,你可以建立一个客户端,向注册中心注册自身,并使用的Spring Cloud DiscoveryClient来向注册中心询问自己的host和port。注解@EnableDiscoveryClient会激活Netflix Eureka DiscoveryClient实现。
package com.example.serviceregistrationanddiscoveryclient;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
public class ServiceRegistrationAndDiscoveryClientApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceRegistrationAndDiscoveryClientApplication.class, args);
}
}
@RestController
class ServiceInstanceRestController {
@Autowired
private DiscoveryClient discoveryClient;
@RequestMapping("/service-instances/{applicationName}")
public List<ServiceInstance> serviceInstancesByApplicationName(
@PathVariable String applicationName) {
return this.discoveryClient.getInstances(applicationName);
}
}
你很快就会看到eureka-client注册到以spring.application.name所配置的名称下。该配置项在SpringCloud经常被使用,因为在服务启动时被使用,所以为了方便,将其配置在eureka-client/src/main/resources/bootstrap.properties,该配置优先于src/main/resources/application.properties。
link:complete/eureka-client/src/main/resources/bootstrap.properties[]
eureka-client定义了一个返回所有注册的ServiceInstance实例的接口 http://localhost:8080/service-instances/a-bootiful-client。
Test the Application
首先启动eureka-service,一旦启动,启动eureka-client。
要使用Maven启动Eureka注册中心,运行下面的命令
./mvnw spring-boot:run -pl eureka-service
要使用Maven启动Eureka客户端,运行下面命令
./mvnw spring-boot:run -pl eureka-client
eureka-client会花费大约1分钟的时间注册它自己以及刷新他自己已注册的实例列表。访问接口http://localhost:8080/service-instances/a-bootiful-client,你会看到eureka-client的ServiceInstance出现在响应中。如果看到空的列表,那就等待一会再次访问。