【云原生】微服务SpringCloud-eureka(server)集群搭建

121 阅读3分钟

携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第2天,点击查看活动详情

工作原理:

Spring Cloud框架下的服务发现Eureka包含两个组件

分别是:Eureka Server与Eureka Client
Eureka Server,也称为服务注册中心。各个服务启动后,会在Eureka Server中进行注册,这样Eureka Server的服务注册表中将会存储所有可用服务节点的信息,服务节点的信息可以在界面中直观的看到。
Eureka Client也称为服务(服务实例)。作为一个Java客户端,用于简化与Eureka Server的交互。Eureka Client内置一个 使用轮询负载算法的负载均衡器。服务启动后,Eureka Client将会向Eureka Server发送心跳更新服务,如果Eureka Server在多个心跳周期内没有接收到某个服务的心跳,Eureka Server将会从服务注册表中把这个服务节点移除(默认90秒)。

Eureka组件的工作原理和核心功能点

上面的图有三台 Eureka Server 组成的集群,每一台 Eureka Server服务在不同的地方。这样三台 Eureka Server 就组建成了一个高可用集群服务,只要三个服务有一个能一直正常运行,就不会影响整个架构的稳定性。

eureka 高可用集群

Eureka服务是一个单点服务,在生产环境就会出现单点故障,为了确保Eureka服务的高可用,我需要搭建Eureka服务的集群。搭建Eureka集群非常简单,只要启动多个Eureka Server服务并且让这些Server端之间彼此进行注册即可实现

 在我们实际的开发生产环境中,eureka 常常是以集群的方式提供服务的,目的就是要保证高可用性,同时它还保证了分区容错性。这也满足了一个健壮的分布式微服务所要求的 CAP 理论原则,即 eureka 保证了高可用性,分区容错性。

项目创建:

 项目搭建的主要步骤和配置就是创建项目和引入pom依赖。新建3个eureka注册中心

  @EnableEurekaServer:项目启动类上使用@EnableEurekaServer注解/项目就是SpringCloud的注册中心。

YML配置

配置3个eureka-server的application.yml

server:
  port: 7001
#Eureka
eureka:
  instance:
    hostname: eureka7001.com     #Eureka服务端实例名字
  client:
    register-with-eureka: false  #表示是否向Eureka注册中心注册自己(服务器端不需要)
    fetch-registry: false  #false表示自己就是注册中心
    service-url:
      defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
 

Maven 依赖

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.liy</groupId>
    <artifactId>eurekaserver-7001</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eurekaserver-7001</name>
    <description>Demo project for Spring Boot</description>
 
    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.3.0.RELEASE</spring-boot.version>
        <spring-cloud.version>Hoxton.SR4</spring-cloud.version>
    </properties>
 
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.liy</groupId>
            <artifactId>eurekaserver-7001</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
 
    <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>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
 
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
 
</project>

本地hosts文件修改

需要配置三个hostname、否则无法集群  

在C:\Windows\System32\drivers\etc\hosts 文件类增加
127.0.0.1   eureka7001.com
127.0.0.1   eureka7002.com
127.0.0.1   eureka7003.com
 
注册集群的三个端口分别为
7001/7002/7003

启动服务测试

启动三个eureka-server进行访问测试

 下面 这里表示这有2个注册中心的集群节点、当前的注册中心会从这两个节点进行同步服务、可以通过我们配置的hostname来进行识别。

  查看当前注册中心的集群节点