首页 > 其他分享 >springcloud或者springboot项目生产如何发版

springcloud或者springboot项目生产如何发版

时间:2022-09-21 12:11:07浏览次数:76  
标签:shutdown springboot springcloud private Collections context 发版 my public

  1. 我们要先从注册中心将服务下线
    为了通用性,不管任何注册中心都能使用统一的逻辑,我们在项目提供下面的Controller:
@RestController
public class ServerDeRegisterController {

    @Autowired
    private ServiceRegistry serviceRegistry;
    @Autowired
    private Registration registration;

    @RequestMapping("/server/deregister")
    public String deRegister(){
        serviceRegistry.deregister(registration);
        return "success";
    }
}

因此:我们调用下线的url(只要改变自己的ip和端口即可):
curl http://127.0.0.1:9000/server/deregister

  1. 下线完后,需要睡眠20s,因为服务下线后,其他服务还会缓存我们服务的ip和port几秒钟
    经过20s后,基本不会有其他服务调用我们的接口了,此时,mq等中间件还会跟我们服务进行连接

  2. 调用springboot shutdownEndpoint进行优雅关闭项目
    需要在yml中配置我们的Endpoint:

management:
  endpoints:
    web:
      base-path: /my/actuator
      exposure:
        include: "*"
      path-mapping:
        shutdown: my-shutdown
  endpoint:
    shutdown:
      enabled: true

本质是调用:

//下面的id="shutdown"在上一步的配置中被替换成了my-shutdown
@Endpoint(id = "shutdown", enableByDefault = false)
public class ShutdownEndpoint implements ApplicationContextAware {

	private static final Map<String, String> NO_CONTEXT_MESSAGE = Collections
			.unmodifiableMap(
					Collections.singletonMap("message", "No context to shutdown."));

	private static final Map<String, String> SHUTDOWN_MESSAGE = Collections
			.unmodifiableMap(
					Collections.singletonMap("message", "Shutting down, bye..."));

	private ConfigurableApplicationContext context;

	@WriteOperation
	public Map<String, String> shutdown() {
		if (this.context == null) {
			return NO_CONTEXT_MESSAGE;
		}
		try {
			return SHUTDOWN_MESSAGE;
		}
		finally {
			Thread thread = new Thread(this::performShutdown);
			thread.setContextClassLoader(getClass().getClassLoader());
			thread.start();
		}
	}

	private void performShutdown() {
		try {
			Thread.sleep(500L);
		}
		catch (InterruptedException ex) {
			Thread.currentThread().interrupt();
		}
		this.context.close();
	}

	@Override
	public void setApplicationContext(ApplicationContext context) throws BeansException {
		if (context instanceof ConfigurableApplicationContext) {
			this.context = (ConfigurableApplicationContext) context;
		}
	}

}

因此关闭项目的url如下(改变下面的Ip和port为自己服务器的即可):

curl -X POST http://127.0.0.1:9000/my/actuator/my-shutdown

5.睡眠 30s,然后再kill掉服务(可能项目已经关闭了,也可能还没,时间自己定)

  1. 重新发布新的项目

最后:上面步骤写成shell脚本,发完一台机,再发另外一台

标签:shutdown,springboot,springcloud,private,Collections,context,发版,my,public
From: https://www.cnblogs.com/yangxiaohui227/p/16715168.html

相关文章