搭建高可用的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. POJ 3666 Making the Grade(数列变成非降序/非升序数组的最小代价,dp)

    传送门: http://poj.org/problem?id=3666 Making the Grade Time Limit: 1000MS   Memory Limit: 65536K Total ...

  2. Java8函数之旅 (七) - 函数式备忘录模式优化递归

    前言 在上一篇开始Java8之旅(六) -- 使用lambda实现Java的尾递归中,我们利用了函数的懒加载机制实现了栈帧的复用,成功的实现了Java版本的尾递归,然而尾递归的使用有一个重要的条件就是 ...

  3. 外部的 JavaScript

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  4. 数据库——MySQL——数据类型

    详细的看后面给的链接,我只是挑了一部分:http://www.runoob.com/mysql/mysql-data-types.html 在之前说了MySQL的存储引擎.它决定了表的类型,而表内存放 ...

  5. 课时92.CSS元素显示模式转换(掌握)

    我们之前学习的显示模式都可以不用记忆,因为这节课我们要学习转换,我们可以任意来进行一个转换的,上面这些东西有一个了解就行了.所有的标签都有一个属性叫做display,display的中文含义就是显示的 ...

  6. ref、refs使用的注意事项

    ref是被用来给元素或子组件注册引用信息.引用信息将注册在父组件的 $refs 对象身上.如果在普通的DOM元素身上使用,引用指向就是DOM元素:如果用在子组件身上,引用就是指向组件实例. 当v-fo ...

  7. linux运维、架构之路-shell编程(二)

    一.流程控制语句 1.if语句 ①if单分支:一个条件一个结果 1 2 3 4 if 条件   then      命令 fi ②if双分支:一个条件两个结果 1 2 3 4 5 6 if 条件    ...

  8. iptables应用

    192.168.4.119 为本机的ip地址:每条链的规则是由上至下进行匹配,因此我们需要把范围小的规则放在上面以防被覆盖. 1)清空iptables默认规则,并自定义规则 [root@iptable ...

  9. javascript--BOM的onload事件和onunload事件

    1.onload事件 onload,页面加载后执行,所谓页面加载完成,指页面上所有的元素创建完毕,引用的所有的外部资源(js.css.图片)等下载完毕. 所以onload执行的比较晚,因为如果页面上有 ...

  10. Redis学习推荐

    Redis快速入门 https://www.yiibai.com/redis/redis_quick_guide.html Redis用途和使用场景 https://blog.csdn.net/wei ...