故事
首页
指南
  • Java
  • Python
  • Linux
  • 前端
  • Docker
  • 实践
  • 折腾
  • 分类
  • 标签
  • 归档
壁纸 (opens new window)
GitHub (opens new window)
首页
指南
  • Java
  • Python
  • Linux
  • 前端
  • Docker
  • 实践
  • 折腾
  • 分类
  • 标签
  • 归档
壁纸 (opens new window)
GitHub (opens new window)
  • Java基础

    • String类的深入学习
    • HashMap源码学习
    • Java8新特性回顾
  • Java框架

    • POI事件模式解析并读取Excel文件数据
    • SpringMVC的执行流程源码分析
    • netty+websocket实现即时通讯功能
    • 分布式定时任务解决方案xxl-job
    • spring-session实现集群session共享
    • springcloud优雅下线服务
      • 问题场景
      • 实现
        • 查询注册中心中服务的状态
        • 通知注册中心服务下线
        • 通知注册中心服务上线
    • Java日志发展历史
    • Mybatis Generator配置
    • JavaSPI机制和Springboot自动装配原理
    • Spring Cloud Config接入
    • 使用spring validation进行参数校验
    • dubbo接口超时配置
    • ConfigurationProperties注解
    • EasyExcel导出excel
  • 中间件

    • 分布式锁解决方案
    • 关于消息中间件MQ
    • kafka学习记录
    • kakfa实践
    • kakfa重复消费问题处理
  • 数据库

    • MySql索引
    • SQL优化学习
    • Otter实现数据全量增量同步
  • 开发工具

    • Maven的生命周期
    • nexus无法下载依赖的问题记录
  • 其他

    • linux服务器安装OpenOffice踩坑
    • POI踩坑-zip file is closed
    • OpenJDK没有jstack等命令的解决办法
    • 微信小程序加密数据对称解密工具类
    • mybatis的classpath配置导致的jar包读取问题
    • feign请求导致的用户ip获取问题记录
    • ribbon刷新服务列表间隔和canal的坑
    • cpu占用率高排查思路
  • 简介
  • java
  • Java框架
storyxc
2021-11-15

springcloud优雅下线服务

# springcloud优雅下线服务

# 问题场景

线上问题需要紧急修复部署,但是服务一直在跑,网关还会一直往服务中路由请求进行处理,如果直接停掉服务,客户端会出现业务中断的问题。因此需要在替换更新前先手动下线服务,这样用户请求不会被分发到下线的节点上,就可以直接进行更新而不影响用户体验。

# 实现

# 查询注册中心中服务的状态

curl -GET -u eureka_username:eureka_password http://eureka_ip:eureka_port/eureka/apps/服务名称/实例ip:实例名称:实例port
1

例如

curl -GET -u admin:admin123 http://127.0.0.1:19991/eureka/apps/app-platform/127.0.0.1:app-platform-1.0.0-SNAPSHOT:8001
1

# 结果

<instance>
  <instanceId>127.0.0.1:app-platform-2.0.0-SNAPSHOT:8001</instanceId>
  <hostName>127.0.0.1</hostName>
  <app>app-platform</app>
  <ipAddr>127.0.0.1</ipAddr>
  <status>UP</status>  # 状态为在线
  <overriddenstatus>UNKNOWN</overriddenstatus> # 重写状态为空
  <port enabled="true">8001</port>
  <securePort enabled="false">443</securePort>
  <countryId>1</countryId>
  <dataCenterInfo class="com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo">
    <name>MyOwn</name>
  </dataCenterInfo>
  <leaseInfo>
    <renewalIntervalInSecs>10</renewalIntervalInSecs>
    <durationInSecs>30</durationInSecs>
    <registrationTimestamp>1636639654493</registrationTimestamp>
    <lastRenewalTimestamp>1636961178498</lastRenewalTimestamp>
    <evictionTimestamp>0</evictionTimestamp>
    <serviceUpTimestamp>1636639654493</serviceUpTimestamp>
  </leaseInfo>
  <metadata>
    <management.port>8001</management.port>
    <nodeId>127.0.0.1_kkg</nodeId>
  </metadata>
  <homePageUrl>http://127.0.0.1:8001/</homePageUrl>
  <statusPageUrl>http://127.0.0.1:8001/actuator/info</statusPageUrl>
  <healthCheckUrl>http://127.0.0.1:8001/actuator/health</healthCheckUrl>
  <vipAddress>app-platform</vipAddress>
  <secureVipAddress>app-platform</secureVipAddress>
  <isCoordinatingDiscoveryServer>false</isCoordinatingDiscoveryServer>
  <lastUpdatedTimestamp>1636639654493</lastUpdatedTimestamp>
  <lastDirtyTimestamp>1636639654480</lastDirtyTimestamp>
  <actionType>ADDED</actionType>
</instance>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

# 通知注册中心服务下线

curl -i -X PUT -u eureka_username:eureka_password http://eureka_ip:eureka_port/eureka/apps/服务名称/实例ip:实例名称:实例port/status?value=OUT_OF_SERVICE
1

例如:

curl -i -X PUT admin:admin123 http://127.0.0.1:19991/eureka/apps/app-platform/127.0.0.1:app-platform-1.0.0-SNAPSHOT:8001/status?value=OUT_OF_SERVICE
1

# 结果

HTTP/1.1 200 # 请求成功
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-Frame-Options: DENY
Content-Type: application/xml
Content-Length: 0
Date: Mon, 15 Nov 2021 07:30:32 GMT
1
2
3
4
5
6
7
8
9
10

# 再次查询服务状态

<instance>
  <instanceId>127.0.0.1:app-platform-2.0.0-SNAPSHOT:8001</instanceId>
  <hostName>127.0.0.1</hostName>
  <app>app-platform</app>
  <ipAddr>127.0.0.1</ipAddr>
  <status>OUT_OF_SERVICE</status> # 服务状态-已下线
  <overriddenstatus>OUT_OF_SERVICE</overriddenstatus> # 重写状态为已下线
  <port enabled="true">8001</port>
  <securePort enabled="false">443</securePort>
  <countryId>1</countryId>
  <dataCenterInfo class="com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo">
    <name>MyOwn</name>
  </dataCenterInfo>
  <leaseInfo>
    <renewalIntervalInSecs>10</renewalIntervalInSecs>
    <durationInSecs>30</durationInSecs>
    <registrationTimestamp>1636639654493</registrationTimestamp>
    <lastRenewalTimestamp>1636961468691</lastRenewalTimestamp>
    <evictionTimestamp>0</evictionTimestamp>
    <serviceUpTimestamp>1636639654493</serviceUpTimestamp>
  </leaseInfo>
  <metadata>
    <management.port>8001</management.port>
    <nodeId>127.0.0.1_kkg</nodeId>
  </metadata>
  <homePageUrl>http://127.0.0.1:8001/</homePageUrl>
  <statusPageUrl>http://127.0.0.1:8001/actuator/info</statusPageUrl>
  <healthCheckUrl>http://127.0.0.1:8001/actuator/health</healthCheckUrl>
  <vipAddress>app-platform</vipAddress>
  <secureVipAddress>app-platform</secureVipAddress>
  <isCoordinatingDiscoveryServer>false</isCoordinatingDiscoveryServer>
  <lastUpdatedTimestamp>1636961432181</lastUpdatedTimestamp>
  <lastDirtyTimestamp>1636639654480</lastDirtyTimestamp>
  <actionType>MODIFIED</actionType>
</instance>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

可以看到此时,服务状态为已经下线,不过还是要等到网关服务不再路由请求到该服务时再停掉服务。

# 通知注册中心服务上线

curl -i -X DELETE -u eureka_username:eureka_password http://eureka_ip:eureka_port/eureka/apps/服务名称/实例ip:实例名称:实例port/status
1

例如:

curl -i -X DELETE -u admin:admin123 http://127.0.0.1:19991/eureka/apps/app-platform/127.0.0.1:app-platform-1.0.0-SNAPSHOT:8001/status
1

# 结果

HTTP/1.1 200 # 请求成功
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-Frame-Options: DENY
Content-Type: application/xml
Content-Length: 0
Date: Mon, 15 Nov 2021 07:37:11 GMT
1
2
3
4
5
6
7
8
9
10

# 再次查询服务状态

<instance>
  <instanceId>127.0.0.1:app-platform-2.0.0-SNAPSHOT:8001</instanceId>
  <hostName>127.0.0.1</hostName>
  <app>app-platform</app>
  <ipAddr>127.0.0.1</ipAddr>
  <status>UP</status> # 此时服务已上线 如果通知后立刻查询,状态可能会是unknown,隔一段时间再查询即可
  <overriddenstatus>UNKNOWN</overriddenstatus>
  <port enabled="true">8001</port>
  <securePort enabled="false">443</securePort>
  <countryId>1</countryId>
  <dataCenterInfo class="com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo">
    <name>MyOwn</name>
  </dataCenterInfo>
  <leaseInfo>
    <renewalIntervalInSecs>10</renewalIntervalInSecs>
    <durationInSecs>30</durationInSecs>
    <registrationTimestamp>1636961828894</registrationTimestamp>
    <lastRenewalTimestamp>1636961861799</lastRenewalTimestamp>
    <evictionTimestamp>0</evictionTimestamp>
    <serviceUpTimestamp>1636639654493</serviceUpTimestamp>
  </leaseInfo>
  <metadata>
    <management.port>8001</management.port>
    <nodeId>127.0.0.1_kkg</nodeId>
  </metadata>
  <homePageUrl>http://127.0.0.1:8001/</homePageUrl>
  <statusPageUrl>http://127.0.0.1:8001/actuator/info</statusPageUrl>
  <healthCheckUrl>http://127.0.0.1:8001/actuator/health</healthCheckUrl>
  <vipAddress>app-platform</vipAddress>
  <secureVipAddress>app-platform</secureVipAddress>
  <isCoordinatingDiscoveryServer>false</isCoordinatingDiscoveryServer>
  <lastUpdatedTimestamp>1636961828894</lastUpdatedTimestamp>
  <lastDirtyTimestamp>1636961828889</lastDirtyTimestamp>
  <actionType>ADDED</actionType>
</instance>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
编辑 (opens new window)
#spring#eureka
上次更新: 2022/04/21, 18:12:24
spring-session实现集群session共享
Java日志发展历史

← spring-session实现集群session共享 Java日志发展历史→

Theme by Vdoing | Copyright © 2019-2023 story | 豫ICP备19046036号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式