Eureka 高可用集群搭建

来自姬鸿昌的知识库
跳到导航 跳到搜索

https://www.bilibili.com/video/BV1eU4y187zE/?p=13

最终效果:

编写一个项目通过不同的配置文件加载不同参数。

最后把集群部署到服务器上。集群设定有两个 Eureka Server,每个 Eureka Server 安装到不同的服务器上。

添加依赖

在集群中依赖和单机版是完全相同的。

为了后面打包发布到服务器中,所以同时也导入了 Spring Boot 打包插件。


pom.xml

<?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>io.github.jihch</groupId>
    <artifactId>eureka-ha</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

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

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.SR8</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>

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

        <!-- 由于已经依赖的 web 所以不需要单独再导入一次 web -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

    </dependencies>

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

</project>

application-eurekaserver1.yml

spring:
  application:
    # 名字不要是下划线,要中划线
    name: eureka-server
server:
  port: 8761
eureka:
  instance:
    # 和配置文件的 application-xxx.yml 相同
    hostname: eurekaserver1
  client:
    service-url:
      defaultZone: http://eurekaserver2:8761/eureka/

application-eurekaserver2.yml

spring:
  application:
    # 名字不要是下划线,要中划线
    name: eureka-server
server:
  port: 8761
eureka:
  instance:
    # 和配置文件的 application-xxx.yml 相同
    hostname: eurekaserver2
  client:
    service-url:
      defaultZone: http://eurekaserver1:8761/eureka/

hosts

192.168.0.120 eurekaserver1
192.168.0.100 eurekaserver2

server.sh

Spring Boot 启动脚本



注册 Eureka client 到 Eureka Server 中

eureka-client

server:
  port: 8081
eureka:
  client:
    service-url:
      # 下面信息也是默认值
      defaultZone: http://eurekaserver1:8761/eureka/,http://eurekaserver2:8761/eureka/


项目

https://github.com/jihch/eureka-ha