• spring cloud gateway+nacos 服务下线感知延迟,未及时出现503,请求依然转发到下线服务


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

    本篇算是配合之前的一篇了。整体问题是gateway对下线服务感知延迟,之前那篇文章是从服务角度解决自身注销的问题(使用undertow,服务停止后nacos下线注销延迟问题)。本篇是解决gateway自身发现服务问题。
    这种问题大体就集中情况
    1 服务端注销未正常运行(这个看一下nacos是否及时删除了节点信息就可以排查出来)
    2 网关服务未及时发现节点的变化(这个可以在debug级别日志验证)
    3 服务端和网关服务不互通

    1.场景描述

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

    2.分析

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

    private Mono> choose(Request lbRequest, String serviceId,
    			Set supportedLifecycleProcessors) {
    		ReactorLoadBalancer 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);
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

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

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

    上面那个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());
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    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 list = cache.get(key, List.class);
    			if (list == null || list.isEmpty()) {
    				return Mono.empty();
    			}
    			return Flux.just(list).materialize().collectList();
    		}
    		...
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

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

    3.解决方案

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

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

    先在写个spring.factories

    org.springframework.boot.autoconfigure.EnableAutoConfiguration=
    
    • 1

    com.dong.gateway.config.SubscribeConfig

    SubscribeConfig:

    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.NacosSubscribe;
    import com.dong.server.gateway.subscribe.NacosEventListener;
    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()
     *
     * @Author: dong
     * @Date: 2021/12/30 10:25
     */
    @Configuration
    @Import(SpringContextHolder.class)
    @AutoConfigureAfter(LoadBalancerCacheAutoConfiguration.class)
    public class SubscribeConfig {
    
        @Bean
        public NacosSubscribe getNacosSubscribe(NacosServiceManager nacosServiceManager, NacosDiscoveryProperties properties){
        	LoadBalancerCacheManager cacheManager = SpringContextHolder.getBean(LoadBalancerCacheManager.class);
            return new NacosSubscribe(nacosServiceManager,properties,new NacosEventListener(loadBalancerCacheManager));
        }
    }
    
    • 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

    NacosSubscribe

    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推送更新事件
     * 启动和加载路由时重新订阅
     * @Author: dong
     * @Date: 2021/12/30 15:45
     */
    @Slf4j
    @AllArgsConstructor
    public class NacosSubscribe implements ApplicationRunner {
        private NacosServiceManager nacosServiceManager;
        private NacosDiscoveryProperties properties;
        private NacosEventListener myEventListener;
    
        private Set getRouteServices(){
        	// TODO 这里返回自己要订阅的服务名称
            return new HashSet();
        }
        // 这里监听时间是为了在路由信息修改时候,重新订阅服务的。如果说路由重新加载不会有订阅变动的话,可以去掉
    @org.springframework.context.event.EventListener({RefreshRoutesEvent.class})
        public void subscribe() {
            NamingService namingService = nacosServiceManager
                    .getNamingService(properties.getNacosProperties());
            try {
                Set 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();
        }
    
    }
    
    • 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
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57

    NacosEventListener

    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推送更新事件
     *
     * @Author: dong
     * @Date: 2021/12/30 16:42
     */
    @Slf4j
    @AllArgsConstructor
    public class NacosEventListener 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 instances = namingEvent.getInstances();
                        cache.put(serviceId,  NacosServiceDiscovery.hostToServiceInstanceList(instances,serviceId));
                    }
                }
            }catch (Exception e){
                log.error("处理nacos推送失败!",e);
            }
        }
    }
    
    • 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
    • 41
    • 42
    • 43
    • 44
    • 45

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

    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 工具类
     *
     * @Author: dong
     * @Date: 2021/8/10 10:27
     */
    @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 getBean(Class 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();
        }
    
    }
    
    • 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
    • 41

    先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦

  • 相关阅读:
    如何在idea中使用maven搭建tomcat环境
    互联网智慧工地源码,“互联网+建筑大数据”SaaS微服务架构,支持PC端、手机端、数据大屏端
    C++库的随机数生成
    javaI/O包中的包装模式
    【数据结构篇】线性表2 —— 栈和队列
    .Net IDE智能提示汉化(.Net6、AspNetCore)
    计算机基础知识——操作系统和应用、控制硬件(三)
    SQL 改写系列十:半连接转内连接
    嵌入式养成计划-33--数据库-sqlite3
    CCF- CSP 202009-2风险人群筛查 满分题解
  • 原文地址:https://blog.csdn.net/asdfadafd/article/details/126114123