注册中心集群
在微服务中,注册中心非常核心,可以实现服务治理,如果一旦注册出现故障的时候,可能会导致整个微服务无法访问,在这时候就需要对注册中心实现高可用集群模式。
Eureka集群相当简单:相互注册
Eureka高可用实际上将自己作为服务向其他服务注册中心注册自己,这样就可以形成一组相互注册的服务注册中心,从而实现服务清单的互相同步,达到高可用效果。
集群的服务名称要统一,要相同!
启动时候 报错 正常! 启动时候互相注册 不会同时启动成功的
启动类都是一样的
package com.toov5; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @EnableEurekaServer //开启注册中心 @SpringBootApplication public class AppEureka { public static void main(String[] args) { SpringApplication.run(AppEureka.class, args); } }
pom都一样的:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.toov5</groupId> <artifactId>EurekaClaster</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.RELEASE</version> </parent> <!-- 管理依赖 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.M7</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!--SpringCloud eureka-server --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies> <!-- 注意: 这里必须要添加, 否者各种依赖有问题 --> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/libs-milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
看不同的yml:
server:
port: 9100
spring:
application:
name: app-toov5-member
#eureka:
# client:
# service-url:
# defaultZone: http://localhost:8100/eureka
###集群地址 或者不写死 端口号改变 端口号改为对方的eureka端口号
eureka:
client:
service-url:
## defaultZone: http://${eureka.instance.hostname}:8100/eureka/ ##其他eureka服务的地址
defaultZone: http://127.0.0.1:8100/eureka/ ##其他eureka服务的地址
register-with-eureka: true
fetch-registry: true
###eureka 服务端口号
server:
port: 8100
###服务注册名称
spring:
application:
name: app-toov5-member ##############要相同
eureka:
instance:
##注册中心ip地址
hostname: 127.0.0.1
###客户端调用地址
client:
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:9100/eureka/ ##其他eureka服务的地址 互相注册 写对方的地址+端口
###因为该应用为注册中心,不会注册自己 (集群设为true)
register-with-eureka: true
###因为自己为注册中心 ,不会去在该应用中的检测服务
fetch-registry: true

