SpringBoot+SpringCoud+Eureka实现高可用注册中心

Eureka高可用原理:

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

Eureka集群环境搭建(本文展示的是两个服务的集群+一个生产者+一个消费者):

Eureka_01配置:

application.yml

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

 

pom.xml 有注释说明的为主要依赖


	
		
			
				org.springframework.cloud
				spring-cloud-dependencies
				Finchley.M7
				pom
				import
			
		
	
	
	
		
			org.springframework.boot
			spring-boot-starter
		

		
			org.springframework.boot
			spring-boot-starter-test
			test
		
		
		
			org.springframework.cloud
			spring-cloud-starter-netflix-eureka-server
		
		
		
	
	
	
		
			spring-milestones
			Spring Milestones
			https://repo.spring.io/libs-milestone
			
				false
			
		
	

 点我获取Eureka_01代码

Eureka_02配置:

application.yml

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

pom.xml配置同Eureka_01一样

点我获取Eureka_02代码

启动项目

注:项目启动过程中出现如下报错是正常现象:因为两台是互相注册,在启动时另一台肯定还没有启动,所以包如下错误1

com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused: connect

效果展示:

SpringBoot+SpringCoud+Eureka实现高可用注册中心_第1张图片

SpringBoot+SpringCoud+Eureka实现高可用注册中心_第2张图片

服务生产者和服务消费者在我的上一篇博客中https://blog.csdn.net/Websphere_zxf/article/details/89501411,这里不做过多解释!特别说明的是:当服务生产者启动时首先会注册到8100中去,而8200中没有该生产者。但是当8100服务关闭后(模仿宕机)该服务生产者会自动注册(约30秒后)到8200中去!

 

 

你可能感兴趣的:(微服务,SpringBoot)