源码仓库地址:https://github.com/heibaiying/spring-samples-for-all

一、项目结构

eureka-server为服务注册中心,负责服务的管理;

eureka-client 为eureka客户端;

二、三步搭建eureka 高可用注册中心

这里我们以单机伪集群的方式搭建,让三个单机注册中心互相注册,实现注册中心的高可用。配置示意图如下:

2.1 引入eureka服务端依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

2.2 创建三份配置文件,分别代表不同注册中心的配置

application-01.yml:

spring:
  application:
    name: server
server:
  port: 8010
eureka:
  server:
    # 关闭自我保护机制 开发的时候可以开启 保证不可用的服务能够及时剔除
    enable-self-preservation: false
  instance:
    hostname: 127.0.0.1
  client:
    serviceUrl:
          defaultZone: http://localhost:8020/eureka/,http://192.168.200.228:8030/eureka/

application-02.yml

spring:
  application:
    name: server
server:
  port: 8020
eureka:
  server:
    # 关闭自我保护机制 开发的时候可以开启 保证不可用的服务能够及时剔除
    enable-self-preservation: false
  instance:
    hostname: localhost
  client:
    serviceUrl:
          defaultZone: http://127.0.0.1:8010/eureka/,http://192.168.200.228:8030/eureka/

application-03.yml

spring:
  application:
    name: server
server:
  port: 8030
eureka:
  server:
    # 关闭自我保护机制 开发的时候可以开启 保证不可用的服务能够及时从列表中剔除
    enable-self-preservation: false
  instance:
    hostname: 192.168.200.228
  client:
    serviceUrl:
          defaultZone: http://127.0.0.1:8010/eureka/,http://localhost:8020/eureka/

需要注意的是Eureka互相注册要求各个Eureka实例的eureka.instance.hostname不同,如果相同,则会被Eureka标记为unavailable-replicas(不可用副本)。

2.3 启动类上增加注解@EnableEurekaServer激活eureka服务端自动配置

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }

}

三、三步搭建eureka 客户端

3.1 引入eureka客户端依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

3.2 eureka 客户端配置,指定注册中心地址

server:
  port: 8040
# 指定服务命名
spring:
  application:
    name: eureka-client
# 指定注册中心地址
eureka:
  client:
    serviceUrl:
      defaultZone: http://127.0.0.1:8010/eureka/,http://localhost:8020/eureka/,http://192.168.200.228:8030/eureka/

3.3 启动类上增加注解@EnableDiscoveryClient激活eureka客户端自动配置

@SpringBootApplication
@EnableDiscoveryClient
public class EurekaClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaClientApplication.class, args);
    }

}

4.启动项目

4.1 这里我们可以采用命令行方式指定配置,分别启动三个注册中心

4.2 高可用集群搭建成功的判定

这里需要主要的是仅仅status中出现其他注册中心时,并不一定是搭建成功的,一定是当注册中心的DS Replicas 和 available replicas中显示其余的注册中心时候,才代表搭建成功。

4.2.1 点击下面注册中心的可用实例列表中的地址,访问链接分以下几个情况:

  1. hostname和prefer-ip-address都没有配置,则访问 主机名:服务名:端口号,
    如:http://desktop-8jgsflj:8761/info
  1. 配置了hostname而没有配置prefer-ip-address,则访问 hostname:服务名:端口号,
     如:http://server:8761/info
  1. 如果配置了prefer-ip-address,则访问 ipAddress:服务名:端口号,
     如:http://192.168.200.228:8761/info

8010 注册中心:

8020 注册中心:

8030 注册中心:

4.3 prefer-ip-address 参数说明

在有的配置示例中,配置了prefer-ip-address为true。

eureka.instance.prefer-ip-address=true

在多机器独立部署的情况下是没有问题的,配置prefer-ip-address为ture,代表发现服务时候优先按照ip去搜寻,对于多集群而言,可以保证尽快准确搜索到服务。而对于单机部署来说,ip地址都是相同的,这会导致其余注册中心出现在unavailable-replicas(不可用副本)中。所以单机部署时候不建议开启这个参数(默认值为false),多机部署时候可以开启。

源码仓库地址:https://github.com/heibaiying/spring-samples-for-all

spring cloud 系列第2篇 —— eureka 高可用注册中心的搭建 (F版本)的更多相关文章

  1. spring cloud 系列第1篇 —— eureka 服务的注册与发现 (F版本)

    源码仓库地址:https://github.com/heibaiying/spring-samples-for-all 一.eureka 简介 Spring Cloud Eureka使用Netflix ...

  2. spring cloud 系列第5篇 —— hystrix+turbine 服务的熔断与监控 (F版本)

    源码Gitub地址:https://github.com/heibaiying/spring-samples-for-all 一.hystrix 简介 1.1 熔断器 在分布式系统中,由于服务之间相互 ...

  3. Spring Cloud系列教程第九篇-Eureka自我保护机制

    Spring Cloud系列教程第九篇-Eureka自我保护机制 本文主要内容: 1:自我保护介绍 2:导致原因分析 3:怎么禁止自我保护 本文是由凯哥(凯哥Java:kagejava)发布的< ...

  4. eureka高可用注册中心

    Eureka高可用注册中心 两个配置文件: application-peer1.properties application-peer2.properties 都需要加上 eureka.client. ...

  5. Spring cloud搭建Eureka高可用注册中心

    注册中心在微服务中是必不可少的一部分,主要用来实现服务自治的功能,本文则主要记载使用Netflix提供的Eureka作为注册中心,来实现服务自治的功能. 实际上Eureka的集群搭建方法很简单:每一台 ...

  6. 笔记:Spring Cloud Eureka 高可用注册中心

    在微服务架构这样的分布式环境中,我们需要充分考虑发生故障的情况,所以在生产环境中必须对各个组件进行高可用部署,对与微服务和服务注册中心都需要高可用部署,Eureka 高可用实际上就是将自己作为服务向其 ...

  7. Spring Cloud之踩坑01 -- Eureka高可用配置

    转载:https://blog.csdn.net/dear_Alice_moon/article/details/79373955 问题描述: 在进行Eureka高可用配置时,控制台一直出现“.... ...

  8. Spring Cloud Eureka 高可用注册中心

    参考:<<spring cloud 微服务实战>> 在微服务架构这样的分布式环境中,各个组件需要进行高可用部署. Eureka Server 高可用实际上就是将自己作为服务向其 ...

  9. spring cloud 系列第8篇 —— config+bus 分布式配置中心与配置热刷新 (F版本)

    源码Gitub地址:https://github.com/heibaiying/spring-samples-for-all 一.config 简介 spring cloud config 分为服务端 ...

随机推荐

  1. Exception: java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams

    RelativeLayout title_bg = (RelativeLayout)FTU_Bluetooth.this.findViewById(R.id.titlebar); LinearLayo ...

  2. Plugin execution not covered by lifecycle configuration

    Eclipse 环境 在工作空间 \.metadata\.plugins\org.eclipse.m2e.core\ 目录下 增加 lifecycle-mapping-metadata.xml 文件 ...

  3. Entity framework 配置文件,实现类,测试类

    配置文件信息App.config: 数据库IP地址为192.168.2.186 ,数据库名为 Eleven-Six , 用户名 123456,密码654321 <?xml version=&qu ...

  4. JS正则--非负整数或小数[小数最多精确到小数点后两位]

    function ValidPrice(obj) { s = obj.value; //var reg = /^[0-9]*\.[0-9]{0,2}$/; var reg = /^[0-9]+([.] ...

  5. .net core config读取

    最简单的方式 引用 Microsoft.Extensions.Configuration Microsoft.Extensions.Configuration.Json json文件 新建一个Conf ...

  6. abp.message

    abp.message.success(app.localize('SomeMessage'), app.localize('Title')) .done(function() { //do some ...

  7. WPF加载等待动画

    原文:WPF加载等待动画 原文地址:https://www.codeproject.com/Articles/57984/WPF-Loading-Wait-Adorner 界面遮罩 <UserC ...

  8. 静态资源(StaticResource)和动态资源(DynamicResource)

    一.文章概述 本演示介绍了WPF的静态资源和动态资源的基本使用,并对两者做了简单的比较. 二.定义并使用资源 <Window x:Class="Demo010.MainWindow&q ...

  9. MVC 自定义路由规则

    using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mv ...

  10. 所有语言的Awesome(2)

    Curated list of awesome lists https://awesomeweekly.co https://github.com/sindresorhus/awesome ✨ Pre ...