050_Eureka 集群环境配置
目录
复制创建Eureka服务端子模块 springcloud-eureka-7002
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">
<parent>
<artifactId>springcloud</artifactId>
<groupId>com.qing</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springcloud-eureka-7002</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!--服务提供者使用eureka,eureka服务端使用eureka-server-->
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<!--热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
</project>
配置文件 application.yml
注:只有端口和7001不同,为7002
server:
port: 7002
# Eureka配置
eureka:
instance:
hostname: localhost # Eureka服务端的实例名称
client:
register-with-eureka: false # 表示是否向eureka注册中心注册自己,这是eureka服务端不需要注册,其他服务需要注册为true
fetch-registry: false # 如果为false,表示这是注册中心,其他服务需要为true
service-url: # 注册url,监控页面是:http://${eureka.instance.hostname}:${server.port}
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
启动类,添加开启Eureka服务端注解
package com.qing.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer // 开启Eureka服务端
public class EurekaServer_7002 {
public static void main(String[] args) {
SpringApplication.run(EurekaServer_7002.class, args);
}
}
启动测试
复制创建Eureka服务端子模块 springcloud-eureka-7003
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">
<parent>
<artifactId>springcloud</artifactId>
<groupId>com.qing</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springcloud-eureka-7003</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!--服务提供者使用eureka,eureka服务端使用eureka-server-->
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<!--热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
</project>
配置文件 application.yml
注:只有端口和7001不同,为7002
server:
port: 7003
# Eureka配置
eureka:
instance:
hostname: localhost # Eureka服务端的实例名称
client:
register-with-eureka: false # 表示是否向eureka注册中心注册自己,这是eureka服务端不需要注册,其他服务需要注册为true
fetch-registry: false # 如果为false,表示这是注册中心,其他服务需要为true
service-url: # 注册url,监控页面是:http://${eureka.instance.hostname}:${server.port}
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
启动类,添加开启Eureka服务端注解
package com.qing.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer // 开启Eureka服务端
public class EurekaServer_7003 {
public static void main(String[] args) {
SpringApplication.run(EurekaServer_7003.class, args);
}
}
启动测试
模拟Eureka服务端部署在多个服务器,修改hosts文件
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
127.0.0.1 eureka7001.com
127.0.0.1 eureka7002.com
127.0.0.1 eureka7003.com
Eureka集群配置
springcloud-eureka-7001
application.yml 配置集群关联其他eureka服务端
server:
port: 7001
# Eureka配置
eureka:
instance:
hostname: eureka7001.com # Eureka服务端的实例名称
client:
register-with-eureka: false # 表示是否向eureka注册中心注册自己,这是eureka服务端不需要注册,其他服务需要注册为true
fetch-registry: false # 如果为false,表示这是注册中心,其他服务需要为true
service-url: # 注册url,监控页面是:http://${eureka.instance.hostname}:${server.port}
# 单机:defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
# 集群(关联):
defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
springcloud-eureka-7002
application.yml 配置集群关联其他eureka服务端
server:
port: 7002
# Eureka配置
eureka:
instance:
hostname: eureka7002.com # Eureka服务端的实例名称
client:
register-with-eureka: false # 表示是否向eureka注册中心注册自己,这是eureka服务端不需要注册,其他服务需要注册为true
fetch-registry: false # 如果为false,表示这是注册中心,其他服务需要为true
service-url: # 注册url,监控页面是:http://${eureka.instance.hostname}:${server.port}
# 单机:defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
# 集群(关联):
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7003.com:7003/eureka/
springcloud-eureka-7003
application.yml 配置集群关联其他eureka服务端
server:
port: 7003
# Eureka配置
eureka:
instance:
hostname: eureka7003.com # Eureka服务端的实例名称
client:
register-with-eureka: false # 表示是否向eureka注册中心注册自己,这是eureka服务端不需要注册,其他服务需要注册为true
fetch-registry: false # 如果为false,表示这是注册中心,其他服务需要为true
service-url: # 注册url,监控页面是:http://${eureka.instance.hostname}:${server.port}
# 单机:defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
# 集群(关联):
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
服务提供者子模块注册到集群
springcloud-provider-dept-8001
application.yml 配置注册集群url
server:
port: 8001
mybatis:
type-aliases-package: com.qing.springcloud.pojo
config-location: classpath:mybatis/mybatis-config.xml
mapper-locations: classpath:mybatis/mapper/*.xml
# 开启驼峰
# configuration:
# map-underscore-to-camel-case: true
spring:
application:
name: springcloud-provider-dept
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: org.gjt.mm.mysql.Driver
url: jdbc:mysql://localhost:3306/db01?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: 123456
# Eureka
eureka:
client:
service-url:
# 单机:defaultZone: http://localhost:7001/eureka/ # 使用Eureka服务端配置的注册url
# 集群:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/ # 使用Eureka服务端配置的注册url
instance:
instance-id: springcloud-provider-dept-zdy # 修改Eureka监控页面上服务默认描述
# 监控信息 actuator-info配置
info:
app.name: qing-springcloud
company.name: 清风阁
集群启动测试:3个Eureka服务端和1个服务提供者
分类:
170_SpringCloud
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~