Embedded Zuul Reverse Proxy

spring Cloud has created an implant Zuul proxy to ease the development of a common manipulation casing where a UI application wants to make proxy calls to one or more back end services. This feature is utilitarian for a user interface to proxy to the back end services it requires, avoiding the need to manage CORS and authentication concerns independently for all the back ends .
To enable it, annotate a give Boot main class with @EnableZuulProxy. Doing so lawsuit local calls to be forwarded to the appropriate service. By convention, a service with an ID of users receives requests from the proxy located at /users ( with the prefix stripped ). The proxy uses Ribbon to locate an case to which to forward through discovery. All requests are executed in a hystrix command, so failure appear in Hystrix metrics. Once the racing circuit is candid, the proxy does not try to contact the avail .

the Zuul starter does not include a discovery client, so, for routes based on service IDs, you need to provide one of those on the classpath as well (Eureka is one choice).

To skip having a service automatically added, set zuul.ignored-services to a list of service ID patterns. If a service matches a convention that is ignored but is besides included in the explicitly configured routes map, it is unignored, as shown in the following example :
application.yml

 zuul:
  ignoredServices: '*'
  routes:
    users: /myusers/**

In the precede example, all services are ignored, except for users .
To augment or change the proxy routes, you can add external configuration, as follows :
application.yml

 zuul:
  routes:
    users: /myusers/**

The preceding example means that HTTP calls to /myusers get forwarded to the users servicing ( for case /myusers/101 is forwarded to /101 ) .
To get more powdered control over a path, you can specify the way and the serviceId independently, as follows :
application.yml

 zuul:
  routes:
    users:
      path: /myusers/**
      serviceId: users_service

The preceding exemplar means that HTTP calls to /myusers get forwarded to the users_service overhaul. The road must have a path that can be specified as an ant-style convention, so /myusers/* merely matches one horizontal surface, but /myusers/** matches hierarchically .
The location of the back goal can be specified as either a serviceId ( for a service from discovery ) or a url ( for a forcible placement ), as shown in the adopt exercise :
application.yml

 zuul:
  routes:
    users:
      path: /myusers/**
      url: https://example.com/users_service

These simple url-routes do not get executed as a HystrixCommand, nor do they load-balance multiple URLs with Ribbon. To achieve those goals, you can specify a serviceId with a static list of servers, as follows :
application.yml

zuul:
  routes:
    echo:
      path: /myusers/**
      serviceId: myusers-service
      stripPrefix: true

hystrix:
  command:
    myusers-service:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: ...

myusers-service:
  ribbon:
    NIWSServerListClassName: com.netflix.loadbalancer.ConfigurationBasedServerList
    listOfServers: https://example1.com,http://example2.com
    ConnectTimeout: 1000
    ReadTimeout: 3000
    MaxTotalHttpConnections: 500
    MaxConnectionsPerHost: 100

Another method acting is specifiying a service-route and configuring a Ribbon customer for the serviceId ( doing thus requires disabling Eureka support in Ribbon — see above for more data ), as shown in the take after case :

application.yml

zuul:
  routes:
    users:
      path: /myusers/**
      serviceId: users

ribbon:
  eureka:
    enabled: false

users:
  ribbon:
    listOfServers: example.com,google.com

You can provide a convention between serviceId and routes by using regexmapper. It uses regular-expression named groups to extract variables from serviceId and inject them into a road blueprint, as shown in the keep up model :
ApplicationConfiguration.java

@Bean
public PatternServiceRouteMapper serviceRouteMapper() {
    return new PatternServiceRouteMapper(
        "(?^.+)-(?v.+$)",
        "${version}/${name}");
}

The preceding example means that a serviceId of myusers-v1 is mapped to route /v1/myusers/**. Any regular formulation is accepted, but all named groups must be portray in both servicePattern and routePattern. If servicePattern does not match a serviceId, the default behavior is used. In the preceding model, a serviceId of myusers is mapped to the “ /myusers/** ” road ( with no version detected ). This feature is disabled by default option and only applies to discovered services .
To add a prefix to all mappings, set zuul.prefix to a rate, such as /api. By default, the proxy prefix is stripped from the request before the request is forwarded by ( you can switch this behavior off with zuul.stripPrefix=false ). You can besides switch off the denude of the service-specific prefix from individual routes, as shown in the follow exemplar :
application.yml

 zuul:
  routes:
    users:
      path: /myusers/**
      stripPrefix: false
zuul.stripPrefix only applies to the prefix set in zuul.prefix.
It does not have any effect on prefixes defined within a given route’s path.

In the preceding case, requests to /myusers/101 are forwarded to /myusers/101 on the users service .
The zuul.routes entries actually bind to an object of type ZuulProperties. If you look at the properties of that object, you can see that it besides has a retryable flag. Set that flag to true to have the Ribbon client automatically retry failed requests. You can besides set that flag to true when you need to modify the parameters of the rehear operations that use the Ribbon node configuration .
By default, the X-Forwarded-Host header is added to the forward requests. To turn it off, set zuul.addProxyHeaders = false. By default, the prefix path is stripped, and the request to the binding end picks up a X-Forwarded-Prefix heading ( /myusers in the examples shown earlier ) .
If you set a default road ( / ), an application with @EnableZuulProxy could act as a standalone waiter. For example, zuul.route.home: / would route all traffic ( “ /** ” ) to the “ home ” service .
If more powdered neglect is needed, you can specify specific patterns to ignore. These patterns are evaluated at the start of the route placement process, which means prefixes should be included in the practice to warrant a match. Ignored patterns span all services and supersede any other route stipulation. The trace exemplar shows how to create ignored patterns :
application.yml

 zuul:
  ignoredPatterns: /**/admin/**
  routes:
    users: /myusers/**

The preceding case means that all calls ( such as /myusers/101 ) are forwarded to /101 on the users serve. however, calls including /admin/ do not resolve .

If you need your routes to have their order preserved, you need to use a YAML file, as the ordering is lost when using a properties file.
The following example shows such a YAML file:

application.yml

 zuul:
  routes:
    users:
      path: /myusers/**
    legacy:
      path: /**

If you were to use a properties file, the legacy path might end up in front of the users path, rendering the users way unapproachable .

Leave a Reply

Your email address will not be published.