03. 微服务 - 集群搭建 - Eureka(二)

打印 上一主题 下一主题

主题 956|帖子 956|积分 2868

媒介

   基于上一篇文章搭建Eureka集群服务
实现3个eureka server节点数据同步
记录搭建过程中的的问题及疑问
  Spring Cloud Eureka利用手册
Eureka Wiki
Eureka集群架构




  • 紧接上篇的demo项目,在其中新建dingsu-eureka-cluster模块
  • 目标是上图中,3个eureka Server之间的数据同步
  • 项目结构如下图

预备工作



  • 因为是在本机模拟三个eureka Server,修改C:\Windows\System32\drivers\etc\hosts,增加3个域名对应到本地IP(复制到其他路径,右键属性取消只读,修改后复制归去覆盖)
  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. # localhost name resolution is handled within DNS itself.
  19. #        127.0.0.1       localhost
  20. #        ::1             localhost
  21. 127.0.0.1       activate.navicat.com
  22. 127.0.0.1                eureka-server-1
  23. 127.0.0.1                eureka-server-2
  24. 127.0.0.1                eureka-server-3
复制代码
父级模块dingsu-eureka-cluster



  • pom.xml
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5.     <modelVersion>4.0.0</modelVersion>
  6.     <parent>
  7.         <groupId>com.hd</groupId>
  8.         <artifactId>confucius</artifactId>
  9.         <version>0.0.1</version>
  10.     </parent>
  11.     <artifactId>dingsu-eureka-cluster</artifactId>
  12.     <packaging>pom</packaging>
  13.     <modules>
  14.         <module>eureka-server-1</module>
  15.         <module>eureka-server-2</module>
  16.         <module>eureka-server-3</module>
  17.     </modules>
  18.     <properties>
  19.         <maven.compiler.source>8</maven.compiler.source>
  20.         <maven.compiler.target>8</maven.compiler.target>
  21.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  22.     </properties>
  23.     <dependencies>
  24.         <dependency>
  25.             <groupId>org.springframework.cloud</groupId>
  26.             <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  27.         </dependency>
  28.     </dependencies>
  29. </project>
复制代码
eureka Server 1设置



  • 设置eureka.client.service-url.defaultZone 注册到2和3
  • application.yml
  1. spring:
  2.   application:
  3.     name: confucius-eureka-server # 注册到Eureka的实例名称,Application列
  4. server:
  5.   port: 4311
  6. eureka:
  7.   server:
  8.     #关闭自我保护,默认为true
  9.     enable-self-preservation: false
  10.     # 清理间隔 ms,默认60*1000
  11.     eviction-interval-timer-in-ms: 30000
  12.   instance:
  13.     #注册实例名称,非ip注册时的副本名称
  14.     hostname: eureka-server-1
  15.     #是否将自己的ip注册到eureka中,默认false
  16.     prefer-ip-address: false
  17.   client:
  18.     #是否作为客户端,注册自己到eureka server中
  19.     register-with-eureka: true
  20.     # 是否作为客户端,拉取其他服务
  21.     fetch-registry: true
  22.     service-url:
  23.       defaultZone: http://eureka-server-2:4322/eureka,http://eureka-server-3:4333/eureka
  24. logging:
  25.   config: classpath:logback.xml
复制代码
eureka Server 2设置



  • 设置eureka.client.service-url.defaultZone 注册到1和3
  • application.yml
  1. spring:
  2.   application:
  3.     name: confucius-eureka-server # 注册到Eureka的实例名称,Application列
  4. server:
  5.   port: 4322
  6. eureka:
  7.   server:
  8.     #关闭自我保护,默认为true
  9.     enable-self-preservation: false
  10.     # 清理间隔 ms,默认60*1000
  11.     eviction-interval-timer-in-ms: 30000
  12.   instance:
  13.     #注册实例名称,非ip注册时的副本名称
  14.     hostname: eureka-server-2
  15.     #是否将自己的ip注册到eureka中,默认false
  16.     prefer-ip-address: false
  17.   client:
  18.     #是否作为客户端,注册自己到eureka server中
  19.     register-with-eureka: true
  20.     # 是否作为客户端,拉取其他服务
  21.     fetch-registry: true
  22.     service-url:
  23.       defaultZone: http://eureka-server-1:4311/eureka,http://eureka-server-3:4333/eureka
  24. logging:
  25.   config: classpath:logback.xml
复制代码
eureka Server 3设置



  • 设置eureka.client.service-url.defaultZone 注册到1和2
  • application.yml
  1. spring:
  2.   application:
  3.     name: confucius-eureka-server # 注册到Eureka的实例名称,Application列
  4. server:
  5.   port: 4333
  6. eureka:
  7.   server:
  8.     #关闭自我保护,默认为true
  9.     enable-self-preservation: true
  10.     # 清理间隔 ms,默认60*1000
  11.     eviction-interval-timer-in-ms: 30000
  12.   instance:
  13.     #注册实例名称,非ip注册时的副本名称
  14.     hostname: eureka-server-3
  15.     #是否将自己的ip注册到eureka中,默认false
  16.     prefer-ip-address: false
  17.   client:
  18.     #是否作为客户端,注册自己到eureka server中
  19.     register-with-eureka: true
  20.     # 是否作为客户端,拉取其他服务
  21.     fetch-registry: true
  22.     service-url:
  23.       defaultZone: http://eureka-server-1:4311/eureka,http://eureka-server-2:4322/eureka
  24. logging:
  25.   config: classpath:logback.xml
复制代码
运行状态


无效副本(unavailable-replicas)问题



  • 自己搭建时,基本都会自界说应用名称、域名、实例名称等,大概率会出现无效副本(unavailable replicas)
  • 如果出现该问题,排查以下几个方面:

    • defaultZone 值中的域名是否一致,因为单机演示时只有一个IP,所以我们注册服务时,instance.prefer-ip-address设置为了false,如果我们全都利用localhost去向其他服务注册,则会出现DS replicas副本同名问题。
    • client.register-with-eureka、fetch-registry 都为 true
    • 三个服务的spring.application.name必要一致,否则被以为是三个独立的服务,无法通过同一个服务名,压根实现不了负载平衡

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

勿忘初心做自己

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表