接上一篇的基础上,实现Eureka简单集群配置,达到高可用要求。Eureka的集群逻辑,和Zookeeper的区别,不做过多介绍
一、修改“ershuai-eureka-server”的application.yml
spring:
application:
name: ershuai-eureka-server
profiles:
active: 1001 # 這里指定启动的application-1001/1002/1003.yml
二、新建“application-1001/1002/1003.yml”
register-with-eureka、fetch-registry 默认是true
server:
port: 1001
eureka:
client:
service-url:
defaultZone: http://localhost:1002/eureka,http://localhost:1003/eureka
#register-with-eureka: false # 是否注册自身到eureka服务器
#fetch-registry: false # 是否从eureka上获取注册信息
instance:
hostname: localhost
instance-id: ${spring.cloud.client.ipAddress}:${server.port} # 指定此实例的ip
prefer-ip-address: true # 注册时使用ip而不是主机名
#server:
#enable-self-preservation: false # 开启保护机制
server:
port: 1002
eureka:
client:
service-url:
defaultZone: http://localhost:1001/eureka,http://localhost:1003/eureka
#register-with-eureka: false # 是否注册自身到eureka服务器
#fetch-registry: false # 是否从eureka上获取注册信息
instance:
hostname: localhost
instance-id: ${spring.cloud.client.ipAddress}:${server.port} # 指定此实例的ip
prefer-ip-address: true # 注册时使用ip而不是主机名
#server:
#enable-self-preservation: false # 开启保护机制
server:
port: 1003
eureka:
client:
service-url:
defaultZone: http://localhost:1001/eureka,http://localhost:1002/eureka
#register-with-eureka: false # 是否注册自身到eureka服务器
#fetch-registry: false # 是否从eureka上获取注册信息
instance:
hostname: localhost
instance-id: ${spring.cloud.client.ipAddress}:${server.port} # 指定此实例的ip
prefer-ip-address: true # 注册时使用ip而不是主机名
#server:
#enable-self-preservation: false # 开启保护机制
三、分别启动1001/1002/1003端口服务
项目右键 或 Main方法右键 -> Run As -> Run Configurations -> Arguments
设置Name(自定义)
设置Program arguments参数:--spring.profiles.active=1001
点击“Run”启动
這里分别启动1001/1002/1003,需要Run As三次,每次启动一个端口服务
分别访问http://localhost:1001/1002/1003端口
可以看到三个端口的服务都互相注册了
四、注册服务
创建一个“ershuai-eureka-client”子项目
pom.xml文件
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
application.yml文件
spring:
application:
name: ershuai-eureka-client
server:
port: 9001
eureka:
client:
service-url:
defaultZone: http://localhost:1001/eureka,http://localhost:1002/eureka,http://localhost:1003/eureka # 多个以','分隔。也可以只填一个
#register-with-eureka: false # 是否注册自身到eureka服务器
#fetch-registry: false # 是否从eureka上获取注册信息
instance:
instance-id: ${spring.cloud.client.ipAddress}:${server.port} # 指定此实例的ip
prefer-ip-address: true # 注册时使用ip而不是主机名
测试controller
@RestController
public class TestController {
@Autowired
DiscoveryClient discoveryClient;
@GetMapping("/test")
public String test() {
String services = "Services: " + this.discoveryClient.getServices();
System.out.println(services);
return services;
}
}
Main启动
@EnableDiscoveryClient
@SpringBootApplication
public class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class, args);
}
}
再次访问http://localhost:1001/1002/1003,可以看到9001已注册到Eureka
访问http://localhost:9001/test
可以看到服务正常
尝试关闭1001/1002/1003,任意一个或两个服务,访问http://localhost:9001/test,依然是可以正常运行的