(二)Eureka 高可用

1、Eureka 高可用介绍

EurekaServer可以是一个集群,形成高可用的Eureka注册中心

多个Eureka Server之间也会互相注册为服务,当服务提供者注册到Eureka Server集群中的某个节点时,该节点会把服务的信息同步给集群中的每个节点,从而实现数据同步。

因此,无论客户端访问到Eureka Server集群中的哪一个节点,都可以获取到完整的服务列表信息。

2、搭建第二个 Eureka

首先需要修改之前 Eureka 注册中心配置,需要指向第二个 Eureka 地址。

#内置的tomcat服务启动监听端口号
server:
  port: 10086
#应用名称
spring:
  application:
    name: EurekaServer
#EurekaServer配置
eureka:
  instance:
    hostname: localhost
  server:
    #关闭自我保护模式(缺省为打开)
    enable-self-preservation: false
    #扫描失效服务的间隔时间(缺省为60*1000ms)
    eviction-interval-timer-in-ms: 1000
  client:
    register-with-eureka: true #需要设置为true允许去其他eurekaserver注册
    fetch-registry: true       #允许从其他中心中心拉取服务器信息
    service-url:
      defaultZone: http://${eureka.instance.hostname}:10087/eureka #注册中心访问地址,指向另外一台eurekaserver

创建一个Maven项目,如下编写。

 修改pom文件,添加如下。

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

 新建一个application.yml,配置如下。

#内置的tomcat服务启动监听端口号
server:
  port: 10087
#应用名称
spring:
  application:
    name: EurekaServer
#EurekaServer配置
eureka:
  instance:
    hostname: localhost
  server:
    #关闭自我保护模式(缺省为打开)
    enable-self-preservation: false
    #扫描失效服务的间隔时间(缺省为60*1000ms)
    eviction-interval-timer-in-ms: 1000
  client:
    register-with-eureka: true #此EurekaServer不在注册到其他的注册中心
    fetch-registry: true       #不在从其他中心中心拉取服务器信息
    service-url:
      defaultZone: http://${eureka.instance.hostname}:10086/eureka #注册中心访问地址,指向另外一台eurekaserver

启动类代码:

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 EurekaServer00Start {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServer00Start.class, args);
    }
}

3、测试

启动完成

 

 4、修改服务提供者、调用者注册中心地址

 

 注意启动的顺序,注册中心、服务提供者、服务消费者。

猜你喜欢

转载自blog.csdn.net/weixin_46085718/article/details/130339537