微服务架构师封神之路11-eureka服务高可用

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

微服务架构师封神之路11-eureka服务高可用

  • 任务
  • eureka server
    • pom.xml 依赖
    • application.yaml settings
    • 3节点集群服务
  • eureka client
    • pom.xml
    • application.yaml
  • test


任务

一些设计比较老的应用使用eureka作为服务注册发现组件。产品实施的时候不得不考虑高可用的问题。
实现三节点集群的高可用eureka服务。
Implementing a 3 nodes eureka cluster for high availibility.


eureka server

pom.xml 依赖


<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.0modelVersion>

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.2.4.RELEASEversion>
    parent>

    <groupId>com.b5wang.cloudlab.springcloudexgroupId>
    <artifactId>scex-eureka-serverartifactId>
    <version>0.1-SNAPSHOTversion>
    <packaging>jarpackaging>

    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <maven.compiler.source>1.8maven.compiler.source>
        <maven.compiler.target>1.8maven.compiler.target>
        <spring-cloud.version>Hoxton.SR1spring-cloud.version>
        <log4j-1.2-api.version>2.13.3log4j-1.2-api.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintagegroupId>
                    <artifactId>junit-vintage-engineartifactId>
                exclusion>
            exclusions>
        dependency>
    dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>${spring-cloud.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>

    <build>
        <finalName>${project.artifactId}finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-site-pluginartifactId>
                <version>3.8.2version>
            plugin>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-project-info-reports-pluginartifactId>
                <version>3.0.0version>
            plugin>
        plugins>
    build>
project>

application.yaml settings

server:
  port: 8761
spring:
  application:
    name: scex-eureka-server
eureka:
  server:
    enable-self-preservation: false
  instance:
    prefer-ip-address: true
  client:
    register-with-eureka: false
    fetch-registry: false

3节点集群服务

先运行mvn clean install将eureka server打包成jar。
分别启动三个eureka实例,为他们分配不同的端口,并且每个实例要作为eureka client去将自己所拥有的应用注册信息同步到另外两个实例上。

java -jar target/scex-eureka-server.jar --server.port=8761 --spring.application.name=eureka-server-1 --eureka.client.service-url.defaultZone=http://127.0.0.1:8762/eureka,http://127.0.0.1:8763/eureka

java -jar target/scex-eureka-server.jar --server.port=8762 --spring.application.name=eureka-server-2 --eureka.client.service-url.defaultZone=http://127.0.0.1:8761/eureka,http://127.0.0.1:8763/eureka

java -jar target/scex-eureka-server.jar --server.port=8763 --spring.application.name=eureka-server-3 --eureka.client.service-url.defaultZone=http://127.0.0.1:8761/eureka,http://127.0.0.1:8762/eureka


eureka client

pom.xml


<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.0modelVersion>

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.2.4.RELEASEversion>
    parent>

    <groupId>com.b5wang.cloudlab.springcloudexgroupId>
    <artifactId>scex-eureka-client-aartifactId>
    <version>0.1-SNAPSHOTversion>
    <packaging>jarpackaging>

    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <maven.compiler.source>1.8maven.compiler.source>
        <maven.compiler.target>1.8maven.compiler.target>
        <spring-cloud.version>Hoxton.SR1spring-cloud.version>
        <log4j-1.2-api.version>2.13.3log4j-1.2-api.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintagegroupId>
                    <artifactId>junit-vintage-engineartifactId>
                exclusion>
            exclusions>
        dependency>
    dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>${spring-cloud.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>

    <build>
        <finalName>${project.artifactId}finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-site-pluginartifactId>
                <version>3.8.2version>
            plugin>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-project-info-reports-pluginartifactId>
                <version>3.0.0version>
            plugin>
        plugins>
    build>
project>

application.yaml

配置eureka server URL时候要放三个实例的URL,

server:
  port: 8081
spring:
  application:
    name: eureka-client-a
eureka:
  instance:
    prefer-ip-address: true
  client:
    serviceUrl:
      defaultZone: http://127.0.0.1:8761/eureka,http://127.0.0.1:8762/eureka,http://127.0.0.1:8763/eureka
    register-with-eureka: true
    fetch-registry: true
logging:
  level:
    org.springframework.cloud.netflix: TRACE

test

  • 启动eureka client,访问eureka server management page,
    http://127.0.0.1:8761/
    http://127.0.0.1:8762/
    http://127.0.0.1:8763/
  • 尝试停止一个或两个eureka server,再观察eureka server是否还能同样的提供服务。

你可能感兴趣的:(微服务架构实践,eureka,微服务,spring,cloud)