启动和使用 Netflix Eureka 服务注册表的过程

时间:2022-12-23 19:03:42

启动和使用 Netflix Eureka 服务注册表的过程

本指南将引导您完成启动和使用 Netflix Eureka 服务注册表的过程。

您将构建什么

您将设置一个Netflix 尤里卡服务注册表然后生成一个客户端,该客户端既向注册表注册自身,又使用它来解析自己的主机。服务注册表非常有用,因为它支持客户端负载平衡,并将服务提供商与使用者分离,而无需 DNS。

你需要什么

  • 约15分钟
  • 最喜欢的文本编辑器或 IDE
  • JDK 1.8或以后
  • 格拉德尔 4+​或梅文 3.2+
  • 您也可以将代码直接导入到 IDE 中:
  • 弹簧工具套件 (STS)
  • 智能理念
  • VSCode

如何完成本指南

像大多数春天一样入门指南,您可以从头开始并完成每个步骤,也可以绕过您已经熟悉的基本设置步骤。无论哪种方式,您最终都会得到工作代码。

要从头开始,请继续从 Spring 初始化开始.

要跳过基础知识,请执行以下操作:

  • 下载​并解压缩本指南的源存储库,或使用吉特:git clone https://github.com/spring-guides/gs-service-registration-and-discovery.git
  • 光盘成gs-service-registration-and-discovery/initial
  • 跳转到启动尤里卡服务注册表.

完成后,您可以根据 中的代码检查结果。​​gs-service-registration-and-discovery/complete​

从 Spring 初始化开始

对于所有 Spring 应用程序,您应该从Spring Initializr.Initializr 提供了一种快速的方式来提取应用程序所需的所有依赖项,并为您完成大量设置。

本指南需要两个应用程序。第一个应用程序(服务应用程序)只需要 Eureka 服务器依赖项。

第二个应用程序(客户端应用程序)需要 Eureka Server 和 Eureka Discovery Client 依赖项。

为方便起见,我们在项目顶部(和目录上方的一个目录)提供了构建文件(文件和文件),您可以使用这些文件同时构建两个项目。我们还在那里添加了Maven和Gradle包装器。​​pom.xml​​​​build.gradle​​​​service​​​​client​

你可以使用这个预初始化项目(对于服务应用程序)或此预初始化项目(对于客户端应用程序),然后单击生成以下载 ZIP 文件。此项目配置为适合本教程中的示例。

手动初始化项目:

  1. 导航到https://start.spring.io.此服务拉入应用程序所需的所有依赖项,并为您完成大部分设置。
  2. 选择 Gradle 或 Maven 以及您要使用的语言。本指南假定您选择了 Java。
  3. 单击依赖关系,然后为服务应用程序选择 Eureka 服务器,为客户端应用程序选择 Eureka 服务器和 Eureka 发现客户端
  4. 单击生成
  5. 下载生成的 ZIP 文件,该文件是配置了您选择的 Web 应用程序的存档。

如果您的 IDE 集成了 Spring Initializr,则可以从 IDE 完成此过程。

您也可以从 Github 分叉项目,然后在 IDE 或其他编辑器中打开它。

启动尤里卡服务注册表

您首先需要一个尤里卡服务注册表。您可以使用Spring Cloud来建立其他应用程序可以与之通信的注册表。这是一个常规的 Spring 引导应用程序,添加了一个注释 () 以启用服务注册表。以下清单(来自 )显示了服务应用程序:​​@EnableEurekaServer​​​​@EnableEurekaServer​​​​eureka-service/src/main/java/com.example.serviceregistrationanddiscoveryservice/ServiceRegistrationAndDiscoveryServiceApplication.java​

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

与注册处交谈

现在,您已经启动了一个服务注册表,您可以建立一个客户端,该客户端既向注册表注册自身,又使用 Spring Cloud 抽象来查询注册表中是否有自己的主机和端口。激活Netflix Eureka实施。(其他服务注册表还有其他实现,例如​​DiscoveryClient​​​​@EnableDiscoveryClient​​​​DiscoveryClient​​桥公司领事或阿帕奇动物园管理员).以下清单(来自 )显示了客户端应用程序:​​eureka-client/src/main/java/example/serviceregistrationanddiscoveryclient/ServiceRegistrationAndDiscoveryClientApplication.java​

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);
}
}

无论选择哪种实现,您都应该很快看到以您在属性中指定的任何名称注册。此属性在 Spring Cloud 中大量使用,通常在服务配置的最早阶段。此属性用于服务引导程序,因此,按照惯例,此属性位于之前找到它的位置。以下清单显示了该文件:​​eureka-client​​​​spring.application.name​​​​eureka-client/src/main/resources/bootstrap.properties​​​​src/main/resources/application.properties​​​​bootstrap.properties​

Unresolved directive in <stdin> - include::complete/eureka-client/src/main/resources/bootstrap.properties[]

定义了一个Spring MVC REST端点(),该端点返回所有已注册实例的枚举。请参阅的​​eureka-client​​​​ServiceInstanceRestController​​​​ServiceInstance​​​​http://localhost:8080/service-instances/a-bootiful-client​​构建一个 RESTful Web 服务了解有关使用 Spring MVC 和 Spring Boot 构建 REST 服务的更多信息的指南。

测试应用程序

通过启动第一个来测试端到端结果,然后在加载后启动 .​​eureka-service​​​​eureka-client​

要使用 Maven 运行 Eureka 服务,请在终端窗口(在目录中)中运行以下命令:​​/complete​

./mvnw spring-boot:run -pl eureka-service

要使用 Maven 运行 Eureka 客户端,请在终端窗口(在目录中)中运行以下命令:​​/complete​

./mvnw spring-boot:run -pl eureka-client

要使用 Gradle 运行 Eureka 服务,请在终端窗口(在目录中)中运行以下命令:​​/complete​

./gradlew :eureka-service:bootRun

要使用 Gradle 运行 Eureka 客户端,请在终端窗口(在目录中)中运行以下命令:​​/complete​

./gradlew :eureka-client:bootRun

在注册表中注册自身并从注册表刷新其自己的已注册实例列表大约需要一分钟。在浏览器中访问 。在那里,您应该看到响应中反映的 for 。如果看到空元素,请稍等片刻,然后刷新页面。​​eureka-client​​​​eureka-client​​​​http://localhost:8080/service-instances/a-bootiful-client​​​​ServiceInstance​​​​eureka-client​​​​<List>​

总结

祝贺!您刚刚使用Spring建立了Netflix Eureka服务注册表,并在客户端应用程序中使用该注册表。