Spring Boot - Eureka 服务器



Eureka 服务器是一个应用程序,它保存所有客户端服务应用程序的信息。每个微服务都将注册到 Eureka 服务器,并且 Eureka 服务器知道在每个端口和 IP 地址上运行的所有客户端应用程序。Eureka 服务器也称为发现服务器。

在本章中,我们将详细了解如何构建 Eureka 服务器。

构建 Eureka 服务器

Eureka 服务器与 Spring Cloud 捆绑在一起。为此,我们需要开发 Eureka 服务器并在默认端口 8761 上运行它。

访问 Spring Initializer 首页 https://start.spring.io/ 并下载带有 Eureka 服务器依赖项的 Spring Boot 项目。如下面的屏幕截图所示:

Build Eureka Server

在主 Spring Boot 应用程序类文件中下载项目后,我们需要添加 @EnableEurekaServer 注解。@EnableEurekaServer 注解用于使您的 Spring Boot 应用程序充当 Eureka 服务器。

主 Spring Boot 应用程序类文件的代码如下所示:

package com.tutorialspoint.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);
   }
}

确保在您的构建配置文件中添加了 Spring Cloud Eureka 服务器依赖项。

Maven 用户依赖项的代码如下所示:

<dependency>
<groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

Gradle 用户依赖项的代码如下所示:

compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-server')

完整的构建配置文件如下所示:

Maven - 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>3.3.3</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>
   <groupId>com.tutorialspoint</groupId>
   <artifactId>eurekaserver</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <name>eurekaserver</name>
   <description>Demo project for Spring Boot</description>
   <url/>
   <licenses>
      <license/>
   </licenses>
   <developers>
      <developer/>
   </developers>
   <scm>
      <connection/>
      <developerConnection/>
      <tag/>
      <url/>
   </scm>
   <properties>
      <java.version>21</java.version>
      <spring-cloud.version>2023.0.3</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>
      </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>

Gradle – build.gradle

buildscript {
   ext {
      springBootVersion = '3.3.3'
   }
   repositories {
      mavenCentral()
   }
   dependencies {
      classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
   }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'

group = 'com.tutorialspoint'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 21

repositories {
   mavenCentral()
}
ext {
   springCloudVersion = '2023.0.3'
}
dependencies {
   compile('org.springframework.cloud:spring-cloud-starter-eureka-server')
   testCompile('org.springframework.boot:spring-boot-starter-test')
}
dependencyManagement {
   imports {
      mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
   }
}

默认情况下,Eureka 服务器会将自身注册到发现中。您应该将以下给定的配置添加到您的 application.properties 文件或 application.yml 文件中。

application.properties 文件如下所示:

eureka.client.registerWithEureka = false
eureka.client.fetchRegistry = false
server.port = 8761

application.yml 文件如下所示:

eureka:
   client:
      registerWithEureka: false
      fetchRegistry: false
server:
   port: 8761

编译和执行

现在,您可以创建一个可执行的 JAR 文件,并使用以下所示的 Maven 或 Gradle 命令运行 Spring Boot 应用程序:

对于 Maven,请使用如下所示的命令:

mvn clean install

“BUILD SUCCESS”后,您可以在 target 目录下找到 JAR 文件。

对于 Gradle,您可以使用如下所示的命令:

gradle clean build

“BUILD SUCCESSFUL”后,您可以在 build/libs 目录下找到 JAR 文件。

现在,使用以下命令运行 JAR 文件:

 java –jar <JARFILE> 

您可以发现应用程序已在 Tomcat 端口 8761 上启动,如下所示:

Standard Commons Logging discovery in action with spring-jcl: please remove commons-logging.jar from classpath in order to avoid potential conflicts

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/

[32m :: Spring Boot :: [39m              [2m (v3.3.3)[0;39m

[2m2024-09-10T17:40:49.095+05:30[0;39m [32m INFO[0;39m [35m19372[0;39m [2m---[0;39m [2m[eurekaserver] [           main][0;39m [2m[0;39m[36mc.t.e.EurekaserverApplication           [0;39m [2m:[0;39m Starting EurekaserverApplication using Java 21.0.3 with PID 19372 (E:\Dev\eurekaserver\target\classes started by Tutorialspoint in E:\Dev\eurekaserver)
[2m2024-09-10T17:40:49.098+05:30[0;39m [32m INFO[0;39m [35m19372[0;39m [2m---[0;39m [2m[eurekaserver] [           main][0;39m [2m[0;39m[36mc.t.e.EurekaserverApplication           [0;39m [2m:[0;39m No active profile set, falling back to 1 default profile: "default"
...
[2m2024-09-10T17:40:52.658+05:30[0;39m [32m INFO[0;39m [35m19372[0;39m [2m---[0;39m [2m[eurekaserver] [           main][0;39m [2m[0;39m[36mc.n.eureka.cluster.PeerEurekaNodes      [0;39m [2m:[0;39m Adding new peer nodes [https://127.0.0.1:8761/eureka/]
...
[2m2024-09-10T17:40:52.971+05:30[0;39m [32m INFO[0;39m [35m19372[0;39m [2m---[0;39m [2m[eurekaserver] [       Thread-9][0;39m [2m[0;39m[36me.s.EurekaServerInitializerConfiguration[0;39m [2m:[0;39m Started Eureka Server
[2m2024-09-10T17:40:52.991+05:30[0;39m [32m INFO[0;39m [35m19372[0;39m [2m---[0;39m [2m[eurekaserver] [           main][0;39m [2m[0;39m[36mo.s.b.w.embedded.tomcat.TomcatWebServer [0;39m [2m:[0;39m Tomcat started on port 8761 (http) with context path '/'
[2m2024-09-10T17:40:52.992+05:30[0;39m [32m INFO[0;39m [35m19372[0;39m [2m---[0;39m [2m[eurekaserver] [           main][0;39m [2m[0;39m[36m.s.c.n.e.s.EurekaAutoServiceRegistration[0;39m [2m:[0;39m Updating port to 8761
[2m2024-09-10T17:40:53.012+05:30[0;39m [32m INFO[0;39m [35m19372[0;39m [2m---[0;39m [2m[eurekaserver] [           main][0;39m [2m[0;39m[36mc.t.e.EurekaserverApplication           [0;39m [2m:[0;39m Started EurekaserverApplication in 4.406 seconds (process running for 5.362)
[2m2024-09-10T17:40:54.031+05:30[0;39m [32m INFO[0;39m [35m19372[0;39m [2m---[0;39m [2m[eurekaserver] [on(4)-127.0.0.1][0;39m [2m[0;39m[36mo.a.c.c.C.[Tomcat].[localhost].[/]      [0;39m [2m:[0;39m Initializing Spring DispatcherServlet 'dispatcherServlet'
[2m2024-09-10T17:40:54.031+05:30[0;39m [32m INFO[0;39m [35m19372[0;39m [2m---[0;39m [2m[eurekaserver] [on(4)-127.0.0.1][0;39m [2m[0;39m[36mo.s.web.servlet.DispatcherServlet       [0;39m [2m:[0;39m Initializing Servlet 'dispatcherServlet'
[2m2024-09-10T17:40:54.033+05:30[0;39m [32m INFO[0;39m [35m19372[0;39m [2m---[0;39m [2m[eurekaserver] [on(4)-127.0.0.1][0;39m [2m[0;39m[36mo.s.web.servlet.DispatcherServlet       [0;39m [2m:[0;39m Completed initialization in 1 ms

现在,在您的 Web 浏览器中点击 URL https://127.0.0.1:8761/,您会发现 Eureka 服务器正在端口 8761 上运行,如下所示:

Eureka Server Running on port 8761
广告