consul 服務中心
使用的版本
- server 1.5.0
1 簡介
功能
- 服務註冊中心-服務提供者
- 服務配置中心-應用啓動前拉取配置
1.1 springcloud 的 demo 演示
1.2 consul 本地 docker 安裝
docker run -d --rm --name consul -p 8400:8400 -p 8500:8500 -p 8600:53/udp -h node1 progrium/consul -server -bootstrap
2. springcloud 註冊服務
2.1. 添加 maven 依賴
2.2. springboot 配置 application.yml 註冊服務
2.1. 添加 maven 依賴
<!--consul服務註冊功能-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-all</artifactId>
</dependency>
<!--使用config, 解析yml-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
2.2. springboot 配置 bootstrap.yml 註冊服務
spring:
application:
name: zuul-service
cloud:
consul:
host: 192.168.9.233
port: 8500
discovery: # consul 註冊中心
prefer-ip-address: true # ip 偏好; 這個必須配
service-name: zuul-service #註冊在consul上面的名字,在consul的調用中,是通過此名字調用的
register-health-check: true #健康檢查,保證服務處於啓動狀態,建議開啓
instance-id: ${spring.application.name}:${spring.cloud.client.ip-address} # 服務id
health-check-url: http://${spring.cloud.client.ip-address}:${server.port}/actuator/health # 健康檢查的URI
2.3 spring-cloud-starter-consul-discovery 的配置屬性詳解
版本: spring-cloud-consul-discovery-2.0.0.RELEASE
配置信息類: org.springframework.cloud.consul.discovery.ConsulDiscoveryProperties
入口方法: 可以從 idea 的 bootstrap.yml 點擊具體屬性進入,注意屬性類的繼承性
推薦技巧: 打開 idea 的 structure 面板查看
Spring Cloud Consul 的官方文檔
spring:
cloud:
consul:
host: 192.168.9.233 # ip
port: 8500 # 端口
discovery: # consul 註冊中心
service-name: zuul-service #註冊在consul上面的名字,在consul的調用中,是通過此名字調用的
register-health-check: true #健康檢查,保證服務處於啓動狀態,建議開啓
instance-id: ${spring.application.name}:${spring.cloud.client.ip-address} # 服務id
health-check-url: http://${spring.cloud.client.ip-address}:${server.port}/actuator/health # 健康檢查的URI
tags: # 標籤,這個屬性是個list,所以用yml的list的格式注入
- gateway
- common
- other
- firstboot
enabled: true # 服務發現是否啓動
management-tags: # 註冊管理服務時使用的標記,默認是management
- sss
# health-check-path: /health/check # 要調用以進行運行狀況檢查的備用服務器路徑,已經使用health-check-url覆蓋了,所以這屬性暫時找不到有什麼用處
health-check-interval: 10s # 字符串,執行運行狀況檢查的頻率(例如10秒),默認爲10秒。
health-check-timeout: 10s # 健康檢查超時(例如10秒)。
health-check-critical-timeout: 30s # 註銷關鍵服務的時間, 要求consul的版本在1.0.7或者更高
prefer-ip-address: true # ip 偏好; 這個必須配 在註冊過程中使用ip地址而不是主機名
prefer-agent-address: false # 來源,我們將如何確定使用的地址
# ip-address: 192.168.9.233 # 訪問服務時要使用的IP地址(還必須設置要使用的首選IP地址)
# hostname: # Hostname to use when accessing server
# port: 8500 # Port to register the service under (defaults to listening port)
# management-port: # Port to register the management service under (defaults to management port)
# catalog-services-watch-delay: 1000 # 查看consul的服務更新間隔,默認1s
# catalog-services-watch-timeout: 2 # 查看consul服務更新間隔的超時時間, 默認兩秒
query-passing: true # 這推動健康檢查 pass 傳遞到服務器。
deregister: false # 在consul禁用自動註銷登記服務。
fail-fast: true # *服務登記期間拋出異常如果這是真的,否則,日誌警告(默認值爲true)。
# health-check-tls-skip-verify: true # 跳過證書校驗, 如果不是則進行證書校驗
config: # consul 配置中心
prefix: config
enabled: true
format: YAML
data-key: settings.yml
3. 配置中心使用(在註冊服務的前提上)
3.1. springboot 配置文件配置 consul 的配置中心路徑和格式
3.2. 在 consul 的 key/value 的控制面板上添加和 springboot 配置的路徑的配置文件,以 yaml 格式
3.3. 把配置保存在對應的項目中,目錄路徑/用_線代替
3.1. springboot 配置文件配置 consul 的配置中心路徑和格式
spring:
application:
name: zuul-service
cloud:
consul:
host: 192.168.9.233
port: 8500
discovery: # consul 註冊中心
prefer-ip-address: true # ip 偏好; 這個必須配
service-name: zuul-service #註冊在consul上面的名字,在consul的調用中,是通過此名字調用的
register-health-check: true #健康檢查,保證服務處於啓動狀態,建議開啓
instance-id: ${spring.application.name}:${spring.cloud.client.ip-address} # 服務id
health-check-url: http://${spring.cloud.client.ip-address}:${server.port}/actuator/health # 健康檢查的URI
config: # consul 配置中心
prefix: config
enabled: true
format: YAML
data-key: settings.yml
3.3. 把配置保存在對應的項目中,目錄路徑/用_線代替
這是 settings.yml,在 consul 的路徑是 config/<service-name>,<profile>/settings.yml
zuul:
routes:
console:
path: /console/**
serviceId: console-service
strip-prefix: false
order:
path: /order/**
serviceId: order-service
strip-prefix: false
add-host-header: true
add-proxy-headers: true
env:
version: 1.0
author: suveng
4. springcloud 註銷服務
4.1. 去掉 maven 依賴
4.2. 刪除配置,application.yml 和 bootstarp.yml 的
4.3. 清除 consul 線上的失效服務
清除線上的失效服務
- 請求 consul 的接口刪除
# 刪除無效服務
http://<server_ip>:<port>/v1/agent/service/deregister/<service_id> put請求
# 刪除無效節點
http://<server_ip>:<port>/v1/agent/force-leave/<node_id>
- 在註冊的時候配置
Spring Cloud Consul 應用下線後,健康檢查自動刪除無效服務
consul 刪除不想要的服務