eureka server集群
maven配置:
<dependencyManagement>
<dependencies>
<!--引入springcloud的版本-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.1.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
eureka server1配置:
server:
port: 8886
spring:
application:
name: eurakaServer1
eureka:
instance:
hostname: eurakaServer1-8886
client:
#register-with-eureka: false
#fetch-registry: false
serviceUrl:
defaultZone: http://localhost:8887/eureka/,http://localhost:8888/eureka/
eureka server2配置:
server:
port: 8887
spring:
application:
name: eurakaServer2
eureka:
instance:
hostname: eurakaServer2-8887
client:
#register-with-eureka: false
#fetch-registry: false
serviceUrl:
defaultZone: http://localhost:8888/eureka/,http://localhost:8886/eureka/
eureka server3配置:
server:
port: 8888
spring:
application:
name: eurakaServer3
eureka:
instance:
hostname: eurakaServer3-8888
client:
#register-with-eureka: false
#fetch-registry: false
serviceUrl:
defaultZone: http://localhost:8886/eureka/,http://localhost:8887/eureka/
集群之间的数据同步:
1、从集群其他节点创建eureka客户端,执行http://localhost:8887/eureka//apps/ 拉取全量服务列表注册到本地
2、Registering application EURAKASERVER3 with eureka with status UP 注册本身到集群其他几点
3、每隔30s发送心跳:http://localhost:8887/eureka//apps/EURAKASERVER3
4、每隔30s拉取增量服务列表:http://localhost:8887/eureka//apps/delta
eureka client
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
server:
port: 7776
spring:
application:
name: eurakaClient1
eureka:
instance:
prefer-ip-address: true # 使用服务的ip地址(下边截图有说明)
client:
register-with-eureka: true # 将自己注册到服务中心
fetch-registry: true # 检索服务
service-url:
defaultZone: http://127.0.0.1:8888/eureka # 服务中心地址,如果是集群环境,逗号分隔地址即可。
其实客户端可服务端启动注册,拉取服务步骤都是一样的
可以通过debug日志查看restful接口的调用:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="LOG_PATH" value="${user.dir}/../logs"/>
<property name="appName" value="eurekaClient1"/>
<contextName>${appName}</contextName>
<logger name="org.apache.zookeeper" level="ERROR" />
<appender name="INFO_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${LOG_PATH}/info.log</File>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<maxFileSize>10MB</maxFileSize>
<fileNamePattern>${LOG_PATH}/info-%d{yyyyMMdd}.log.%i</fileNamePattern>
<maxHistory>7</maxHistory>
<totalSizeCap>3GB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} -%msg%n</pattern>
</encoder>
</appender>
<appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level>
</filter>
<File>${LOG_PATH}/error.log</File>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${LOG_PATH}/error-%d{yyyyMMdd}.log.%i</fileNamePattern>
<maxFileSize>10MB</maxFileSize>
<maxHistory>7</maxHistory>
<totalSizeCap>3GB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} -%msg%n</pattern>
</encoder>
</appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>
[%level] %date{yyyy-MM-dd HH:mm:ss.SSS} %logger{96}:[%line] - %msg%n
</pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="INFO_FILE"/>
<appender-ref ref="STDOUT"/>
</root>
</configuration>
与nacos区别
1、nacos不止只有pull,而且当server有服务注册/下线,则会立即push给客户端,及时性比较高
2、在nacos集群同步,是通过异步批量同步的,其实从队列里面批量拿更新的服务,超过1000则分发给集群其他节点,如果有更新没有超过1000,则5s分发,跟eureka有着明显的区别
3、eureka server 对实例的注册,发现,是通过三级缓存实现的,nacos则是通过copyOnwrite的机制实现的,nacos及时性比eureka高