高可用注册中心

在微服务中,注册中心非常核心,可以实现服务治理,如果一旦注册出现故障的时候,可能会导致整个微服务无法访问,在这时候就需要对注册中心实现高可用集群模式。

默认情况下Eureka是让服务注册中心,不注册自己

#是否将自己注册到Eureka Server,默认为true。由于应用为注册中心,所以设置为false,代表不想注册中心注册资金
    register-with-eureka: false
#是否从Eureka Server获取注册信息,默认为true。由于注册中心的职责就是维护服务实例,他并不需要去检索服务,所以也设置false
    fetch-registry: false

Eureka高可用实际上将自己作为服务向其他服务注册中心注册自己,这样就可以形成一组相互注册的服务注册中心,从而实现服务清单的互相同步,达到高可用效果。

Eureka集群环境搭建

Maven配置

	<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>

Eureka01配置

###服务端口号
server:
  port: 8100
###eureka 基本信息配置
spring: 
 application: 
  name: eureka-server
eureka:
  instance:
    ###注册到eurekaip地址
    hostname: 127.0.0.1
  client:
    serviceUrl:
      defaultZone: http://127.0.0.1:8200/eureka/
###因为自己是为注册中心,不需要自己注册自己
    register-with-eureka: false
###因为自己是为注册中心,不需要检索服务
    fetch-registry: false

Eureka02配置

###服务端口号
server:
  port: 8200
###eureka 基本信息配置
spring: 
 application: 
  name: eureka-server
eureka:
  instance:
    ###注册到eurekaip地址
    hostname: 127.0.0.1
  client:
    serviceUrl:
      defaultZone: http://127.0.0.1:8100/eureka/
###因为自己是为注册中心,不需要自己注册自己
    register-with-eureka: false
###因为自己是为注册中心,不需要检索服务
    fetch-registry: false

客户端集成Eureka集群

server:
  port: 8000
spring:
  application:
    name: app-shop-member
#eureka:
#  client:
#    service-url:
#      defaultZone: http://localhost:8100/eureka
###集群地址
eureka:
  client:
    service-url:
           defaultZone: http://localhost:8100/eureka,http://localhost:8200/eureka    
    register-with-eureka: true
    fetch-registry: true

资料

SpringCloud笔记(二)高可用注册中心的实现

Last updated

Was this helpful?