zoukankan      html  css  js  c++  java
  • springcloud基于ribbon的canary路由方案

    思路

    根据eureka的metadata进行自定义元数据,然后使用ribbon对该元数据进行过滤和匹配,选择server。

    实现

    这里使用header来传递路由信息,改造ribbon-discovery-filter-spring-cloud-starter,使其不影响静态server list。

    filter

    public class TagFilter extends ZuulFilter {
    
        private static final Logger LOGGER = LoggerFactory.getLogger(TagFilter.class);
    
        @Override
        public String filterType() {
            return "pre";
        }
    
        @Override
        public int filterOrder() {
            return 7;
        }
    
        @Override
        public boolean shouldFilter() {
            return true;
        }
    
        @Override
        public Object run() {
            RequestContext requestContext = RequestContext.getCurrentContext();
            HttpServletRequest request = requestContext.getRequest();
            String tag = request.getHeader("tag");
    
            RibbonFilterContextHolder.getCurrentContext().add("tag", tag);
            LOGGER.info("route {} to {}",request.getRequestURI(),tag);
    
            return null;
        }
    }

    predicate

    public class MetadataAwarePredicate extends AbstractServerPredicate{
    
        @Override
        public boolean apply(PredicateKey input) {
            if(input == null || !(input.getServer() instanceof DiscoveryEnabledServer)){
                return true;
            }
            DiscoveryEnabledServer server = (DiscoveryEnabledServer) input.getServer();
            final RibbonFilterContext context = RibbonFilterContextHolder.getCurrentContext();
            final Set<Map.Entry<String, String>> attributes = Collections.unmodifiableSet(context.getAttributes().entrySet());
            final Map<String, String> metadata = server.getInstanceInfo().getMetadata();
            return metadata.entrySet().containsAll(attributes);
        }
    }

    rule

    public class MetadataAwareRule extends ZoneAvoidanceRule {
    
        @Override
        public AbstractServerPredicate getPredicate() {
            return new MetadataAwarePredicate();
        }
    }

    autoconfig

    @Configuration
    @ConditionalOnClass(DiscoveryEnabledNIWSServerList.class)
    @AutoConfigureBefore(RibbonClientConfiguration.class)
    @ConditionalOnProperty(value = "ribbon.filter.metadata.enabled", matchIfMissing = true)
    public class RibbonMetaFilterAutoConfiguration {
    
        @Bean
        @ConditionalOnMissingBean
        @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
        public MetadataAwareRule metadataAwareRule() {
            return new MetadataAwareRule();
        }
    }
  • 相关阅读:
    Windows Phone学习笔记(6) — — 套接字概述
    Windows Phone学习笔记(4) — — 本地数据库操作
    Windows Phone学习笔记(7) — — TCP套接字
    Windows Phone学习笔记(8) — — UDP套接字
    Windows Phone学习笔记(10) — — 设置页面
    模拟退火摘要
    记 我的第一篇博客
    Splay算法摘要
    调格式专区
    路由器 设置DMZ主机 端口转发 实现外网访问
  • 原文地址:https://www.cnblogs.com/zhangjianbin/p/7228341.html
Copyright © 2011-2022 走看看