基于Eureka服务治理的高可用性注册中心搭建

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yaomingyang/article/details/85105400

高可用注册中心

在微服务这样的分布式环境中,我们需要充分考虑故障发生的情况,所以在生产环境中必须对各个组件进行高可用部署,对于微服务如此,对于微服务注册中心也是一样。上一篇文章我们搭建了单节点的服务治理示例,接下来我们构建一个高可用性注册中心以增强系统的可用性。

在Eureka的服务治理体系中,所有节点即是服务提供方,也是服务消费方,服务注册中心也不例外。Eureka Server的高可用实际上就是将自己作为服务向其它服务注册中心注册自己,这样就可以形成一组互相注册的服务注册中心,以实现服务清单的相互同步,达到高可用的效果。接下来我们尝试一下搭建一个高可用注册中心。

  1. 创建基础的spring boot项目eureka-server,在pom.xml配置文件中引入Eureka Server的依赖
	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>
  1. 在启动主类上添加@EnableEurekaServer注解启动一个 服务注册中心给其它服务
package com.eureka.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

	public static void main(String[] args) {
		SpringApplication.run(EurekaServerApplication.class, args);
	}

}
  1. 创建一个spring boot项目eureka-server-peer,配置如上两步
  2. eureka-server项目创建配置文件application-peer.properties作为注册中心的配置文件
#注册中心端口号
server.port=9001
##注册中心服务地址
eureka.instance.hostname=peer1
#eureka.instance.prefer-ip-address=false
#eureka.instance.instance-id=${eureka.instance.hostname}:${server.port}
#是否开启自我保护机制
eureka.server.enable-self-preservation=true
#是否将自己注册为服务
eureka.client.register-with-eureka=true
#是否禁止向注册中心检索服务
eureka.client.fetch-registry=true
#服务地址
eureka.client.service-url.defaultZone=http://peer2:9002/eureka/

application.properties文件的配置如下:

#注册中心应用名称
spring.application.name=eureka-server
##注册中心使用的配置文件名
spring.profiles.active=peer
  1. eureka-server-peer项目创建application-peer.properties配置文件作为注册中心的配置文件
#注册中心端口号
server.port=9002
##注册中心服务地址
eureka.instance.hostname=peer2
#eureka.instance.prefer-ip-address=false
#eureka.instance.instance-id=${eureka.instance.hostname}:${server.port}
#是否开启自我保护机制
eureka.server.enable-self-preservation=true
#是否将自己注册为服务
eureka.client.register-with-eureka=true
#是否禁止向注册中心检索服务
eureka.client.fetch-registry=true
#注册中心
eureka.client.service-url.defaultZone=http://peer1:9001/eureka/

application.properties的配置如下:

#注册中心应用名称
spring.application.name=eureka-server
##注册中心使用的配置文件名
spring.profiles.active=peer
  1. 修改hosts配置文件,windows环境在C:\Windows\System32\drivers\etc目录下

在这里插入图片描述

  • 启动两个注册中心,启动第一个的时候可能会有错误,但是这个不影响,因为第二个注册中心还未启动,启动成功后分别访问http://localhost:9001/,http://localhost:9002/

http://localhost:9001/端口返回结构
在这里插入图片描述
在这里插入图片描述

http://localhost:9002/端口返回结果
在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/yaomingyang/article/details/85105400
今日推荐