提高系统的可靠性,我们使用Eureka集群,搭建的过程很简单,在Eureka工程的配置文件application.yml中进行配置。
1.使Eureka之间相互注册
2.用户微服务,注册到任意一个Eureka Server上都会进行同步。
Eureka 配置文件application.yml
Peer Awareness:同伴意识。
spring:
application:
name: EUREKA-HA
---
server:
port: 8761
spring:
profiles: peer1
eureka:
instance:
hostname: peer1
client:
serviceUrl:
defaultZone: http://peer2:8762/eureka/,http://peer2:8763/eureka/
---
server:
port: 8762
spring:
profiles: peer2
eureka:
instance:
hostname: peer2
client:
serviceUrl:
defaultZone: http://peer1:8761/eureka/,http://peer3:8763/eureka/
---
server:
port: 8763
spring:
profiles: peer3
eureka:
instance:
hostname: peer3
client:
serviceUrl:
defaultZone: http://peer1:8761/eureka/,http://peer2:8762/eureka/
启动类
package com.dynamic.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer// 声明一个server:@EnableEurekaServer
public class EurekaApplication
{
public static void main( String[] args )
{
SpringApplication.run(EurekaApplication.class, args);
}
}
去掉POM.xml中的认证依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
用户微服务配置文件application.yml将服务注册到Eureka
eureka:
client:
serviceUrl:
defaultZone: http://peer1:8761/eureka/ #http://user:pass123@localhost:8761/eureka
instance:
prefer-ip-address: true
instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}}
修改计算机的host配置
win + r -----> drivers ------> etc ------>hosts
刚启动的时候,相互之间注册,其他相互依赖服务没有正常启动,所以会出现异常。
通过修改心跳检测的时长,可以提高服务注册的速度,但是线上不建议修改。