说明:
单个搭建:https://blog.csdn.net/a526001650a/article/details/106859559
一、导入SpringCloud与SpringBoot依赖包(必须版本对应,此处为2.2.x <-> Hoxton):
1.导入SpringBoot依赖包:
<project>
<!-- 导入spring-boot的2.2.x版本,对应spring-cloud的Hoxton版本 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.8.RELEASE</version>
</parent>
...
</project>
2.定死SpringCloud依赖包:
<project>
...
<!-- 定死spring-cloud为Hoxton版本 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
二、Eureka的Server端集群配置(此处搭建3个服务端):
1.导入Eureka的Server端依赖包:
<!-- 导入Eureka的Server端依赖包 -->
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
2.三个Eureka的Server端工程/application.yml集群信息配置:
(1)eurekaserver01中的application.yml:
server: #配置此Web微服务端口号
port: 10001
eureka:
instance:
hostname: eurekaserver01
client:
register-with-eureka: false #是否将自已注册到Eureka服务中(默认,不用注册)
fetch-registry: false #配置是否从Eureka中获取注册信息
service-url: #配置其他Eureka服务器的URL,每个地址以逗号隔开,建立集群
defaultZone: http://127.0.0.1:10002/eureka/,http://127.0.0.1:10003/eureka/
(2)eurekaserver02中的application.yml:
server: #配置此Web微服务端口号
port: 10002
eureka:
instance:
hostname: eurekaserver02
client:
register-with-eureka: false #是否将自已注册到Eureka服务中(默认,不用注册)
fetch-registry: false #配置是否从Eureka中获取注册信息
service-url: #配置其他Eureka服务器的URL,每个地址以逗号隔开,建立集群
defaultZone: http://127.0.0.1:10001/eureka/,http://127.0.0.1:10003/eureka/
(3)eurekaserver03中的application.yml:
server: #配置此Web微服务端口号
port: 10003
eureka:
instance:
hostname: eurekaserver03
client:
register-with-eureka: false #是否将自已注册到Eureka服务中(默认,不用注册)
fetch-registry: false #配置是否从Eureka中获取注册信息
service-url: #配置其他Eureka服务器的URL,每个地址以逗号隔开,建立集群
defaultZone: http://127.0.0.1:10001/eureka/,http://127.0.0.1:10002/eureka/
3.三个Eureka的Server端启动类:
(1)eurekaserver01中的Application类:
@SpringBootApplication //配置启动类
@EnableEurekaServer //配置Eureka服务端启动类
public class EurekaServer01Application {
public static void main(String[] args) {
SpringApplication.run(EurekaServer01Application.class); //加载启动类
}
}
(2)eurekaserver02中的Application类:
@SpringBootApplication //配置启动类
@EnableEurekaServer //配置Eureka服务端启动类
public class EurekaServer02Application {
public static void main(String[] args) {
SpringApplication.run(EurekaServer02Application.class); //加载启动类
}
}
(3)eurekaserver03中的Application类:
@SpringBootApplication //配置启动类
@EnableEurekaServer //配置Eureka服务端启动类
public class EurekaServer03Application {
public static void main(String[] args) {
SpringApplication.run(EurekaServer03Application.class); //加载启动类
}
}
三、Eureka的Client端搭建:
1.导入Eureka的Client端依赖包:
<dependencies>
<!-- 导入Eureka的Client端依赖包 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
2.application.yml配置Eureka的Client端信息和Server端集群地址:
server: #配置此Web微服务端口号
port: 10000
eureka:
client:
service-url: #导入Eureka Server配置的集群地址,每个地址用逗号隔开
defaultZone: http://127.0.0.1:10001/eureka/, http://127.0.0.1:10002/eureka/, http://127.0.0.1:10003/eureka/
instance: #配置支持跨域访问
prefer-ip-adress: true
3.编写Eureka的Client端启动类:
@SpringBootApplication //配置启动类
@EnableEurekaClient //配置Eureka客户端
public class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class); //加载启动类
}
}
4.查看效果:
http://127.0.0.1:10001/
http://127.0.0.1:10002/
http://127.0.0.1:10003/