How to configure domain specific routes in Zuul
Asked Answered
P

1

10

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?

Primrose answered 19/5, 2016 at 16:16 Comment(0)
S
2

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.

Shirleneshirley answered 25/9, 2017 at 8:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.