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