zoukankan      html  css  js  c++  java
  • SpringCloud(五)Eureka Server高可用集群与常见问题

    Eureka Server高可用集群

    只使用单个Eureka Server风险太大,如果Eureka Server宕机,则服务就会瘫痪,所有我们应该搭建Eureka的高可用集群,EurekaServer之间相互注册,每个EurekaServer都注册服务,那么即使一台EurekaServer坏掉也不会导致系统崩溃

    实例:

    准备两台EurekaServer,需要相互进行注册

    1号Server 9000

    2号Server 8000

     #模拟两个EurekaServer
     #端口9000
     #端口8000
     #两个EurekaServer需要相互注册
     
     server:
      port: 9000
     #配置eureka server
     eureka:
      instance:
        hostname: localhost #主机地址名称
      client:
        register-with-eureka: true #是否将自己注册到注册中心
        fetch-registry: true #是否要从eureka中获取注册的信息
         #配置暴露给EurekaClient的请求地址
        service-url:
          defaultZone: http://${eureka.instance.hostname}:8000/eureka/
     #模拟两个EurekaServer
     #端口9000
     #端口8000
     #两个EurekaServer需要相互注册
     
     server:
      port: 9000
     #配置eureka server
     eureka:
      instance:
        hostname: localhost #主机地址名称
      client:
         #register-with-eureka: true #是否将自己注册到注册中心
         #fetch-registry: true #是否要从eureka中获取注册的信息
         #配置暴露给EurekaClient的请求地址
        service-url:
          defaultZone: http://${eureka.instance.hostname}:8000/eureka/
     spring:
      application:
        name: eureka-server

    2 将微服务注册到两个EurekaServer上

    在EurekaClient上注册多个EurekaServer

    服务提供者:

     server:
      port: 9001
     spring:
      application:
        name: service-product #服务名称
      datasource:
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3306/shops?useUnicode=true&characterEncoding=utf8
        username: root
        password: 123456
      jpa:
        database: mysql
        show-sql: true
        open-in-view: true
     #配置Eureka
     eureka:
      client:
        service-url:
          defaultZone: http://localhost:9000/eureka/,http://localhost:8000/eureka/ #多个EurekaServer之间用逗号分隔

    服务消费者:

     server:
      port: 9002
     spring:
      application:
        name: order-product #服务名称
      datasource:
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3306/shops?useUnicode=true&characterEncoding=utf8
        username: root
        password: 123456
      jpa:
        database: mysql
        show-sql: true
        open-in-view: true
     #配置Eureka
     eureka:
      client:
        service-url:
          defaultZone: http://localhost:9000/eureka/,http://localhost:8000/eureka/
      instance:
        prefer-ip-address: true #使用IP地址进行注册

    各EurekaServer之间有服务同步功能

    细节问题:

    1 在控制台显示服务IP

    在服务提供者,通过eureka.instance.instance-id配置控制台显示服务ip

     server:
      port: 9001
     spring:
      application:
        name: service-product #服务名称
      datasource:
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3306/shops?useUnicode=true&characterEncoding=utf8
        username: root
        password: 123456
      jpa:
        database: mysql
        show-sql: true
        open-in-view: true
     #配置Eureka
     eureka:
      client:
        service-url:
          defaultZone: http://localhost:9000/eureka/
      instance:
        prefer-ip-address: true #使用IP地址进行注册
        instance-id: ${spring.cloud.client.ip-address}:${server.port} #向注册中心注册服务ID
     

    2 Eureka的服务剔除问题

    Eureka的服务提供者默认每30s发送一次心跳机制,以告诉EureakServer目前自己存活,EureakServer如果90s没有心跳,则认为该提供者已死,但实际开发中90s显然太长,所有我们需要进行自定义配置。

    在服务提供者,设置心跳间隔,设置续约到期时间

     server:
      port: 9001
     spring:
      application:
        name: service-product #服务名称
      datasource:
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3306/shops?useUnicode=true&characterEncoding=utf8
        username: root
        password: 123456
      jpa:
        database: mysql
        show-sql: true
        open-in-view: true
     #配置Eureka
     eureka:
      client:
        service-url:
          defaultZone: http://localhost:9000/eureka/,http://localhost:8000/eureka/ #多个EurekaServer之间用逗号分隔
      instance:
        prefer-ip-address: true #使用IP地址进行注册
        instance-id: ${spring.cloud.client.ip-address}:${server.port} #向注册中心注册服务ID
        lease-renewal-interval-in-seconds: 10  #续约到期时间,默认90s(单位为s)

     

    Eureka自我保护机制

    EurekaServer统计所有的心跳比率在15min之内是否小于85%

    统计有多少服务心跳没有接受,如果大于85%,则开启自我保护机制,如果开启自我保护机制,服务就不会自动剔除了,一般开发中不需要自我保护机制

     eureka:
      instance:
        hostname: localhost #主机地址名称
      client:
        register-with-eureka: true #是否将自己注册到注册中心
        fetch-registry: true #是否要从eureka中获取注册的信息
         #配置暴露给EurekaClient的请求地址
        service-url:
          defaultZone: http://${eureka.instance.hostname}:9000/eureka/
      server:
        enable-self-preservation: false #关闭自我保护机制
        eviction-interval-timer-in-ms: 4000 #单位ms,剔除服务间隔,4s就会对不存在的服务删除,4s删除一次

    以上为自我开发时自定义配置,如果项目上线阶段最好保持默认值,因为Eureka包含许多算法,把默认值精确到了每一个业务,所以上线阶段不推荐修改!

     

  • 相关阅读:
    viewpager切换时底下的背景图标动画切换
    hdu 1594水题
    hdu 4256大水题
    hdu 1856并查集
    hdu4247水题
    hdu 4252单调栈
    hdu 4248排列问题
    hdu 1210
    hdu4245
    hdu 1593找规律题
  • 原文地址:https://www.cnblogs.com/qyx66/p/12237273.html
Copyright © 2011-2022 走看看