spring cloud配置Eureka集群注册中心
spring boot 2.3.5
spring cloud Hoxton.RELEASE
pom文件
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.5.RELEASE</version>
<relativePath/>
</parent>
<properties>
<spring-cloud.version>Hoxton.RELEASE</spring-cloud.version>
</properties>
<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>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring-boot.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
更改hosts文件,我们本机测试,模拟三台不同的url模拟集群
管理员打开保存即可
127.0.0.1 eureka9999.com
127.0.0.1 eureka9998.com
127.0.0.1 eureka9997.com
IDEA创建项目
创建一个父工程,三个model
Application.yml
三个模块基本一致,spring boot启动类和yml文件修改即可
启动类
@EnableEurekaServer
@SpringBootApplication
//启动类加上EnableEurekaServer注解即可
public class EurekaApplication
{
public static void main( String[] args )
{
SpringApplication.run(EurekaApplication.class, args);
}
}
server:
port: 9998
spring:
application:
name: base-eureka #注册中心名称
eureka:
server:
enable-self-preservation: true
instance:
hostname: eureka9998.com #本机设置好的hosts的映射
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
#集群注册地址,三个集群,比如当前是A,把A注册到B和C当中,多个用逗号隔开即可
defaultZone: http://eureka9999.com:9999/eureka/,http://eureka9997.com:9997/eureka/
启动项目测试
三个服务都启动
访问http://eureka9997.com:9997/
访问端口9997注册中心就是另外两个服务
至此spring cloud集群就可以了,对你有帮助点赞评论支持一下,有问题可以咨询QQ!