注册中心高可用

理论上来讲,服务消费者本地缓存了服务提供者的地址。即使 Eureka Server 宕机,也不会影响服务之间的调用,但是一旦涉及到服务的上下线,本地的缓存信息将会出现偏差,从而影响到了整个微服务架构的稳定性,因此搭建 Eureka Server 集群来提高整个架构的高可用性,是非常有必要的。这样就可以使注册中心高可用。

搭建Eureka集群

开启 Eureka 集群配置后,服务启动时 Eureka Server 会将注册信息向其它 Eureka Server 进行同步,因此搭建高可用架构只需要将 Eureke Server 配置指向其它可用的 serviceUrl 即可。

我们在上面 Eureka 单个示例的基础上,复制出三份来分别命名为:eureka-a、eureka-b、eureka-c 三个示例项目,使用这三个示例项目搭建 Eureka Server 的集群。

接下来需要分别修改 eureka-a、eureka-b、eureka-c 的配置信息。

eureka-a的配置信息如下

@SpringBootApplication
@EnableEurekaServer
public class EurekaAApplication {

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

}
spring:
  application:
    name: eureka server
server:
  port: 8001

eureka:
  instance:
    hostname: eurekaA
  client:
    serviceUrl.defaultZone: http://eurekaB:8002/eureka/,http://eurekaC:8003/eureka/
    register-with-eureka: true
    fetch-registry: true

完整的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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>cn.com.scitc</groupId>
    <artifactId>eureka-a</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka-a</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud-version>Greenwich.SR1</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-starter-test</artifactId>
            <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>
        </dependencies>
    </dependencyManagement>

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

</project>

eureka-b的配置信息如下:

@SpringBootApplication
@EnableEurekaServer
public class EurekaBApplication {

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

}

spring:
  application:
    name: eureka server
server:
  port: 8002

eureka:
  instance:
    hostname: eurekaB
  client:
    serviceUrl.defaultZone: http://eurekaA:8001/eureka/,http://eurekaC:8003/eureka/
    register-with-eureka: true
    fetch-registry: true

完整的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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>cn.com.scitc</groupId>
    <artifactId>eureka-b</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka-b</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring.cloud.version>Greenwich.SR1</spring.cloud.version>
    </properties>

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

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

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </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>
        </dependencies>
    </dependencyManagement>

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

</project>

eureka-c的配置信息如下:

@SpringBootApplication
@EnableEurekaServer
public class EurekaCApplication {

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

}

spring:
  application:
    name: eureka server
server:
  port: 8003

eureka:
  instance:
    hostname: eurekaC
  client:
    serviceUrl.defaultZone: http://eurekaA:8001/eureka/,http://eurekaB:8002/eureka/
    register-with-eureka: true
    fetch-registry: true
<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>cn.com.scitc</groupId>
    <artifactId>eureka-c</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka-c</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring.cloud.version>Greenwich.SR1</spring.cloud.version>
    </properties>

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

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

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </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>
        </dependencies>
    </dependencyManagement>

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

</project>

eureka.client.register-with-eureka和eureka.client.fetch-registry配置为 true ,表示将自己注册到注册中心,并且从注册中心获取注册信息

eureka.client.serviceUrl.defaultZone,指向其它两个注册中心,重点配置内容

测试

依次启动 eureka-a、eureka-b、eureka-c 项目,等待全部启动完成之后,浏览器访问地址:localhost:8001
在这里插入图片描述

在这里插入图片描述

总结

我们学习了为什么需要使用注册中心,以及 Eureka 作为注册中心最关键的组件,都有哪些特点和优势。

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐