Spring-Cloud-Eureka 高可用
1 有三台安装好jdk8的linux服务器,分别是 192.168.230.180,192.168.230.181,192.168.230.182
2 建立三个spring-boot项目,分别命名为 eureka1,eureka2,eureka3
3 springboot使用1.5.4版本,如下
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/>
</parent>
4 spring cloud使用Dalston.SR4 版本,如下
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
5 三个项目,每个项目的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.0http://maven.apache.org/xsd/maven-4.0.0.xsd
">
<modelVersion>4.0.0</modelVersion>
<groupId>spring-cloud-eureka</groupId>
<artifactId>spring-cloud-eureka</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/>
</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-eureka-server</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR4</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>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>
<useIncrementalCompilation>true</useIncrementalCompilation>
</configuration>
</plugin>
</plugins>
</build>
</project>
6 建立三个启动类,每个启动类的内容如下:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.
SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
7 给每个项目建立一个配置文件 application.properties,部署到180,181,182上面的三个项目的三个配置文件分别如下
180:
spring.application.name=eureka-server
server.port=8761
eureka.instance.prefer-ip-address=true
eureka.instance.hostname=192.168.230.180
eureka.client.serviceUrl.defaultZone=
http://192.168.230.181:8761/eureka/,http://192.168.230.182:8761/eureka/
81:
181:
spring.application.name=eureka-server
server.port=8761
eureka.instance.prefer-ip-address=true
eureka.instance.hostname=192.168.230.181
eureka.client.serviceUrl.defaultZone=
http://192.168.230.180:8761/eureka/,http://192.168.230.182:8761/eureka/
182:
spring.application.name=eureka-server
server.port=8761
eureka.instance.prefer-ip-address=true
eureka.instance.hostname=192.168.230.182
eureka.client.serviceUrl.defaultZone=
http://192.168.230.180:8761/eureka/,http://192.168.230.181:8761/eureka/
8 分别给每个项目打成jar包,然后分别上传到三个服务器上面,分别用 java -jar xxx.jar & 命令启动
9 启动后,分别访问如下三个连接
http://192.168.230.180:8761/http://192.168.230.181:8761/http://192.168.230.182:8761/
可以看到,
1)三个页面中 每个页面 DS Replicas 下面列出了其它两个ip
2)Instances currently registered with Eureka 下面列出了EUREKA-SERVER服务,显示有三个,状态栏分别列出了这三个
3) 下面的General Info中,registered-replicas行和available-replicas行都分别列出了其它两个的地址
符合上面三个条件,说明spring cloud eureka高可用注册中心搭建成功
如果在General Info中,unavailable-replicas行出现了其它注册中心地址,说明搭建失败
10注意:
在配置文件中,默认不能使用ip来分别表示各个服务器,需要加上eureka.instance.prefer-ip-address=true配置,但是在同一个电脑上搭建伪集群,建议不适用ip,分别给ip起别名,使用ip会失败