spring cloud gateway服务下线感知延迟,未及时出现503

本篇算是配合之前的一篇了。整体问题是gateway对下线服务感知延迟,之前那篇文章是从服务角度解决自身注销的问题(使用undertow,服务停止后nacos下线注销延迟问题)。本篇是解决gateway自身发现服务问题。

1.场景描述

注册中心使用的nacos,客户端版本1.4.1。
gateway版本3.0.1。
nacos服务下线(包含手动点下线和服务正常停机)gateway在短暂几秒内还回继续将流量转发到已下线的服务上导致500。过几秒之后恢复正常,响应码变成503。表面上看,应该是gateway服务没有及时发现服务的下线。

2.分析

如果遇到这种问题,可以先排查一下面几种情况在尝试解决
1 服务端注销未正常运行(这个看一下nacos是否及时删除了节点信息就可以排查出来)
2 网关服务未及时发现节点的变化(这个可以在debug级别日志验证)
3 服务端和网关服务不互通

日志级别调整到debug,发现通过netty发送的下线通知已经抵达gateway服务。这说明nacos注册中心和spring boot服务通讯和订阅是没问题的。
从转发的入口着手:ReactiveLoadBalancerClientFilter#choose 这个方法就是gateway转发时选择服务的

private Mono<Response<ServiceInstance>> choose(Request<RequestDataContext> lbRequest, String serviceId,
			Set<LoadBalancerLifecycle> supportedLifecycleProcessors) {
		ReactorLoadBalancer<ServiceInstance> loadBalancer = this.clientFactory.getInstance(serviceId,
				ReactorServiceInstanceLoadBalancer.class);
		if (loadBalancer == null) {
			throw new NotFoundException("No loadbalancer available for " + serviceId);
		}
		supportedLifecycleProcessors.forEach(lifecycle -> lifecycle.onStart(lbRequest));
		// 最后是通过ReactorLoadBalancer的实现进行选择
		return loadBalancer.choose(lbRequest);
	}

ReactorLoadBalancer是负载均衡的接口,提供了两个实现,一个随机获取,一个轮询。
默认是使用轮询实现(RoundRobinLoadBalancer)。
RoundRobinLoadBalancer中选择服务的实现逻辑

public Mono<Response<ServiceInstance>> choose(Request request) {
		ServiceInstanceListSupplier supplier = serviceInstanceListSupplierProvider
				.getIfAvailable(NoopServiceInstanceListSupplier::new);
		// 在这个get方法中返回了可选服务器的集合
		return supplier.get(request).next()
				.map(serviceInstances -> processInstanceResponse(supplier, serviceInstances));
	}

上面那个get的实现是:CachingServiceInstanceListSupplier#CachingServiceInstanceListSupplier这个类中提供的

public CachingServiceInstanceListSupplier(ServiceInstanceListSupplier delegate, CacheManager cacheManager) {
		super(delegate);
		this.serviceInstances = CacheFlux.lookup(key -> {
			// 这里发现有缓存!感觉目的地近了。
			Cache cache = cacheManager.getCache(SERVICE_INSTANCE_CACHE_NAME);
			....
				}).then());
	}

2.1定位问题

调试一下看看:

  • 服务A启动注册到nacos
  • gateway正常将/test/hello转发至服务A
  • 在nacos管理端让服务A下线
  • 立刻访问不停/test/hello
  • 最初几秒内发现gateway还是会把流量打到服务A
  • 之后正常响应503

在获取服务集群信息的地方打断点

public CachingServiceInstanceListSupplier(ServiceInstanceListSupplier delegate, CacheManager cacheManager) {
		super(delegate);
		this.serviceInstances = CacheFlux.lookup(key -> {
			// TODO: configurable cache name
			Cache cache = cacheManager.getCache(SERVICE_INSTANCE_CACHE_NAME);
			if (cache == null) {
				if (log.isErrorEnabled()) {
					log.error("Unable to find cache: " + SERVICE_INSTANCE_CACHE_NAME);
				}
				return Mono.empty();
			}
			// 在异常的时间段,这个list还是有信息。集合没内容之后开始响应503
			List<ServiceInstance> list = cache.get(key, List.class);
			if (list == null || list.isEmpty()) {
				return Mono.empty();
			}
			return Flux.just(list).materialize().collectList();
		}
		...
	}

看来是这个缓存没有及时刷新的原因!后续找了一段时间,没找到刷新缓存的地方就放弃了。还是用笨方法先解决吧

3.解决方案

已经知道了问题所在,想办法解决就是了。
整体思路:在订阅nacos服务变化中进行功能拓展,刷新缓存。

三个类:
MySubscribeConfig:进行订阅配置的入口
MyNacosSubscribe:订阅nacos的实现,用来发布订阅消息
MyNacosEventListener:消息处理的实现,在这里刷新缓存

先在写个spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.dong.gateway.config.MySubscribeConfig

MySubscribeConfig:


import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
import com.alibaba.cloud.nacos.NacosServiceManager;
import com.dong.common.core.util.SpringContextHolder;
import com.dong.server.gateway.subscribe.MyNacosSubscribe;
import com.dong.server.gateway.subscribe.MyNacosEventListener;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.cloud.loadbalancer.cache.LoadBalancerCacheManager;
import org.springframework.cloud.loadbalancer.config.LoadBalancerCacheAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * 首先订阅当前网关关注的服务
 * nacos服务更新通知,但是gateway有一套自己的服务缓存列表。每次接到通知更新不及时导致转发到已经下线的服务
 * gateway获取缓存参考:org.springframework.cloud.loadbalancer.core.CachingServiceInstanceListSupplier
 * nacos订阅参考:com.alibaba.cloud.nacos.discovery.NacosWatch#start()
 *
 */
@Configuration
@Import(SpringContextHolder.class)
@AutoConfigureAfter(LoadBalancerCacheAutoConfiguration.class)
public class MySubscribeConfig {

    @Bean
    public MyNacosSubscribe getMyNacosSubscribe(NacosServiceManager nacosServiceManager, NacosDiscoveryProperties properties){
    	LoadBalancerCacheManager cacheManager = SpringContextHolder.getBean(LoadBalancerCacheManager.class);
        return new MyNacosSubscribe(nacosServiceManager,properties,new MyNacosEventListener(loadBalancerCacheManager));
    }
}

MyNacosSubscribe

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
import com.alibaba.cloud.nacos.NacosServiceManager;
import com.alibaba.nacos.api.naming.NamingService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.cloud.gateway.event.RefreshRoutesEvent;

import java.net.URI;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
 * 订阅nacos推送更新事件
 * 启动和加载路由时重新订阅
 */
@Slf4j
@AllArgsConstructor
public class MyNacosSubscribe implements ApplicationRunner {
    private NacosServiceManager nacosServiceManager;
    private NacosDiscoveryProperties properties;
    private MyNacosEventListener myEventListener;

    private Set<String> getRouteServices(){
    	// TODO 这里返回自己要订阅的服务名称
        return new HashSet();
    }
    // 这里监听时间是为了在路由信息修改时候,重新订阅服务的。如果说路由重新加载不会有订阅变动的话,可以去掉
@org.springframework.context.event.EventListener({RefreshRoutesEvent.class})
    public void subscribe() {
        NamingService namingService = nacosServiceManager
                .getNamingService(properties.getNacosProperties());
        try {
            Set<String> services = getRouteServices();
            if(CollectionUtil.isNotEmpty(services)){
                for (String service : services) {
                    namingService.subscribe(service, properties.getGroup(),
                            null, myEventListener);
                }
            }
        } catch (Exception e) {
            log.error("namingService subscribe failed, properties:{}", properties, e);
        }
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        subscribe();
    }

}

MyNacosEventListener

import com.alibaba.cloud.nacos.discovery.NacosServiceDiscovery;
import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.api.naming.listener.Event;
import com.alibaba.nacos.api.naming.listener.EventListener;
import com.alibaba.nacos.api.naming.listener.NamingEvent;
import com.alibaba.nacos.api.naming.pojo.Instance;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.Cache;
import org.springframework.cloud.loadbalancer.cache.LoadBalancerCacheManager;
import org.springframework.cloud.loadbalancer.core.CachingServiceInstanceListSupplier;

import java.util.List;

/**
 * 处理nacos推送更新事件
 */
@Slf4j
@AllArgsConstructor
public class MyNacosEventListener implements EventListener {

    private LoadBalancerCacheManager loadBalancerCacheManager;
    @Override
    public void onEvent(Event event) {
        try {
            if (event instanceof NamingEvent) {
                Cache cache = loadBalancerCacheManager.getCache(CachingServiceInstanceListSupplier.SERVICE_INSTANCE_CACHE_NAME);
                if(cache!=null){
                    NamingEvent namingEvent = ((NamingEvent) event);
                    String serviceName = namingEvent.getServiceName();
                    String[] split = serviceName.split(Constants.SERVICE_INFO_SPLITER);
                    String serviceId = split[1];
                    log.debug("收到更新服务消息:{}",serviceId);
                    List<Instance> instances = namingEvent.getInstances();
                    cache.put(serviceId,  NacosServiceDiscovery.hostToServiceInstanceList(instances,serviceId));
                }
            }
        }catch (Exception e){
            log.error("处理nacos推送失败!",e);
        }
    }
}

20220620更新:MySubscribeConfig中用到的工具类

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Lazy;

/**
 * spring工具类
 */
@Slf4j
@Lazy(false)
public class SpringContextHolder implements ApplicationContextAware, DisposableBean {

    private static ApplicationContext applicationContext = null;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) {
        SpringContextHolder.applicationContext = applicationContext;
    }

    public static <T> T getBean(Class<T> requiredType) {
        return applicationContext.getBean(requiredType);
    }

    public static void clearHolder() {
        if (log.isDebugEnabled()) {
            log.debug("清除SpringContextHolder中的ApplicationContext:" + applicationContext);
        }
        applicationContext = null;
    }


    @Override
    public void destroy() {
        SpringContextHolder.clearHolder();
    }

}
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐