[英]how to deregister the service in consul when using spring cloud consul
我使用 consul( docker.io/bitnami/consul:1.10.2-debian-10-r0
) 作為我的注冊中心,首先我使用 gradle 添加 spring consul 依賴項( 2.2.7.RELEASE
):
implementation "org.springframework.cloud:spring-cloud-starter-consul-discovery"
然后在我項目的application.properties
中添加配置:
# registry center
spring.cloud.consul.host=consul-1630121482-headless.reddwarf-pro.svc.cluster.local
spring.cloud.consule.port=8500
spring.cloud.consul.enabled=true
spring.cloud.consul.discovery.health-check-interval=15s
spring.cloud.consul.discovery.instance-id= ${spring.application.name}:${vcap.application.instance_id:${spring.application.instance_id:${random.value}}}
spring.cloud.consul.discovery.prefer-ip-address= true
spring.cloud.consul.discovery.deregister=false
# service check failed, delete service
spring.cloud.consul.discovery.health-check-critical-timeout=10m
該寄存器可以注冊成功並且工作正常。 但是現在我面臨一個問題,即遺留服務沒有自動刪除。 我已經添加了注銷配置,為什么不起作用? 為什么該服務仍然沒有自動注銷? ` 這是我的 kubernetes yaml 配置:
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: consul-1630121482
namespace: reddwarf-pro
uid: 2c00fca9-6d80-42a2-9bb9-9f80e1ed3cbe
resourceVersion: '4946609'
generation: 4
creationTimestamp: '2021-08-28T03:31:27Z'
labels:
app.kubernetes.io/instance: consul-1630121482
app.kubernetes.io/managed-by: Helm
app.kubernetes.io/name: consul
helm.sh/chart: consul-9.3.5
annotations:
kubectl.kubernetes.io/last-applied-configuration: >
{"apiVersion":"apps/v1","kind":"StatefulSet","metadata":{"annotations":{"meta.helm.sh/release-name":"consul-1630121482","meta.helm.sh/release-namespace":"reddwarf-pro"},"creationTimestamp":"2021-08-28T03:31:27Z","generation":3,"labels":{"app.kubernetes.io/instance":"consul-1630121482","app.kubernetes.io/managed-by":"Helm","app.kubernetes.io/name":"consul","helm.sh/chart":"consul-9.3.5"},"managedFields":[{"apiVersion":"apps/v1","fieldsType":"FieldsV1","fieldsV1":{"f:metadata":{"f:annotations":{".":{},"f:meta.helm.sh/release-name":{},"f:meta.helm.sh/release-namespace":{}},"f:labels":{".":{},"f:app.kubernetes.io/instance":{},"f:app.kubernetes.io/managed-by":{},"f:app.kubernetes.io/name":{},"f:helm.sh/chart":{}}},"f:spec":{"f:podManagementPolicy":{},"f:replicas":{},"f:revisionHistoryLimit":{},"f:selector":{},"f:serviceName":{},"f:template":{"f:metadata":{"f:labels":{".":{},"f:app.kubernetes.io/instance":{},"f:app.kubernetes.io/managed-by":{},"f:app.kubernetes.io/name":{},"f:helm.sh/chart":{}}},"f:spec":{"f:affinity":{".":{},"f:podAntiAffinity":{".":{},"f:preferredDuringSchedulingIgnoredDuringExecution":{}}},"f:containers":{"k:{\"name\":\"consul\"}":{".":{},"f:env":{".":{},"k:{\"name\":\"BITNAMI_DEBUG\"}":{".":{},"f:name":{},"f:value":{}},"k:{\"name\":\"CONSUL_BOOTSTRAP_EXPECT\"}":{".":{},"f:name":{},"f:value":{}},"k:{\"name\":\"CONSUL_DATACENTER\"}":{".":{},"f:name":{},"f:value":{}},"k:{\"name\":\"CONSUL_DISABLE_KEYRING_FILE\"}":{".":{},"f:name":{},"f:value":{}},"k:{\"name\":\"CONSUL_DNS_PORT_NUMBER\"}":{".":{},"f:name":{}},"k:{\"name\":\"CONSUL_DOMAIN\"}":{".":{},"f:name":{},"f:value":{}},"k:{\"name\":\"CONSUL_HTTP_PORT_NUMBER\"}":{".":{},"f:name":{},"f:value":{}},"k:{\"name\":\"CONSUL_NODE_NAME\"}":{".":{},"f:name":{},"f:valueFrom":{".":{},"f:fieldRef":{".":{},"f:apiVersion":{},"f:fieldPath":{}}}},"k:{\"name\":\"CONSUL_RAFT_MULTIPLIER\"}":{".":{},"f:name":{},"f:value":{}},"k:{\"name\":\"CONSUL_RETRY_JOIN\"}":{".":{},"f:name":{},"f:value":{}},"k:{\"name\":\"CONSUL_RPC_PORT_NUMBER\"}":{".":{},"f:name":{}},"k:{\"name\":\"CONSUL_SERF_LAN_PORT_NUMBER\"}":{".":{},"f:name":{}},"k:{\"name\":\"CONSUL_UI\"}":{".":{},"f:name":{},"f:value":{}}},"f:image":{},"f:imagePullPolicy":{},"f:lifecycle":{".":{},"f:preStop":{".":{},"f:exec":{".":{},"f:command":{}}}},"f:livenessProbe":{".":{},"f:exec":{".":{},"f:command":{}},"f:failureThreshold":{},"f:initialDelaySeconds":{},"f:periodSeconds":{},"f:successThreshold":{},"f:timeoutSeconds":{}},"f:name":{},"f:ports":{".":{},"k:{\"containerPort\":8300,\"protocol\":\"TCP\"}":{".":{},"f:containerPort":{},"f:name":{},"f:protocol":{}},"k:{\"containerPort\":8301,\"protocol\":\"TCP\"}":{".":{},"f:containerPort":{},"f:name":{},"f:protocol":{}},"k:{\"containerPort\":8301,\"protocol\":\"UDP\"}":{".":{},"f:containerPort":{},"f:name":{},"f:protocol":{}},"k:{\"containerPort\":8400,\"protocol\":\"TCP\"}":{".":{},"f:containerPort":{},"f:name":{},"f:protocol":{}},"k:{\"containerPort\":8500,\"protocol\":\"TCP\"}":{".":{},"f:containerPort":{},"f:name":{},"f:protocol":{}},"k:{\"containerPort\":8600,\"protocol\":\"TCP\"}":{".":{},"f:containerPort":{},"f:name":{},"f:protocol":{}},"k:{\"containerPort\":8600,\"protocol\":\"UDP\"}":{".":{},"f:containerPort":{},"f:name":{},"f:protocol":{}}},"f:readinessProbe":{".":{},"f:exec":{".":{},"f:command":{}},"f:failureThreshold":{},"f:initialDelaySeconds":{},"f:periodSeconds":{},"f:successThreshold":{},"f:timeoutSeconds":{}},"f:resources":{},"f:securityContext":{".":{},"f:runAsUser":{}},"f:terminationMessagePath":{},"f:terminationMessagePolicy":{},"f:volumeMounts":{".":{},"k:{\"mountPath\":\"/bitnami/consul\"}":{".":{},"f:mountPath":{},"f:name":{}}}}},"f:dnsPolicy":{},"f:restartPolicy":{},"f:schedulerName":{},"f:securityContext":{".":{},"f:fsGroup":{}},"f:terminationGracePeriodSeconds":{}}},"f:updateStrategy":{"f:type":{}},"f:volumeClaimTemplates":{}}},"manager":"Go-http-client","operation":"Update","time":"2021-08-28T03:31:27Z"},{"apiVersion":"apps/v1","fieldsType":"FieldsV1","fieldsV1":{"f:status":{"f:collisionCount":{},"f:currentReplicas":{},"f:currentRevision":{},"f:observedGeneration":{},"f:readyReplicas":{},"f:replicas":{},"f:updateRevision":{},"f:updatedReplicas":{}}},"manager":"kube-controller-manager","operation":"Update","time":"2021-08-29T11:46:21Z"}],"name":"consul-1630121482","namespace":"reddwarf-pro","selfLink":"/apis/apps/v1/namespaces/reddwarf-pro/statefulsets/consul-1630121482","uid":"2c00fca9-6d80-42a2-9bb9-9f80e1ed3cbe"},"spec":{"podManagementPolicy":"Parallel","replicas":3,"revisionHistoryLimit":10,"selector":{"matchLabels":{"app.kubernetes.io/instance":"consul-1630121482","app.kubernetes.io/name":"consul"}},"serviceName":"consul-1630121482-headless","template":{"metadata":{"creationTimestamp":null,"labels":{"app.kubernetes.io/instance":"consul-1630121482","app.kubernetes.io/managed-by":"Helm","app.kubernetes.io/name":"consul","helm.sh/chart":"consul-9.3.5"}},"spec":{"affinity":{"podAntiAffinity":{"preferredDuringSchedulingIgnoredDuringExecution":[{"podAffinityTerm":{"labelSelector":{"matchLabels":{"app.kubernetes.io/instance":"consul-1630121482","app.kubernetes.io/name":"consul"}},"namespaces":["reddwarf-pro"],"topologyKey":"kubernetes.io/hostname"},"weight":1}]}},"containers":[{"env":[{"name":"BITNAMI_DEBUG","value":"false"},{"name":"CONSUL_NODE_NAME","valueFrom":{"fieldRef":{"apiVersion":"v1","fieldPath":"metadata.name"}}},{"name":"CONSUL_RETRY_JOIN","value":"consul-1630121482-headless.reddwarf-pro.svc.cluster.local"},{"name":"CONSUL_DISABLE_KEYRING_FILE","value":"true"},{"name":"CONSUL_BOOTSTRAP_EXPECT","value":"3"},{"name":"CONSUL_RAFT_MULTIPLIER","value":"1"},{"name":"CONSUL_DOMAIN","value":"consul"},{"name":"CONSUL_DATACENTER","value":"dc1"},{"name":"CONSUL_UI","value":"true"},{"name":"CONSUL_HTTP_PORT_NUMBER","value":"8500"},{"name":"CONSUL_DNS_PORT_NUMBER"},{"name":"CONSUL_RPC_PORT_NUMBER"},{"name":"CONSUL_SERF_LAN_PORT_NUMBER"}],"image":"docker.io/bitnami/consul:1.10.2-debian-10-r0","imagePullPolicy":"IfNotPresent","lifecycle":{"preStop":{"exec":{"command":["consul","leave"]}}},"livenessProbe":{"exec":{"command":["consul","operator","raft","list-peers"]},"failureThreshold":6,"initialDelaySeconds":50,"periodSeconds":10,"successThreshold":1,"timeoutSeconds":5},"name":"consul","ports":[{"containerPort":8500,"name":"http","protocol":"TCP"},{"containerPort":8400,"name":"rpc","protocol":"TCP"},{"containerPort":8301,"name":"serflan-tcp","protocol":"TCP"},{"containerPort":8301,"name":"serflan-udp","protocol":"UDP"},{"containerPort":8300,"name":"rpc-server","protocol":"TCP"},{"containerPort":8600,"name":"dns-tcp","protocol":"TCP"},{"containerPort":8600,"name":"dns-udp","protocol":"UDP"}],"readinessProbe":{"exec":{"command":["consul","members"]},"failureThreshold":6,"initialDelaySeconds":25,"periodSeconds":10,"successThreshold":1,"timeoutSeconds":5},"resources":{},"securityContext":{"runAsUser":1001},"terminationMessagePath":"/dev/termination-log","terminationMessagePolicy":"File","volumeMounts":[{"mountPath":"/bitnami/consul","name":"data"}]}],"dnsPolicy":"ClusterFirst","restartPolicy":"Always","schedulerName":"default-scheduler","securityContext":{"fsGroup":1001},"terminationGracePeriodSeconds":30}},"updateStrategy":{"type":"RollingUpdate"},"volumeClaimTemplates":[{"apiVersion":"v1","kind":"PersistentVolumeClaim","metadata":{"creationTimestamp":null,"name":"data"},"spec":{"accessModes":["ReadWriteOnce"],"resources":{"requests":{"storage":"8Gi"}},"volumeMode":"Filesystem"},"status":{"phase":"Pending"}}]}}
meta.helm.sh/release-name: consul-1630121482
meta.helm.sh/release-namespace: reddwarf-pro
managedFields:
- manager: Go-http-client
operation: Update
apiVersion: apps/v1
time: '2021-08-28T03:31:27Z'
fieldsType: FieldsV1
fieldsV1:
'f:metadata':
'f:annotations':
.: {}
'f:meta.helm.sh/release-name': {}
'f:meta.helm.sh/release-namespace': {}
'f:labels':
.: {}
'f:app.kubernetes.io/instance': {}
'f:app.kubernetes.io/managed-by': {}
'f:app.kubernetes.io/name': {}
'f:helm.sh/chart': {}
'f:spec':
'f:podManagementPolicy': {}
'f:replicas': {}
'f:revisionHistoryLimit': {}
'f:selector': {}
'f:serviceName': {}
'f:template':
'f:metadata':
'f:labels':
.: {}
'f:app.kubernetes.io/instance': {}
'f:app.kubernetes.io/managed-by': {}
'f:app.kubernetes.io/name': {}
'f:helm.sh/chart': {}
'f:spec':
'f:affinity':
.: {}
'f:podAntiAffinity':
.: {}
'f:preferredDuringSchedulingIgnoredDuringExecution': {}
'f:containers':
'k:{"name":"consul"}':
.: {}
'f:env':
.: {}
'k:{"name":"BITNAMI_DEBUG"}':
.: {}
'f:name': {}
'f:value': {}
'k:{"name":"CONSUL_BOOTSTRAP_EXPECT"}':
.: {}
'f:name': {}
'f:value': {}
'k:{"name":"CONSUL_DATACENTER"}':
.: {}
'f:name': {}
'f:value': {}
'k:{"name":"CONSUL_DISABLE_KEYRING_FILE"}':
.: {}
'f:name': {}
'f:value': {}
'k:{"name":"CONSUL_DNS_PORT_NUMBER"}':
.: {}
'f:name': {}
'k:{"name":"CONSUL_DOMAIN"}':
.: {}
'f:name': {}
'f:value': {}
'k:{"name":"CONSUL_HTTP_PORT_NUMBER"}':
.: {}
'f:name': {}
'f:value': {}
'k:{"name":"CONSUL_NODE_NAME"}':
.: {}
'f:name': {}
'f:valueFrom':
.: {}
'f:fieldRef':
.: {}
'f:apiVersion': {}
'f:fieldPath': {}
'k:{"name":"CONSUL_RAFT_MULTIPLIER"}':
.: {}
'f:name': {}
'f:value': {}
'k:{"name":"CONSUL_RETRY_JOIN"}':
.: {}
'f:name': {}
'f:value': {}
'k:{"name":"CONSUL_RPC_PORT_NUMBER"}':
.: {}
'f:name': {}
'k:{"name":"CONSUL_SERF_LAN_PORT_NUMBER"}':
.: {}
'f:name': {}
'k:{"name":"CONSUL_UI"}':
.: {}
'f:name': {}
'f:value': {}
'f:image': {}
'f:imagePullPolicy': {}
'f:lifecycle':
.: {}
'f:preStop':
.: {}
'f:exec':
.: {}
'f:command': {}
'f:livenessProbe':
.: {}
'f:exec':
.: {}
'f:command': {}
'f:failureThreshold': {}
'f:periodSeconds': {}
'f:successThreshold': {}
'f:timeoutSeconds': {}
'f:name': {}
'f:ports':
.: {}
'k:{"containerPort":8300,"protocol":"TCP"}':
.: {}
'f:containerPort': {}
'f:name': {}
'f:protocol': {}
'k:{"containerPort":8301,"protocol":"TCP"}':
.: {}
'f:containerPort': {}
'f:name': {}
'f:protocol': {}
'k:{"containerPort":8301,"protocol":"UDP"}':
.: {}
'f:containerPort': {}
'f:name': {}
'f:protocol': {}
'k:{"containerPort":8400,"protocol":"TCP"}':
.: {}
'f:containerPort': {}
'f:name': {}
'f:protocol': {}
'k:{"containerPort":8500,"protocol":"TCP"}':
.: {}
'f:containerPort': {}
'f:name': {}
'f:protocol': {}
'k:{"containerPort":8600,"protocol":"TCP"}':
.: {}
'f:containerPort': {}
'f:name': {}
'f:protocol': {}
'k:{"containerPort":8600,"protocol":"UDP"}':
.: {}
'f:containerPort': {}
'f:name': {}
'f:protocol': {}
'f:readinessProbe':
.: {}
'f:exec':
.: {}
'f:command': {}
'f:failureThreshold': {}
'f:periodSeconds': {}
'f:successThreshold': {}
'f:timeoutSeconds': {}
'f:resources': {}
'f:securityContext':
.: {}
'f:runAsUser': {}
'f:terminationMessagePath': {}
'f:terminationMessagePolicy': {}
'f:volumeMounts':
.: {}
'k:{"mountPath":"/bitnami/consul"}':
.: {}
'f:mountPath': {}
'f:name': {}
'f:dnsPolicy': {}
'f:restartPolicy': {}
'f:schedulerName': {}
'f:securityContext':
.: {}
'f:fsGroup': {}
'f:terminationGracePeriodSeconds': {}
'f:updateStrategy':
'f:type': {}
'f:volumeClaimTemplates': {}
- manager: kubectl-client-side-apply
operation: Update
apiVersion: apps/v1
time: '2021-08-29T11:48:47Z'
fieldsType: FieldsV1
fieldsV1:
'f:metadata':
'f:annotations':
'f:kubectl.kubernetes.io/last-applied-configuration': {}
'f:spec':
'f:template':
'f:spec':
'f:containers':
'k:{"name":"consul"}':
'f:livenessProbe':
'f:initialDelaySeconds': {}
'f:readinessProbe':
'f:initialDelaySeconds': {}
- manager: kube-controller-manager
operation: Update
apiVersion: apps/v1
time: '2021-08-29T11:51:17Z'
fieldsType: FieldsV1
fieldsV1:
'f:status':
'f:collisionCount': {}
'f:currentReplicas': {}
'f:currentRevision': {}
'f:observedGeneration': {}
'f:readyReplicas': {}
'f:replicas': {}
'f:updateRevision': {}
'f:updatedReplicas': {}
selfLink: /apis/apps/v1/namespaces/reddwarf-pro/statefulsets/consul-1630121482
status:
observedGeneration: 4
replicas: 3
readyReplicas: 3
currentReplicas: 3
updatedReplicas: 3
currentRevision: consul-1630121482-69bbcb9d47
updateRevision: consul-1630121482-69bbcb9d47
collisionCount: 0
spec:
replicas: 3
selector:
matchLabels:
app.kubernetes.io/instance: consul-1630121482
app.kubernetes.io/name: consul
template:
metadata:
creationTimestamp: null
labels:
app.kubernetes.io/instance: consul-1630121482
app.kubernetes.io/managed-by: Helm
app.kubernetes.io/name: consul
helm.sh/chart: consul-9.3.5
spec:
containers:
- name: consul
image: 'docker.io/bitnami/consul:1.10.2-debian-10-r0'
ports:
- name: http
containerPort: 8500
protocol: TCP
- name: rpc
containerPort: 8400
protocol: TCP
- name: serflan-tcp
containerPort: 8301
protocol: TCP
- name: serflan-udp
containerPort: 8301
protocol: UDP
- name: rpc-server
containerPort: 8300
protocol: TCP
- name: dns-tcp
containerPort: 8600
protocol: TCP
- name: dns-udp
containerPort: 8600
protocol: UDP
env:
- name: BITNAMI_DEBUG
value: 'false'
- name: CONSUL_NODE_NAME
valueFrom:
fieldRef:
apiVersion: v1
fieldPath: metadata.name
- name: CONSUL_RETRY_JOIN
value: consul-1630121482-headless.reddwarf-pro.svc.cluster.local
- name: CONSUL_DISABLE_KEYRING_FILE
value: 'true'
- name: CONSUL_BOOTSTRAP_EXPECT
value: '3'
- name: CONSUL_RAFT_MULTIPLIER
value: '1'
- name: CONSUL_DOMAIN
value: consul
- name: CONSUL_DATACENTER
value: dc1
- name: CONSUL_UI
value: 'true'
- name: CONSUL_HTTP_PORT_NUMBER
value: '8500'
- name: CONSUL_DNS_PORT_NUMBER
- name: CONSUL_RPC_PORT_NUMBER
- name: CONSUL_SERF_LAN_PORT_NUMBER
resources: {}
volumeMounts:
- name: data
mountPath: /bitnami/consul
livenessProbe:
exec:
command:
- consul
- operator
- raft
- list-peers
initialDelaySeconds: 50
timeoutSeconds: 5
periodSeconds: 10
successThreshold: 1
failureThreshold: 6
readinessProbe:
exec:
command:
- consul
- members
initialDelaySeconds: 25
timeoutSeconds: 5
periodSeconds: 10
successThreshold: 1
failureThreshold: 6
lifecycle:
preStop:
exec:
command:
- consul
- leave
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
imagePullPolicy: IfNotPresent
securityContext:
runAsUser: 1001
restartPolicy: Always
terminationGracePeriodSeconds: 30
dnsPolicy: ClusterFirst
securityContext:
fsGroup: 1001
affinity:
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 1
podAffinityTerm:
labelSelector:
matchLabels:
app.kubernetes.io/instance: consul-1630121482
app.kubernetes.io/name: consul
namespaces:
- reddwarf-pro
topologyKey: kubernetes.io/hostname
schedulerName: default-scheduler
volumeClaimTemplates:
- kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: data
creationTimestamp: null
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 8Gi
volumeMode: Filesystem
status:
phase: Pending
serviceName: consul-1630121482-headless
podManagementPolicy: Parallel
updateStrategy:
type: RollingUpdate
revisionHistoryLimit: 10
我已經嘗試添加配置:
spring:
application:
name: dolphin-gateway
cloud:
consul:
discovery:
health-check-interval: 15s
deregister: true
health-check-critical-timeout: 5s
instance-id: ${spring.application.name}:${vcap.application.instance_id:${spring.application.instance_id:${random.value}}}
prefer-ip-address: true
enabled: true
host: consul-1630121482-headless.reddwarf-pro.svc.cluster.local
這是領事儀表板用戶界面: https ://consul.poemhub.top/ui/dc1/services/dolphin-gateway/instances。 這是無效的服務:
These worked for me.
spring.cloud.consul.discovery.enabled=true
spring.cloud.consul.discovery.register=true
spring.cloud.consul.discovery.deregister=false
spring.cloud.consul.discovery.health-check-path=/context-path/actuator/health
spring.cloud.consul.discovery.health-check-interval=3s
spring.cloud.consul.discovery.health-check-timeout=3s
spring.cloud.consul.discovery.health-check-critical-timeout=5s
spring.cloud.consul.discovery.scheme=http
spring.cloud.consul.discovery.health-check-tls-skip-verify=true
spring.cloud.consul.discovery.prefer-ip-address=true
spring.cloud.consul.discovery.register-health-check=true
spring.cloud.consul.port=8500
spring.cloud.consul.host=consul-headless
Apply these in application.properties file.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.