搭建高可用的Eureka注册中心

一、搭建高可用的Eureka的作用

  当服务器因种种原因导致Eureka注册中心(后面简称Eureka)服务当机(服务器跪了,异常关闭停止服务)。这样就会影响到整个业务的流程,因为你把所有的业务都注册到了Eureka中,当Eureka所依赖的docker(容器)当机了,这就会影响到所有在Eureka中注册的服务全部error。因为Eureka的请求流程在上一篇中说过,当A服务向B服务发送一个请求的时候,他是不会直接请求B服务,首先A服务先会到Eureka,Eureka拿到A服务请求的api,Eureka会通过这个api会去找在Eureka中注册的所有服务(这个过程叫做服务发现),找到服务之后,Eureka会返回给A一个服务列表(因为B服务可能会做负载均衡,就是会向Eureka注册两个B服务,这个过程叫做服务消费),当A服务拿到Eureka返回来的服务列表,根据自身的一些机制比如feign,ribbon等(这个后面都会讲到)进行处理。

二、第一个Eureka搭建

  1. pom.xml文件的配置

     1 <?xml version="1.0" encoding="UTF-8"?>
    2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    4 <modelVersion>4.0.0</modelVersion>
    5
    6 <groupId>cn.ds</groupId>
    7 <artifactId>eureka-server-001</artifactId>
    8 <version>0.0.1-SNAPSHOT</version>
    9 <packaging>jar</packaging>
    10
    11 <name>eureka-server-001</name>
    12 <description>Eureka服务-001</description>
    13
    14 <parent>
    15 <groupId>org.springframework.boot</groupId>
    16 <artifactId>spring-boot-starter-parent</artifactId>
    17 <version>2.0.3.RELEASE</version>
    18 <relativePath/> <!-- lookup parent from repository -->
    19 </parent>
    20
    21 <properties>
    22 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    23 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    24 <java.version>1.8</java.version>
    25 <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
    26 </properties>
    27
    28 <dependencies>
    29 <dependency>
    30 <groupId>org.springframework.boot</groupId>
    31 <artifactId>spring-boot-starter-web</artifactId>
    32 </dependency>
    33 <!-- 引入Eureka服务 -->
    34 <dependency>
    35 <groupId>org.springframework.cloud</groupId>
    36 <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    37 </dependency>
    38 <dependency>
    39 <groupId>org.springframework.boot</groupId>
    40 <artifactId>spring-boot-devtools</artifactId>
    41 <scope>runtime</scope>
    42 </dependency>
    43 <dependency>
    44 <groupId>org.springframework.boot</groupId>
    45 <artifactId>spring-boot-starter-test</artifactId>
    46 <scope>test</scope>
    47 </dependency>
    48 </dependencies>
    49
    50 <dependencyManagement>
    51 <dependencies>
    52 <dependency>
    53 <groupId>org.springframework.cloud</groupId>
    54 <artifactId>spring-cloud-dependencies</artifactId>
    55 <version>${spring-cloud.version}</version>
    56 <type>pom</type>
    57 <scope>import</scope>
    58 </dependency>
    59 </dependencies>
    60 </dependencyManagement>
    61
    62 <build>
    63 <plugins>
    64 <plugin>
    65 <groupId>org.springframework.boot</groupId>
    66 <artifactId>spring-boot-maven-plugin</artifactId>
    67 </plugin>
    68 </plugins>
    69 </build>
    70
    71
    72 </project>
  2. application.properties配置:这里需要注意的是,IP地址不能写localhost或者127.0.0.1,后面会说到这个IP地址该怎么配置
     1 #    Eureka注册中心配置
    2 # server-port:项目端口号
    3 # spring-application-name:项目注册到Eureka显示的调用名称,类似于域名
    4 # eureka.client.register-with-erueka:是否将自己注册到Eureka,默认为true
    5 # eureka.client.fetch-registry:是否向Eureka获取注册信息,默认为true
    6 # spring.jmx.default-domain:区分spring-boot项目
    7 # eureka.instance.hostname:作为eureka-server-001服务配置中心
    8 # eureka.client.service-url.defaultZone:双节点注册
    9 server.port = 8080
    10 spring.application.name = eureka-server
    11 eureka.client.register-with-eureka = true
    12 eureka.client.fetch-registry = true
    13 spring.jmx.default-domain = erueka-server-001
    14 eureka.instance.hostname = eureka-server-001
    15 eureka.client.service-url.defaultZone = http://eureka-server-001:8080/eureka/,http://eureka-server-002:8081/eureka/
  3. springboot启动类配置
     1 package cn.yuzhenzi;
    2
    3 import org.springframework.boot.SpringApplication;
    4 import org.springframework.boot.autoconfigure.SpringBootApplication;
    5 import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    6
    7 /**
    8 * @author 玉眞子
    9 * @name springboot启动类
    10 * */
    11 @SpringBootApplication
    12 @EnableEurekaServer //启动注册中心
    13 public class Application {
    14
    15 public static void main(String[] args) {
    16 SpringApplication.run(Application.class, args);
    17 }
    18 }  

三、第二个Eureka搭建

  1. 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>cn.ds</groupId>
    <artifactId>eureka-server-002</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging> <name>eureka-server-002</name>
    <description>Eureka服务-002</description> <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.3.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
    </parent> <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
    </properties> <dependencies>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- 引入Eureka服务 -->
    <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>
    </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>
  2. application.properties文件的配置:这里需要注意的是,IP地址不能写localhost或者127.0.0.1,后面会说到这个IP地址该怎么配置
     #    Eureka注册中心配置
    # server-port:项目端口号
    # spring-application-name:项目注册到Eureka显示的调用名称,类似于域名
    # eureka.client.register-with-erueka:是否将自己注册到Eureka,默认为true
    # eureka.client.fetch-registry:是否向Eureka获取注册信息,默认为true
    # spring.jmx.default-domain:区分spring-boot项目
    # eureka.instance.hostname:作为eureka-server-001服务配置中心
    # eureka.client.service-url.defaultZone:双节点注册
    server.port = 8081
    spring.application.name = eureka-server
    eureka.client.register-with-eureka = true
    eureka.client.fetch-registry = true
    spring.jmx.default-domain = eureka-server-002
    eureka.instance.hostname = eureka-server-002
    eureka.client.service-url.defaultZone = http://eureka-server-002:8081/eureka/,http://eureka-server-001:8080/eureka/
  3. springboot启动类的配置
     package cn.yuzhenzi;
    
     import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient; /**
    * @author 玉眞子
    * @name springboot启动类
    * */
    @SpringBootApplication
    @EnableEurekaServer //启动注册中心
    public class Application { public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
    }
    }

四、IP配置以及注意事项

  1. IP配置
    1. windows配置:/windows/system32/drivers/etc/hosts
    2. Linux配置:/etc/hosts
    3. 如图所示:
       1 # Copyright (c) 1993-2009 Microsoft Corp.
      2 #
      3 # This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
      4 #
      5 # This file contains the mappings of IP addresses to host names. Each
      6 # entry should be kept on an individual line. The IP address should
      7 # be placed in the first column followed by the corresponding host name.
      8 # The IP address and the host name should be separated by at least one
      9 # space.
      10 #
      11 # Additionally, comments (such as these) may be inserted on individual
      12 # lines or following the machine name denoted by a '#' symbol.
      13 #
      14 # For example:
      15 #
      16 # 102.54.94.97 rhino.acme.com # source server
      17 # 38.25.63.10 x.acme.com # x client host
      18
      19 # localhost name resolution is handled within DNS itself.
      20 # 127.0.0.1 localhost
      21 # ::1 localhost
      22 127.0.0.1 eureka-server-001
      23 127.0.0.1 eureka-server-002
  2. 注意事项:当项目启动时会报错,这是正常现象,因为eureka-server-001要注册到eureka-server-002上面,但是eureka-server-002项目还没有启动起来,所以会报错

五、测试分别访问http://localhost:8080/eureka/和http://localhost:8081/eureka/

搭建高可用的Eureka注册中心的更多相关文章

  1. 在Docker环境下部署高可用的Eureka注册中心

    Eureka Server的同步遵循着一个非常简单的原则,只要有一条边将节点连接,就可以进行信息传播和同步 由于Eureka Server进行相互注册的方式来实现高可用的部署,所以我们只需要将Eure ...

  2. 微服务配置内容《网上copy》=========》如何创建一个高可用的服务注册中心

    前言:首先要知道什么是一个高可用的服务注册中心,基于spring boot建成的服务注册中心是一个单节点的服务注册中心,这样一旦发生了故障,那么整个服务就会瘫痪,所以我们需要一个高可用的服务注册中心, ...

  3. 史上最简单的SpringCloud教程 | 第十篇: 高可用的服务注册中心(Finchley版本)

    转载请标明出处: 原文首发于 https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f10-eureka/ 本文出自方志朋的博客 文章 史上最简单 ...

  4. SpringCloud教程 | 第十篇: 高可用的服务注册中心

    版权声明:本文为博主原创文章,欢迎转载,转载请注明作者.原文超链接 ,博主地址:http://blog.csdn.net/forezp. http://blog.csdn.net/forezp/art ...

  5. 【SpringCloud】 第十篇: 高可用的服务注册中心

    前言: 必需学会SpringBoot基础知识 简介: spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选. ...

  6. SpringCloud 教程 (三)高可用的服务注册中心

    一.准备工作 Eureka can be made even more resilient and available by running multiple instances and asking ...

  7. springcloud 高可用的服务注册中心

    https://blog.csdn.net/forezp/article/details/81041101 上面是方老师的博客,看liuyan也有好多同学不是很清楚,这里自己也记录一下具体的做法. 1 ...

  8. spring cloud 入门系列三:使用Eureka 搭建高可用服务注册中心

    在上一篇中分享了如何使用Eureka 进行服务治理,里面搭建的服务注册中心是单体的, 但是在实际的应用中,分布式系统为了防止单体服务宕机带来严重后果,一般都会采用服务器集群的形式,服务注册中心也是一样 ...

  9. Spring Cloud第三篇 | 搭建高可用Eureka注册中心

    ​ ​本文是Spring Cloud专栏的第三篇文章,了解前两篇文章内容有助于更好的理解后面文章: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring ...

随机推荐

  1. C++/C 内存大小

    #include <stdio.h> struct test1{    char a1;    int a2;    double a3;}; struct test2{    char ...

  2. IOS中使用百度地图定位后获取城市坐标,城市名称,城市编号信息

    IOS中使用百度地图定位后获取城市坐标,城市名称,城市编号信息 /**当获取到定位的坐标后,回调函数*/ - (void)didUpdateBMKUserLocation:(BMKUserLocati ...

  3. <<学会提问>>第一章学习笔记

    中国应不应该现在取消死刑? 中医是不是伪科学? 读书无用论? 集体主义和团队精神? 欧洲难民危机,你是支持接收难民,还是反对? 欧洲白左是不是幼稚圣母,抑或是右派种族歧视,顽固保守? 如何看待&quo ...

  4. hdu 3966 Aragorn's Story : 树链剖分 O(nlogn)建树 O((logn)²)修改与查询

    /** problem: http://acm.hdu.edu.cn/showproblem.php?pid=3966 裸板 **/ #include<stdio.h> #include& ...

  5. JDK1.8的安装

    [环境准备] OS版本:Windows10企业版.64位操作系统: JDK版本:jdk-8u131-windows-x64.exe [彻底卸载已安装的JDK] 01:卸载或删除JDK服务.有三种方式: ...

  6. 在Python中使用正则表达式去掉字符串里的html标签

    有时候会获得一些带html标签的字符串,需要把html标签去掉,获得干净的字符串,这时候可以使用正则表达式. 代码如下: import re htmeString = '''<ul id=&qu ...

  7. ECSHOP和SHOPEX快递单号查询百世快递插件V8.6专版

    发布ECSHOP说明: ECSHOP快递物流单号查询插件特色 本ECSHOP快递物流单号跟踪插件提供国内外近2000家快递物流订单单号查询服务例如申通快递.顺丰快递.圆通快递.EMS快递.汇通快递.宅 ...

  8. 图的遍历(Python实现)

    图的遍历(Python实现) 记录两种图的遍历算法——广度优先(BFS)与深度优先(DFS). 图(graph)在物理存储上采用邻接表,而邻接表是用python中的字典来实现的. 两种遍历方式的代码如 ...

  9. py3.7.1下pyinstaller 的安装及打包 坑

    实在无语了,写了个小程序,用pyinstaller打包,运行就出现这个pip install pywin32-ctypes.明明全部都已经安装了啊. 解决办法: 不要在工程设置里安装pyinstall ...

  10. debounce、throttle、requestAnimationFrame

    今天review同事代码,代码实现了返回顶部的功能,用到了lodash库中的throttle,我看着眼生,于是乎去看了下lodash文档,然后牵出了debounce,具体的知识点,这里不再赘述,底部的 ...