The Ribbon uses Eureka Meta for dynamic routing

Source: Internet
Author: User

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
    • Spring Cloud:ribbon Dynamic Routing

The Ribbon uses Eureka Meta for dynamic routing

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.