Skip to content
欢迎扫码关注公众号

Spring Cloud 微服务平滑更新问题

之前使用 Eureka 作为微服务注册中心的时候,就试图解决在服务关闭时出现的短暂服务中断问题。总体思路是有的,但是由于发布那边主要是运维在处理, Kubernetes 的相关功能自己不了解,后来也就放弃了。最近负责使用阿里云容器服务发布了一个项目时,尝试解决了下这个问题。

微服务之间的调用或者网关转发请求到服务,是通过向服务注册中心请求来获取可用的服务列表。为了提高性能,每个微服务都会缓存一份获取到的服务列表。这样就导致在一个微服务容器关闭时,即使微服务注册中心及时的注销了这个服务节点,但是还是会有一部分请求被分发到这个已关闭的节点。

对此有两个方案:

  1. 启用服务的重试功能;

    这个方案需要保证所有接口的处理都是幂等的,对接口处理的设计要求比较高,而且也可能会带来额外的问题;

  2. 在服务关闭前从注册中心注销后,仍然保留一段时间服务响应能力,之后再关闭。

    这个方案之前就曾尝试过,比如通过 curl 命令注销 Eureka 服务,这次使用的是阿里开源的 Nacos 注册中心,也有类似的功能。这种方式可以成功注销服务,但是由于一般每个服务节点都通过心跳来监控微服务的可用状态,所以很快(Nacos默认好像是一秒)就会再次注册。

由于所有接口都需要实现幂等的效果,要求太高,而且第二种方案更友好一些,就按这个思路来调查。

尝试了使用@PreDestoryGracefulShutdownTomcat 均未实现想要的效果。

最后通过参考 这篇文章 自定义了一个 EndPoint,在其中注销 Nacos 服务。

之所以通过 Actuator 暴露接口来调用是因为通过设置来保证其安全性,具体的方法可以参考这篇博客

这种方式通过在微服务中调用 Nacos 提供的接口来注销,注销之后就不会再发送心跳。之后通过 Sleep 一定的时间来阻止服务的关闭,使其仍然可以保持响应一段时间。这里需要配合阿里云容器服务( Kubernetes 版)的 停止前处理 功能来触发。

具体的实现方法如下:

1. 添加自定义的 EndPoint

java
import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.actuate.endpoint.annotation.DeleteOperation;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.Selector;
import org.springframework.stereotype.Component;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

@Component
@Endpoint(id = "nacos")
@RequiredArgsConstructor
@Slf4j
public class NacosEndpoint {

    private final NacosDiscoveryProperties nacosDiscoveryProperties;

    @Value("${spring.application.name}")
    private String applicationName;

    @Value("${server.port}")
    private Integer port;

    @DeleteOperation
    public Map<String, String> deregister() {
        return deregisterAndSleep(0);
    }

    @DeleteOperation
    public Map<String, String> deregisterAndSleep(@Selector int seconds) {
        Map<String, String> result = new HashMap<>();

        InetAddress addr = null;
        try {
            addr = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            log.error(e.getMessage(), e);
            result.put("code", "failure");
            result.put("message", e.getMessage());
            return result;
        }

        log.info(String.format("Deregister nacos instance (%s - %s:%d)", applicationName, addr.getHostAddress(), port));

        NamingService namingService = nacosDiscoveryProperties.namingServiceInstance();
        try {
            namingService.deregisterInstance(applicationName, addr.getHostAddress(), port);
        } catch (NacosException e) {
            log.error(e.getMessage(), e);
            result.put("code", "failure");
            result.put("message", e.getMessage());
            return result;
        }

        log.info("Deregister nacos instance success.");

        if (seconds > 0) {
            try {
                log.info(String.format("Thread sleep %d seconds.", seconds));
                Thread.sleep(seconds * 1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
                log.error(e.getMessage(), e);
                result.put("code", "failure");
                result.put("message", e.getMessage());
                return result;
            }
        }

        result.put("code", "ok");
        return result;
    }
}

2. 配置 K8S

在 阿里云容器服务( Kubernetes 版)的 停止前处理 中调用该 EndPoint ["curl","-X","DELETE","http://localhost:5678/customize-actuator/nacos/30"]

customize-actuator 为自定义的 Actuator 路径,具体的参考这篇文章

Page Layout Max Width

Adjust the exact value of the page width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the page layout
A ranged slider for user to choose and customize their desired width of the maximum width of the page layout can go.

Content Layout Max Width

Adjust the exact value of the document content width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the content layout
A ranged slider for user to choose and customize their desired width of the maximum width of the content layout can go.