一、程序配置
1、pom添加依赖:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>
(这里因为在pom中通过dependencyManagement配置将spring-cloud-dependencies import进来,所以不需要写版本号)
或者在是使用Spring Initializr创建SpringBoot项目时,添加上起步依赖Eureka Server (路径:Cloud Discovery -- Eureka Server)。
完整pom为:
<?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.example</groupId> <artifactId>microservice-discovery-eureka-ha</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>microservice-discovery-eureka-ha</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.1.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>Greenwich.M3</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-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> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
2、在SpringBoot启动类上添加注释@EnableEurekaServer
3、配置application.yml或application.properties
spring: application: name: microservice-discovery-eureka-ha --- spring: profiles: peer1 server: port: 8761 eureka: instance: hostname: peer1 client: service-url: defaultZone: http://peer2:8762/eureka/ register-with-eureka: false #作为对比,这里表示不将peer1注册到Eureka Server集群中,peer2中默认为true fetch-registry: false #作为对比,这里表示peer1不会从Eureka Server集群获取注册表信息 --- spring: profiles: peer2 server: port: 8762 eureka: instance: hostname: peer2 client: service-url: defaultZone: http://peer1:8761/eureka/
例子程序的github地址是:https://github.com/zjianliu/Eureka-Server
二、程序运行
将程序打包后,执行jar包:
java -jar xxx.jar --spring.profiles.active=peer1
java -ajr xxx.jar --spring.profiles.active=peer2
通过spring.profiles.active指定使用application.yml中配置的哪一个profile启动程序。
三、程序截图
我们可以看到,peer2将自身注册到Eureka Server集群中了,而peer1没有。
Eureka Server配置启动好之后,我们就可以写微服务,并通过配置将其注册到Eureka Server集群中了