Spring Cloud 之 Eureka 高可用集群搭建(二)

1、创建一个module名称为eureka-server

2、build.gradle中加入依赖

1 dependencies {
2     compile("org.springframework.cloud:spring-cloud-starter-netflix-eureka-server")
3 }

3、创建Eureka启动类

除了SpringBootApplication注解,还需要加上Eureka注解,@EnableEurekaServer

/**
 * @author Leo
 */
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}

4、application.yml配置文件,本文演示创建3个eureka实例,注意defaultZone配置

spring:
  profiles:
    active: instance3
  application:
    name: eureka-server

eureka:
  client:
    register-with-eureka: false #由于该应用为注册中心,设置为false,表明不向注册中心注册自己
  server:
    enable-self-preservation: false # 关闭自我保护
    fetch-registry: false #是否从eureka服务器获取注册信息,这里不需要
logging:
  level:
    com:
      netflix:
        eureka: OFF
        discovery: OFF

management:
  endpoints:
    web:
      exposure:
        include: '*'
  endpoint:
    health:
      show-details: ALWAYS

---
spring:
  profiles: instance1
server:
  port: 8761
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8762/eureka,http://localhost:8763/eureka/

---
spring:
  profiles: instance2
server:
  port: 8762
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka,http://localhost:8763/eureka/

---
spring:
  profiles: instance3
server:
  port: 8763
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka,http://localhost:8762/eureka/

5、启动EurekaServerApplication服务

idea内启动时,依次修改spring.profiles.active为instance1,instance2,instance3,启动3个eureka节点

正式部署时通过如下方式启动

java -jar eureka-server-1.0.0-SNAPSHOT.jar --spring.profiles.active=instance1
java -jar eureka-server-1.0.0-SNAPSHOT.jar --spring.profiles.active=instance2
java -jar eureka-server-1.0.0-SNAPSHOT.jar --spring.profiles.active=instance3

6、查看集群状态

节点1:http://localhost:8761/

节点2:http://localhost:8762/

节点3:http://localhost:8763/

原文地址:https://www.cnblogs.com/shileibrave/p/14429339.html