Eureka Server可通过运行多台实例并相互注册的方式来实现高可用,各实例是平等的,没有master和slave之分,不象zookeeper一样有选举机制。
本例用三台eureka server+一台eureka clien搭建高可用环境:
一、Eureka Server
1、修改host文件:
127.0.0.1 peer1 peer2 peer3
2、application.yml配置文件
spring:
application:
name: microservice-discovery-eureka-server
logging:
level:
com:
netflix:
eureka: on
discovery: on
---
spring:
# 指定profile=peer1
profiles: peer1
server:
port: 8761
eureka:
instance:
# 当指定profile=peer1时,主机名是peer1
hostname: peer1
client:
service-url:
defaultZone: http://peer2:8762/eureka,http://peer3:8763/eureka
server:
#关闭自我保护,从而会保证注销微服务
enable-self-preservation: false
# 清理时间间隔(单位毫秒,默认60*1000)
eviction-interval-timer-in-ms: 3000
---
spring:
# 指定profile=peer2
profiles: peer2
server:
port: 8762
eureka:
instance:
# 当指定profile=peer2时,主机名是peer2
hostname: peer2
client:
service-url:
defaultZone: http://peer1:8761/eureka,http://peer3:8763/eureka
server:
#关闭自我保护,从而会保证注销微服务
enable-self-preservation: false
# 清理时间间隔(单位毫秒,默认60*1000)
eviction-interval-timer-in-ms: 3000
---
spring:
# 指定profile=peer3
profiles: peer3
server:
port: 8763
eureka:
instance:
# 当指定profile=peer3时,主机名是peer3
hostname: peer3
client:
service-url:
defaultZone: http://peer1:8761/eureka,http://peer2:8762/eureka
server:
#关闭自我保护,从而会保证注销微服务
enable-self-preservation: false
# 清理时间间隔(单位毫秒,默认60*1000)
eviction-interval-timer-in-ms: 3000
# 说明:
# 1、使用连接字符串(---)将配置文件分为四段。第二、第三、第四段分别为spring.properties指定了一个值,该值表示它所在的那段内容应在哪个profile里。
# 第一段由于没有指定spring。profiles,因此这段内容会对所有profile生效。
# 2、因配置中使用了主机名,故需修改host文件(添加127.0.0.1 peer1 peer2 peer3)
3、启动类:
package com.ding.eureka.server;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
4、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>
<groupId>com.ding</groupId>
<artifactId>eureka-ha-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>eureka-ha-server</name>
<description>Eureka HA Server for Spring Cloud</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.M9</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</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>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
二、Eureka Client
1、application.yml配置文件
spring:
application:
name: a-bootiful-client
cloud:
client:
ipAddress: 192.168.0.50
server:
port: 8080
eureka:
instance:
# eureka client向eureka server发送心跳的时间间隔,默认30秒,将此值改小可解决eureka注册慢的问题
lease-renewal-interval-in-seconds: 5
# 续约到期时间(默认90秒)
lease-expiration-duration-in-seconds: 5
# 将instance id设置成IP:端口的形式
# 说明:通过试验,spring.cloud.client.ipAddress及server.port要配置文件中必须有值
instance-id: ${spring.cloud.client.ipAddress}:${server.port}
client:
service-url:
defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/,http://localhost:8763/eureka/
2、启动类:
package com.ding.eureka.client;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@EnableDiscoveryClient
@SpringBootApplication
public class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class, args);
}
}
@RestController
class ServiceInstanceRestController {
@Autowired
private DiscoveryClient discoveryClient;
@RequestMapping("/service-instances/{applicationName}")
public List<ServiceInstance> serviceInstancesByApplicationName(@PathVariable String applicationName) {
return this.discoveryClient.getInstances(applicationName);
}
@RequestMapping("/test")
public String test() {
return "hello";
}
}
3、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>
<groupId>com.ding</groupId>
<artifactId>eureka-ha-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>eureka-ha-client</name>
<description>Eureka HA Client for Spring Cloud</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<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>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.M9</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>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/libs-milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
三、测试步骤
1、打包Eureka Server 及Eureka Client
可使用该命令跳过junit测试:mvn clean package -Dmaven.test.skip=true
2、在jar包对应的目录下分别执行以下三个命令,以启动三个eureka-ha-server实例
java -jar eureka-ha-server-1-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer1
java -jar eureka-ha-server-1-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer2
java -jar eureka-ha-server-1-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer3
说明:通过--spring.profiles.active=peer3以指定使用哪个profile启动
3、启动eureka-ha-client
4、分别访问http://peer1:8761,http://peer2:8762,http://peer3:8763来查看