Eureka Server 集群搭建

Eureka Client(包括 service provider 和 service consumer)会定期(默认是30秒,可以修改)向 Eureka Server 发送心跳来进行服务续约(renew),同时 Eureka Client 也会定期(默认30秒,可以修改)从 Eureka Server 获取服务注册列表缓存到本地,可见 Eureka Server 在微服务系统中有着举足轻重的作用,所以,一般情况下都会对 Eureka Server 进行高可用配置。

新建一个 Maven 项目 eureka-server-cluster,其 pom 文件内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.wuychn</groupId>
    <artifactId>eureka-server-cluster</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring.cloud.version>Finchley.RELEASE</spring.cloud.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring.cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <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-web</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

在启动类上加上 @EnableEurekaServer 注解开启 Eureka Server 的功能:

package com.wuychn;

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

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerClusterApplication {

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

application.yml 文件如下,采用了多 profiles 的配置方式:

spring:
  application:
    name: eureka-server
  profiles:
    active: peer1

application-peer1.yml:

server:
  port: 9001
eureka:
  instance:
    hostname: peer1
  client:
    fetch-registry: true
    register-with-eureka: true
    serviceUrl:
      defaultZone: http://peer2:9002/eureka/,http://peer3:9003/eureka/

application-peer2.yml:

server:
  port: 9002
eureka:
  instance:
    hostname: peer2
  client:
    fetch-registry: true
    register-with-eureka: true
    serviceUrl:
      defaultZone: http://peer1:9001/eureka/,http://peer3:9003/eureka/

application-peer3.yml:

server:
  port: 9003
eureka:
  instance:
    hostname: peer3
  client:
    fetch-registry: true
    register-with-eureka: true
    serviceUrl:
      defaultZone: http://peer1:9001/eureka/,http://peer2:9002/eureka/

注意配置文件中使用了 peer1、peer2、peer3 这样的主机名,需要在 hosts 文件中添加如下配置:

127.0.0.1			peer1
127.0.0.1			peer2
127.0.0.1			peer3

以 application-peer1.yml 为例,首先配置了端口为 9091,主机名为 peer1,fetch-registry 配置项的意思是是否从注册中心获取服务注册列表信息,在 Eureka Server 单点配置的时候,这里要配置为 false,而集群环境则需要配置为 true,该配置项默认值为 true,因此也可以省略。register-with-eureka 配置项的意思是是否注册到服务注册中心,同 fetch-registry 一样,在 Eureka Server 单点配置时需要配置为 false,集群环境需要配置为 true,该配置项默认值为 true,因此也可以省略。defaultZone 指定集群中其他节点的地址,多个用逗号分隔。

修改 application.yml 配置文件,分别使用 application-peer1.yml、application-peer2.yml 和 application-peer3.yml 启动三个 Eureka Server 节点,在浏览器中访问 localhost:9001(localhost:9002、localhost:9003 类似),可以看到集群配置成功:

 

项目结构如图:

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Eureka和Seata分别是服务发现和分布式事务解决方案,它们可以结合使用,提高微服务架构的可靠性和稳定性。下面是它们的集群搭建步骤: 1. Eureka集群搭建 - 部署多个Eureka Server实例,每个实例都需要配置相同的应用名称(application.name)和注册中心地址(eureka.client.service-url.defaultZone),以实现服务注册和发现的高可用。 - 配置Eureka Server实例之间的相互注册,每个Eureka Server实例都需要配置其他Eureka Server实例的地址(eureka.client.service-url.defaultZone),这样它们才能相互发现和注册服务。 - 可以通过配置负载均衡器(如Nginx)来实现Eureka Server集群的负载均衡和高可用。 2. Seata集群搭建 - 部署多个Seata Server实例,每个实例都需要配置相同的注册中心地址(registry.type)和事务组名称(tx-service-group),以实现分布式事务的高可用。 - 配置Seata Server实例之间的相互注册,每个Seata Server实例都需要配置其他Seata Server实例的地址(registry.address),这样它们才能相互发现和协调分布式事务。 - 可以通过配置负载均衡器(如Nginx)来实现Seata Server集群的负载均衡和高可用。 需要注意的是,Eureka和Seata的集群搭建都需要考虑到高可用和负载均衡的问题,具体实现可以根据实际情况选择不同的方案。同时,集群搭建也需要注意配置的正确性和安全性,避免出现故障或安全问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值