參考書籍:《Spring Cloud微服務(wù)實戰(zhàn)》
服務(wù)端配置
總節(jié)點
server.port=4444
spring.application.name = peer
eureka.client.serviceUrl.defaultZone = http://peer2:1112/eureka/,http://peer1:1111/eureka/
節(jié)點一
spring.application.name=peer
spring.profiles.active=peer1
server.port=1111
eureka.instance.hostname=peer1
#eureka.client.register-with-eureka=true 默認(rèn)為true
#eureka.client.fetch-registry = true
eureka.client.serviceUrl.defaultZone=http://peer2:1112/eureka/
節(jié)點二
spring.application.name=peer
spring.profiles.active=peer2
server.port=1112
eureka.instance.hostname=peer2
#eureka.client.register-with-eureka=true
#eureka.client.fetch-registry = true
eureka.client.serviceUrl.defaultZone=http://peer1:1111/eureka/
配置host文件:
127.0.0.1 peer1
127.0.0.1 peer2
打包jar
進(jìn)入jar路徑啟動命令
java -jar learn-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer1
java -jar learn-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer2
java -jar learn-0.0.1-SNAPSHOT.jar
- 訪問
http://localhost:1112/
http://localhost:4444/
pom
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.5.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--spring cloub-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
- 最后注意問題:出現(xiàn)available-replicas為空
原因:節(jié)點一與節(jié)點二下面的服務(wù)中心的配置名稱保持一致,微服務(wù)名稱大于服務(wù)
spring.application.name=peer