zoukankan      html  css  js  c++  java
  • 服务的注册与发现(Eureka)三:集群搭建

    背景

           在微服务中,注册中心非常核心,可以实现服务治理,如果一旦注册出现故障的时候,可能会导致整个微服务无法访问,在这时候就需要对注册中心实现高可用集群模式。

    Eureka高可用原理

    默认情况下Eureka是让服务注册中心,不注册自己

    #eureka 基本信息配置
    eureka:
      instance:
        #注册到eureka的ip地址
        hostname: localhost
      client:
        serviceUrl:
          #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址(单机)。
          defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
        #因为自己是为注册中心,不需要自己注册自己
        register-with-eureka: false
        #因为自己是为注册中心,不需要检索服务
        fetch-registry: false

    Eureka高可用实际上将自己作为服务向其他服务注册中心注册自己,这样就可以形成一组相互注册的服务注册中心,从而实现服务清单的互相同步,达到高可用效果。

    案例搭建

    示例代码

    建立eureka-cluster-provider-consumer子工程,依赖包无变化,这里就不贴依赖了。

    Eureka Server

    服务EurekaCluster7001的application.yml配置

    #服务端口号
    server:
      port: 7001
    #eureka 基本信息配置
    
    spring:
      application:
        name: eureka-cluster
    
    eureka:
      server:
        # 测试时关闭自我保护机制,保证不可用服务及时踢出
        enable-self-preservation: false  # 关闭自我保护模式(缺省为打开)
      instance:
        #注册到eureka的ip地址
        hostname: eureka7001
      client:
        serviceUrl:
          #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址。
          defaultZone: http://eureka7002:7002/eureka/

     启动EurekaCluster7001服务

    package net.riking.springcloud.eureka.cluster;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
    
    @EnableEurekaServer//开启对EurekaServer的支持,即:作为Eureka服务端
    @SpringBootApplication
    public class AppEurekaCluster7001 {
    
        public static void main(String[] args) {
            SpringApplication.run(AppEurekaCluster7001.class, args);
        }
    
    }

    服务EurekaCluster7002的application.yml配置

    #服务端口号
    server:
      port: 7002
    
    #eureka 基本信息配置
    eureka:
      server:
        # 测试时关闭自我保护机制,保证不可用服务及时踢出
        enable-self-preservation: false  # 关闭自我保护模式(缺省为打开)
      instance:
        #注册到eureka的ip地址
        hostname: eureka7002
      client:
        serviceUrl:
          #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址。
          defaultZone: http://eureka7001:7001/eureka/
    

    启动EurekaCluster7002服务

    package net.riking.springcloud.eureka.cluster;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
    
    @EnableEurekaServer//开启对EurekaServer的支持,即:作为Eureka服务端
    @SpringBootApplication
    public class AppEurekaCluster7002 {
    
        public static void main(String[] args) {
            SpringApplication.run(AppEurekaCluster7002.class, args);
        }
    
    }

    启动工程后,访问:http://eureka7001:7001/,可以看到下面的页面,

    同理访问:http://eureka7002:7002/,可以看到下面的页面

    从上面两张图可以看出,DS Replicas、registered-replicas、available-replicas分别有了对方的地址,即:相互Replicate、相互注册,则说明Eureka集群成功。

    Eureka Client

    application.yml配置

    #服务启动端口号
    server:
      port: 8001
    
    #服务名称(服务注册到eureka名称)
    spring:
      application:
        name: provider
    
    #客户端注册进eureka服务列表内
    eureka:
      client:
        service-url:
          defaultZone: http://eureka7001:7001/eureka,http://eureka7002:7002/eureka
          #defaultZone: http://eureka7001:7001/eureka

    启动provider服务

    package net.riking.springcloud.provider;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    
    @SpringBootApplication
    @EnableEurekaClient  //开启对EurekaClient的支持,即:作为Eureka客户端,高版本可省略
    public class AppProvider {
        public static void main(String[] args) {
            SpringApplication.run(AppProvider.class, args);
        }
    
    }

     启动工程后,访问:http://eureka7001:7001/,可以看到下面的页面,

     

    Eureka高可用验证


    停掉EurekaCluster7002服务作为模拟宕机,访问http://eureka7002:7002/显示无法访问,http://eureka7001:7001/正常访问,http://localhost:8001/user/provider?username=Eureka集群  正常访问,  EurekaCluster7001节点信息如下:

     

    PROVIDER访问信息如下:

  • 相关阅读:
    js实现左侧弹出效果
    [z]重建索引
    Query to find the eligible indexes for rebuilding
    查询oracle比较慢的session和sql
    [z]根据awr报告查看最慢的sql语句
    有关Oracle统计信息的知识点[z]
    [z]表空间对应文件的AUTOEXTEND ON NEXT指定的值对性能的影响
    [z]dbms_stats.lock_table_stats对于没有统计信息的表分区同样有效
    统计sql
    SQL truncate 、delete与drop区别[z]
  • 原文地址:https://www.cnblogs.com/kongliuyi/p/11345148.html
Copyright © 2011-2022 走看看