媒介
基于上一篇文章搭建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(复制到其他路径,右键属性取消只读,修改后复制归去覆盖)
- # Copyright (c) 1993-2009 Microsoft Corp.
- #
- # This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
- #
- # This file contains the mappings of IP addresses to host names. Each
- # entry should be kept on an individual line. The IP address should
- # be placed in the first column followed by the corresponding host name.
- # The IP address and the host name should be separated by at least one
- # space.
- #
- # Additionally, comments (such as these) may be inserted on individual
- # lines or following the machine name denoted by a '#' symbol.
- #
- # For example:
- #
- # 102.54.94.97 rhino.acme.com # source server
- # 38.25.63.10 x.acme.com # x client host
- # localhost name resolution is handled within DNS itself.
- # 127.0.0.1 localhost
- # ::1 localhost
- 127.0.0.1 activate.navicat.com
- 127.0.0.1 eureka-server-1
- 127.0.0.1 eureka-server-2
- 127.0.0.1 eureka-server-3
复制代码 父级模块dingsu-eureka-cluster
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <parent>
- <groupId>com.hd</groupId>
- <artifactId>confucius</artifactId>
- <version>0.0.1</version>
- </parent>
- <artifactId>dingsu-eureka-cluster</artifactId>
- <packaging>pom</packaging>
- <modules>
- <module>eureka-server-1</module>
- <module>eureka-server-2</module>
- <module>eureka-server-3</module>
- </modules>
- <properties>
- <maven.compiler.source>8</maven.compiler.source>
- <maven.compiler.target>8</maven.compiler.target>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- </properties>
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
- </dependency>
- </dependencies>
- </project>
复制代码 eureka Server 1设置
- 设置eureka.client.service-url.defaultZone 注册到2和3
- application.yml
- spring:
- application:
- name: confucius-eureka-server # 注册到Eureka的实例名称,Application列
- server:
- port: 4311
- eureka:
- server:
- #关闭自我保护,默认为true
- enable-self-preservation: false
- # 清理间隔 ms,默认60*1000
- eviction-interval-timer-in-ms: 30000
- instance:
- #注册实例名称,非ip注册时的副本名称
- hostname: eureka-server-1
- #是否将自己的ip注册到eureka中,默认false
- prefer-ip-address: false
- client:
- #是否作为客户端,注册自己到eureka server中
- register-with-eureka: true
- # 是否作为客户端,拉取其他服务
- fetch-registry: true
- service-url:
- defaultZone: http://eureka-server-2:4322/eureka,http://eureka-server-3:4333/eureka
- logging:
- config: classpath:logback.xml
复制代码 eureka Server 2设置
- 设置eureka.client.service-url.defaultZone 注册到1和3
- application.yml
- spring:
- application:
- name: confucius-eureka-server # 注册到Eureka的实例名称,Application列
- server:
- port: 4322
- eureka:
- server:
- #关闭自我保护,默认为true
- enable-self-preservation: false
- # 清理间隔 ms,默认60*1000
- eviction-interval-timer-in-ms: 30000
- instance:
- #注册实例名称,非ip注册时的副本名称
- hostname: eureka-server-2
- #是否将自己的ip注册到eureka中,默认false
- prefer-ip-address: false
- client:
- #是否作为客户端,注册自己到eureka server中
- register-with-eureka: true
- # 是否作为客户端,拉取其他服务
- fetch-registry: true
- service-url:
- defaultZone: http://eureka-server-1:4311/eureka,http://eureka-server-3:4333/eureka
- logging:
- config: classpath:logback.xml
复制代码 eureka Server 3设置
- 设置eureka.client.service-url.defaultZone 注册到1和2
- application.yml
- spring:
- application:
- name: confucius-eureka-server # 注册到Eureka的实例名称,Application列
- server:
- port: 4333
- eureka:
- server:
- #关闭自我保护,默认为true
- enable-self-preservation: true
- # 清理间隔 ms,默认60*1000
- eviction-interval-timer-in-ms: 30000
- instance:
- #注册实例名称,非ip注册时的副本名称
- hostname: eureka-server-3
- #是否将自己的ip注册到eureka中,默认false
- prefer-ip-address: false
- client:
- #是否作为客户端,注册自己到eureka server中
- register-with-eureka: true
- # 是否作为客户端,拉取其他服务
- fetch-registry: true
- service-url:
- defaultZone: http://eureka-server-1:4311/eureka,http://eureka-server-2:4322/eureka
- logging:
- 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必要一致,否则被以为是三个独立的服务,无法通过同一个服务名,压根实现不了负载平衡
|