springcloud eureka集群配置
环境配置
修改hosts文件,添加如下配置:
Windows: C:\Windows\System32\drivers\etc\hosts
Linux: /etc/hosts
127.0.0.1 eureka7001
127.0.0.1 eureka7002
127.0.0.1 eureka7003
创建项目
创建3个一样的项目,如下:
pom.xml
<?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.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.zy</groupId>
<artifactId>uereka7003</artifactId>
<version>1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.6</version>
<!-- <relativePath/>-->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2021.0.5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
启动类
@SpringBootApplication
@EnableEurekaServer //表示本类是EurekaServer服务端
public class EurekaServer_7003 {
public static void main(String[] args) {
SpringApplication.run(EurekaServer_7003.class, args);
}
}
SpringBoot配置文件
server:
port: 7003
# servlet:
# context-path: /eureka //1. 这个路径千万不能配置,否则副本会出现在unavailable-replicas中
#Eureka配置
eureka:
instance:
hostname: eureka7003
prefer-ip-address: false
instance-id: eureka7003 #2. 配置了这个,在status列会显示此配置value,否则会显示主机名:${spring.application.name}:${server.port}
client:
# register-with-eureka: false #3. 表示是否向eureka注册中心注册自己 #配置false,会被放入unavailable-replicas中
fetch-registry: false #如果为false,表示自己是注册中心
service-url:
defaultZone: http://eureka7001:7001/eureka,http://eureka7002:7002/eureka
spring:
application:
name: eureka-registration-center
上面代码中说明2配图展示如下:
最终界面展示
GitHub地址
待给~