Spring Cloud Netflix(二)

Eureka 的高可用

Eureka 通过将注册表信息存储在 ClientServer,并使用心跳检测进行状态的检测和同步。这就使得单节点的 Eureka 服务具备了一定的可用性和故障恢复能力。即使在 Eureka Server 挂掉之后,因为 Client 本身存储了注册表信息,这里面就包含了其他服务的 HostIp等信息,这样就可以继续发起服务调用。

但是这种方式仍然存在一些问题。比较核心的问题就是新的服务无法在注册中心注册自己,其他的 Client 中注册表信息中还未包含这个服务信息,就导致了新的服务无法被其他服务调用。

为了实现 Eureka 的高可用,通过搭建 Eureka 集群便可以解决这种问题。在集群模式中,所有的 Eureka Server 会互相注册,实现注册表信息的同步和共享。


Eureka Server Cluster

1. Eureka-Server

1.1. 依赖

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

1.2. 主类

package com.mawen;

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

/**
 * @author mw118
 * @version 1.0
 * @date 2021/1/10 22:44
 */
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerPeerApplication {

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

1.3. 配置

spring:
  application:
    name: eureka-server-peer

eureka:
  client:
    fetch-registry: false
    register-with-eureka: true
    serviceUrl:
      defaultZone: http://peer1:8761/eureka/,http://peer2:8762/eureka/,http://peer3:8763/eureka/

---
spring:
  profiles: peer1
eureka:
  instance:
    hostname: peer1
server:
  port: 8761

---
spring:
  profiles: peer2
eureka:
  instance:
    hostname: peer2
server:
  port: 8762

---
spring:
  profiles: peer3
eureka:
  instance:
    hostname: peer3
server:
  port: 8763

2. Eureka-Client

2.1. 依赖

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

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

2.2. 主类

package com.mawen;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/**
 * @author mw118
 * @version 1.0
 * @date 2021/1/11 8:09
 */
@SpringBootApplication
@EnableDiscoveryClient
public class EurekaClientPeerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaClientPeerApplication.class, args);
    }
}

2.3. 配置

spring:
  application:
    name: eureka-client-peer

eureka:
  client:
    serviceUrl:
      defaultZone: http://peer1:8761/eureka/,http://peer2:8762/eureka/,http://peer3:8763/eureka/
    fetch-registry: false

server:
  port: 8765

3. 配置Host

# peer

127.0.0.1 peer1
127.0.0.1 peer2
127.0.0.1 peer3

4. 启动服务

4.1. 启动server peer1, peer2peer3

peer1

peer1

peer2

peer2

peer3

peer3

访问peer1:8761

peer节点均启动

4.2. 启动 Client

client启动


资源

代码

参考

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值