使用Eureka注册服务



本章将详细讲解如何将Spring Boot微服务应用程序注册到Eureka Server。在注册应用程序之前,请确保Eureka Server正在8761端口运行,或者首先构建Eureka Server并运行它。有关构建Eureka服务器的更多信息,您可以参考上一章Spring Boot - Eureka Server

构建Eureka客户端

Eureka客户端包含在Spring Cloud包中。为此,我们需要开发Eureka客户端并在默认端口8080上运行它。

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

Build Eureka Client

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

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

package com.tutorialspoint.eurekaclient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class EurekaclientApplication {
   public static void main(String[] args) {
      SpringApplication.run(EurekaclientApplication.class, args);
   }
}

确保在您的构建配置文件中添加了Spring Cloud Eureka客户端依赖项。

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

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

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

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

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

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-client</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-client')
   testCompile('org.springframework.boot:spring-boot-starter-test')
}
dependencyManagement {
   imports {
      mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
   }
}

要将Spring Boot应用程序注册到Eureka Server,我们需要在我们的application.properties文件或application.yml文件中添加以下配置,并在我们的配置中指定Eureka Server URL。

application.yml文件的代码如下所示:

eureka:
   client:
      serviceUrl:
         defaultZone: https://127.0.0.1:8761/eureka
      instance:
      preferIpAddress: true
spring:
   application:
      name: eurekaclient

application.properties文件的代码如下所示:

eureka.client.serviceUrl.defaultZone  = https://127.0.0.1:8761/eureka
eureka.client.instance.preferIpAddress = true
spring.application.name = eurekaclient

现在,在主Spring Boot应用程序中添加返回字符串的Rest端点,并在构建配置文件中添加Spring Boot Starter Web依赖项。观察下面的代码:

package com.tutorialspoint.eurekaclient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class EurekaclientApplication {

   public static void main(String[] args) {
      SpringApplication.run(EurekaclientApplication.class, args);
   }

   @GetMapping(value = "/")
   public String home() {
      return "Eureka Client application";
   }
}

编译和执行

您可以创建一个可执行的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端口8080上启动,并且Eureka客户端应用程序已注册到Eureka Server,如下所示:

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-11T12:46:03.672+05:30[0;39m [32m INFO[0;39m [35m19164[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 19164
...
[2m[0;39m[36mc.n.e.registry.AbstractInstanceRegistry [0;39m [2m:[0;39m Registered instance EUREKACLIENT/DESKTOP-DTHL8BI:eurekaclient with status UP (replication=false)
[2m2024-09-11T12:54:08.159+05:30[0;39m [32m INFO[0;39m [35m19164[0;39m [2m---[0;39m [2m[eurekaserver] [nio-8761-exec-3][0;39m [2m[0;39m[36mc.n.e.registry.AbstractInstanceRegistry [0;39m [2m:[0;39m Registered instance EUREKACLIENT/DESKTOP-DTHL8BI:eurekaclient with status UP (replication=true)

在您的Web浏览器中访问URL https://127.0.0.1:8761/,您可以看到Eureka客户端应用程序已注册到Eureka Server。

Eureka Client Application

现在在您的Web浏览器中访问URL https://127.0.0.1:8080/,查看Rest端点输出。

Eureka Client Application Output
广告