首先,我们将两台eureka服务器集群在了一起,具体这样做的好处是:
1.服务注册点的可靠性会提高,不会因为一台服务器崩溃了而导致后边的服务消费者们找不到服务点。
2.多台eureka服务器集群之后就会出现服务共享的好处,这样的话,客户端的服务消费者只需要在注册时申明一个注册点即可,然后注册的服务在eureka1上显示之后,同样可以在相关联的集群中的其他机子上边出现
具体操作:
yml(peer1)文件配置:
这里面的peer1和peer2分别是在host文件里面进行过修改的localhost
server:
port: 8082
eureka:
instance:
# 注意这里面的hostlocal是指本台主机的名称**
hostname:** peer1
*# 只发送一次心跳包就注册相应的服务
# lease-expiration-duration-in-seconds: 1
***client:
***# 禁止自动注册
register-with-eureka: false
*service-url:
defaultZone: http://peer2:8083/eureka
**spring:
application:
***# 服务注册中心点一
*name: eureka-server-center-A
yml(peer2)文件配置:
server:
port: 8083
eureka:
instance:
hostname: peer2
*# 只发送一次心跳包就注册相应的服务
# lease-expiration-duration-in-seconds: 1
***client:
***# 禁止自动注册
register-with-eureka: false
*service-url:
defaultZone: http://peer1:8082/eureka
**spring:
application:
***# 服务注册中心点一
*name: eureka-server-center-A
注册完毕之后,变回形成一个回链状的eureka集群;
同理,两台集群和三台集群以及N台集群的做法都是类似的,只需要连城一个回链即可
*主入口:
*@SpringBootApplication
@EnableEurekaServer
public class EurekaServerA {
public static void main(String[] args) {
SpringApplication.run(EurekaServerA.class,args);
}
}
pom:
子pom:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
父pom:
<project xmlns="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.sise</groupId>
<artifactId>xm</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>mall-common</module>
<module>mall-service-api</module>
<module>mall-common-config-server</module>
<module>mall-eureka-server</module>
<module>mall-service-feign</module>
<module>mall-zuul</module>
</modules>
<packaging>pom</packaging>
<distributionManagement>
<site>
<id>website</id>
<url>scp://webhost.company.com/www/website</url>
</site>
</distributionManagement>
*<!--如果有这个父类的级别配置的话,我们在后期引入jar的时候不需要自己设定相应的jar版本号-->
*<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.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-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</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>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.6</version>
<scope>provided</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.RC1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
*<!--如果需要将项目打包成一个jar包的话,需要添加这个配置-->
*<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>repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
将相应的服务打包好之后:执行命令即可:
java -jar cc.jar --spring.profiles.active=peer1
java -jar cc.jar --spring.profiles.active=peer2
\
http://peer1:8082/
http://peer2:8083/