consul–基础–7.2–集成springBoot–yml常用配置
1、配置说明
spring:
application:
name: ${project.name}
profiles:
active: dev
cloud:
consul:
# 单机配置
# host: 10.220.147.130
# port: 8500
# 集群配置
host: 0.220.147.130:8500,10.220.147.131:8500
config:
# 开启配置中心
enabled: true
# watch选项为配置监视功能,主要监视配置的改变
watch:
#启用配置自动刷新
enabled: true
#刷新频率,单位:毫秒
delay: 10000
#刷新延迟时间,单位:秒
wait-time: 30
# 表示如果没有发现配置,是否抛出异常,true为是,false为否,当为false时,consul会打印warn级别的日志信息
fail-fast: false
# consul创建key值与环境的分隔符
profile-separator: s
# 配置文件的文件格式
format: yaml
# 配置文件的存储的根路径,默认为config
prefix: config
# 配置文件存储key的值,或者理解为文件的名称,默认为data
data-key: data
# 服务发现配置
discovery:
# 启用服务发现
enabled: true
# 启用服务注册
register: true
# 服务停止时取消注册
deregister: true
# 显示ip地址
prefer-ip-address: true
# 注册到consule的ip地址
ip-address: 118.118.118.118
# 健康检查频率,也就是心跳频率
health-check-interval: 5s
# 严重服务(监控检查不通过的服务)的检查间隔频率
health-check-critical-timeout: 1s
# 健康检查的路径
health-check-path: /actuator/health
# 服务注册标识(服务 ID),格式为:应用名称+服务器IP+端口,请勿使用随机数,因为它会在服务重启过程中,会在consul上生成多个实例
instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
# 注册的节点名称
service-name: consul-01
# 使用心跳检查push机制定期向consul server汇报自身存活情况,逾期没有汇报则server认为异常。
heartbeat:
enabled: true
#元数据和Consul标签
tags: version=1.0,author=bayuqi
2、案例
- 先让用户服务注册到consul
- 注册成功后,再停止用户服务
2.1、配置
server:
port: 9006
servlet:
context-path: /
spring:
application:
name: user-server
# config:
# import: "optional:consul:"
cloud:
consul:
host: 192.168.187.89
port: 8500
discovery:
prefer-ip-address: true
instance-id: ${spring.application.name}:${spring.application.instance_id:${spring.cloud.client.ip-address}-${server.port}}
# 健康检查的路径
health-check-path: '/service'
# 健康检查频率,也就是心跳频率
health-check-interval: 1s
# 严重服务(监控检查不通过的服务)的检查间隔频率
health-check-critical-timeout: 3s
# 健康检查超时时间
health-check-timeout: 2s
2.2、健康检查
@RequestMapping("/service")
public String service() throws UnknownHostException {
InetAddress localHost = InetAddress.getLocalHost();
String hostIP = localHost.getHostAddress();
long currentTime = System.currentTimeMillis();
long intervalTime = (currentTime - previousTime);//请求间隔时间
previousTime = currentTime;
String result = "用户服务:" + hostIP + ":" + serverPort + ",请求间隔时间:" + intervalTime;
System.out.println(result);
return result;
}
结果:
用户服务:2.0.0.1:9006,请求间隔时间:1006
用户服务:2.0.0.1:9006,请求间隔时间:1004
用户服务:2.0.0.1:9006,请求间隔时间:1007
用户服务:2.0.0.1:9006,请求间隔时间:1005
用户服务:2.0.0.1:9006,请求间隔时间:1004
用户服务:2.0.0.1:9006,请求间隔时间:1007
用户服务:2.0.0.1:9006,请求间隔时间:1022
用户服务:2.0.0.1:9006,请求间隔时间:1006
用户服务:2.0.0.1:9006,请求间隔时间:1006
用户服务:2.0.0.1:9006,请求间隔时间:1007
用户服务:2.0.0.1:9006,请求间隔时间:1004
用户服务:2.0.0.1:9006,请求间隔时间:1004
用户服务:2.0.0.1:9006,请求间隔时间:1003
2.3、consul 日志
# user-server服务注册
2024-08-20T10:15:11.134+0800 [WARN] agent.http: This request used the token query parameter which is deprecated and will be removed in a future Consul version: logUrl="/v1/catalog/services?wait=2s&token=<hidden>"
2024-08-20T10:15:11.134+0800 [WARN] agent.http: This request used the token query parameter which is deprecated and will be removed in a future Consul version: logUrl=/v1/agent/service/register?token=<hidden>
# 检查到注销服务时间间隔最小值:check=service:user-server-192-168-112-140-9006 最小值间隔值=1ms
2024-08-20T10:15:11.134+0800 [WARN] agent: check has deregister interval below minimum: check=service:user-server-192-168-112-140-9006 minimum_interval=1m0s
# 服务同步
2024-08-20T10:15:11.145+0800 [INFO] agent: Synced service: service=user-server-192-168-112-140-9006
2024-08-20T10:15:11.664+0800 [INFO] agent: Synced check: check=service:user-server-192-168-112-140-9006
2024-08-20T10:15:12.141+0800 [WARN] agent.http: This request used the token query parameter which is deprecated and will be removed in a future Consul version: logUrl="/v1/catalog/services?wait=2s&index=190&token=<hidden>"
2024-08-20T10:15:13.153+0800 [WARN] agent.http: This request used the token query parameter which is deprecated and will be removed in a future Consul version: logUrl="/v1/catalog/services?wait=2s&index=199&token=<hidden>"
# 检测到服务user-server-192-168-112-140-9006 当前状态是严重的,可以看到状态严重的检查是每隔3秒检查1次
2024-08-20T10:15:33.777+0800 [WARN] agent: Check is now critical: check=service:user-server-192-168-112-140-9006
2024-08-20T10:15:33.782+0800 [INFO] agent: Synced check: check=service:user-server-192-168-112-140-9006
2024-08-20T10:15:36.778+0800 [WARN] agent: Check is now critical: check=service:user-server-192-168-112-140-9006
2024-08-20T10:15:39.781+0800 [WARN] agent: Check is now critical: check=service:user-server-192-168-112-140-9006
2024-08-20T10:15:42.782+0800 [WARN] agent: Check is now critical: check=service:user-server-192-168-112-140-9006
2024-08-20T10:15:45.788+0800 [WARN] agent: Check is now critical: check=service:user-server-192-168-112-140-9006
2024-08-20T10:15:48.789+0800 [WARN] agent: Check is now critical: check=service:user-server-192-168-112-140-9006
2024-08-20T10:15:51.790+0800 [WARN] agent: Check is now critical: check=service:user-server-192-168-112-140-9006
2024-08-20T10:15:54.792+0800 [WARN] agent: Check is now critical: check=service:user-server-192-168-112-140-9006
2024-08-20T10:15:57.794+0800 [WARN] agent: Check is now critical: check=service:user-server-192-168-112-140-9006
2024-08-20T10:16:00.795+0800 [WARN] agent: Check is now critical: check=service:user-server-192-168-112-140-9006
2024-08-20T10:16:03.796+0800 [WARN] agent: Check is now critical: check=service:user-server-192-168-112-140-9006
2024-08-20T10:16:06.799+0800 [WARN] agent: Check is now critical: check=service:user-server-192-168-112-140-9006
2024-08-20T10:16:09.800+0800 [WARN] agent: Check is now critical: check=service:user-server-192-168-112-140-9006
2024-08-20T10:16:12.801+0800 [WARN] agent: Check is now critical: check=service:user-server-192-168-112-140-9006
2024-08-20T10:16:15.802+0800 [WARN] agent: Check is now critical: check=service:user-server-192-168-112-140-9006
2024-08-20T10:16:18.804+0800 [WARN] agent: Check is now critical: check=service:user-server-192-168-112-140-9006
2024-08-20T10:16:21.806+0800 [WARN] agent: Check is now critical: check=service:user-server-192-168-112-140-9006
2024-08-20T10:16:24.807+0800 [WARN] agent: Check is now critical: check=service:user-server-192-168-112-140-9006
2024-08-20T10:16:27.809+0800 [WARN] agent: Check is now critical: check=service:user-server-192-168-112-140-9006
2024-08-20T10:16:30.811+0800 [WARN] agent: Check is now critical: check=service:user-server-192-168-112-140-9006
2024-08-20T10:16:33.814+0800 [WARN] agent: Check is now critical: check=service:user-server-192-168-112-140-9006
2024-08-20T10:16:36.815+0800 [WARN] agent: Check is now critical: check=service:user-server-192-168-112-140-9006
2024-08-20T10:16:39.817+0800 [WARN] agent: Check is now critical: check=service:user-server-192-168-112-140-9006
2024-08-20T10:16:42.825+0800 [WARN] agent: Check is now critical: check=service:user-server-192-168-112-140-9006
2024-08-20T10:16:45.826+0800 [WARN] agent: Check is now critical: check=service:user-server-192-168-112-140-9006
2024-08-20T10:16:48.829+0800 [WARN] agent: Check is now critical: check=service:user-server-192-168-112-140-9006
2024-08-20T10:16:51.831+0800 [WARN] agent: Check is now critical: check=service:user-server-192-168-112-140-9006
2024-08-20T10:16:54.833+0800 [WARN] agent: Check is now critical: check=service:user-server-192-168-112-140-9006
2024-08-20T10:16:57.836+0800 [WARN] agent: Check is now critical: check=service:user-server-192-168-112-140-9006
2024-08-20T10:17:00.837+0800 [WARN] agent: Check is now critical: check=service:user-server-192-168-112-140-9006
# 准备注销服务
2024-08-20T10:16:58.858+0800 [INFO] agent: Deregistered service: service=user-server-192-168-112-140-9006
# 由于超过健康检查的"disregister_critical_service_after"超时,已注销具有严重健康状况的服务
2024-08-20T10:17:00.837+0800 [INFO] agent: deregistered service with critical health due to exceeding health check's 'deregister_critical_service_after' timeout: service=user-server-192-168-112-140-9006 check=service:user-server-192-168-112-140-9006 timeout=1m0s