Spring Boot 简明教程

Service Registration with Eureka

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

In this chapter, you are going to learn in detail about How to register the Spring Boot Micro service application into the Eureka Server. Before registering the application, please make sure Eureka Server is running on the port 8761 or first build the Eureka Server and run it. For further information on building the Eureka server, you can refer to the previous chapter.

首先,你需要在我们的构建配置文件中添加以下依赖项来将微服务注册到 Eureka Server 中。

First, you need to add the following dependencies in our build configuration file to register the microservice with the Eureka server.

Maven 用户可以在 pom.xml 文件中添加以下依赖项 −

Maven users can add the following dependencies into the pom.xml file −

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

Gradle 用户可以在 build.gradle 文件中添加以下依赖项 −

Gradle users can add the following dependencies into the build.gradle file −

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

现在,我们需要在 Spring Boot 应用程序主类文件中添加 @EnableEurekaClient 注解。@EnableEurekaClient 注解让你的 Spring Boot 应用程序充当 Eureka 客户端。

Now, we need to add the @EnableEurekaClient annotation in the main Spring Boot application class file. The @EnableEurekaClient annotation makes your Spring Boot application act as a Eureka client.

Spring Boot 应用程序主类如下所示 −

The main Spring Boot application is as given below −

package com.tutorialspoint.eurekaclient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

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

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

To register the Spring Boot application into Eureka Server we need to add the following configuration in our application.properties file or application.yml file and specify the Eureka Server URL in our configuration.

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

The code for application.yml file is given below −

eureka:
   client:
      serviceUrl:
         defaultZone: http://localhost:8761/eureka
      instance:
      preferIpAddress: true
spring:
   application:
      name: eurekaclient

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

The code for application.properties file is given below −

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

现在,添加 Rest Endpoint 在 Spring Boot 应用程序中返回字符串,以及 Spring Boot Starter web 依赖项在构建配置文件中。观察以下给出的代码 −

Now, add the Rest Endpoint to return String in the main Spring Boot application and the Spring Boot Starter web dependency in build configuration file. Observe the code given below −

package com.tutorialspoint.eurekaclient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableEurekaClient
@RestController
public class EurekaclientApplication {
   public static void main(String[] args) {
      SpringApplication.run(EurekaclientApplication.class, args);
   }
   @RequestMapping(value = "/")
   public String home() {
      return "Eureka Client application";
   }
}

整个配置文件如下所示。

The entire configuration file is given below.

For Maven user - pom.xml

For Maven user - 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
   http://maven.apache.org/xsd/maven-4.0.0.xsd">

   <modelVersion>4.0.0</modelVersion>
   <groupId>com.tutorialspoint</groupId>
   <artifactId>eurekaclient</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>jar</packaging>

   <name>eurekaclient</name>
   <description>Demo project for Spring Boot</description>

   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>1.5.9.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>

   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
      <java.version>1.8</java.version>
      <spring-cloud.version>Edgware.RELEASE</spring-cloud.version>
   </properties>

   <dependencies>
      <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-eureka</artifactId>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</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>

</projecta>

For Gradle user – build.gradle

For Gradle user – build.gradle

buildscript {
   ext {
      springBootVersion = '1.5.9.RELEASE'
   }
   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 = 1.8

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

您可以创建一个可执行 JAR 文件并使用以下 Maven 或 Gradle 命令运行 Spring Boot 应用程序 -

You can create an executable JAR file, and run the Spring Boot application by using the following Maven or Gradle commands −

对于 Maven,可以使用以下命令:

For Maven, you can use the following command −

mvn clean install

“BUILD SUCCESS”之后,您可以在目标目录中找到 JAR 文件。

After “BUILD SUCCESS”, you can find the JAR file under the target directory.

对于 Gradle,可以使用以下命令:

For Gradle, you can use the following command −

gradle clean build

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

After “BUILD SUCCESSFUL”, you can find the JAR file under the build/libs directory.

现在,使用命令运行 JAR 文件,如下所示 −

Now, run the JAR file by using the command as shown −

java –jar <JARFILE>

现在,该应用程序已经使用 https(如下图所示)在 Tomcat 端口 8080 上启动,并且 Eureka Client 应用程序已经注册到 Eureka Server 中 −

Now, the application has started on the Tomcat port 8080 and Eureka Client application is registered with the Eureka Server as shown below −

started application on tomcat port

访问 URL [role="bare"] [role="bare"]http://localhost:8761/ 中的 Web 浏览器,你就可以看到 Eureka Client 应用程序已注册到 Eureka Server 中。

Hit the URL [role="bare"]http://localhost:8761/ in your web browser and you can see the Eureka Client application is registered with Eureka Server.

eureka client application

现在在你的网页浏览器中点击 URL http://localhost:8080/ 并查看 rest 端点的输出。

Now hit the URL http://localhost:8080/ in your web browser and see the Rest Endpoint output.

eureka client application output