Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure domain specific routes in Zuul

We have 2 domains that point to our single Zuul ELB. Both sites use a lot of the same services so although our production environment will be setup a little different right now we only have the one Zuul ELB.

How can we route the same path to different services based on the domain name?

I would like to end up with something like this:

zuul:
  host: http://a-zuul-host.elb.amazonaws.com:80
  ignoredServices: '*'
  routes:
    app1:
      path: /
      domain: app1.com
      serviceId: APP_1_SERVICE
    app2:
      path: /
      domain: app2.com
      serviceId: APP_2_SERVICE

Is this even possible or would I be required to setup another zuul instance?

like image 579
Randyaa Avatar asked May 19 '16 16:05

Randyaa


People also ask

How configure Zuul API gateway?

Setting up Zuul API Gateway Server There are three steps to set up the Zuul API Gateway: Create a component for the Zuul API Gateway. Decide the things that the Zuul API Gateway should do. All the important requests are configured to pass through the Zuul API Gateway.

What is dynamic routing in Zuul?

zuul dynamic routing Gateway service is the only access to traffic. Don't stop the service. So dynamic routing is particularly necessary. Database dynamic routing is based on event refresh mechanism to modify zuul's routing properties.

How do I add dependency to Zuul?

Creating Zuul Server Application The Zuul Server is bundled with Spring Cloud dependency. You can download the Spring Boot project from Spring Initializer page https://start.spring.io/ and choose the Zuul Server dependency. Add the @EnableZuulProxy annotation on your main Spring Boot application.

How does Zuul routing work?

Zuul is an edge service that proxies requests to multiple backing services. It provides a unified “front door” to your system, which allows a browser, mobile app, or other user interface to consume services from multiple hosts without managing cross-origin resource sharing (CORS) and authentication for each one.


1 Answers

Here is one possible solution:

  1. Add a wildcard routing rule in your zuul configurations to route any request to /**, and disable stripping prefix for this routing rule.
  2. Add a new routing filter of type route and make it run before RibbonRoutingFilter because an exception will occur in this filter for an invalid host /**.
  3. Add domain configurations in your zuul yaml configurations for your different domains.
  4. Use your filter along with the domain configurations to change your host based on the domain.

Here is a sample code:

zuul:
  ignored-patterns: /health, /info
  routes:
    wildcard:
      path: /**
      url: /**
      stripPrefix: false

You've to ignore /health because it is going to be matched with the wildcard routing rule, and will cause a problem -- should be mapped to one of spring actuators instead, so we ignore it and let everything be handled as before.

For the domain configurations here is a sample:

domain:
  service_1:
    domain-matcher: https://api_1.com:[0-9]+
    url: https://service_1.com
  service_2:
    domain-matcher: https://api_2.com:[0-9]+
    url: https://service_2.com

In your filter now just have to set our host based on the matching domain.

like image 160
Karim Masoud Avatar answered Oct 14 '22 14:10

Karim Masoud