Springcloud Ribbon-based Canary routing scheme

Source: Internet
Author: User

Ideas

Customize the metadata based on the metadata of Eureka, and then use the Ribbon to filter and match the metadata and select Server.

Realize

The header is used here to pass the routing information and transform the ribbon-discovery-filter-spring-cloud-starter so that it does not affect the static server list.

Filter
 Public classTagfilter extends Zuulfilter {Private StaticFinal Logger Logger = Loggerfactory.getlogger (tagfilter.class); @Override PublicString FilterType () {return "Pre"; } @Override Public intFilterorder () {return 7; } @Override PublicBoolean shouldfilter () {return true; } @Override PublicObject 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 classMetadataawarepredicate extends abstractserverpredicate{@Override PublicBoolean 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 (); returnMetadata.entryset (). Containsall (attributes); }}
Rule
 Public class Metadataawarerule extends Zoneavoidancerule {    @Override    public  Abstractserverpredicate getpredicate () {        returnnew  metadataawarepredicate ();}    } 

AutoConfig

@Configuration @conditionalonclass (discoveryenabledniwsserverlist.class) @AutoConfigureBefore (ribbonclientconfiguration.class) @ConditionalOnProperty (value="ribbon.filter.metadata.enabled", matchifmissing =true) Public classribbonmetafilterautoconfiguration {@Bean @ConditionalOnMissingBean @Scope (configurablebeanfactory.scope_pr Ototype) Publicmetadataawarerule Metadataawarerule () {return NewMetadataawarerule (); }}
    • Spring Cloud:ribbon Dynamic Routing

    • Springcloud Ribbon downgrade, current limit, grayscale publishing

Springcloud Ribbon-based Canary routing scheme

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.