Eureka 注册中心搭建(集群)
- 导入依赖
导入依赖时注意下springcloud 和 springboot 的版本对应关系 可以在官网找到相关信息
假设选springcloud 的Hoxton SR3版本,
springboot 可使用2.2.5.RELEASE版本
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<!-- springcloud 和springboot版本 需对应-->
<version>Hoxton.SR3</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>
</plugins>
</build>
- 配置文件
server:
port: 8000
spring:
application:
name: spring-cloud-eureka
eureka:
client:
register-with-eureka: false # the default is true
fetch-registry: false # This client gets the registration information on the eureka server registry, the default is true
#注册中心路径,如果有多个eureka server,在这里需要配置其他eureka server的地址,
#用","进行区分,如"http://address:8888/eureka,http://address:8887/eureka"
service-url:
defaultZone: http://localhost:8001/eureka/,http://localhost:8002/eureka/
instance:
# ip-address: 127.0.0.1
hostname: port1
----
server:
port: 8001
spring:
application:
name: spring-cloud-eureka
eureka:
client:
register-with-eureka: false # the default is true
fetch-registry: false # This client gets the registration information on the eureka server registry, the default is true
#注册中心路径,如果有多个eureka server,在这里需要配置其他eureka server的地址,
#用","进行区分,如"http://address:8888/eureka,http://address:8887/eureka"
service-url:
defaultZone: http://localhost:8000/eureka/,http://localhost:8002/eureka/
instance:
# ip-address: 127.0.0.1
hostname: port2
------
server:
port: 8002
spring:
application:
name: spring-cloud-eureka
eureka:
client:
register-with-eureka: false # the default is true
fetch-registry: false # This client gets the registration information on the eureka server registry, the default is true
#注册中心路径,如果有多个eureka server,在这里需要配置其他eureka server的地址,
#用","进行区分,如"http://address:8888/eureka,http://address:8887/eureka"
service-url:
defaultZone: http://localhost:8000/eureka/,http://localhost:8001/eureka/
instance:
# ip-address: 127.0.0.1
hostname: port3
需要在hosts文件配置 hostname和ip 的对应关系(实际请求会根据hosts文件中的配置进行域名解析,可以了解下hosts文件–>Linux 下hosts文件详解)
- 启动类
加上@EnableEurekaServer
- 启动
用 IDEA maven 打个包
windows 环境启动
D:\IdeaProject\springcloud-learn\springcloud-eureka\spring-cloud-eureka-cluster\target>java -jar spring-cloud-eureka-cluster-1.0-SNAPSHOT.jar --spring.profiles.active=po
rt1
// 启动三个terminal
java -jar spring-cloud-eureka-cluster-1.0-SNAPSHOT.jar --spring.profiles.active=po
rt1
java -jar spring-cloud-eureka-cluster-1.0-SNAPSHOT.jar --spring.profiles.active=po
rt2
java -jar spring-cloud-eureka-cluster-1.0-SNAPSHOT.jar --spring.profiles.active=po
rt3