1.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.zlpay</groupId>
<artifactId>zl-eureka</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging> <name>zl-eureka</name>
<description>服务注册与发现</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.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>Finchley.SR1</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-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</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>

2.配置文件application.yml

#服务注册中心实例的主机名
spring:
application:
name: zl-eureka
#节点1
profiles: peer1
server:
#端口
port: 5001
eureka:
instance:
hostname: peer1
client:
#是否向服务注册中心注册自己
registerWithEureka: true
#是否检索服务
fetchRegistry: true
serviceUrl:
#服务注册中心的配置内容,指定服务注册中心的位置
defaultZone: http://peer2:5002/eureka/,http://peer3:5003/eureka/
--- spring:
application:
name: zl-eureka
profiles: peer2
server:
port: 5002
eureka:
instance:
hostname: peer2
client:
#是否向服务注册中心注册自己
registerWithEureka: true
#是否检索服务
fetchRegistry: true
serviceUrl:
#服务注册中心的配置内容,指定服务注册中心的位置
defaultZone: http://peer1:5001/eureka/,http://peer3:5003/eureka/
--- spring:
application:
name: zl-eureka
profiles: peer3
server:
port: 5003
eureka:
instance:
hostname: peer3
client:
#是否向服务注册中心注册自己
registerWithEureka: true
#是否检索服务
fetchRegistry: true
serviceUrl:
#服务注册中心的配置内容,指定服务注册中心的位置
defaultZone: http://peer1:5001/eureka/,http://peer2:5002/eureka/

3.启动类注解

@EnableEurekaServer
@SpringBootApplication
public class ZlEurekaApplication { public static void main(String[] args) {
SpringApplication.run(ZlEurekaApplication.class, args);
}
}

4.启动

首先配置hosts文件,追加以下内容:

127.0.0.1 peer1
127.0.0.1 peer2
127.0.0.1 peer3

4.1 Eclipse启动

Run Configurations中运行3次启动三个节点,profile值分别是peer1、peer2、peer3

搭建Eureka集群-LMLPHP

4.2 Jar包启动

先将项目打成jar包,然后使用jar命令:

java -jar zl-eureka-0.0.1.jar --spring.profiles.active=peer1

java -jar zl-eureka-0.0.1.jar --spring.profiles.active=peer2

java -jar zl-eureka-0.0.1.jar --spring.profiles.active=peer3

4.3 war包启动

待续…

05-11 17:51