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 有注释说明的为主要依赖

<!-- springCloud管理依赖 -->
	<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>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<!--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>

 点我获取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

效果展示:

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

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值