基于consul的服务注册
案例准备#
(1)复制一份新的工程进行配置#
拷贝一份新的工程,起名为 shop_consul_parent ,并导入相关的子模块
(2)修改微服务的相关pom文件#
修改每个微服务的pom文件,添加SpringCloud提供的基于Consul的依赖
<!--SpringCloud提供的基于Consul的服务发现--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency> <!--actuator用于心跳检查--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
其中 spring-cloud-starter-consul-discovery 是SpringCloud提供的对consul支持的相关依赖。
spring-boot-starter-actuator 适用于完成心跳检测响应的相关依赖。
配置服务注册#
修改每个微服务的application.yml配置文件,添加consul服务发现的相关配置信息
spring: ...省略 cloud: consul: #consul相关配置 host: 192.168.74.101 #ConsulServer请求地址 port: 8500 #ConsulServer端口 discovery: #是否注册 register: true #实例ID instance-id: ${spring.application.name}-1 #服务实例名称 service-name: ${spring.application.name} #服务实例端口 port: ${server.port} #健康检查路径 healthCheckPath: /actuator/health #健康检查时间间隔 healthCheckInterval: 15s #开启ip地址注册 prefer-ip-address: true #实例的请求ip ip-address: ${spring.cloud.client.ip-address}
其中 spring.cloud.consul 中添加consul的相关配置:
host:表示Consul的Server的请求地址
port:表示Consul的Server的端口
discovery:服务注册与发现的相关配置
instance-id : 实例的唯一id(推荐必填),spring cloud官网文档的推荐,为了保证生成一个唯一的id ,也可以换成${spring.application.name}:${spring.cloud.client.ipAddress}
prefer-ip-address:开启ip地址注册
ip-address:当前微服务的请求ip
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)