【SrpingCloud】二、Eureka集群配置
一、添加Eureka的【客户端】依赖
- 注意:eureka监控中心要导入的是 spring-cloud-starter-eureka-server 而不是 spring-cloud-starter-eureka
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
二、编写配置文件
- 注意:为了成功配置eureka集群,必须要把 localhost 改成别的名字,我改成了 eureka7001.com
server:
port: 7001
eureka:
instance:
hostname: eureka7001.com #Eureka服务端的实例名称
client: #client:客户
register-with-eureka: false # 表示是否向Eureka注册中心注册自己
fetch-registry: false # 如果为false表示自己为注册中心
service-url: # 监控页面
# 单个eureka的配置
# defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
#集群eureka(多个eureka互相关联)
defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
三、在主启动类开启配置
- @EnableEurekaServer : Eureka服务端的启动类
@SpringBootApplication
@EnableEurekaServer
public class EurekaServer_7001 {
public static void main(String[] args){
SpringApplication.run(EurekaServer_7001.class,args);
}
}
四、单个Eureka效果展示
五、配置多个Eureka,实现Eureka集群
1、 重复一 ,二,三即可
2、注意事项
- 第一个Eureka的localhost名字:eureka7001.com
- 第二个Eureka的localhost名字:eureka7002.com
- 第二个Eureka的localhost名字:eureka7003.com
3、配置文件的差别
server:
port: 7001
eureka:
instance:
hostname: eureka7001.com #Eureka服务端的实例名称
client: #client:客户
register-with-eureka: false # 表示是否向Eureka注册中心注册自己
fetch-registry: false # 如果为false表示自己为注册中心
service-url: # 监控页面
# 单个eureka的配置
# defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
#集群eureka(多个eureka互相关联)
defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
- defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
- 这行代码实现了把 Eureka7001 和 Eureka7002 、 Eureka7003进行关联
六、Eurek集群效果展示
七、把服务端添加进 Eureka监控中心
- 添加服务端的 Eureka 依赖
- Eureka监控依赖:spring-cloud-starter-eureka
- 完善监控信息:spring-boot-starter-actuator
<!-- Eureka-->
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<!-- actuator 完善监控信息-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
- 编写配置文件
- defaultZone : 把服务端发送到指定地址
- instance : 修改 Eureka 上的默认描述信息
# Eureka配置,把服务注册到注册中心
eureka:
client:
service-url:
# 把8002发布到下面的地址
# defaultZone: http://localhost:7001/eureka/
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
instance:
instance-id: springcloud-provider-dept-8003 #修改 Eureka 上的默认描述信息
# info配置
info:
app-name: lzy-springcloud
company-name: lzy-company
- 在主启动类开启配置
@EnableEurekaClient
@EnableDiscoveryClient
八、效果展示