zoukankan      html  css  js  c++  java
  • Eureka单机高可用伪集群配置

    Eureka Server高可用集群
    理论上来讲,因为服务消费者本地缓存了服务提供者的地址,即使Eureka Server宕机,也不会影响服务之间的调用,但是一旦新服务上线,已经缓存在本地的服务提供者不可用了,服务消费者也无法知道,所以保证Eureka Server的高可用还是很有必要的。

    在分布式系统中,任何的地方存在单点,整个体系就不是高可用的,Eureka 也一样,Eureka Server不是以单点存在的,而是以集群的方式对外提供服务。


    模拟在一台机器上搭建Eureka集群,配置peer1、peer2、peer3三个节点组成Eureka的集群。

    1、配置域名解析
    Hosts文件打开方法:打开windows命令窗口,输入“drivers”,选择“etc”文件夹,选择“hosts”文件添加peer配置。

    2、新建 Eureka 服务端集群项目
    a、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.0</modelVersion>
        <parent>
            <groupId>com.mimaxueyuan</groupId>
            <artifactId>mima-cloud-parent</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </parent>
        <artifactId>mima-cloud-eureka-ha</artifactId>
        <packaging>jar</packaging>
        
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    <!--         <dependency> -->
    <!--             <groupId>org.springframework.boot</groupId> -->
    <!--             <artifactId>spring-boot-starter-security</artifactId> -->
    <!--         </dependency> -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka-server</artifactId>
            </dependency>
        </dependencies>
    </project>
    View Code

    b、application.yml

    spring:
      application:
        name: mima-cloud-eureka-ha
      profiles:
        active: peer1
    ---
    server:
      port: 8762
    spring:
      profiles: peer1
    eureka:
      instance:
        hostname: peer1
        prefer-ip-address: true
        instance-id: ${spring.application.name}:${server.port}
      client:
        serviceUrl:
          defaultZone: http://peer2:8763/eureka/,http://peer3:8764/eureka/
    ---
    server:
      port: 8763
    spring:
      profiles: peer2
    eureka:
      instance:
        hostname: peer2
        prefer-ip-address: true
        instance-id: ${spring.application.name}:${server.port}
      client:
        serviceUrl:
          defaultZone: http://peer1:8762/eureka/,http://peer3:8764/eureka/
    ---
    server:
      port: 8764
    spring:
      profiles: peer3
    eureka:
      instance:
        hostname: peer3
        prefer-ip-address: true
        instance-id: ${spring.application.name}:${server.port}
      client:
        serviceUrl:
          defaultZone: http://peer1:8762/eureka/,http://peer2:8763/eureka

    配置文件是通过三个Eureka Server互相注册,这里有四段配置,第一段配置为公共配置,配置了应用名称,第二段为名peer1的配置,第三段为peer2的配置,第三段为peer3的配置。在项目启动可以通过
    --spring.profiles.active={配置名称} 来启动不同的配置。

    c、Eureka服务端启动类

    package com.mimaxueyuan.cloud.eureka;
    
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.builder.SpringApplicationBuilder;
    import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
    
    //eureka高可用
    @SpringBootApplication
    @EnableEurekaServer
    public class EurekaHAApplication {
        
        public static void main(String[] args) {
            new SpringApplicationBuilder(EurekaHAApplication.class).web(true).run(args);
        }
        
    }

    3、eureka客户端修改eureka服务端的地址

    eureka:
      instance:
        prefer-ip-address: true
        instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${server.port}
      client: 
        serviceUrl: 
          defaultZone: http://peer1:8762/eureka/,http://peer2:8763/eureka/,http://peer3:8764/eureka/
          #defaultZone: http://127.0.0.1:8762/eureka/,http://127.0.0.1:8763/eureka/,http://127.0.0.1:8764/eureka/

     

    4、启动eurekaClient客户端

    5、启动eurekaServer服务端
    因使用eureka集群,启动的时候需要指定配置文件:

    启动peer1节点命令:
    java -jar D:jareurekaServereurekaServerHigh.jar--spring.profiles.active=peer1

    启动peer2节点命令:
    java -jar D:jareurekaServereurekaServerHigh.jar--spring.profiles.active=peer2

    启动peer3节点命令:
    java -jar D:jareurekaServereurekaServerHigh.jar--spring.profiles.active=peer3

    从上面可以看到,eurekaClient客户端MIMA-CLOUD-PRODUCER已注册到Eureka集群中。

    现把MIMA-CLOUD-PRODUCER服务关闭,刷新注册中心节点

    非Java服务注册到Eureak Server
    作为服务注册中心,应该是语言无关的,使用其他语言的服务也可以通过调用Eureka Server的Rest API 注册服务,这里不详细展开。

  • 相关阅读:
    html笔记
    Git入门学习总结
    使用OpenSSH远程管理Linux服务器
    Linux 网卡驱动的安装
    vi的使用
    Linux下常用的数据恢复工具
    网络文件系统(NFS)的使用
    文件系统管理
    磁盘存储管理
    用户权限管理
  • 原文地址:https://www.cnblogs.com/linjiqin/p/10090738.html
Copyright © 2011-2022 走看看