Order
With the Eureka metadata information, coupled with the Ribbon routing function, you can implement many functions in Api-gateway, such as grayscale testing, production debugging and so on. Here's how to use the Ribbon-discovery-filter-spring-cloud-starter provided by Jmnarloch, and use a few simple lines of code to get it all done.
Maven
<dependency> <groupId>io.jmnarloch</groupId> <artifactId> ribbon-discovery-filter-spring-cloud-starter</artifactid> <version>2.1. 0</version> </dependency>
Eureka Meta Data Configuration
eureka: instance: metadataMap: tag: pre-prod
Routing
@Component Public classDynamicroutesfilter extends Zuulfilter {Private StaticFinal Logger Logger = Loggerfactory.getlogger (dynamicroutesfilter.class); @Override PublicString FilterType () {return "Pre"; } @Override Public intFilterorder () {return 0; } @Override PublicBoolean shouldfilter () {return true; } @Override PublicObject Run () {RequestContext RequestContext=Requestcontext.getcurrentcontext (); HttpServletRequest Request=requestcontext.getrequest (); String Tag= Request.getheader ("Tag"); if(Tag! =NULL) {Ribbonfiltercontextholder.getcurrentcontext (). Add ("Tag", tag); } return NULL; }}
According to the requested tag, route to a service marked with the specified tag value. Note that if the request does not specify tag, then the code needs to be empty, and if you do not specify tag, all services with the same name will be routed to the default.
Access needs to be specified on the header Tagdoc
The Ribbon uses Eureka Meta for dynamic routing