Spring-cloud-netflix: Dynamic route configuration based on environment variables

Created on 8 Jun 2017  路  6Comments  路  Source: spring-cloud/spring-cloud-netflix

Trying to override zuul routes configuration based on environment variables. Spring seems to do the right thing, i.e. the configuration is updated but zuul seems to also create its own configuration keys. This generates an exception when this services gets a request:

Caused by: mousio.etcd4j.responses.EtcdException: [100]: Key not found, cause: /spring/cloud/discovery/SVC_PATH

How can I override the route configuration via environment variables without creating new routes?

Configuration:
Using Spring cloud version Camden.RELEASE, which I think uses the 1.2.0.RELEASE version of spring-cloud-netflix.

application.yaml:

zuul:
  ignoredServices: '*'
  routes:
    svc:
      path: /svc/**
      url: http://svc-test/api/v1/

Environment variables:

export ZUUL_ROUTES_SVC_PATH=/override/**
export ZUUL_ROUTES_SVC_URL=http://localhost

When accessing the config properties from the management controller, I get the following:

routes": {
        "svc": {
          "id": "svclmsdata",
          "path": "/override/**",
          "serviceId": null,
          "url": "http://localhost",
          "stripPrefix": true,
          "retryable": null,
          "sensitiveHeaders": [
          ],
          "customSensitiveHeaders": false,
          "location": "http://localhost"
        },
        "SVC_URL": {
          "id": "http://localhost",
          "path": "/http://localhost",
          "serviceId": "SVC_URL",
          "url": null,
          "stripPrefix": true,
          "retryable": null,
          "sensitiveHeaders": [
          ],
          "customSensitiveHeaders": false,
          "location": "SVC_URL"
        },
        "SVC_PATH": {
          "id": "/override/**",
          "path": "/override/**",
          "serviceId": "SVC_PATH",
          "url": null,
          "stripPrefix": true,
          "retryable": null,
          "sensitiveHeaders": [
          ],
          "customSensitiveHeaders": false,
          "location": "SVC_PATH"
        }
      }

As you can see, SVC_URL and SVC_PATH look like 2 new service route entries.

question

All 6 comments

I have not tried this myself, but have you tried doing something like

zuul:
  ignoredServices: '*'
  routes:
    svc:
      path: ${ZUUL_ROUTES_SVC_PATH:/svc/**}
      url: ${ZUUL_ROUTES_SVC_URL:http://svc-test/api/v1/}

Zuul will still see SVC_PATH and SVC_URL as route services.
At the end, I would like to add routes dynamically through env. variables, without having to update the core configuration.

As mentioned above, ZUUL_ROUTES_SVC_PATH=my_value environment variable does update the value of the config tree

zuul:
  routes:
    svc:
      path: my_value

but it also creates a new routes, which trips zuul.

I would like to define routes like this:
export ZUUL_ROUTES_NEWSERVICE1_URL=something
export ZUUL_ROUTES_NEWSERVICE1_PATH=something
export ZUUL_ROUTES_NEWSERVICE2_URL=something2
export ZUUL_ROUTES_NEWSERVICE2_PATH=something2

I think I am missing something, how does Zuul even know about those environment variables?

Any @ConfigurationProperties can be populated by environment variables. Unfortunately, zuul.routes is a map, which, in boot 1.x.x, have a hard time. I'd recommend using SPRING_APPLICATION_JSON.

With SPRING_APPLICATION_JSON='{"zuul":{"routes":{"svc":{"url":"http://localhost:8080/api/v1/"}}}} as env. var, I was able to override the url in the application.yaml file - great!

Ah I see, I didnt know that, thanks @spencergibb!

Was this page helpful?
0 / 5 - 0 ratings