Spring Boot 向Eureka注册服务,在本章中,您将详细了解如何将 Spring Boot 微服务应用程序注册到 Eureka Server 中。在注册应用程序之前,请确保 Eureka Server 在端口 8761 上运行或首先构建 Eureka Server 并运行它。关于构建Eureka服务器的更多信息,您可以参考上一章。
首先,您需要在我们的构建配置文件中添加以下依赖项,以向 Eureka 服务器注册微服务。
Maven 用户可以将以下依赖项添加到pom.xml文件中 –
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency>
Gradle 用户可以将以下依赖项添加到build.gradle文件中 –
compile('org.springframework.cloud:spring-cloud-starter-eureka')
现在,我们需要在主 Spring Boot 应用程序类文件中添加 @EnableEurekaClient 注解。@EnableEurekaClient 注释使您的 Spring Boot 应用程序充当 Eureka 客户端。
主要的 Spring Boot 应用程序如下所示 –
package net.zuze.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 应用程序注册到 Eureka Server,我们需要在我们的 application.properties 文件或 application.yml 文件中添加以下配置,并在我们的配置中指定 Eureka Server URL。
application.yml 文件的代码如下 –
eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka instance: preferIpAddress: true spring: application: name: eurekaclient
application.properties 文件的代码如下 –
eureka.client.serviceUrl.defaultZone = http://localhost:8761/eureka eureka.client.instance.preferIpAddress = true spring.application.name = eurekaclient
现在,在主 Spring Boot 应用程序中添加 Rest Endpoint 以返回 String,并在构建配置文件中添加 Spring Boot Starter Web 依赖项。观察下面给出的代码 –
package net.zuze.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"; } }
整个配置文件如下。
对于 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>net.zuze</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>
对于 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 = 'net.zuze' 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 应用程序 –
对于 Maven,您可以使用以下命令 –
mvn clean install
“BUILD SUCCESS”后,可以在目标目录下找到JAR文件。
对于 Gradle,您可以使用以下命令 –
gradle clean build
“BUILD SUCCESSFUL”后,您可以在build/libs 目录下找到JAR 文件。
现在,使用如下所示的命令运行 JAR 文件 –
java –jar <JARFILE>
现在,该应用程序已在 Tomcat 端口 8080 上启动,Eureka 客户端应用程序已向 Eureka 服务器注册
在您的 Web 浏览器中点击 URL http://localhost:8761/,您可以看到 Eureka Client 应用程序已注册到 Eureka Server。
现在在您的 Web 浏览器中点击 URL http://localhost:8080/并查看 Rest Endpoint 输出。