1 有三臺(tái)安裝好jdk8的linux服務(wù)器,分別是 192.168.230.180延曙,192.168.230.181,192.168.230.182
2 建立三個(gè)spring-boot項(xiàng)目,分別命名為 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 三個(gè)項(xiàng)目褥蚯,每個(gè)項(xiàng)目的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 建立三個(gè)啟動(dòng)類(lèi)挚冤,每個(gè)啟動(dòng)類(lèi)的內(nèi)容如下:
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 給每個(gè)項(xiàng)目建立一個(gè)配置文件 application.properties,部署到180,181,182上面的三個(gè)項(xiàng)目的三個(gè)配置文件分別如下
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 分別給每個(gè)項(xiàng)目打成jar包赞庶,然后分別上傳到三個(gè)服務(wù)器上面训挡,分別用 java -jar xxx.jar & 命令啟動(dòng)
9 啟動(dòng)后,分別訪(fǎng)問(wèn)如下三個(gè)連接
http://192.168.230.180:8761/http://192.168.230.181:8761/http://192.168.230.182:8761/
可以看到歧强,
1)三個(gè)頁(yè)面中 每個(gè)頁(yè)面 DS Replicas 下面列出了其它兩個(gè)ip
2)Instances currently registered with Eureka 下面列出了EUREKA-SERVER服務(wù)澜薄,顯示有三個(gè),狀態(tài)欄分別列出了這三個(gè)
3) 下面的General Info中摊册,registered-replicas行和available-replicas行都分別列出了其它兩個(gè)的地址
符合上面三個(gè)條件肤京,說(shuō)明spring cloud eureka高可用注冊(cè)中心搭建成功
如果在General Info中,unavailable-replicas行出現(xiàn)了其它注冊(cè)中心地址丧靡,說(shuō)明搭建失敗
10注意:
在配置文件中蟆沫,默認(rèn)不能使用ip來(lái)分別表示各個(gè)服務(wù)器,需要加上eureka.instance.prefer-ip-address=true配置温治,但是在同一個(gè)電腦上搭建偽集群饭庞,建議不適用ip,分別給ip起別名熬荆,使用ip會(huì)失敗