优雅下线服务

Eureka

修改配置文件

bootstrap.properties
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#启用shutdown
endpoints.shutdown.enabled=true
#禁用登陆认证
endpoints.shutdown.sensitive=false
#启用pause
endpoints.pause.enabled=true
#禁用登陆认证
endpoints.pause.sensitive=false
endpoints.restart.enabled=true
endpoints.restart.sensitive=false
#client间隔n秒去拉取服务注册信息
eureka.client.registry-fetch-interval-seconds=5
#eureka server缓存刷新时间 ms
ribbon.ServerListRefreshInterval=5000

通过http请求停止服务

#将服务下线
http://ip:port/pause
#停止服务,等价于kill
http://ip:port/shutdown

参考文章:
https://blog.csdn.net/nihao12323432/article/details/81205288
https://www.jianshu.com/p/07c2e0d59dc9

Nacos

编写Controller,通过Nacos提供的api下线服务

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
36
37
38
39
40
import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
import com.alibaba.cloud.nacos.registry.NacosRegistration;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.tool.api.R;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* @author ruanqi
* @date 2021/6/11 18:19
* @see ShutdownController
*/
@RestController
@RequestMapping("/nacos/service")
@AllArgsConstructor
@Slf4j
public class ShutdownController {

private NacosRegistration nacosRegistration;

private NacosDiscoveryProperties nacosDiscoveryProperties;

@SneakyThrows
@DeleteMapping("/shutdown")
@SuppressWarnings("all")
public R shutdown() {
String serviceName = nacosDiscoveryProperties.getService();
String groupName = nacosDiscoveryProperties.getGroup();
String clusterName = nacosDiscoveryProperties.getClusterName();
String ip = nacosDiscoveryProperties.getIp();
int port = nacosDiscoveryProperties.getPort();
log.info("deregister from nacos, serviceName:{}, groupName:{}, clusterName:{}, ip:{}, port:{}", serviceName, groupName, clusterName, ip, port);
nacosRegistration.getNacosNamingService().deregisterInstance(serviceName, groupName, ip, port, clusterName);
return R.status(true);
}

}

通过http请求访问该接口

curl -X DELETE http://ip:port/nacos/service/shutdown

参考文章:https://nacos.io/zh-cn/docs/sdk.html

作者

qrua7

发布于

2019-06-10

更新于

2025-10-28

许可协议

评论

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×