1. 程式人生 > >springcloud基於ribbon的canary路由方案

springcloud基於ribbon的canary路由方案

collect void ins request == null figure bsp 灰度

思路

根據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();
    }
}
  • Spring Cloud: Ribbon dynamic routing

  • SpringCloud Ribbon 降級、限流、灰度發布

springcloud基於ribbon的canary路由方案