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://: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://:8761/,您會發現 Eureka 伺服器正在埠 8761 上執行,如下所示:

Eureka Server Running on port 8761
廣告