Spring Boot 简明教程

Spring Boot - Eureka Server

Eureka 服务器是一个保存所有客户端服务应用程序的信息的应用程序。每个微服务将在 Eureka 服务器中进行注册,Eureka 服务器知道运行在每个端口和 IP 地址上的所有客户端应用程序。Eureka 服务器还可以被称为发现服务器。

Eureka Server is an application that holds the information about all client-service applications. Every Micro service will register into the Eureka server and Eureka server knows all the client applications running on each port and IP address. Eureka Server is also known as Discovery Server.

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

In this chapter, we will learn in detail about How to build a Eureka server.

Building a Eureka Server

Eureka 服务器随 Spring Cloud 软件包提供。为此,我们需要开发 Eureka 服务器并在默认端口 8761 上运行它。

Eureka Server comes with the bundle of Spring Cloud. For this, we need to develop the Eureka server and run it on the default port 8761.

访问 Spring Initializer 主页 https://start.spring.io/ ,并使用 Eureka 服务器依赖项下载 Spring Boot 项目。它在下面屏幕截图中展示 −

Visit the Spring Initializer homepage https://start.spring.io/ and download the Spring Boot project with Eureka server dependency. It is shown in the screenshot below −

build eureka server

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

After downloading the project in main Spring Boot Application class file, we need to add @EnableEurekaServer annotation. The @EnableEurekaServer annotation is used to make your Spring Boot application acts as a Eureka Server.

main Spring Boot 应用程序类文件代码如下所示 −

The code for main Spring Boot application class file is as shown below −

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 服务器依赖项。

Make sure Spring cloud Eureka server dependency is added in your build configuration file.

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

The code for Maven user dependency is shown below −

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

Gradle 用户依赖项代码如下 −

The code for Gradle user dependency is given below −

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

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

The complete build configuration file is given below −

Maven pom.xml

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

   <modelVersion>4.0.0</modelVersion>
   <groupId>com.tutorialspoint</groupId>
   <artifactId>eurekaserver</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>jar</packaging>
   <name>eurekaserver</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-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

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

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

By default, the Eureka Server registers itself into the discovery. You should add the below given configuration into your application.properties file or application.yml file.

application.properties 文件如下 −

application.properties file is given below −

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

application.yml 文件如下 −

The application.yml file is given below −

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

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

Now, you can create an executable JAR file, and run the Spring Boot application by using the Maven or Gradle commands shown below −

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

For Maven, use the command as shown below −

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 command shown below −

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 following command −

 java –jar <JARFILE>

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

You can find that the application has started on the Tomcat port 8761 as shown below −

application started on tomcat port 8761

现在,在您的网络浏览器中点击 URL http://localhost:8761/ ,您会发现 Eureka Server 在端口 8761 上运行,如下所示 −

Now, hit the URL http://localhost:8761/ in your web browser and you can find the Eureka Server running on the port 8761 as shown below −

eureka server running on port 8761