-
创建三个model:cloud-eureka-server7001,cloud-eureka-server7002,cloud-eureka-server7003
-
改pom文件
-
修改系统映射配置
-
改yml文件
-
主启动
一、创建三个Eureka server的model,cloud-eureka-server7001,cloud-eureka-server7002,cloud-eureka-server7003,
二、修改pom文件,
<dependencies>
<!-- Eureka server-->
<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-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!-- 引入自己定义的api通用包,可以使用Payment支付entity -->
<dependency>
<groupId>com.niucheng.springcloud</groupId>
<artifactId>cloud-api-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
三、修改系统映射配置
在host文件中加入以下配置,注意:IP地址后面的域名要与appliaction.yml中的hostname对应
127.0.0.1 eureka7001.com
127.0.0.1 eureka7002.com
127.0.0.1 eureka7003.com
四、改yml文件
cloud-eureka-server7001的appliaction.yml
注意:多个Eureka集群时,defaultZone的需要用逗号隔开
server:
port: 7001
eureka:
instance:
hostname: eureka7001.com #Eureka服务端的实例名称
client:
register-with-eureka: false #flase 表示不向注册中心注册自己
fetch-registry: false #false 表示自己端就是注册中心,我的职责就是维护服务实例,并不需要检索服务
service-url:
#设置与Eureka server交互的地址查询服务和注册服务都需要依赖的地址
#defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
cloud-eureka-server7002的appliaction.yml
server:
port: 7002
eureka:
instance:
hostname: eureka7002.com #Eureka服务端的实例名称
client:
register-with-eureka: false #flase 表示不向注册中心注册自己
fetch-registry: false #false 表示自己端就是注册中心,我的职责就是维护服务实例,并不需要检索服务
service-url:
#设置与Eureka server交互的地址查询服务和注册服务都需要依赖的地址
#defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7003.com:7003/eureka/
cloud-eureka-server7003的appliaction.yml
server:
port: 7003
eureka:
instance:
hostname: eureka7003.com #Eureka服务端的实例名称
client:
register-with-eureka: false #flase 表示不向注册中心注册自己
fetch-registry: false #false 表示自己端就是注册中心,我的职责就是维护服务实例,并不需要检索服务
service-url:
#设置与Eureka server交互的地址查询服务和注册服务都需要依赖的地址
#defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7001.com:7001/eureka/
五、主启动
注意:主启动需要加上@EnableEurekaServer 注解
package com.niucheng.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaMain7001 {
/**
* http://localhost:7001/
* @param args
*/
public static void main(String[] args) {
SpringApplication.run(EurekaMain7001.class,args);
}
}
六、测试
在浏览器中输入
都可以访问页面,不同之处是 DS Replicas指向的服务不同。