启动 Eureka 服务
按照前述课程启动Eureka服务。
使用 Spring Boot创建新的微服务
按照前述课程创建Spring Boot应用即可。
增加Spring Cloud依赖
为了使我们能够在Eureka Discovery Server中注册新的Spring Boot Web服务,我们需要将Spring Cloud依赖项添加到刚创建的Spring Boot项目的POM.xml文件中。
更新
增加Spring Cloud 版本:
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.RC2</spring-cloud.version>
</properties>
更新
增加Spring Cloud Starter Netflix Eureka Client依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
增加
<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>
完整的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.api.users</groupId>
<artifactId>PhotoAppApiUsers</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>PhotoAppApiUsers</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.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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>
更新application.properties
文件
eureka.client.serviceUrl.defaultZone = http://localhost:8010/eureka
server.port=0
spring.application.name=PhotoAppApi-Users
- eureka.client.serviceUrl.defaultZone – Eureka 服务地址.
- server.port – 端口号,设置为0的时候将会随机生成
- spring.application.name - 给当前Spring Boot应用起的名称
还有其他选项本文先不设置。
增加Enable Discovery Client注解
下面是应用程序类的示例,添加了@EnableDiscoveryClient注解。
package com.xarhsoft.photoapp.api.users;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class PhotoAppApiApplication {
public static void main(String[] args) {
SpringApplication.run(PhotoAppApiApplication.class, args);
}
}
启动Spring Boot
Maven命令如下:
mvn spring-boot:run
在Eureka服务中预览注册的Spring-boot服务
访问Eureka的服务地址http://localhost:8010,可以看到服务已注册: