在本教程中,将学习如何启动自己的Eureka Discovery Server,以便构建的微服务可以在该服务器上注册,并可以被其他微服务发现。
创建一个新的Spring Boot App
Eureka Discovery Server 是一个Spring Boot 应用程序。因此,要为目创建新的Eureka Discovery服务器,我们首先需要创建一个新的Spring Boot 应用程序。按照本教程,了解如何创建一个非常简单的Spring Boot应用程序,该应用程序支持RESTful Web Services的开发。
配置Eureka Discovery Server POM.XML文件
现在,创建了Spring Boot Application时,打开其POM.xml文件并添加以下依赖项。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-eureka-server</artifactId>
<type>jar</type>
</dependency>
以上两个依赖项添加到.
然后将以下部分也添加到POM.xml文件中。
<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>
并添加,设置java版本 :
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.RC2</spring-cloud.version>
</properties>
完整的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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.appsdeveloperblog.photoapp.discovery</groupId>
<artifactId>PhotoAppDiscoveryService</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>PhotoAppDiscoveryService</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.RC2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-eureka-server</artifactId>
<type>jar</type>
</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>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
</project>
增加Eureka Server注解
现在,在项目的POM.xml文件中添加了所需的依赖项时,就可以开始使用Eureka Server注解了。 为了使应用程序能够用作Eureka Server,需要向包含公共静态void main(String [] args)函数的Application类中添加@EnableEurekaServer注解。 下面是Java类的示例,在类名称上方添加了@EnableEurekaServer注解。
package com.xarhsoft.photoapp.discovery.PhotoAppDiscoveryService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class PhotoAppDiscoveryServiceApplication {
public static void main(String[] args) {
SpringApplication.run(PhotoAppDiscoveryServiceApplication.class, args);
}
}
配置application.properties文件:
server.port=8010
spring.application.name=PhotoAppApi-eureka-server
eureka.client.registerWithEureka=false
eureka.client.fetchRegistry=false
eureka.client.serviceUrl.defaultZone = http://localhost:8010/eureka
- server.port – eureka服务端口号
- spring.application.name – eureka服务应用的名称
- eureka.client.registerWithEureka 和 eureka.client.fetchRegistry 将它们设置为false是因为我们希望阻止该Spring Boot应用程序向发现服务注册,因为该应用程序本身就是发现服务。
启动Eureka发现服务
现在准备启动新的Eureka Discovery Server,由于它是Spring Boot应用程序,因此可以使用maven命令启动它,如以下示例所示:
mvn spring-boot:run
启动后访问如下地址即可:
http://localhost:8010
设置为8010端口时,打开地址看到如下结果:

现在Eureka Discovery服务器尚未注册任何微服务,在后面的教程中进行注册。 本教程的目的是学习如何启动自己的发现服务。
本文指导如何创建并启动一个Eureka服务发现服务器。首先,通过SpringBoot创建新应用,然后在POM.xml中添加Eureka客户端和服务端依赖。接着,启用EurekaServer注解并配置application.properties,设置服务端口和应用名称。最后,通过`mvn spring-boot:run`启动服务,访问http://localhost:8010查看运行情况。至此,一个基础的Eureka服务发现服务器已搭建完成。
3040

被折叠的 条评论
为什么被折叠?



