搭建高可用的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. 【Linux-CentOS】CentOS安装Win双系统后Win启动项丢失及默认启动项修改

    转载自:搁浅bky,有部分更正,建议看此文. 1.Windows启动项消失的原因:   在安装Win7.8/10系统+CentOS7双系统后,默认会将mbr(Main Boot Record)改写为g ...

  2. lucene&solr学习——solr学习(二) Solr管理索引库

    1.什么是solrJ solrj是访问Solr服务的java客户端,提供索引和搜索的请求方法,SolrJ通常在嵌入在业务系统中,通过SolrJ的API接口操作Solr服务,如下图: 依赖jar包: 2 ...

  3. Eclipse插件的卸载和安装

    Eclipse 卸载插件: 右下角会有卸载进度 卸载完后 然后需要重启 Eclipse安装插件 选择本地下载好的插件 点击 Ok 插件下载地址:https://jaist.dl.sourceforge ...

  4. Spring技术内幕阅读笔记(一)

    1.BeanFactory:实现ioc容器的最基本形式.String FACTORY_BEAN_PREFIX = "&";Object getBean(String var ...

  5. VSS使用方法详解

    Microsoft Visual SourceSafe是美国微软公司出品的版本控制系统,简称VSS.它提供了还原点和并行协作功能,从而使应用程序开发组织能够同时处理软件的多个版本.该版本控制系统引入了 ...

  6. stl之std::remove_copy

    template <class InputIterator, class OutputIterator, class T> OutputIterator remove_copy (Inpu ...

  7. iOS | CAShapeLayer转场动画

    什么也不说了,作为一名乐于分享技术的小开发,直接先上个样式最为直观贴切,有需要的朋友可以直接拿过去用. 需要demo请点击这里 :github 在这个demo中,核心为选用画布CAShapeLayer ...

  8. Vue组件通讯黑科技

    Vue组件通讯 组件可谓是 Vue框架的最有特色之一, 可以将一大块拆分为小零件最后组装起来.这样的好处易于维护.扩展和复用等. 提到 Vue的组件, 相必大家对Vue组件之间的数据流并不陌生.最常规 ...

  9. 转载:EJB到底是什么

    这篇博客用通俗易懂的语言对EJB进行了介绍,写得很好,笔者在这里转载一下. 链接:https://www.cnblogs.com/strugglion/p/6027318.html

  10. Ehcache基于java API实现

    上代码: package com.utils.cacheutils; import com.situopenapi.constant.EhcacheConstants; import com.situ ...