Spring Cloud搭建注册中心 Eureka以及Eureka的高可用集群

1 注册中心Eureka原理

在这里插入图片描述

2 搭建注册中心

2.1 注册中心导入依赖

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

2.2 注册中心配置文件application.yml

server:
  port: 1000
eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false #禁用注册中心向自己注册
    fetchRegistry: false  #不让注册中心获取服务的注册列表
    serviceUrl:
      defaultZone: http://localhost:1000/eureka/
      #注册中心的注册地址 ,其他微服务需要向这个地址注册

2.3 主配置类

打上注解@EnableEurekaServer:

/**
 * @EnableEurekaServer :开启注册中心
 */
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication1000 {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication1000.class);
    }
}

2.4 微服务模块导入依赖

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

2.5 微服务模块配置文件

application.yml:

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:1000/eureka/ #注册中心地址
server:
  port: 2000
spring:
  application:
    name: user-server

2.6 微服务模块主配置类

/**
 * @EnableDiscoveryClient:开启注册中心客户端 EurekaClient
 */
@SpringBootApplication
@EnableDiscoveryClient
public class UserServerApplication2000 {
    public static void main(String[] args) {
        SpringApplication.run(UserServerApplication2000.class);
    }
}

2.7 测试

搭建好了之后,先运行Eureka服务,再运行微服务。可以通过访问localhost:1000来测试,
在这里插入图片描述
这里的Instance currently registered with Eureka可以看到你注册到注册中心的微服务。

3 Eureka Server高可用集群

3.1 单节点故障

3.1.1 什么是Eureka Server单节点故障

当只有一个EurekaSever时,如果该EurekaSever 下线了,那么整个微服务都不可用。

3.1.2 解决方案

做EurekaServer高可用集群

3.2 EurekaServer集群

3.2.1 原理图

在这里插入图片描述

3.2.2 搭建EurekaServer集群

  • 在hosts文件中创建两个本地域名,文件位置C:\Windows\System32\drivers\etc\hosts,在末尾加上:
    127.0.0.1 peer1
    127.0.0.1 peer2
  • 修改注册中心配置文件application.yml
spring:
  profiles:
    active: peer1
---
server:
  port: 1000
eureka:
  instance:
    hostname: peer1 #主机
  client:
    registerWithEureka: false #禁止注册中心向自己注册
    fetchRegistry: false #禁止注册中心拉取注册地址清单
    serviceUrl: #微服务向注册中心注册的地址
      defaultZone: http://peer2:1001/eureka/
spring:
  profiles: peer1
---
server:
  port: 1001
eureka:
  instance:
    hostname: peer2 #主机
  client:
    registerWithEureka: false #禁止注册中心向自己注册
    fetchRegistry: false #禁止注册中心拉取注册地址清单
    serviceUrl: #微服务向注册中心注册的地址
      defaultZone: http://peer1:1000/eureka/
spring:
  profiles: peer2
  • 修改微服务的配置文件application.yml
eureka:
  client:
    serviceUrl:
      defaultZone: http://peer1:1000/eureka/,http://peer2:1001/eureka/ #注册中心地址
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值