• SpringCloud教程第10篇:高可用的服务注册中心(F版本)


    一、准备工作

    Eureka通过运行多个实例,使其更具有高可用性。事实上,这是它默认的熟性,你需要做的就是给对等的实例一个合法的关联serviceurl。

    这篇文章我们基于SpringCloud教程第1篇:Eureka(F版本)文章的工程

    二、改造工作

    在eureka-server工程中resources文件夹下,创建配置文件application-peer1.yml:

    server:
      port: 8761
    
    spring:
      profiles: peer1
    eureka:
      instance:
        hostname: peer1
      client:
        serviceUrl:
          defaultZone: http://peer2:8769/eureka/

    并且创建另外一个配置文件application-peer2.yml:

    server:
      port: 8769
    
    spring:
      profiles: peer2
    eureka:
      instance:
        hostname: peer2
      client:
        serviceUrl:
          defaultZone: http://peer1:8761/eureka/

    这时eureka-server就已经改造完毕。

    you could use this configuration to test the peer awareness on a single host (there’s not much value in doing that in production) by manipulating /etc/hosts to resolve the host names.

    按照官方文档的指示,需要改变etc/hosts,linux系统通过vim /etc/hosts ,加上:

    127.0.0.1 peer1
    127.0.0.1 peer2

    windows电脑,在c:/windows/systems/drivers/etc/hosts 修改。

    这时需要改造下service-hi:

    eureka:
      client:
        serviceUrl:
          defaultZone: http://peer1:8761/eureka/
    server:
      port: 8762
    spring:
      application:
        name: service-hi

    三、启动工程

    启动eureka-server:

    java -jar eureka-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer1
    
    java -jar eureka-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer2

    启动service-hi:

    java -jar service-hi-0.0.1-SNAPSHOT.jar

    访问:localhost:8761,如图:

    你会发现注册了service-hi,并且有个peer2节点,同理访问localhost:8769你会发现有个peer1节点。

    client只向8761注册,但是你打开8769,你也会发现,8769也有 client的注册信息。

    Prefer IP Address
    In some cases, it is preferable for Eureka to advertise the IP Adresses of services rather than the hostname. Set eureka.instance.preferIpAddress to true and when the application registers with eureka, it will use its IP Address rather than its hostname.
    
    摘自官网

    eureka.instance.preferIpAddress=true是通过设置ip让eureka让其他服务注册它。也许能通过去改变host的方式。

    此时的架构图:

    Eureka-eserver peer1 8761,Eureka-eserver peer2 8769相互感应,当有服务注册时,两个Eureka-eserver是对等的,它们都存有相同的信息,这就是通过服务器的冗余来增加可靠性,当有一台服务器宕机了,服务并不会终止,因为另一台服务存有相同的数据。

    源码:https://github.com/CMRcircle/learnspringcloud/tree/master/sc-f-chapter10

  • 相关阅读:
    动手做第一个Chrome插件
    Discuz NT 架构剖析之Config机制
    用游标实现查询当前服务器所有数据库所有表的SQL
    Discuz X3.2 网站快照被劫持的解决方法
    centos下MYSQL 没有ROOT用户的解决方法。
    redis命令1
    在当今快节奏的软件更迭当中,我们是否还需要进行系统的学习?
    StructureMap 代码分析之Widget 之Registry 分析 (1)
    C#面试题汇总(未完成)
    C#:解决WCF中服务引用 自动生成代码不全的问题。
  • 原文地址:https://www.cnblogs.com/chengmr/p/13254934.html
Copyright © 2020-2023  润新知