一.构建三个节点的Eureka Server集群
-
application.yml配置文件
---
server:
port: 8771
spring:
profiles: server1
application:
name: eurekaCluster
eureka:
instance:
hostname: server1
client:
service-url:
defaultZone: http://server2:8772/eureka/,http://server3:8773/eureka/
fetch-registry: true
---
server:
port: 8772
spring:
profiles: server2
application:
name: eurekaCluster
eureka:
instance:
hostname: server2
client:
service-url:
defaultZone: http://server1:8771/eureka/,http://server3:8773/eureka/
fetch-registry: true
---
server:
port: 8773
spring:
profiles: server3
application:
name: eurekaCluster
eureka:
instance:
hostname: server3
client:
service-url:
defaultZone: http://server1:8771/eureka/,http://server2:8772/eureka/
fetch-registry: true
-
pom文件
<?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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>eureka-server-cluster</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-server-cluster</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<!--提供springMvc支持-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--eureka-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<version>1.3.4.RELEASE</version>
</dependency>
</dependencies>
<!--引入spring cloud依赖-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
-
配置hosts文件
以mac为例子,编辑 hosts文件,分别 配置 server1,server2,server3 模拟三个服务器
vim sudo /etc/hosts
hosts配置
配置完后可以输入命令测试
ping server1
分别以三种配置启动三个启动项
键入命令
java -jar eureka-server-cluster-0.0.1-SNAPSHOT.jar --spring.profiles.active=server1
java -jar eureka-server-cluster-0.0.1-SNAPSHOT.jar --spring.profiles.active=server2
java -jar eureka-server-cluster-0.0.1-SNAPSHOT.jar --spring.profiles.active=server3
如果是IDEA可以直接配置启动参数
image.png
启动前两个注册中心时会报Cannot execute request on any known server的错误,但是当三个启动项全都启动起来以后,报错就会消失
image.png
二.注册到Eureka Server集群上
-
application.yml
server:
port: 9092
spring:
application:
name: eureka-client
eureka:
instance:
hostname: localhost
# 使用IP注册
preferIpAddress: true
# 心跳间隔
lease-renewal-interval-in-seconds: 3
# 服务失效时间: 如果多久没有收到请求,则可以删除服务
lease-expiration-duration-in-seconds: 7
client:
# 关闭eureka client
# enabled: false
# 注册自身到eureka服务器
register-with-eureka: true
# 表示是否从eureka服务器获取注册信息
fetch-registry: false
# 客户端从Eureka Server集群里更新Eureka Server信息的频率
eureka-service-url-poll-interval-seconds: 60
# 定义从注册中心获取注册服务的信息
registry-fetch-interval-seconds: 5
# 设置eureka服务器所在的地址,查询服务和注册服务都需要依赖这个地址
service-url:
# defaultZone: http://${eureka.instance.hostname}:8761/eureka/
defaultZone: http://server1:8771/eureka/,http://server2:8772/eureka/,http://server3:8773/eureka/
-
启动类
package com.basil;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class, args);
}
}
-
pom文件
<?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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>eureka-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-client</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<!--提供springMvc支持-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--eureka-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<version>1.3.4.RELEASE</version>
</dependency>
<!--<dependency>-->
<!--<groupId>org.springframework.cloud</groupId>-->
<!--<artifactId>spring-cloud-starter</artifactId>-->
<!--</dependency>-->
</dependencies>
<!--引入spring cloud依赖-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
成功启动后
image.png