使用Spring Boot设置Netflix Eureka服务器和客户端

原始地址:https://dev.to/nagarajendra/netflix-eureka-server-and-client-setup-with-spring-boot-3j9n

概述

  • 我们将设置一个Eureka服务器(服务注册表,用于注册多个服务/微服务)。
  • 我们将设置多个Eureka客户端(注册到Eureka服务器的REST服务)。
  • 我们将通过Eureka进行客户端负载均衡和服务发现。
    这是视频教程。请给我的频道点赞并订阅。

设置Eureka服务器

设置eureka服务器所需的依赖项是 spring-cloud-starter-netflix-eureka-server,同时也需要 spring-boot-starter-parent。您可以通过进入"sprong-cloud-starter-netflix-eureka-client"并提供组ID、artifact ID以及所需的依赖项来生成代码,如下所示,并点击生成,这将生成包含依赖项的示例代码到您的 https://start.spring.io/pom.xml文件中。

相应的 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.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>eurekaserver</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eurekaserver</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR1</spring-cloud.version>
</properties>
<dependencies>
<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>
</project>

在打开该项目后,用注解 @EnableEurekaServer 标记主Springboot类,然后添加 @SpringbootApplication 注解,类应该像下面这样:

package com.example.eurekaserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaserverApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaserverApplication.class, args);
}
}

设置服务器的最后一步是添加 application.yml 文件,当启动应用程序时,该文件将具有与eureka相关的属性。如果找不到 @EnableEurekaServer 文件,它将查找 bootstrap.yml 文件以获取属性,yml文件应该像下面这样:

application.yml
server:
port: 9090
eureka:
client:
register-with-eureka: false
fetch-registry: false
serviceUrl:
defaultZone: http://localhost:9090/eureka/

上面的属性yaml文件定义了eureka在哪里运行的 defaultZone 。serviceUrl 属性设置为false,因为我们不希望将Eureka服务器注册到自身。

一切准备就绪后,项目结构应该如下图所示。然后,启动Spring Boot应用程序,Eureka服务器应该在您在 application.yml 文件中指定的任何端口上运行,例如 localhost:9090

搞定,我们已经设置好了Eureka服务器。

现在,我们将继续创建两个名为client1和client2的客户端,就像上述所示,同时添加一个Eureka Discovery Client依赖。

创建了两个项目后,我们将添加 @EnableDiscoveryClient 注解到根类上,如下所示:SprintBootApplication Client1 Client2

package com.example.client2;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class Client2Application {
public static void main(String[] args) {
SpringApplication.run(Client2Application.class, args);
}
}

类似地,我们需要更新application.yml文件,如下所示,提供所需的eureka配置详细信息。

spring:
application:
name: 'client1'
server:
port: 8081
eureka:
instance:
hostname: ${vcap.application.uris[0]:localhost}
prefer-ip-address: true
lease-renewal-interval-in-seconds: 10
lease-expiration-duration-in-seconds: 20
client:
service-url:
defaultZone: http://localhost:9090/eureka

在上面的代码中,我们提供了spring应用程序名称和eureka客户端服务URL,其中eureka服务器所在的eureka client service URL的位置。因此,一旦启动客户端,它将查找服务URL并尝试注册自己。属性 lease-renewal-interval-in-seconds 允许Eureka服务器何时检查客户端的健康状况。更改完成后,启动两个客户端。lease-renewal-interval-in-seconds为10,lease-expiration-duration-in-seconds为20。

如果返回到Eureka服务器,您现在将能够看到以 client1 client2 为名字注册的两个客户端,如下所示:CLIENT2

现在设置已经完成,这两个客户端可以通过服务发现进行通信。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值