1072 lines
190 KiB
HTML
1072 lines
190 KiB
HTML
<html><head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
|
<title>Spring Cloud Netflix</title><link rel="stylesheet" type="text/css" href="css/manual-singlepage.css"><meta name="generator" content="DocBook XSL Stylesheets V1.79.1"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div lang="en" class="book"><div class="titlepage"><div><div><h1 class="title"><a name="d0e3"></a>Spring Cloud Netflix</h1></div></div><hr></div><div class="toc"><p><b>Table of Contents</b></p><dl class="toc"><dt><span class="preface"><a href="#d0e9"></a></span></dt><dt><span class="chapter"><a href="#_service_discovery_eureka_clients">1. Service Discovery: Eureka Clients</a></span></dt><dd><dl><dt><span class="section"><a href="#netflix-eureka-client-starter">1.1. How to Include Eureka Client</a></span></dt><dt><span class="section"><a href="#_registering_with_eureka">1.2. Registering with Eureka</a></span></dt><dt><span class="section"><a href="#_authenticating_with_the_eureka_server">1.3. Authenticating with the Eureka Server</a></span></dt><dt><span class="section"><a href="#_status_page_and_health_indicator">1.4. Status Page and Health Indicator</a></span></dt><dt><span class="section"><a href="#_registering_a_secure_application">1.5. Registering a Secure Application</a></span></dt><dt><span class="section"><a href="#_eurekas_health_checks">1.6. Eureka’s Health Checks</a></span></dt><dt><span class="section"><a href="#_eureka_metadata_for_instances_and_clients">1.7. Eureka Metadata for Instances and Clients</a></span></dt><dd><dl><dt><span class="section"><a href="#_using_eureka_on_cloud_foundry">1.7.1. Using Eureka on Cloud Foundry</a></span></dt><dt><span class="section"><a href="#_using_eureka_on_aws">1.7.2. Using Eureka on AWS</a></span></dt><dt><span class="section"><a href="#_changing_the_eureka_instance_id">1.7.3. Changing the Eureka Instance ID</a></span></dt></dl></dd><dt><span class="section"><a href="#_using_the_eurekaclient">1.8. Using the EurekaClient</a></span></dt><dd><dl><dt><span class="section"><a href="#_eurekaclient_without_jersey">1.8.1. EurekaClient without Jersey</a></span></dt></dl></dd><dt><span class="section"><a href="#_alternatives_to_the_native_netflix_eurekaclient">1.9. Alternatives to the Native Netflix EurekaClient</a></span></dt><dt><span class="section"><a href="#_why_is_it_so_slow_to_register_a_service">1.10. Why Is It so Slow to Register a Service?</a></span></dt><dt><span class="section"><a href="#_zones">1.11. Zones</a></span></dt></dl></dd><dt><span class="chapter"><a href="#spring-cloud-eureka-server">2. Service Discovery: Eureka Server</a></span></dt><dd><dl><dt><span class="section"><a href="#netflix-eureka-server-starter">2.1. How to Include Eureka Server</a></span></dt><dt><span class="section"><a href="#spring-cloud-running-eureka-server">2.2. How to Run a Eureka Server</a></span></dt><dt><span class="section"><a href="#spring-cloud-eureka-server-zones-and-regions">2.3. High Availability, Zones and Regions</a></span></dt><dt><span class="section"><a href="#spring-cloud-eureka-server-standalone-mode">2.4. Standalone Mode</a></span></dt><dt><span class="section"><a href="#spring-cloud-eureka-server-peer-awareness">2.5. Peer Awareness</a></span></dt><dt><span class="section"><a href="#spring-cloud-eureka-server-prefer-ip-address">2.6. When to Prefer IP Address</a></span></dt><dt><span class="section"><a href="#_securing_the_eureka_server">2.7. Securing The Eureka Server</a></span></dt></dl></dd><dt><span class="chapter"><a href="#_circuit_breaker_hystrix_clients">3. Circuit Breaker: Hystrix Clients</a></span></dt><dd><dl><dt><span class="section"><a href="#_how_to_include_hystrix">3.1. How to Include Hystrix</a></span></dt><dt><span class="section"><a href="#netflix-hystrix-starter">3.2. Propagating the Security Context or Using Spring Scopes</a></span></dt><dt><span class="section"><a href="#_health_indicator">3.3. Health Indicator</a></span></dt><dt><span class="section"><a href="#_hystrix_metrics_stream">3.4. Hystrix Metrics Stream</a></span></dt></dl></dd><dt><span class="chapter"><a href="#_circuit_breaker_hystrix_dashboard">4. Circuit Breaker: Hystrix Dashboard</a></span></dt><dt><span class="chapter"><a href="#_hystrix_timeouts_and_ribbon_clients">5. Hystrix Timeouts And Ribbon Clients</a></span></dt><dd><dl><dt><span class="section"><a href="#netflix-hystrix-dashboard-starter">5.1. How to Include the Hystrix Dashboard</a></span></dt><dt><span class="section"><a href="#_turbine">5.2. Turbine</a></span></dt><dd><dl><dt><span class="section"><a href="#_clusters_endpoint">5.2.1. Clusters Endpoint</a></span></dt></dl></dd><dt><span class="section"><a href="#_turbine_stream">5.3. Turbine Stream</a></span></dt></dl></dd><dt><span class="chapter"><a href="#spring-cloud-ribbon">6. Client Side Load Balancer: Ribbon</a></span></dt><dd><dl><dt><span class="section"><a href="#netflix-ribbon-starter">6.1. How to Include Ribbon</a></span></dt><dt><span class="section"><a href="#_customizing_the_ribbon_client">6.2. Customizing the Ribbon Client</a></span></dt><dt><span class="section"><a href="#_customizing_the_default_for_all_ribbon_clients">6.3. Customizing the Default for All Ribbon Clients</a></span></dt><dt><span class="section"><a href="#_customizing_the_ribbon_client_by_setting_properties">6.4. Customizing the Ribbon Client by Setting Properties</a></span></dt><dt><span class="section"><a href="#_using_ribbon_with_eureka">6.5. Using Ribbon with Eureka</a></span></dt><dt><span class="section"><a href="#spring-cloud-ribbon-without-eureka">6.6. Example: How to Use Ribbon Without Eureka</a></span></dt><dt><span class="section"><a href="#_example_disable_eureka_use_in_ribbon">6.7. Example: Disable Eureka Use in Ribbon</a></span></dt><dt><span class="section"><a href="#_using_the_ribbon_api_directly">6.8. Using the Ribbon API Directly</a></span></dt><dt><span class="section"><a href="#ribbon-child-context-eager-load">6.9. Caching of Ribbon Configuration</a></span></dt><dt><span class="section"><a href="#how-to-configure-hystrix-thread-pools">6.10. How to Configure Hystrix Thread Pools</a></span></dt><dt><span class="section"><a href="#how-to-provdie-a-key-to-ribbon">6.11. How to Provide a Key to Ribbon’s <code class="literal">IRule</code></a></span></dt></dl></dd><dt><span class="chapter"><a href="#_external_configuration_archaius">7. External Configuration: Archaius</a></span></dt><dt><span class="chapter"><a href="#_router_and_filter_zuul">8. Router and Filter: Zuul</a></span></dt><dd><dl><dt><span class="section"><a href="#netflix-zuul-starter">8.1. How to Include Zuul</a></span></dt><dt><span class="section"><a href="#netflix-zuul-reverse-proxy">8.2. Embedded Zuul Reverse Proxy</a></span></dt><dt><span class="section"><a href="#_zuul_http_client">8.3. Zuul Http Client</a></span></dt><dt><span class="section"><a href="#_cookies_and_sensitive_headers">8.4. Cookies and Sensitive Headers</a></span></dt><dt><span class="section"><a href="#_ignored_headers">8.5. Ignored Headers</a></span></dt><dt><span class="section"><a href="#_management_endpoints">8.6. Management Endpoints</a></span></dt><dd><dl><dt><span class="section"><a href="#_routes_endpoint">8.6.1. Routes Endpoint</a></span></dt><dt><span class="section"><a href="#_filters_endpoint">8.6.2. Filters Endpoint</a></span></dt></dl></dd><dt><span class="section"><a href="#_strangulation_patterns_and_local_forwards">8.7. Strangulation Patterns and Local Forwards</a></span></dt><dt><span class="section"><a href="#_uploading_files_through_zuul">8.8. Uploading Files through Zuul</a></span></dt><dt><span class="section"><a href="#_query_string_encoding">8.9. Query String Encoding</a></span></dt><dt><span class="section"><a href="#_plain_embedded_zuul">8.10. Plain Embedded Zuul</a></span></dt><dt><span class="section"><a href="#_disable_zuul_filters">8.11. Disable Zuul Filters</a></span></dt><dt><span class="section"><a href="#hystrix-fallbacks-for-routes">8.12. Providing Hystrix Fallbacks For Routes</a></span></dt><dt><span class="section"><a href="#_zuul_timeouts">8.13. Zuul Timeouts</a></span></dt><dt><span class="section"><a href="#zuul-redirect-location-rewrite">8.14. Rewriting the <code class="literal">Location</code> header</a></span></dt><dt><span class="section"><a href="#_metrics">8.15. Metrics</a></span></dt><dt><span class="section"><a href="#zuul-developer-guide">8.16. Zuul Developer Guide</a></span></dt><dd><dl><dt><span class="section"><a href="#_the_zuul_servlet">8.16.1. The Zuul Servlet</a></span></dt><dt><span class="section"><a href="#_zuul_requestcontext">8.16.2. Zuul RequestContext</a></span></dt><dt><span class="section"><a href="#_enablezuulproxy_vs_enablezuulserver">8.16.3. <code class="literal">@EnableZuulProxy</code> vs. <code class="literal">@EnableZuulServer</code></a></span></dt><dt><span class="section"><a href="#zuul-developer-guide-enable-filters">8.16.4. <code class="literal">@EnableZuulServer</code> Filters</a></span></dt><dt><span class="section"><a href="#_enablezuulproxy_filters">8.16.5. <code class="literal">@EnableZuulProxy</code> Filters</a></span></dt><dt><span class="section"><a href="#_custom_zuul_filter_examples">8.16.6. Custom Zuul Filter Examples</a></span></dt><dd><dl><dt><span class="section"><a href="#zuul-developer-guide-sample-pre-filter">How to Write a Pre Filter</a></span></dt><dt><span class="section"><a href="#zuul-developer-guide-sample-route-filter">How to Write a Route Filter</a></span></dt><dt><span class="section"><a href="#zuul-developer-guide-sample-post-filter">How to Write a Post Filter</a></span></dt></dl></dd><dt><span class="section"><a href="#_how_zuul_errors_work">8.16.7. How Zuul Errors Work</a></span></dt><dt><span class="section"><a href="#_zuul_eager_application_context_loading">8.16.8. Zuul Eager Application Context Loading</a></span></dt></dl></dd></dl></dd><dt><span class="chapter"><a href="#_polyglot_support_with_sidecar">9. Polyglot support with Sidecar</a></span></dt><dt><span class="chapter"><a href="#retrying-failed-requests">10. Retrying Failed Requests</a></span></dt><dd><dl><dt><span class="section"><a href="#_backoff_policies">10.1. BackOff Policies</a></span></dt><dt><span class="section"><a href="#_configuration">10.2. Configuration</a></span></dt><dd><dl><dt><span class="section"><a href="#_zuul">10.2.1. Zuul</a></span></dt></dl></dd></dl></dd><dt><span class="chapter"><a href="#_http_clients">11. HTTP Clients</a></span></dt></dl></div><div class="preface"><div class="titlepage"><div><div><h1 class="title"><a name="d0e9" href="#d0e9"></a></h1></div></div></div><p><span class="strong"><strong>2.1.0.M2</strong></span></p><p>This project provides Netflix OSS integrations for Spring Boot apps through autoconfiguration
|
|
and binding to the Spring Environment and other Spring programming model idioms. With a few
|
|
simple annotations you can quickly enable and configure the common patterns inside your
|
|
application and build large distributed systems with battle-tested Netflix components. The
|
|
patterns provided include Service Discovery (Eureka), Circuit Breaker (Hystrix),
|
|
Intelligent Routing (Zuul) and Client Side Load Balancing (Ribbon).</p></div><div class="chapter"><div class="titlepage"><div><div><h1 class="title"><a name="_service_discovery_eureka_clients" href="#_service_discovery_eureka_clients"></a>1. Service Discovery: Eureka Clients</h1></div></div></div><p>Service Discovery is one of the key tenets of a microservice-based architecture.
|
|
Trying to hand-configure each client or some form of convention can be difficult to do and can be brittle.
|
|
Eureka is the Netflix Service Discovery Server and Client.
|
|
The server can be configured and deployed to be highly available, with each server replicating state about the registered services to the others.</p><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="netflix-eureka-client-starter" href="#netflix-eureka-client-starter"></a>1.1 How to Include Eureka Client</h2></div></div></div><p>To include the Eureka Client in your project, use the starter with a group ID of <code class="literal">org.springframework.cloud</code> and an artifact ID of <code class="literal">spring-cloud-starter-netflix-eureka-client</code>.
|
|
See the <a class="link" href="http://projects.spring.io/spring-cloud/" target="_top">Spring Cloud Project page</a> for details on setting up your build system with the current Spring Cloud Release Train.</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_registering_with_eureka" href="#_registering_with_eureka"></a>1.2 Registering with Eureka</h2></div></div></div><p>When a client registers with Eureka, it provides meta-data about itself — such as host, port, health indicator URL, home page, and other details.
|
|
Eureka receives heartbeat messages from each instance belonging to a service.
|
|
If the heartbeat fails over a configurable timetable, the instance is normally removed from the registry.</p><p>The following example shows a minimal Eureka client application:</p><pre class="programlisting"><em><span class="hl-annotation" style="color: gray">@SpringBootApplication</span></em>
|
|
<em><span class="hl-annotation" style="color: gray">@RestController</span></em>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">class</span> Application {
|
|
|
|
<em><span class="hl-annotation" style="color: gray">@RequestMapping("/")</span></em>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> String home() {
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">return</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"Hello world"</span>;
|
|
}
|
|
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">static</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">void</span> main(String[] args) {
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">new</span> SpringApplicationBuilder(Application.<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">class</span>).web(true).run(args);
|
|
}
|
|
|
|
}</pre><p>Note that the preceding example shows a normal <a class="link" href="https://projects.spring.io/spring-boot/" target="_top">Spring Boot</a> application.
|
|
By having <code class="literal">spring-cloud-starter-netflix-eureka-client</code> on the classpath, your application automatically registers with the Eureka Server. Configuration is required to locate the Eureka server, as shown in the following example:</p><p><b>application.yml. </b>
|
|
</p><pre class="screen">eureka:
|
|
client:
|
|
serviceUrl:
|
|
defaultZone: http://localhost:8761/eureka/</pre><p>
|
|
</p><p>In the preceding example, "defaultZone" is a magic string fallback value that provides the service URL for any client that does not express a preference (in other words, it is a useful default).</p><p>The default application name (that is, the service ID), virtual host, and non-secure port (taken from the <code class="literal">Environment</code>) are <code class="literal">${spring.application.name}</code>, <code class="literal">${spring.application.name}</code> and <code class="literal">${server.port}</code>, respectively.</p><p>Having <code class="literal">spring-cloud-starter-netflix-eureka-client</code> on the classpath makes the app into both a Eureka <span class="quote">“<span class="quote">instance</span>”</span> (that is, it registers itself) and a <span class="quote">“<span class="quote">client</span>”</span> (it can query the registry to locate other services).
|
|
The instance behaviour is driven by <code class="literal">eureka.instance.*</code> configuration keys, but the defaults are fine if you ensure that your application has a value for <code class="literal">spring.application.name</code> (this is the default for the Eureka service ID or VIP).</p><p>See <a class="link" href="http://github.com/spring-cloud/spring-cloud-netflix/tree/master/spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/EurekaInstanceConfigBean.java" target="_top">EurekaInstanceConfigBean</a> and <a class="link" href="http://github.com/spring-cloud/spring-cloud-netflix/tree/master/spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/EurekaClientConfigBean.java" target="_top">EurekaClientConfigBean</a> for more details on the configurable options.</p><p>To disable the Eureka Discovery Client, you can set <code class="literal">eureka.client.enabled</code> to <code class="literal">false</code>.</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_authenticating_with_the_eureka_server" href="#_authenticating_with_the_eureka_server"></a>1.3 Authenticating with the Eureka Server</h2></div></div></div><p>HTTP basic authentication is automatically added to your eureka client if one of the <code class="literal">eureka.client.serviceUrl.defaultZone</code> URLs has credentials embedded in it (curl style, as follows: <code class="literal"><a class="link" href="http://user:password@localhost:8761/eureka" target="_top">http://user:password@localhost:8761/eureka</a></code>).
|
|
For more complex needs, you can create a <code class="literal">@Bean</code> of type <code class="literal">DiscoveryClientOptionalArgs</code> and inject <code class="literal">ClientFilter</code> instances into it, all of which is applied to the calls from the client to the server.</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Note"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="images/note.png"></td><th align="left">Note</th></tr><tr><td align="left" valign="top"><p>Because of a limitation in Eureka, it is not possible to support per-server basic auth credentials, so only the first set that are found is used.</p></td></tr></table></div></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_status_page_and_health_indicator" href="#_status_page_and_health_indicator"></a>1.4 Status Page and Health Indicator</h2></div></div></div><p>The status page and health indicators for a Eureka instance default to <code class="literal">/info</code> and <code class="literal">/health</code> respectively, which are the default locations of useful endpoints in a Spring Boot Actuator application.
|
|
You need to change these, even for an Actuator application if you use a non-default context path or servlet path (such as <code class="literal">server.servletPath=/custom</code>). The following example shows the default values for the two settings:</p><p><b>application.yml. </b>
|
|
</p><pre class="screen">eureka:
|
|
instance:
|
|
statusPageUrlPath: ${server.servletPath}/info
|
|
healthCheckUrlPath: ${server.servletPath}/health</pre><p>
|
|
</p><p>These links show up in the metadata that is consumed by clients and are used in some scenarios to decide whether to send requests to your application, so it is helpful if they are accurate.</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Note"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="images/note.png"></td><th align="left">Note</th></tr><tr><td align="left" valign="top"><p>In Dalston it was also required to set the status and health check URLs when changing
|
|
that management context path. This requirement was removed beginning in Edgware.</p></td></tr></table></div></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_registering_a_secure_application" href="#_registering_a_secure_application"></a>1.5 Registering a Secure Application</h2></div></div></div><p>If your app wants to be contacted over HTTPS, you can set two flags in the <code class="literal">EurekaInstanceConfig</code>:</p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><code class="literal">eureka.instance.[nonSecurePortEnabled]=[false]</code></li><li class="listitem"><code class="literal">eureka.instance.[securePortEnabled]=[true]</code></li></ul></div><p>Doing so makes Eureka publish instance information that shows an explicit preference for secure communication.
|
|
The Spring Cloud <code class="literal">DiscoveryClient</code> always returns a URI starting with <code class="literal">https</code> for a service configured this way.
|
|
Similarly, when a service is configured this way, the Eureka (native) instance information has a secure health check URL.</p><p>Because of the way Eureka works internally, it still publishes a non-secure URL for the status and home pages unless you also override those explicitly.
|
|
You can use placeholders to configure the eureka instance URLs, as shown in the following example:</p><p><b>application.yml. </b>
|
|
</p><pre class="screen">eureka:
|
|
instance:
|
|
statusPageUrl: https://${eureka.hostname}/info
|
|
healthCheckUrl: https://${eureka.hostname}/health
|
|
homePageUrl: https://${eureka.hostname}/</pre><p>
|
|
</p><p>(Note that <code class="literal">${eureka.hostname}</code> is a native placeholder only available
|
|
in later versions of Eureka. You could achieve the same thing with
|
|
Spring placeholders as well — for example, by using <code class="literal">${eureka.instance.hostName}</code>.)</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Note"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="images/note.png"></td><th align="left">Note</th></tr><tr><td align="left" valign="top"><p>If your application runs behind a proxy, and the SSL termination is in the proxy (for example, if you run in Cloud Foundry or other platforms as a service), then you need to ensure that the proxy <span class="quote">“<span class="quote">forwarded</span>”</span> headers are intercepted and handled by the application.
|
|
If the Tomcat container embedded in a Spring Boot application has explicit configuration for the 'X-Forwarded-\*` headers, this happens automatically.
|
|
The links rendered by your app to itself being wrong (the wrong host, port, or protocol) is a sign that you got this configuration wrong.</p></td></tr></table></div></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_eurekas_health_checks" href="#_eurekas_health_checks"></a>1.6 Eureka’s Health Checks</h2></div></div></div><p>By default, Eureka uses the client heartbeat to determine if a client is up.
|
|
Unless specified otherwise, the Discovery Client does not propagate the current health check status of the application, per the Spring Boot Actuator.
|
|
Consequently, after successful registration, Eureka always announces that the application is in 'UP' state. This behavior can be altered by enabling Eureka health checks, which results in propagating application status to Eureka.
|
|
As a consequence, every other application does not send traffic to applications in states other then 'UP'.
|
|
The following example shows how to enable health checks for the client:</p><p><b>application.yml. </b>
|
|
</p><pre class="screen">eureka:
|
|
client:
|
|
healthcheck:
|
|
enabled: true</pre><p>
|
|
</p><div class="warning" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Warning"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Warning]" src="images/warning.png"></td><th align="left">Warning</th></tr><tr><td align="left" valign="top"><p><code class="literal">eureka.client.healthcheck.enabled=true</code> should only be set in <code class="literal">application.yml</code>. Setting the value in <code class="literal">bootstrap.yml</code> causes undesirable side effects, such as registering in Eureka with an <code class="literal">UNKNOWN</code> status.</p></td></tr></table></div><p>If you require more control over the health checks, consider implementing your own <code class="literal">com.netflix.appinfo.HealthCheckHandler</code>.</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_eureka_metadata_for_instances_and_clients" href="#_eureka_metadata_for_instances_and_clients"></a>1.7 Eureka Metadata for Instances and Clients</h2></div></div></div><p>It is worth spending a bit of time understanding how the Eureka metadata works, so you can use it in a way that makes sense in your platform.
|
|
There is standard metadata for information such as hostname, IP address, port numbers, the status page, and health check.
|
|
These are published in the service registry and used by clients to contact the services in a straightforward way.
|
|
Additional metadata can be added to the instance registration in the <code class="literal">eureka.instance.metadataMap</code>, and this metadata is accessible in the remote clients.
|
|
In general, additional metadata does not change the behavior of the client, unless the client is made aware of the meaning of the metadata.
|
|
There are a couple of special cases, described later in this document, where Spring Cloud already assigns meaning to the metadata map.</p><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="_using_eureka_on_cloud_foundry" href="#_using_eureka_on_cloud_foundry"></a>1.7.1 Using Eureka on Cloud Foundry</h3></div></div></div><p>Cloud Foundry has a global router so that all instances of the same app have the same hostname (other PaaS solutions with a similar architecture have the same arrangement).
|
|
This is not necessarily a barrier to using Eureka.
|
|
However, if you use the router (recommended or even mandatory, depending on the way your platform was set up), you need to explicitly set the hostname and port numbers (secure or non-secure) so that they use the router.
|
|
You might also want to use instance metadata so that you can distinguish between the instances on the client (for example, in a custom load balancer).
|
|
By default, the <code class="literal">eureka.instance.instanceId</code> is <code class="literal">vcap.application.instance_id</code>, as shown in the following example:</p><p><b>application.yml. </b>
|
|
</p><pre class="screen">eureka:
|
|
instance:
|
|
hostname: ${vcap.application.uris[0]}
|
|
nonSecurePort: 80</pre><p>
|
|
</p><p>Depending on the way the security rules are set up in your Cloud Foundry instance, you might be able to register and use the IP address of the host VM for direct service-to-service calls.
|
|
This feature is not yet available on Pivotal Web Services (<a class="link" href="https://run.pivotal.io" target="_top">PWS</a>).</p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="_using_eureka_on_aws" href="#_using_eureka_on_aws"></a>1.7.2 Using Eureka on AWS</h3></div></div></div><p>If the application is planned to be deployed to an AWS cloud, the Eureka instance must be configured to be AWS-aware. You can do so by customizing the <a class="link" href="http://github.com/spring-cloud/spring-cloud-netflix/tree/master/spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/EurekaInstanceConfigBean.java" target="_top">EurekaInstanceConfigBean</a> as follows:</p><pre class="programlisting"><em><span class="hl-annotation" style="color: gray">@Bean</span></em>
|
|
<em><span class="hl-annotation" style="color: gray">@Profile("!default")</span></em>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> EurekaInstanceConfigBean eurekaInstanceConfig(InetUtils inetUtils) {
|
|
EurekaInstanceConfigBean b = <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">new</span> EurekaInstanceConfigBean(inetUtils);
|
|
AmazonInfo info = AmazonInfo.Builder.newBuilder().autoBuild(<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"eureka"</span>);
|
|
b.setDataCenterInfo(info);
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">return</span> b;
|
|
}</pre></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="_changing_the_eureka_instance_id" href="#_changing_the_eureka_instance_id"></a>1.7.3 Changing the Eureka Instance ID</h3></div></div></div><p>A vanilla Netflix Eureka instance is registered with an ID that is equal to its host name (that is, there is only one service per host).
|
|
Spring Cloud Eureka provides a sensible default, which is defined as follows:</p><p><code class="literal">${spring.cloud.client.hostname}:${spring.application.name}:${spring.application.instance_id:${server.port}}}</code></p><p>An example is <code class="literal">myhost:myappname:8080</code>.</p><p>By using Spring Cloud, you can override this value by providing a unique identifier in <code class="literal">eureka.instance.instanceId</code>, as shown in the following example:</p><p><b>application.yml. </b>
|
|
</p><pre class="screen">eureka:
|
|
instance:
|
|
instanceId: ${spring.application.name}:${vcap.application.instance_id:${spring.application.instance_id:${random.value}}}</pre><p>
|
|
</p><p>With the metadata shown in the preceding example and multiple service instances deployed on localhost, the random value is inserted there to make the instance unique.
|
|
In Cloud Foundry, the <code class="literal">vcap.application.instance_id</code> is populated automatically in a Spring Boot application, so the random value is not needed.</p></div></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_using_the_eurekaclient" href="#_using_the_eurekaclient"></a>1.8 Using the EurekaClient</h2></div></div></div><p>Once you have an application that is a discovery client, you can use it to discover service instances from the <a class="link" href="#spring-cloud-eureka-server" title="2. Service Discovery: Eureka Server">Eureka Server</a>.
|
|
One way to do so is to use the native <code class="literal">com.netflix.discovery.EurekaClient</code> (as opposed to the Spring Cloud <code class="literal">DiscoveryClient</code>), as shown in the following example:</p><pre class="screen">@Autowired
|
|
private EurekaClient discoveryClient;
|
|
|
|
public String serviceUrl() {
|
|
InstanceInfo instance = discoveryClient.getNextServerFromEureka("STORES", false);
|
|
return instance.getHomePageUrl();
|
|
}</pre><div class="tip" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Tip"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Tip]" src="images/tip.png"></td><th align="left">Tip</th></tr><tr><td align="left" valign="top"><p>Do not use the <code class="literal">EurekaClient</code> in a <code class="literal">@PostConstruct</code> method or in a <code class="literal">@Scheduled</code> method (or anywhere where the <code class="literal">ApplicationContext</code> might not be started yet).
|
|
It is initialized in a <code class="literal">SmartLifecycle</code> (with <code class="literal">phase=0</code>), so the earliest you can rely on it being available is in another <code class="literal">SmartLifecycle</code> with a higher phase.</p></td></tr></table></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="_eurekaclient_without_jersey" href="#_eurekaclient_without_jersey"></a>1.8.1 EurekaClient without Jersey</h3></div></div></div><p>By default, EurekaClient uses Jersey for HTTP communication.
|
|
If you wish to avoid dependencies from Jersey, you can exclude it from your dependencies.
|
|
Spring Cloud auto-configures a transport client based on Spring <code class="literal">RestTemplate</code>.
|
|
The following example shows Jersey being excluded:</p><pre class="screen"><dependency>
|
|
<groupId>org.springframework.cloud</groupId>
|
|
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
|
|
<exclusions>
|
|
<exclusion>
|
|
<groupId>com.sun.jersey</groupId>
|
|
<artifactId>jersey-client</artifactId>
|
|
</exclusion>
|
|
<exclusion>
|
|
<groupId>com.sun.jersey</groupId>
|
|
<artifactId>jersey-core</artifactId>
|
|
</exclusion>
|
|
<exclusion>
|
|
<groupId>com.sun.jersey.contribs</groupId>
|
|
<artifactId>jersey-apache-client4</artifactId>
|
|
</exclusion>
|
|
</exclusions>
|
|
</dependency></pre></div></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_alternatives_to_the_native_netflix_eurekaclient" href="#_alternatives_to_the_native_netflix_eurekaclient"></a>1.9 Alternatives to the Native Netflix EurekaClient</h2></div></div></div><p>You need not use the raw Netflix <code class="literal">EurekaClient</code>.
|
|
Also, it is usually more convenient to use it behind a wrapper of some sort.
|
|
Spring Cloud has support for <a class="link" href="#">Feign</a> (a REST client builder) and <a class="link" href="#spring-cloud-ribbon" title="6. Client Side Load Balancer: Ribbon">Spring <code class="literal">RestTemplate</code></a> through the logical Eureka service identifiers (VIPs) instead of physical URLs.
|
|
To configure Ribbon with a fixed list of physical servers, you can set <code class="literal"><client>.ribbon.listOfServers</code> to a comma-separated list of physical addresses (or hostnames), where <code class="literal"><client></code> is the ID of the client.</p><p>You can also use the <code class="literal">org.springframework.cloud.client.discovery.DiscoveryClient</code>, which provides a simple API (not specific to Netflix) for discovery clients, as shown in the following example:</p><pre class="screen">@Autowired
|
|
private DiscoveryClient discoveryClient;
|
|
|
|
public String serviceUrl() {
|
|
List<ServiceInstance> list = discoveryClient.getInstances("STORES");
|
|
if (list != null && list.size() > 0 ) {
|
|
return list.get(0).getUri();
|
|
}
|
|
return null;
|
|
}</pre></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_why_is_it_so_slow_to_register_a_service" href="#_why_is_it_so_slow_to_register_a_service"></a>1.10 Why Is It so Slow to Register a Service?</h2></div></div></div><p>Being an instance also involves a periodic heartbeat to the registry
|
|
(through the client’s <code class="literal">serviceUrl</code>) with a default duration of 30 seconds.
|
|
A service is not available for discovery by clients until the instance, the server, and the client all have the same metadata in their local
|
|
cache (so it could take 3 heartbeats).
|
|
You can change the period by setting <code class="literal">eureka.instance.leaseRenewalIntervalInSeconds</code>.
|
|
Setting it to a value of less than 30 speeds up the process of getting clients connected to other services.
|
|
In production, it is probably better to stick with the default, because of internal computations in the server that make assumptions about the lease renewal period.</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_zones" href="#_zones"></a>1.11 Zones</h2></div></div></div><p>If you have deployed Eureka clients to multiple zones, you may prefer that those clients use services within the same zone before trying services in another zone.
|
|
To set that up, you need to configure your Eureka clients correctly.</p><p>First, you need to make sure you have Eureka servers deployed to each zone and that
|
|
they are peers of each other.
|
|
See the section on <a class="link" href="#spring-cloud-eureka-server-zones-and-regions" title="2.3 High Availability, Zones and Regions">zones and regions</a>
|
|
for more information.</p><p>Next, you need to tell Eureka which zone your service is in.
|
|
You can do so by using the <code class="literal">metadataMap</code> property.
|
|
For example, if <code class="literal">service 1</code> is deployed to both <code class="literal">zone 1</code> and <code class="literal">zone 2</code>, you need to set the following Eureka properties in <code class="literal">service 1</code>:</p><p><span class="strong"><strong>Service 1 in Zone 1</strong></span></p><pre class="screen">eureka.instance.metadataMap.zone = zone1
|
|
eureka.client.preferSameZoneEureka = true</pre><p><span class="strong"><strong>Service 1 in Zone 2</strong></span></p><pre class="screen">eureka.instance.metadataMap.zone = zone2
|
|
eureka.client.preferSameZoneEureka = true</pre></div></div><div class="chapter"><div class="titlepage"><div><div><h1 class="title"><a name="spring-cloud-eureka-server" href="#spring-cloud-eureka-server"></a>2. Service Discovery: Eureka Server</h1></div></div></div><p>This section describes how to set up a Eureka server.</p><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="netflix-eureka-server-starter" href="#netflix-eureka-server-starter"></a>2.1 How to Include Eureka Server</h2></div></div></div><p>To include Eureka Server in your project, use the starter with a group ID of <code class="literal">org.springframework.cloud</code> and an artifact ID of <code class="literal">spring-cloud-starter-netflix-eureka-server</code>.
|
|
See the <a class="link" href="http://projects.spring.io/spring-cloud/" target="_top">Spring Cloud Project page</a> for details on setting up your build system with the current Spring Cloud Release Train.</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="spring-cloud-running-eureka-server" href="#spring-cloud-running-eureka-server"></a>2.2 How to Run a Eureka Server</h2></div></div></div><p>The following example shows a minimal Eureka server:</p><pre class="programlisting"><em><span class="hl-annotation" style="color: gray">@SpringBootApplication</span></em>
|
|
<em><span class="hl-annotation" style="color: gray">@EnableEurekaServer</span></em>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">class</span> Application {
|
|
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">static</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">void</span> main(String[] args) {
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">new</span> SpringApplicationBuilder(Application.<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">class</span>).web(true).run(args);
|
|
}
|
|
|
|
}</pre><p>The server has a home page with a UI and HTTP API endpoints for the normal Eureka functionality under <code class="literal">/eureka/*</code>.</p><p>The following links have some Eureka background reading: <a class="link" href="https://github.com/cfregly/fluxcapacitor/wiki/NetflixOSS-FAQ#eureka-service-discovery-load-balancer" target="_top">flux capacitor</a> and <a class="link" href="https://groups.google.com/forum/?fromgroups#!topic/eureka_netflix/g3p2r7gHnN0" target="_top">google group discussion</a>.</p><div class="tip" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Tip"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Tip]" src="images/tip.png"></td><th align="left">Tip</th></tr><tr><td align="left" valign="top"><p>Due to Gradle’s dependency resolution rules and the lack of a parent bom feature, depending on <code class="literal">spring-cloud-starter-netflix-eureka-server</code> can cause failures on application startup.
|
|
To remedy this issue, add the Spring Boot Gradle plugin and import the Spring cloud starter parent bom as follows:</p><p><b>build.gradle. </b>
|
|
</p><pre class="programlisting">buildscript {
|
|
dependencies {
|
|
classpath(<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"org.springframework.boot:spring-boot-gradle-plugin:{spring-boot-docs-version}"</span>)
|
|
}
|
|
}
|
|
|
|
apply plugin: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"spring-boot"</span>
|
|
|
|
dependencyManagement {
|
|
imports {
|
|
mavenBom <span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"org.springframework.cloud:spring-cloud-dependencies:{spring-cloud-version}"</span>
|
|
}
|
|
}</pre><p>
|
|
</p></td></tr></table></div></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="spring-cloud-eureka-server-zones-and-regions" href="#spring-cloud-eureka-server-zones-and-regions"></a>2.3 High Availability, Zones and Regions</h2></div></div></div><p>The Eureka server does not have a back end store, but the service instances in the registry all have to send heartbeats to keep their registrations up to date (so this can be done in memory).
|
|
Clients also have an in-memory cache of Eureka registrations (so they do not have to go to the registry for every request to a service).</p><p>By default, every Eureka server is also a Eureka client and requires (at least one) service URL to locate a peer.
|
|
If you do not provide it, the service runs and works, but it fills your logs with a lot of noise about not being able to register with the peer.</p><p>See also <a class="link" href="#spring-cloud-ribbon" title="6. Client Side Load Balancer: Ribbon">below for details of Ribbon support</a> on the client side for Zones and Regions.</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="spring-cloud-eureka-server-standalone-mode" href="#spring-cloud-eureka-server-standalone-mode"></a>2.4 Standalone Mode</h2></div></div></div><p>The combination of the two caches (client and server) and the heartbeats make a standalone Eureka server fairly resilient to failure, as long as there is some sort of monitor or elastic runtime (such as Cloud Foundry) keeping it alive.
|
|
In standalone mode, you might prefer to switch off the client side behavior so that it does not keep trying and failing to reach its peers.
|
|
The following example shows how to switch off the client-side behavior:</p><p><b>application.yml (Standalone Eureka Server). </b>
|
|
</p><pre class="screen">server:
|
|
port: 8761
|
|
|
|
eureka:
|
|
instance:
|
|
hostname: localhost
|
|
client:
|
|
registerWithEureka: false
|
|
fetchRegistry: false
|
|
serviceUrl:
|
|
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/</pre><p>
|
|
</p><p>Notice that the <code class="literal">serviceUrl</code> is pointing to the same host as the local instance.</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="spring-cloud-eureka-server-peer-awareness" href="#spring-cloud-eureka-server-peer-awareness"></a>2.5 Peer Awareness</h2></div></div></div><p>Eureka can be made even more resilient and available by running multiple instances and asking them to register with each other.
|
|
In fact, this is the default behavior, so all you need to do to make it work is add a valid <code class="literal">serviceUrl</code> to a peer, as shown in the following example:</p><p><b>application.yml (Two Peer Aware Eureka Servers). </b>
|
|
</p><pre class="screen">---
|
|
spring:
|
|
profiles: peer1
|
|
eureka:
|
|
instance:
|
|
hostname: peer1
|
|
client:
|
|
serviceUrl:
|
|
defaultZone: http://peer2/eureka/
|
|
|
|
---
|
|
spring:
|
|
profiles: peer2
|
|
eureka:
|
|
instance:
|
|
hostname: peer2
|
|
client:
|
|
serviceUrl:
|
|
defaultZone: http://peer1/eureka/</pre><p>
|
|
</p><p>In the preceding example, we have a YAML file that can be used to run the same server on two hosts (<code class="literal">peer1</code> and <code class="literal">peer2</code>) by running it in different Spring profiles.
|
|
You could use this configuration to test the peer awareness on a single host (there is not much value in doing that in production) by manipulating <code class="literal">/etc/hosts</code> to resolve the host names.
|
|
In fact, the <code class="literal">eureka.instance.hostname</code> is not needed if you are running on a machine that knows its own hostname (by default, it is looked up by using <code class="literal">java.net.InetAddress</code>).</p><p>You can add multiple peers to a system, and, as long as they are all connected to each other by at least one edge, they synchronize
|
|
the registrations amongst themselves.
|
|
If the peers are physically separated (inside a data center or between multiple data centers), then the system can, in principle, survive <span class="quote">“<span class="quote">split-brain</span>”</span> type failures.
|
|
You can add multiple peers to a system, and as long as they are all
|
|
directly connected to each other, they will synchronize
|
|
the registrations amongst themselves.</p><p><b>application.yml (Three Peer Aware Eureka Servers). </b>
|
|
</p><pre class="screen">eureka:
|
|
client:
|
|
serviceUrl:
|
|
defaultZone: http://peer1/eureka/,http://peer2/eureka/,http://peer3/eureka/
|
|
|
|
---
|
|
spring:
|
|
profiles: peer1
|
|
eureka:
|
|
instance:
|
|
hostname: peer1
|
|
|
|
---
|
|
spring:
|
|
profiles: peer2
|
|
eureka:
|
|
instance:
|
|
hostname: peer2
|
|
|
|
---
|
|
spring:
|
|
profiles: peer3
|
|
eureka:
|
|
instance:
|
|
hostname: peer3</pre><p>
|
|
</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="spring-cloud-eureka-server-prefer-ip-address" href="#spring-cloud-eureka-server-prefer-ip-address"></a>2.6 When to Prefer IP Address</h2></div></div></div><p>In some cases, it is preferable for Eureka to advertise the IP addresses of services rather than the hostname.
|
|
Set <code class="literal">eureka.instance.preferIpAddress</code> to <code class="literal">true</code> and, when the application registers with eureka, it uses its IP address rather than its hostname.</p><div class="tip" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Tip"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Tip]" src="images/tip.png"></td><th align="left">Tip</th></tr><tr><td align="left" valign="top"><p>If the hostname cannot be determined by Java, then the IP address is sent to Eureka.
|
|
Only explict way of setting the hostname is by setting <code class="literal">eureka.instance.hostname</code> property.
|
|
You can set your hostname at the run-time by using an environment variable — for example, <code class="literal">eureka.instance.hostname=${HOST_NAME}</code>.</p></td></tr></table></div></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_securing_the_eureka_server" href="#_securing_the_eureka_server"></a>2.7 Securing The Eureka Server</h2></div></div></div><p>You can secure your Eureka server simply by adding Spring Security to your
|
|
server’s classpath via <code class="literal">spring-boot-starter-security</code>. By default when Spring Security is on the classpath it will require that
|
|
a valid CSRF token be sent with every request to the app. Eureka clients will not generally possess a valid
|
|
cross site request forgery (CSRF) token you will need to disable this requirement for the <code class="literal">/eureka/**</code> endpoints.
|
|
For example:</p><pre class="programlisting"><em><span class="hl-annotation" style="color: gray">@EnableWebSecurity</span></em>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">class</span> WebSecurityConfig <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">extends</span> WebSecurityConfigurerAdapter {
|
|
|
|
<em><span class="hl-annotation" style="color: gray">@Override</span></em>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">protected</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">void</span> configure(HttpSecurity http) <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">throws</span> Exception {
|
|
http.csrf().ignoringAntMatchers(<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"/eureka/**"</span>);
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">super</span>.configure(http);
|
|
}
|
|
}</pre><p>For more information on CSRF see the <a class="link" href="https://docs.spring.io/spring-security/site/docs/current/reference/htmlsingle/#csrf" target="_top">Spring Security documentation</a>.</p><p>A demo Eureka Server can be found in the Spring Cloud Samples <a class="link" href="https://github.com/spring-cloud-samples/eureka/tree/Eureka-With-Security" target="_top">repo</a>.</p></div></div><div class="chapter"><div class="titlepage"><div><div><h1 class="title"><a name="_circuit_breaker_hystrix_clients" href="#_circuit_breaker_hystrix_clients"></a>3. Circuit Breaker: Hystrix Clients</h1></div></div></div><p>Netflix has created a library called <a class="link" href="https://github.com/Netflix/Hystrix" target="_top">Hystrix</a> that implements the <a class="link" href="http://martinfowler.com/bliki/CircuitBreaker.html" target="_top">circuit breaker pattern</a>.
|
|
In a microservice architecture, it is common to have multiple layers of service calls, as shown in the following example:</p><div class="figure"><a name="d0e624" href="#d0e624"></a><p class="title"><b>Figure 3.1. Microservice Graph</b></p><div class="figure-contents"><div class="mediaobject"><img src="https://raw.githubusercontent.com/spring-cloud/spring-cloud-netflix/master/docs/src/main/asciidoc/images/Hystrix.png" alt="Hystrix"></div></div></div><br class="figure-break"><p>A service failure in the lower level of services can cause cascading failure all the way up to the user.
|
|
When calls to a particular service exceed <code class="literal">circuitBreaker.requestVolumeThreshold</code> (default: 20 requests) and the failure percentage is greater than <code class="literal">circuitBreaker.errorThresholdPercentage</code> (default: >50%) in a rolling window defined by <code class="literal">metrics.rollingStats.timeInMilliseconds</code> (default: 10 seconds), the circuit opens and the call is not made.
|
|
In cases of error and an open circuit, a fallback can be provided by the developer.</p><div class="figure"><a name="d0e644" href="#d0e644"></a><p class="title"><b>Figure 3.2. Hystrix fallback prevents cascading failures</b></p><div class="figure-contents"><div class="mediaobject"><img src="https://raw.githubusercontent.com/spring-cloud/spring-cloud-netflix/master/docs/src/main/asciidoc/images/HystrixFallback.png" alt="HystrixFallback"></div></div></div><br class="figure-break"><p>Having an open circuit stops cascading failures and allows overwhelmed or failing services time to recover.
|
|
The fallback can be another Hystrix protected call, static data, or a sensible empty value.
|
|
Fallbacks may be chained so that the first fallback makes some other business call, which in turn falls back to static data.</p><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_how_to_include_hystrix" href="#_how_to_include_hystrix"></a>3.1 How to Include Hystrix</h2></div></div></div><p>To include Hystrix in your project, use the starter with a group ID of <code class="literal">org.springframework.cloud</code>
|
|
and a artifact ID of <code class="literal">spring-cloud-starter-netflix-hystrix</code>.
|
|
See the <a class="link" href="http://projects.spring.io/spring-cloud/" target="_top">Spring Cloud Project page</a> for details on setting up your build system with the current Spring Cloud Release Train.</p><p>The following example shows a minimal Eureka server with a Hystrix circuit breaker:</p><pre class="screen">@SpringBootApplication
|
|
@EnableCircuitBreaker
|
|
public class Application {
|
|
|
|
public static void main(String[] args) {
|
|
new SpringApplicationBuilder(Application.class).web(true).run(args);
|
|
}
|
|
|
|
}
|
|
|
|
@Component
|
|
public class StoreIntegration {
|
|
|
|
@HystrixCommand(fallbackMethod = "defaultStores")
|
|
public Object getStores(Map<String, Object> parameters) {
|
|
//do stuff that might fail
|
|
}
|
|
|
|
public Object defaultStores(Map<String, Object> parameters) {
|
|
return /* something useful */;
|
|
}
|
|
}</pre><p>The <code class="literal">@HystrixCommand</code> is provided by a Netflix contrib library called <a class="link" href="https://github.com/Netflix/Hystrix/tree/master/hystrix-contrib/hystrix-javanica" target="_top"><span class="quote">“<span class="quote">javanica</span>”</span></a>.
|
|
Spring Cloud automatically wraps Spring beans with that annotation in a proxy that is connected to the Hystrix circuit breaker.
|
|
The circuit breaker calculates when to open and close the circuit and what to do in case of a failure.</p><p>To configure the <code class="literal">@HystrixCommand</code> you can use the <code class="literal">commandProperties</code>
|
|
attribute with a list of <code class="literal">@HystrixProperty</code> annotations. See
|
|
<a class="link" href="https://github.com/Netflix/Hystrix/tree/master/hystrix-contrib/hystrix-javanica#configuration" target="_top">here</a>
|
|
for more details. See the <a class="link" href="https://github.com/Netflix/Hystrix/wiki/Configuration" target="_top">Hystrix wiki</a>
|
|
for details on the properties available.</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="netflix-hystrix-starter" href="#netflix-hystrix-starter"></a>3.2 Propagating the Security Context or Using Spring Scopes</h2></div></div></div><p>If you want some thread local context to propagate into a <code class="literal">@HystrixCommand</code>, the default declaration does not work, because it executes the command in a thread pool (in case of timeouts).
|
|
You can switch Hystrix to use the same thread as the caller through configuration or directly in the annotation, by asking it to use a different <span class="quote">“<span class="quote">Isolation Strategy</span>”</span>.
|
|
The following example demonstrates setting the thread in the annotation:</p><pre class="programlisting"><em><span class="hl-annotation" style="color: gray">@HystrixCommand(fallbackMethod = "stubMyService",
|
|
commandProperties = {
|
|
@HystrixProperty(name="execution.isolation.strategy", value="SEMAPHORE")
|
|
}
|
|
)</span></em>
|
|
...</pre><p>The same thing applies if you are using <code class="literal">@SessionScope</code> or <code class="literal">@RequestScope</code>.
|
|
If you encounter a runtime exception that says it cannot find the scoped context, you need to use the same thread.</p><p>You also have the option to set the <code class="literal">hystrix.shareSecurityContext</code> property to <code class="literal">true</code>.
|
|
Doing so auto-configures a Hystrix concurrency strategy plugin hook to transfer the <code class="literal">SecurityContext</code> from your main thread to the one used by the Hystrix command.
|
|
Hystrix does not let multiple Hystrix concurrency strategy be registered so an extension mechanism is available by declaring your own <code class="literal">HystrixConcurrencyStrategy</code> as a Spring bean.
|
|
Spring Cloud looks for your implementation within the Spring context and wrap it inside its own plugin.</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_health_indicator" href="#_health_indicator"></a>3.3 Health Indicator</h2></div></div></div><p>The state of the connected circuit breakers are also exposed in the <code class="literal">/health</code> endpoint of the calling application, as shown in the following example:</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">{</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"hystrix"</span>: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">{</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"openCircuitBreakers"</span>: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">[</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"StoreIntegration::getStoresByLocationLink"</span>
|
|
]<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">,</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"status"</span>: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"CIRCUIT_OPEN"</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">},</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"status"</span>: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"UP"</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">}</span></pre></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_hystrix_metrics_stream" href="#_hystrix_metrics_stream"></a>3.4 Hystrix Metrics Stream</h2></div></div></div><p>To enable the Hystrix metrics stream, include a dependency on <code class="literal">spring-boot-starter-actuator</code> and set
|
|
<code class="literal">management.endpoints.web.exposure.include: hystrix.stream</code>.
|
|
Doing so exposes the <code class="literal">/actuator/hystrix.stream</code> as a management endpoint, as shown in the following example:</p><pre class="programlisting"> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"><dependency></span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"><groupId></span>org.springframework.boot<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"></groupId></span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"><artifactId></span>spring-boot-starter-actuator<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"></artifactId></span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"></dependency></span></pre></div></div><div class="chapter"><div class="titlepage"><div><div><h1 class="title"><a name="_circuit_breaker_hystrix_dashboard" href="#_circuit_breaker_hystrix_dashboard"></a>4. Circuit Breaker: Hystrix Dashboard</h1></div></div></div><p>One of the main benefits of Hystrix is the set of metrics it gathers about each HystrixCommand.
|
|
The Hystrix Dashboard displays the health of each circuit breaker in an efficient manner.</p><div class="figure"><a name="d0e765" href="#d0e765"></a><p class="title"><b>Figure 4.1. Hystrix Dashboard</b></p><div class="figure-contents"><div class="mediaobject"><img src="https://raw.githubusercontent.com/spring-cloud/spring-cloud-netflix/master/docs/src/main/asciidoc/images/Hystrix.png" alt="Hystrix"></div></div></div><br class="figure-break"></div><div class="chapter"><div class="titlepage"><div><div><h1 class="title"><a name="_hystrix_timeouts_and_ribbon_clients" href="#_hystrix_timeouts_and_ribbon_clients"></a>5. Hystrix Timeouts And Ribbon Clients</h1></div></div></div><p>When using Hystrix commands that wrap Ribbon clients you want to make sure your Hystrix timeout
|
|
is configured to be longer than the configured Ribbon timeout, including any potential
|
|
retries that might be made. For example, if your Ribbon connection timeout is one second and
|
|
the Ribbon client might retry the request three times, than your Hystrix timeout should
|
|
be slightly more than three seconds.</p><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="netflix-hystrix-dashboard-starter" href="#netflix-hystrix-dashboard-starter"></a>5.1 How to Include the Hystrix Dashboard</h2></div></div></div><p>To include the Hystrix Dashboard in your project, use the starter with a group ID of <code class="literal">org.springframework.cloud</code> and an artifact ID of <code class="literal">spring-cloud-starter-netflix-hystrix-dashboard</code>.
|
|
See the <a class="link" href="http://projects.spring.io/spring-cloud/" target="_top">Spring Cloud Project page</a> for details on setting up your build system with the current Spring Cloud Release Train.</p><p>To run the Hystrix Dashboard, annotate your Spring Boot main class with <code class="literal">@EnableHystrixDashboard</code>.
|
|
Then visit <code class="literal">/hystrix</code> and point the dashboard to an individual instance’s <code class="literal">/hystrix.stream</code> endpoint in a Hystrix client application.</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Note"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="images/note.png"></td><th align="left">Note</th></tr><tr><td align="left" valign="top"><p>When connecting to a <code class="literal">/hystrix.stream</code> endpoint that uses HTTPS, the certificate used by the server must be trusted by the JVM.
|
|
If the certificate is not trusted, you must import the certificate into the JVM in order for the Hystrix Dashboard to make a successful connection to the stream endpoint.</p></td></tr></table></div></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_turbine" href="#_turbine"></a>5.2 Turbine</h2></div></div></div><p>Looking at an individual instance’s Hystrix data is not very useful in terms of the overall health of the system. <a class="link" href="https://github.com/Netflix/Turbine" target="_top">Turbine</a> is an application that aggregates all of the relevant <code class="literal">/hystrix.stream</code> endpoints into a combined <code class="literal">/turbine.stream</code> for use in the Hystrix Dashboard.
|
|
Individual instances are located through Eureka.
|
|
Running Turbine requires annotating your main class with the <code class="literal">@EnableTurbine</code> annotation (for example, by using spring-cloud-starter-netflix-turbine to set up the classpath).
|
|
All of the documented configuration properties from <a class="link" href="https://github.com/Netflix/Turbine/wiki/Configuration-(1.x)" target="_top">the Turbine 1 wiki</a> apply.
|
|
The only difference is that the <code class="literal">turbine.instanceUrlSuffix</code> does not need the port prepended, as this is handled automatically unless <code class="literal">turbine.instanceInsertPort=false</code>.</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Note"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="images/note.png"></td><th align="left">Note</th></tr><tr><td align="left" valign="top"><p>By default, Turbine looks for the <code class="literal">/hystrix.stream</code> endpoint on a registered instance by looking up its <code class="literal">hostName</code> and <code class="literal">port</code> entries in Eureka and then appending <code class="literal">/hystrix.stream</code> to it.
|
|
If the instance’s metadata contains <code class="literal">management.port</code>, it is used instead of the <code class="literal">port</code> value for the <code class="literal">/hystrix.stream</code> endpoint.
|
|
By default, the metadata entry called <code class="literal">management.port</code> is equal to the <code class="literal">management.port</code> configuration property.
|
|
It can be overridden though with following configuration:</p></td></tr></table></div><pre class="screen">eureka:
|
|
instance:
|
|
metadata-map:
|
|
management.port: ${management.port:8081}</pre><p>The <code class="literal">turbine.appConfig</code> configuration key is a list of Eureka serviceIds that turbine uses to lookup instances.
|
|
The turbine stream is then used in the Hystrix dashboard with a URL similar to the following:</p><p><code class="literal"><a class="link" href="http://my.turbine.server:8080/turbine.stream?cluster=CLUSTERNAME" target="_top">http://my.turbine.server:8080/turbine.stream?cluster=CLUSTERNAME</a></code></p><p>The cluster parameter can be omitted if the name is <code class="literal">default</code>.
|
|
The <code class="literal">cluster</code> parameter must match an entry in <code class="literal">turbine.aggregator.clusterConfig</code>.
|
|
Values returned from Eureka are upper-case. Consequently, the following example works if there is an application called <code class="literal">customers</code> registered with Eureka:</p><pre class="screen">turbine:
|
|
aggregator:
|
|
clusterConfig: CUSTOMERS
|
|
appConfig: customers</pre><p>If you need to customize which cluster names should be used by Turbine (because you do not want to store cluster names in
|
|
<code class="literal">turbine.aggregator.clusterConfig</code> configuration), provide a bean of type <code class="literal">TurbineClustersProvider</code>.</p><p>The <code class="literal">clusterName</code> can be customized by a SPEL expression in <code class="literal">turbine.clusterNameExpression</code> with root as an instance of <code class="literal">InstanceInfo</code>.
|
|
The default value is <code class="literal">appName</code>, which means that the Eureka <code class="literal">serviceId</code> becomes the cluster key (that is, the <code class="literal">InstanceInfo</code> for customers has an <code class="literal">appName</code> of <code class="literal">CUSTOMERS</code>).
|
|
A different example is <code class="literal">turbine.clusterNameExpression=aSGName</code>, which gets the cluster name from the AWS ASG name.
|
|
The following listing shows another example:</p><pre class="screen">turbine:
|
|
aggregator:
|
|
clusterConfig: SYSTEM,USER
|
|
appConfig: customers,stores,ui,admin
|
|
clusterNameExpression: metadata['cluster']</pre><p>In the preceding example, the cluster name from four services is pulled from their metadata map and is expected to have values that include <code class="literal">SYSTEM</code> and <code class="literal">USER</code>.</p><p>To use the <span class="quote">“<span class="quote">default</span>”</span> cluster for all apps, you need a string literal expression (with single quotes and escaped with double quotes if it is in YAML as well):</p><pre class="screen">turbine:
|
|
appConfig: customers,stores
|
|
clusterNameExpression: "'default'"</pre><p>Spring Cloud provides a <code class="literal">spring-cloud-starter-netflix-turbine</code> that has all the dependencies you need to get a Turbine server running. To ad Turnbine, create a Spring Boot application and annotate it with <code class="literal">@EnableTurbine</code>.</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Note"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="images/note.png"></td><th align="left">Note</th></tr><tr><td align="left" valign="top"><p>By default, Spring Cloud lets Turbine use the host and port to allow multiple processes per host, per cluster.
|
|
If you want the native Netflix behavior built into Turbine to <span class="emphasis"><em>not</em></span> allow multiple processes per host, per cluster (the key to the instance ID is the hostname), set <code class="literal">turbine.combineHostPort=false</code>.</p></td></tr></table></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="_clusters_endpoint" href="#_clusters_endpoint"></a>5.2.1 Clusters Endpoint</h3></div></div></div><p>In some situations it might be useful for other applications to know what custers have been configured
|
|
in Turbine. To support this you can use the <code class="literal">/clusters</code> endpoint which will return a JSON array of
|
|
all the configured clusters.</p><p><b>GET /clusters. </b>
|
|
</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">[</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">{</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"name"</span>: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"RACES"</span><span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">,</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"link"</span>: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"http://localhost:8383/turbine.stream?cluster=RACES"</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">},</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">{</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"name"</span>: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"WEB"</span><span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">,</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"link"</span>: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"http://localhost:8383/turbine.stream?cluster=WEB"</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">}</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">]</span></pre><p>
|
|
</p><p>This endpoint can be disabled by setting <code class="literal">turbine.endpoints.clusters.enabled</code> to <code class="literal">false</code>.</p></div></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_turbine_stream" href="#_turbine_stream"></a>5.3 Turbine Stream</h2></div></div></div><p>In some environments (such as in a PaaS setting), the classic Turbine model of pulling metrics from all the distributed Hystrix commands does not work.
|
|
In that case, you might want to have your Hystrix commands push metrics to Turbine. Spring Cloud enables that with messaging.
|
|
To do so on the client, add a dependency to <code class="literal">spring-cloud-netflix-hystrix-stream</code> and the <code class="literal">spring-cloud-starter-stream-*</code> of your choice.
|
|
See the <a class="link" href="https://docs.spring.io/spring-cloud-stream/docs/current/reference/htmlsingle/" target="_top">Spring Cloud Stream documentation</a> for details on the brokers and how to configure the client credentials. It should work out of the box for a local broker.</p><p>On the server side, create a Spring Boot application and annotate it with <code class="literal">@EnableTurbineStream</code>.
|
|
The Turbine Stream server requires the use of Spring Webflux, therefore <code class="literal">spring-boot-starter-webflux</code> needs to be included in your project.
|
|
By default <code class="literal">spring-boot-starter-webflux</code> is included when adding <code class="literal">spring-cloud-starter-netflix-turbine-stream</code> to your application.</p><p>You can then point the Hystrix Dashboard to the Turbine Stream Server instead of individual Hystrix streams.
|
|
If Turbine Stream is running on port 8989 on myhost, then put <code class="literal"><a class="link" href="http://myhost:8989" target="_top">http://myhost:8989</a></code> in the stream input field in the Hystrix Dashboard.
|
|
Circuits are prefixed by their respective <code class="literal">serviceId</code>, followed by a dot (<code class="literal">.</code>), and then the circuit name.</p><p>Spring Cloud provides a <code class="literal">spring-cloud-starter-netflix-turbine-stream</code> that has all the dependencies you need to get a Turbine Stream server running.
|
|
You can then add the Stream binder of your choice — such as <code class="literal">spring-cloud-starter-stream-rabbit</code>.</p><p>Turbine Stream server also supports the <code class="literal">cluster</code> parameter.
|
|
Unlike Turbine server, Turbine Stream uses eureka serviceIds as cluster names and these are not configurable.</p><p>If Turbine Stream server is running on port 8989 on <code class="literal">my.turbine.server</code> and you have two eureka serviceIds <code class="literal">customers</code> and <code class="literal">products</code> in your environment, the following URLs will be available on your Turbine Stream server. <code class="literal">default</code> and empty cluster name will provide all metrics that Turbine Stream server receives.</p><pre class="screen">http://my.turbine.sever:8989/turbine.stream?cluster=customers
|
|
http://my.turbine.sever:8989/turbine.stream?cluster=products
|
|
http://my.turbine.sever:8989/turbine.stream?cluster=default
|
|
http://my.turbine.sever:8989/turbine.stream</pre><p>So, you can use eureka serviceIds as cluster names for your Turbine dashboard (or any compatible dashboard).
|
|
You don’t need to configure any properties like <code class="literal">turbine.appConfig</code>, <code class="literal">turbine.clusterNameExpression</code> and <code class="literal">turbine.aggregator.clusterConfig</code> for your Turbine Stream server.</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Note"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="images/note.png"></td><th align="left">Note</th></tr><tr><td align="left" valign="top"><p>Turbine Stream server gathers all metrics from the configured input channel with Spring Cloud Stream. It means that it doesn’t gather Hystrix metrics actively from each instance. It just can provide metrics that were already gathered into the input channel by each instance.</p></td></tr></table></div></div></div><div class="chapter"><div class="titlepage"><div><div><h1 class="title"><a name="spring-cloud-ribbon" href="#spring-cloud-ribbon"></a>6. Client Side Load Balancer: Ribbon</h1></div></div></div><p>Ribbon is a client-side load balancer that gives you a lot of control over the behavior of HTTP and TCP clients.
|
|
Feign already uses Ribbon, so, if you use <code class="literal">@FeignClient</code>, this section also applies.</p><p>A central concept in Ribbon is that of the named client.
|
|
Each load balancer is part of an ensemble of components that work together to contact a remote server on demand, and the ensemble has a name that you give it as an application developer (for example, by using the <code class="literal">@FeignClient</code> annotation).
|
|
On demand, Spring Cloud creates a new ensemble as an <code class="literal">ApplicationContext</code> for each named client by using
|
|
<code class="literal">RibbonClientConfiguration</code>.
|
|
This contains (amongst other things) an <code class="literal">ILoadBalancer</code>, a <code class="literal">RestClient</code>, and a <code class="literal">ServerListFilter</code>.</p><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="netflix-ribbon-starter" href="#netflix-ribbon-starter"></a>6.1 How to Include Ribbon</h2></div></div></div><p>To include Ribbon in your project, use the starter with a group ID of <code class="literal">org.springframework.cloud</code> and an artifact ID of <code class="literal">spring-cloud-starter-netflix-ribbon</code>.
|
|
See the <a class="link" href="http://projects.spring.io/spring-cloud/" target="_top">Spring Cloud Project page</a> for details on setting up your build system with the current Spring Cloud Release Train.</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_customizing_the_ribbon_client" href="#_customizing_the_ribbon_client"></a>6.2 Customizing the Ribbon Client</h2></div></div></div><p>You can configure some bits of a Ribbon client by using external properties in <code class="literal"><client>.ribbon.*</code>, which is similar to using the Netflix APIs natively, except that you can use Spring Boot configuration files.
|
|
The native options can be inspected as static fields in <a class="link" href="https://github.com/Netflix/ribbon/blob/master/ribbon-core/src/main/java/com/netflix/client/config/CommonClientConfigKey.java" target="_top"><code class="literal">CommonClientConfigKey</code></a> (part of ribbon-core).</p><p>Spring Cloud also lets you take full control of the client by declaring additional configuration (on top of the <code class="literal">RibbonClientConfiguration</code>) using <code class="literal">@RibbonClient</code>, as shown in the following example:</p><pre class="programlisting"><em><span class="hl-annotation" style="color: gray">@Configuration</span></em>
|
|
<em><span class="hl-annotation" style="color: gray">@RibbonClient(name = "custom", configuration = CustomConfiguration.class)</span></em>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">class</span> TestConfiguration {
|
|
}</pre><p>In this case, the client is composed from the components already in <code class="literal">RibbonClientConfiguration</code>, together with any in <code class="literal">CustomConfiguration</code> (where the latter generally overrides the former).</p><div class="warning" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Warning"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Warning]" src="images/warning.png"></td><th align="left">Warning</th></tr><tr><td align="left" valign="top"><p>The <code class="literal">CustomConfiguration</code> clas must be a <code class="literal">@Configuration</code> class, but take care that it is not in a <code class="literal">@ComponentScan</code> for the main application context.
|
|
Otherwise, it is shared by all the <code class="literal">@RibbonClients</code>. If you use <code class="literal">@ComponentScan</code> (or <code class="literal">@SpringBootApplication</code>), you need to take steps to avoid it being included (for instance, you can put it in a separate, non-overlapping package or specify the packages to scan explicitly in the <code class="literal">@ComponentScan</code>).</p></td></tr></table></div><p>The following table shows the beans that Spring Cloud Netflix provides by default for Ribbon:</p><div class="informaltable"><table class="informaltable" style="border-collapse: collapse;border-top: 1px solid ; border-bottom: 1px solid ; " width="60%"><colgroup><col class="col_1"><col class="col_2"><col class="col_3"></colgroup><thead><tr><th style="border-right: 1px solid ; border-bottom: 1px solid ; " align="center" valign="top">Bean Type</th><th style="border-right: 1px solid ; border-bottom: 1px solid ; " align="center" valign="top">Bean Name</th><th style="border-bottom: 1px solid ; " align="center" valign="top">Class Name</th></tr></thead><tbody><tr><td style="border-right: 1px solid ; border-bottom: 1px solid ; " align="left" valign="top"><p><code class="literal">IClientConfig</code></p></td><td style="border-right: 1px solid ; border-bottom: 1px solid ; " align="left" valign="top"><p><code class="literal">ribbonClientConfig</code></p></td><td style="border-bottom: 1px solid ; " align="left" valign="top"><p><code class="literal">DefaultClientConfigImpl</code></p></td></tr><tr><td style="border-right: 1px solid ; border-bottom: 1px solid ; " align="left" valign="top"><p><code class="literal">IRule</code></p></td><td style="border-right: 1px solid ; border-bottom: 1px solid ; " align="left" valign="top"><p><code class="literal">ribbonRule</code></p></td><td style="border-bottom: 1px solid ; " align="left" valign="top"><p><code class="literal">ZoneAvoidanceRule</code></p></td></tr><tr><td style="border-right: 1px solid ; border-bottom: 1px solid ; " align="left" valign="top"><p><code class="literal">IPing</code></p></td><td style="border-right: 1px solid ; border-bottom: 1px solid ; " align="left" valign="top"><p><code class="literal">ribbonPing</code></p></td><td style="border-bottom: 1px solid ; " align="left" valign="top"><p><code class="literal">DummyPing</code></p></td></tr><tr><td style="border-right: 1px solid ; border-bottom: 1px solid ; " align="left" valign="top"><p><code class="literal">ServerList<Server></code></p></td><td style="border-right: 1px solid ; border-bottom: 1px solid ; " align="left" valign="top"><p><code class="literal">ribbonServerList</code></p></td><td style="border-bottom: 1px solid ; " align="left" valign="top"><p><code class="literal">ConfigurationBasedServerList</code></p></td></tr><tr><td style="border-right: 1px solid ; border-bottom: 1px solid ; " align="left" valign="top"><p><code class="literal">ServerListFilter<Server></code></p></td><td style="border-right: 1px solid ; border-bottom: 1px solid ; " align="left" valign="top"><p><code class="literal">ribbonServerListFilter</code></p></td><td style="border-bottom: 1px solid ; " align="left" valign="top"><p><code class="literal">ZonePreferenceServerListFilter</code></p></td></tr><tr><td style="border-right: 1px solid ; border-bottom: 1px solid ; " align="left" valign="top"><p><code class="literal">ILoadBalancer</code></p></td><td style="border-right: 1px solid ; border-bottom: 1px solid ; " align="left" valign="top"><p><code class="literal">ribbonLoadBalancer</code></p></td><td style="border-bottom: 1px solid ; " align="left" valign="top"><p><code class="literal">ZoneAwareLoadBalancer</code></p></td></tr><tr><td style="border-right: 1px solid ; " align="left" valign="top"><p><code class="literal">ServerListUpdater</code></p></td><td style="border-right: 1px solid ; " align="left" valign="top"><p><code class="literal">ribbonServerListUpdater</code></p></td><td style="" align="left" valign="top"><p><code class="literal">PollingServerListUpdater</code></p></td></tr></tbody></table></div><p>Creating a bean of one of those type and placing it in a <code class="literal">@RibbonClient</code> configuration (such as <code class="literal">FooConfiguration</code> above) lets you override each one of the beans described, as shown in the following example:</p><pre class="programlisting"><em><span class="hl-annotation" style="color: gray">@Configuration</span></em>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">protected</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">static</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">class</span> FooConfiguration {
|
|
<em><span class="hl-annotation" style="color: gray">@Bean</span></em>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> ZonePreferenceServerListFilter serverListFilter() {
|
|
ZonePreferenceServerListFilter filter = <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">new</span> ZonePreferenceServerListFilter();
|
|
filter.setZone(<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"myTestZone"</span>);
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">return</span> filter;
|
|
}
|
|
|
|
<em><span class="hl-annotation" style="color: gray">@Bean</span></em>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> IPing ribbonPing() {
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">return</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">new</span> PingUrl();
|
|
}
|
|
}</pre><p>The include statement in the preceding example replaces <code class="literal">NoOpPing</code> with <code class="literal">PingUrl</code> and provides a custom <code class="literal">serverListFilter</code>.</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_customizing_the_default_for_all_ribbon_clients" href="#_customizing_the_default_for_all_ribbon_clients"></a>6.3 Customizing the Default for All Ribbon Clients</h2></div></div></div><p>A default configuration can be provided for all Ribbon Clients by using the <code class="literal">@RibbonClients</code> annotation and registering a default configuration, as shown in the following example:</p><pre class="programlisting"><em><span class="hl-annotation" style="color: gray">@RibbonClients(defaultConfiguration = DefaultRibbonConfig.class)</span></em>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">class</span> RibbonClientDefaultConfigurationTestsConfig {
|
|
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">static</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">class</span> BazServiceList <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">extends</span> ConfigurationBasedServerList {
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> BazServiceList(IClientConfig config) {
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">super</span>.initWithNiwsConfig(config);
|
|
}
|
|
}
|
|
}
|
|
|
|
<em><span class="hl-annotation" style="color: gray">@Configuration</span></em>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">class</span> DefaultRibbonConfig {
|
|
|
|
<em><span class="hl-annotation" style="color: gray">@Bean</span></em>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> IRule ribbonRule() {
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">return</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">new</span> BestAvailableRule();
|
|
}
|
|
|
|
<em><span class="hl-annotation" style="color: gray">@Bean</span></em>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> IPing ribbonPing() {
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">return</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">new</span> PingUrl();
|
|
}
|
|
|
|
<em><span class="hl-annotation" style="color: gray">@Bean</span></em>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> ServerList<Server> ribbonServerList(IClientConfig config) {
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">return</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">new</span> RibbonClientDefaultConfigurationTestsConfig.BazServiceList(config);
|
|
}
|
|
|
|
<em><span class="hl-annotation" style="color: gray">@Bean</span></em>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> ServerListSubsetFilter serverListFilter() {
|
|
ServerListSubsetFilter filter = <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">new</span> ServerListSubsetFilter();
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">return</span> filter;
|
|
}
|
|
|
|
}
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-comment">// end::sample_default_ribbon_config[]</span></pre></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_customizing_the_ribbon_client_by_setting_properties" href="#_customizing_the_ribbon_client_by_setting_properties"></a>6.4 Customizing the Ribbon Client by Setting Properties</h2></div></div></div><p>Starting with version 1.2.0, Spring Cloud Netflix now supports customizing Ribbon clients by setting properties to be compatible with the <a class="link" href="https://github.com/Netflix/ribbon/wiki/Working-with-load-balancers#components-of-load-balancer" target="_top">Ribbon documentation</a>.</p><p>This lets you change behavior at start up time in different environments.</p><p>The following list shows the supported properties>:</p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><code class="literal"><clientName>.ribbon.NFLoadBalancerClassName</code>: Should implement <code class="literal">ILoadBalancer</code></li><li class="listitem"><code class="literal"><clientName>.ribbon.NFLoadBalancerRuleClassName</code>: Should implement <code class="literal">IRule</code></li><li class="listitem"><code class="literal"><clientName>.ribbon.NFLoadBalancerPingClassName</code>: Should implement <code class="literal">IPing</code></li><li class="listitem"><code class="literal"><clientName>.ribbon.NIWSServerListClassName</code>: Should implement <code class="literal">ServerList</code></li><li class="listitem"><code class="literal"><clientName>.ribbon.NIWSServerListFilterClassName</code>: Should implement <code class="literal">ServerListFilter</code></li></ul></div><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Note"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="images/note.png"></td><th align="left">Note</th></tr><tr><td align="left" valign="top"><p>Classes defined in these properties have precedence over beans defined by using <code class="literal">@RibbonClient(configuration=MyRibbonConfig.class)</code> and the defaults provided by Spring Cloud Netflix.</p></td></tr></table></div><p>To set the <code class="literal">IRule</code> for a service name called <code class="literal">users</code>, you could set the following properties:</p><p><b>application.yml. </b>
|
|
</p><pre class="screen">users:
|
|
ribbon:
|
|
NIWSServerListClassName: com.netflix.loadbalancer.ConfigurationBasedServerList
|
|
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.WeightedResponseTimeRule</pre><p>
|
|
</p><p>See the <a class="link" href="https://github.com/Netflix/ribbon/wiki/Working-with-load-balancers" target="_top">Ribbon documentation</a> for implementations provided by Ribbon.</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_using_ribbon_with_eureka" href="#_using_ribbon_with_eureka"></a>6.5 Using Ribbon with Eureka</h2></div></div></div><p>When Eureka is used in conjunction with Ribbon (that is, both are on the classpath), the <code class="literal">ribbonServerList</code> is overridden with an extension of <code class="literal">DiscoveryEnabledNIWSServerList</code>, which populates the list of servers from Eureka.
|
|
It also replaces the <code class="literal">IPing</code> interface with <code class="literal">NIWSDiscoveryPing</code>, which delegates to Eureka to determine if a server is up.
|
|
The <code class="literal">ServerList</code> that is installed by default is a <code class="literal">DomainExtractingServerList</code>. Its purpose is to make metadata available to the load balancer without using AWS AMI metadata (which is what Netflix relies on).
|
|
By default, the server list is constructed with <span class="quote">“<span class="quote">zone</span>”</span> information, as provided in the instance metadata (so, on the remote clients, set <code class="literal">eureka.instance.metadataMap.zone</code>).
|
|
If that is missing and if the <code class="literal">approximateZoneFromHostname</code> flag is set, it can use the domain name from the server hostname as a proxy for the zone.
|
|
Once the zone information is available, it can be used in a <code class="literal">ServerListFilter</code>.
|
|
By default, it is used to locate a server in the same zone as the client, because the default is a <code class="literal">ZonePreferenceServerListFilter</code>.
|
|
By default, the zone of the client is determined in the same way as the remote instances (that is, through <code class="literal">eureka.instance.metadataMap.zone</code>).</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Note"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="images/note.png"></td><th align="left">Note</th></tr><tr><td align="left" valign="top"><p>The orthodox <span class="quote">“<span class="quote">archaius</span>”</span> way to set the client zone is through a configuration property called "@zone".
|
|
If it is available, Spring Cloud uses that in preference to all other settings (note that the key must be quoted in YAML configuration).</p></td></tr></table></div><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Note"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="images/note.png"></td><th align="left">Note</th></tr><tr><td align="left" valign="top"><p>If there is no other source of zone data, then a guess is made, based on the client configuration (as opposed to the instance configuration).
|
|
We take <code class="literal">eureka.client.availabilityZones</code>, which is a map from region name to a list of zones, and pull out the first zone for the instance’s own region (that is, the <code class="literal">eureka.client.region</code>, which defaults to "us-east-1", for compatibility with native Netflix).</p></td></tr></table></div></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="spring-cloud-ribbon-without-eureka" href="#spring-cloud-ribbon-without-eureka"></a>6.6 Example: How to Use Ribbon Without Eureka</h2></div></div></div><p>Eureka is a convenient way to abstract the discovery of remote servers so that you do not have to hard code their URLs in clients.
|
|
However, if you prefer not to use Eureka, Ribbon and Feign also work.
|
|
Suppose you have declared a <code class="literal">@RibbonClient</code> for "stores", and Eureka is not in use (and not even on the classpath).
|
|
The Ribbon client defaults to a configured server list.
|
|
You can supply the configuration as follows:</p><p><b>application.yml. </b>
|
|
</p><pre class="screen">stores:
|
|
ribbon:
|
|
listOfServers: example.com,google.com</pre><p>
|
|
</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_example_disable_eureka_use_in_ribbon" href="#_example_disable_eureka_use_in_ribbon"></a>6.7 Example: Disable Eureka Use in Ribbon</h2></div></div></div><p>Setting the <code class="literal">ribbon.eureka.enabled</code> property to <code class="literal">false</code> explicitly disables the use of Eureka in Ribbon, as shown in the following example:</p><p><b>application.yml. </b>
|
|
</p><pre class="screen">ribbon:
|
|
eureka:
|
|
enabled: false</pre><p>
|
|
</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_using_the_ribbon_api_directly" href="#_using_the_ribbon_api_directly"></a>6.8 Using the Ribbon API Directly</h2></div></div></div><p>You can also use the <code class="literal">LoadBalancerClient</code> directly, as shown in the following example:</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">class</span> MyClass {
|
|
<em><span class="hl-annotation" style="color: gray">@Autowired</span></em>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">private</span> LoadBalancerClient loadBalancer;
|
|
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">void</span> doStuff() {
|
|
ServiceInstance instance = loadBalancer.choose(<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"stores"</span>);
|
|
URI storesUri = URI.create(String.format(<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"http://%s:%s"</span>, instance.getHost(), instance.getPort()));
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-comment">// ... do something with the URI</span>
|
|
}
|
|
}</pre></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="ribbon-child-context-eager-load" href="#ribbon-child-context-eager-load"></a>6.9 Caching of Ribbon Configuration</h2></div></div></div><p>Each Ribbon named client has a corresponding child application Context that Spring Cloud maintains.
|
|
This application context is lazily loaded on the first request to the named client.
|
|
This lazy loading behavior can be changed to instead eagerly load these child application contexts at startup, by specifying the names of the Ribbon clients, as shown in the following example:</p><p><b>application.yml. </b>
|
|
</p><pre class="screen">ribbon:
|
|
eager-load:
|
|
enabled: true
|
|
clients: client1, client2, client3</pre><p>
|
|
</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="how-to-configure-hystrix-thread-pools" href="#how-to-configure-hystrix-thread-pools"></a>6.10 How to Configure Hystrix Thread Pools</h2></div></div></div><p>If you change <code class="literal">zuul.ribbonIsolationStrategy</code> to <code class="literal">THREAD</code>, the thread isolation strategy for Hystrix is used for all routes.
|
|
In that case, the <code class="literal">HystrixThreadPoolKey</code> is set to <code class="literal">RibbonCommand</code> as the default.
|
|
It means that HystrixCommands for all routes are executed in the same Hystrix thread pool.
|
|
This behavior can be changed with the following configuration:</p><p><b>application.yml. </b>
|
|
</p><pre class="screen">zuul:
|
|
threadPool:
|
|
useSeparateThreadPools: true</pre><p>
|
|
</p><p>The preceding example results in HystrixCommands being executed in the Hystrix thread pool for each route.</p><p>In this case, the default <code class="literal">HystrixThreadPoolKey</code> is the same as the service ID for each route.
|
|
To add a prefix to <code class="literal">HystrixThreadPoolKey</code>, set <code class="literal">zuul.threadPool.threadPoolKeyPrefix</code> to the value that you want to add, as shown in the following example:</p><p><b>application.yml. </b>
|
|
</p><pre class="screen">zuul:
|
|
threadPool:
|
|
useSeparateThreadPools: true
|
|
threadPoolKeyPrefix: zuulgw</pre><p>
|
|
</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="how-to-provdie-a-key-to-ribbon" href="#how-to-provdie-a-key-to-ribbon"></a>6.11 How to Provide a Key to Ribbon’s <code class="literal">IRule</code></h2></div></div></div><p>If you need to provide your own <code class="literal">IRule</code> implementation to handle a special routing requirement like a <span class="quote">“<span class="quote">canary</span>”</span> test, pass some information to the <code class="literal">choose</code> method of <code class="literal">IRule</code>.</p><p><b>com.netflix.loadbalancer.IRule.java. </b>
|
|
</p><pre class="screen">public interface IRule{
|
|
public Server choose(Object key);
|
|
:</pre><p>
|
|
</p><p>You can provide some information that is used by your <code class="literal">IRule</code> implementation to choose a target server, as shown in the following example:</p><pre class="screen">RequestContext.getCurrentContext()
|
|
.set(FilterConstants.LOAD_BALANCER_KEY, "canary-test");</pre><p>If you put any object into the <code class="literal">RequestContext</code> with a key of <code class="literal">FilterConstants.LOAD_BALANCER_KEY</code>, it is passed to the <code class="literal">choose</code> method of the <code class="literal">IRule</code> implementation.
|
|
The code shown in the preceding example must be executed before <code class="literal">RibbonRoutingFilter</code> is executed.
|
|
Zuul’s pre filter is the best place to do that.
|
|
You can access HTTP headers and query parameters through the <code class="literal">RequestContext</code> in pre filter, so it can be used to determine the <code class="literal">LOAD_BALANCER_KEY</code> that is passed to Ribbon.
|
|
If you do not put any value with <code class="literal">LOAD_BALANCER_KEY</code> in <code class="literal">RequestContext</code>, null is passed as a parameter of the <code class="literal">choose</code> method.</p></div></div><div class="chapter"><div class="titlepage"><div><div><h1 class="title"><a name="_external_configuration_archaius" href="#_external_configuration_archaius"></a>7. External Configuration: Archaius</h1></div></div></div><p><a class="link" href="https://github.com/Netflix/archaius" target="_top">Archaius</a> is the Netflix client-side configuration library.
|
|
It is the library used by all of the Netflix OSS components for configuration.
|
|
Archaius is an extension of the <a class="link" href="http://commons.apache.org/proper/commons-configuration" target="_top">Apache Commons Configuration</a> project.
|
|
It allows updates to configuration by either polling a source for changes or by letting a source push changes to the client.
|
|
Archaius uses Dynamic<Type>Property classes as handles to properties, as shown in the following example:</p><p><b>Archaius Example. </b>
|
|
</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">class</span> ArchaiusTest {
|
|
DynamicStringProperty myprop = DynamicPropertyFactory
|
|
.getInstance()
|
|
.getStringProperty(<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"my.prop"</span>);
|
|
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">void</span> doSomething() {
|
|
OtherClass.someMethod(myprop.get());
|
|
}
|
|
}</pre><p>
|
|
</p><p>Archaius has its own set of configuration files and loading priorities.
|
|
Spring applications should generally not use Archaius directly, but the need to configure the Netflix tools natively remains.
|
|
Spring Cloud has a Spring Environment Bridge so that Archaius can read properties from the Spring Environment.
|
|
This bridge allows Spring Boot projects to use the normal configuration toolchain while letting them configure the Netflix tools as documented (for the most part).</p></div><div class="chapter"><div class="titlepage"><div><div><h1 class="title"><a name="_router_and_filter_zuul" href="#_router_and_filter_zuul"></a>8. Router and Filter: Zuul</h1></div></div></div><p>Routing is an integral part of a microservice architecture.
|
|
For example, <code class="literal">/</code> may be mapped to your web application, <code class="literal">/api/users</code> is mapped to the user service and <code class="literal">/api/shop</code> is mapped to the shop service.
|
|
<a class="link" href="https://github.com/Netflix/zuul" target="_top">Zuul</a> is a JVM-based router and server-side load balancer from Netflix.</p><p><a class="link" href="http://www.slideshare.net/MikeyCohen1/edge-architecture-ieee-international-conference-on-cloud-engineering-32240146/27" target="_top">Netflix uses Zuul</a> for the following:</p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">Authentication</li><li class="listitem">Insights</li><li class="listitem">Stress Testing</li><li class="listitem">Canary Testing</li><li class="listitem">Dynamic Routing</li><li class="listitem">Service Migration</li><li class="listitem">Load Shedding</li><li class="listitem">Security</li><li class="listitem">Static Response handling</li><li class="listitem">Active/Active traffic management</li></ul></div><p>Zuul’s rule engine lets rules and filters be written in essentially any JVM language, with built-in support for Java and Groovy.</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Note"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="images/note.png"></td><th align="left">Note</th></tr><tr><td align="left" valign="top"><p>The configuration property <code class="literal">zuul.max.host.connections</code> has been replaced by two new properties, <code class="literal">zuul.host.maxTotalConnections</code> and <code class="literal">zuul.host.maxPerRouteConnections</code>, which default to 200 and 20 respectively.</p></td></tr></table></div><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Note"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="images/note.png"></td><th align="left">Note</th></tr><tr><td align="left" valign="top"><p>The default Hystrix isolation pattern (<code class="literal">ExecutionIsolationStrategy</code>) for all routes is <code class="literal">SEMAPHORE</code>.
|
|
<code class="literal">zuul.ribbonIsolationStrategy</code> can be changed to <code class="literal">THREAD</code> if that isolation pattern is preferred.</p></td></tr></table></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="netflix-zuul-starter" href="#netflix-zuul-starter"></a>8.1 How to Include Zuul</h2></div></div></div><p>To include Zuul in your project, use the starter with a group ID of <code class="literal">org.springframework.cloud</code> and a artifact ID of <code class="literal">spring-cloud-starter-netflix-zuul</code>.
|
|
See the <a class="link" href="http://projects.spring.io/spring-cloud/" target="_top">Spring Cloud Project page</a> for details on setting up your build system with the current Spring Cloud Release Train.</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="netflix-zuul-reverse-proxy" href="#netflix-zuul-reverse-proxy"></a>8.2 Embedded Zuul Reverse Proxy</h2></div></div></div><p>Spring Cloud has created an embedded Zuul proxy to ease the development of a common use case where a UI application wants to make proxy calls to one or more back end services.
|
|
This feature is useful 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.</p><p>To enable it, annotate a Spring Boot main class with <code class="literal">@EnableZuulProxy</code>. Doing so causes local calls to be forwarded to the appropriate service.
|
|
By convention, a service with an ID of <code class="literal">users</code> receives requests from the proxy located at <code class="literal">/users</code> (with the prefix stripped).
|
|
The proxy uses Ribbon to locate an instance to which to forward through discovery.
|
|
All requests are executed in a <a class="link" href="#hystrix-fallbacks-for-routes" title="8.12 Providing Hystrix Fallbacks For Routes">hystrix command</a>, so failures appear in Hystrix metrics.
|
|
Once the circuit is open, the proxy does not try to contact the service.</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Note"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="images/note.png"></td><th align="left">Note</th></tr><tr><td align="left" valign="top"><p>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).</p></td></tr></table></div><p>To skip having a service automatically added, set <code class="literal">zuul.ignored-services</code> to a list of service ID patterns.
|
|
If a service matches a pattern that is ignored but is also included in the explicitly configured routes map, it is unignored, as shown in the following example:</p><p><b>application.yml. </b>
|
|
</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> zuul</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> ignoredServices</span>: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">'*'</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> routes</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> users</span>: /myusers/**</pre><p>
|
|
</p><p>In the preceding example, all services are ignored, <span class="strong"><strong>except</strong></span> for <code class="literal">users</code>.</p><p>To augment or change the proxy routes, you can add external configuration, as follows:</p><p><b>application.yml. </b>
|
|
</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> zuul</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> routes</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> users</span>: /myusers/**</pre><p>
|
|
</p><p>The preceding example means that HTTP calls to <code class="literal">/myusers</code> get forwarded to the <code class="literal">users</code> service (for example <code class="literal">/myusers/101</code> is forwarded to <code class="literal">/101</code>).</p><p>To get more fine-grained control over a route, you can specify the path and the serviceId independently, as follows:</p><p><b>application.yml. </b>
|
|
</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> zuul</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> routes</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> users</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> path</span>: /myusers/**
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> serviceId</span>: users_service</pre><p>
|
|
</p><p>The preceding example means that HTTP calls to <code class="literal">/myusers</code> get forwarded to the <code class="literal">users_service</code> service.
|
|
The route must have a <code class="literal">path</code> that can be specified as an ant-style pattern, so <code class="literal">/myusers/*</code> only matches one level, but <code class="literal">/myusers/**</code> matches hierarchically.</p><p>The location of the back end can be specified as either a <code class="literal">serviceId</code> (for a service from discovery) or a <code class="literal">url</code> (for a physical location), as shown in the following example:</p><p><b>application.yml. </b>
|
|
</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> zuul</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> routes</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> users</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> path</span>: /myusers/**
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> url</span>: http://example.com/users_service</pre><p>
|
|
</p><p>These simple url-routes do not get executed as a <code class="literal">HystrixCommand</code>, nor do they load-balance multiple URLs with Ribbon.
|
|
To achieve those goals, you can specify a <code class="literal">serviceId</code> with a static list of servers, as follows:</p><p><b>application.yml. </b>
|
|
</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">zuul</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> routes</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> echo</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> path</span>: /myusers/**
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> serviceId</span>: myusers-service
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> stripPrefix</span>: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">true</span>
|
|
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">hystrix</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> command</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> myusers-service</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> execution</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> isolation</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> thread</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> timeoutInMilliseconds</span>: ...
|
|
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">myusers-service</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> ribbon</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> NIWSServerListClassName</span>: com.netflix.loadbalancer.ConfigurationBasedServerList
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> listOfServers</span>: http://example1.com,http://example2.com
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> ConnectTimeout</span>: <span class="hl-number">1000</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> ReadTimeout</span>: <span class="hl-number">3000</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> MaxTotalHttpConnections</span>: <span class="hl-number">500</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> MaxConnectionsPerHost</span>: <span class="hl-number">100</span></pre><p>
|
|
</p><p>Another method is specifiying a service-route and configuring a Ribbon client for the <code class="literal">serviceId</code> (doing so requires disabling Eureka support in Ribbon — see <a class="link" href="#spring-cloud-ribbon-without-eureka" title="6.6 Example: How to Use Ribbon Without Eureka">above for more information</a>), as shown in the following example:</p><p><b>application.yml. </b>
|
|
</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">zuul</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> routes</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> users</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> path</span>: /myusers/**
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> serviceId</span>: users
|
|
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">ribbon</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> eureka</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> enabled</span>: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">false</span>
|
|
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">users</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> ribbon</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> listOfServers</span>: example.com,google.com</pre><p>
|
|
</p><p>You can provide a convention between <code class="literal">serviceId</code> and routes by using <code class="literal">regexmapper</code>.
|
|
It uses regular-expression named groups to extract variables from <code class="literal">serviceId</code> and inject them into a route pattern, as shown in the following example:</p><p><b>ApplicationConfiguration.java. </b>
|
|
</p><pre class="programlisting"><em><span class="hl-annotation" style="color: gray">@Bean</span></em>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> PatternServiceRouteMapper serviceRouteMapper() {
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">return</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">new</span> PatternServiceRouteMapper(
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"(?<name>^.+)-(?<version>v.+$)"</span>,
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"${version}/${name}"</span>);
|
|
}</pre><p>
|
|
</p><p>The preceding example means that a <code class="literal">serviceId</code> of <code class="literal">myusers-v1</code> is mapped to route <code class="literal">/v1/myusers/**</code>.
|
|
Any regular expression is accepted, but all named groups must be present in both <code class="literal">servicePattern</code> and <code class="literal">routePattern</code>.
|
|
If <code class="literal">servicePattern</code> does not match a <code class="literal">serviceId</code>, the default behavior is used.
|
|
In the preceding example, a <code class="literal">serviceId</code> of <code class="literal">myusers</code> is mapped to the "/myusers/**" route (with no version detected).
|
|
This feature is disabled by default and only applies to discovered services.</p><p>To add a prefix to all mappings, set <code class="literal">zuul.prefix</code> to a value, such as <code class="literal">/api</code>.
|
|
By default, the proxy prefix is stripped from the request before the request is forwarded by (you can switch this behavior off with <code class="literal">zuul.stripPrefix=false</code>).
|
|
You can also switch off the stripping of the service-specific prefix from individual routes, as shown in the following example:</p><p><b>application.yml. </b>
|
|
</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> zuul</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> routes</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> users</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> path</span>: /myusers/**
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> stripPrefix</span>: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">false</span></pre><p>
|
|
</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Note"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="images/note.png"></td><th align="left">Note</th></tr><tr><td align="left" valign="top"><p><code class="literal">zuul.stripPrefix</code> only applies to the prefix set in <code class="literal">zuul.prefix</code>.
|
|
It does not have any effect on prefixes defined within a given route’s <code class="literal">path</code>.</p></td></tr></table></div><p>In the preceding example, requests to <code class="literal">/myusers/101</code> are forwarded to <code class="literal">/myusers/101</code> on the <code class="literal">users</code> service.</p><p>The <code class="literal">zuul.routes</code> entries actually bind to an object of type <code class="literal">ZuulProperties</code>.
|
|
If you look at the properties of that object, you can see that it also has a <code class="literal">retryable</code> flag.
|
|
Set that flag to <code class="literal">true</code> to have the Ribbon client automatically retry failed requests.
|
|
You can also set that flag to <code class="literal">true</code> when you need to modify the parameters of the retry operations that use the Ribbon client configuration.</p><p>By default, the <code class="literal">X-Forwarded-Host</code> header is added to the forwarded requests.
|
|
To turn it off, set <code class="literal">zuul.addProxyHeaders = false</code>.
|
|
By default, the prefix path is stripped, and the request to the back end picks up a <code class="literal">X-Forwarded-Prefix</code> header (<code class="literal">/myusers</code> in the examples shown earlier).</p><p>If you set a default route (<code class="literal">/</code>), an application with <code class="literal">@EnableZuulProxy</code> could act as a standalone server. For example, <code class="literal">zuul.route.home: /</code> would route all traffic ("/**") to the "home" service.</p><p>If more fine-grained ignoring is needed, you can specify specific patterns to ignore.
|
|
These patterns are evaluated at the start of the route location process, which means prefixes should be included in the pattern to warrant a match.
|
|
Ignored patterns span all services and supersede any other route specification.
|
|
The following example shows how to create ignored patterns:</p><p><b>application.yml. </b>
|
|
</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> zuul</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> ignoredPatterns</span>: /**/admin/**
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> routes</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> users</span>: /myusers/**</pre><p>
|
|
</p><p>The preceding example means that all calls (such as <code class="literal">/myusers/101</code>) are forwarded to <code class="literal">/101</code> on the <code class="literal">users</code> service.
|
|
However, calls including <code class="literal">/admin/</code> do not resolve.</p><div class="warning" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Warning"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Warning]" src="images/warning.png"></td><th align="left">Warning</th></tr><tr><td align="left" valign="top"><p>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:</p></td></tr></table></div><p><b>application.yml. </b>
|
|
</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> zuul</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> routes</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> users</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> path</span>: /myusers/**
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> legacy</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> path</span>: /**</pre><p>
|
|
</p><p>If you were to use a properties file, the <code class="literal">legacy</code> path might end up in front of the <code class="literal">users</code>
|
|
path, rendering the <code class="literal">users</code> path unreachable.</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_zuul_http_client" href="#_zuul_http_client"></a>8.3 Zuul Http Client</h2></div></div></div><p>The default HTTP client used by Zuul is now backed by the Apache HTTP Client instead of the deprecated Ribbon <code class="literal">RestClient</code>.
|
|
To use <code class="literal">RestClient</code> or <code class="literal">okhttp3.OkHttpClient</code>, set <code class="literal">ribbon.restclient.enabled=true</code> or <code class="literal">ribbon.okhttp.enabled=true</code>, respectively.
|
|
If you would like to customize the Apache HTTP client or the OK HTTP client, provide a bean of type <code class="literal">ClosableHttpClient</code> or <code class="literal">OkHttpClient</code>.</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_cookies_and_sensitive_headers" href="#_cookies_and_sensitive_headers"></a>8.4 Cookies and Sensitive Headers</h2></div></div></div><p>You can share headers between services in the same system, but you probably do not want sensitive headers leaking downstream into external servers.
|
|
You can specify a list of ignored headers as part of the route configuration.
|
|
Cookies play a special role, because they have well defined semantics in browsers, and they are always to be treated as sensitive.
|
|
If the consumer of your proxy is a browser, then cookies for downstream services also cause problems for the user, because they all get jumbled up together (all downstream services look like they come from the same place).</p><p>If you are careful with the design of your services, (for example, if only one of the downstream services sets cookies), you might be able to let them flow from the back end all the way up to the caller.
|
|
Also, if your proxy sets cookies and all your back-end services are part of the same system, it can be natural to simply share them (and, for instance, use Spring Session to link them up to some shared state).
|
|
Other than that, any cookies that get set by downstream services are likely to be not useful to the caller, so it is recommended that you make (at least) <code class="literal">Set-Cookie</code> and <code class="literal">Cookie</code> into sensitive headers for routes that are not part of your domain.
|
|
Even for routes that are part of your domain, try to think carefully about what it means before letting cookies flow between them and the proxy.</p><p>The sensitive headers can be configured as a comma-separated list per route, as shown in the following example:</p><p><b>application.yml. </b>
|
|
</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> zuul</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> routes</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> users</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> path</span>: /myusers/**
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> sensitiveHeaders</span>: Cookie,Set-Cookie,Authorization
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> url</span>: https://downstream</pre><p>
|
|
</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Note"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="images/note.png"></td><th align="left">Note</th></tr><tr><td align="left" valign="top"><p>This is the default value for <code class="literal">sensitiveHeaders</code>, so you need not set it unless you want it to be different.
|
|
This is new in Spring Cloud Netflix 1.1 (in 1.0, the user had no control over headers, and all cookies flowed in both directions).</p></td></tr></table></div><p>The <code class="literal">sensitiveHeaders</code> are a blacklist, and the default is not empty.
|
|
Consequently, to make Zuul send all headers (except the <code class="literal">ignored</code> ones), you must explicitly set it to the empty list.
|
|
Doing so is necessary if you want to pass cookie or authorization headers to your back end. The following example shows how to use <code class="literal">sensitiveHeaders</code>:</p><p><b>application.yml. </b>
|
|
</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> zuul</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> routes</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> users</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> path</span>: /myusers/**
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> sensitiveHeaders</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> url</span>: https://downstream</pre><p>
|
|
</p><p>You can also set sensitive headers, by setting <code class="literal">zuul.sensitiveHeaders</code>.
|
|
If <code class="literal">sensitiveHeaders</code> is set on a route, it overrides the global <code class="literal">sensitiveHeaders</code> setting.</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_ignored_headers" href="#_ignored_headers"></a>8.5 Ignored Headers</h2></div></div></div><p>In addition to the route-sensitive headers, you can set a global value called <code class="literal">zuul.ignoredHeaders</code> for values (both request and response) that should be discarded during interactions with downstream services.
|
|
By default, if Spring Security is not on the classpath, these are empty.
|
|
Otherwise, they are initialized to a set of well known <span class="quote">“<span class="quote">security</span>”</span> headers (for example, involving caching) as specified by Spring Security.
|
|
The assumption in this case is that the downstream services might add these headers, too, but we want the values from the proxy.
|
|
To not discard these well known security headers when Spring Security is on the classpath, you can set <code class="literal">zuul.ignoreSecurityHeaders</code> to <code class="literal">false</code>.
|
|
Doing so can be useful if you disabled the HTTP Security response headers in Spring Security and want the values provided by downstream services.</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_management_endpoints" href="#_management_endpoints"></a>8.6 Management Endpoints</h2></div></div></div><p>By default, if you use <code class="literal">@EnableZuulProxy</code> with the Spring Boot Actuator, you enable two additional endpoints:</p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">Routes</li><li class="listitem">Filters</li></ul></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="_routes_endpoint" href="#_routes_endpoint"></a>8.6.1 Routes Endpoint</h3></div></div></div><p>A GET to the routes endpoint at <code class="literal">/routes</code> returns a list of the mapped routes:</p><p><b>GET /routes. </b>
|
|
</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">{</span>
|
|
/stores/**: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"http://localhost:8081"</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">}</span></pre><p>
|
|
</p><p>Additional route details can be requested by adding the <code class="literal">?format=details</code> query string to <code class="literal">/routes</code>.
|
|
Doing so produces the following output:</p><p><b>GET /routes/details. </b>
|
|
</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">{</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"/stores/**"</span>: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">{</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"id"</span>: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"stores"</span><span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">,</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"fullPath"</span>: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"/stores/**"</span><span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">,</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"location"</span>: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"http://localhost:8081"</span><span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">,</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"path"</span>: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"/**"</span><span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">,</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"prefix"</span>: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"/stores"</span><span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">,</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"retryable"</span>: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">false</span><span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">,</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"customSensitiveHeaders"</span>: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">false</span><span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">,</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"prefixStripped"</span>: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">true</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">}</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">}</span></pre><p>
|
|
</p><p>A <code class="literal">POST</code> to <code class="literal">/routes</code> forces a refresh of the existing routes (for example, when there have been changes in the service catalog).
|
|
You can disable this endpoint by setting <code class="literal">endpoints.routes.enabled</code> to <code class="literal">false</code>.</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Note"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="images/note.png"></td><th align="left">Note</th></tr><tr><td align="left" valign="top"><p>the routes should respond automatically to changes in the service catalog, but the <code class="literal">POST</code> to <code class="literal">/routes</code> is a way to force the change
|
|
to happen immediately.</p></td></tr></table></div></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="_filters_endpoint" href="#_filters_endpoint"></a>8.6.2 Filters Endpoint</h3></div></div></div><p>A <code class="literal">GET</code> to the filters endpoint at <code class="literal">/filters</code> returns a map of Zuul filters by type.
|
|
For each filter type in the map, you get a list of all the filters of that type, along with their details.</p></div></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_strangulation_patterns_and_local_forwards" href="#_strangulation_patterns_and_local_forwards"></a>8.7 Strangulation Patterns and Local Forwards</h2></div></div></div><p>A common pattern when migrating an existing application or API is to <span class="quote">“<span class="quote">strangle</span>”</span> old endpoints, slowly replacing them with different implementations.
|
|
The Zuul proxy is a useful tool for this because you can use it to handle all traffic from the clients of the old endpoints but redirect some of the requests to new ones.</p><p>The following example shows the configuration details for a <span class="quote">“<span class="quote">strangle</span>”</span> scenario:</p><p><b>application.yml. </b>
|
|
</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> zuul</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> routes</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> first</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> path</span>: /first/**
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> url</span>: http://first.example.com
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> second</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> path</span>: /second/**
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> url</span>: forward:/second
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> third</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> path</span>: /third/**
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> url</span>: forward:/<span class="hl-number">3</span>rd
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> legacy</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> path</span>: /**
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> url</span>: http://legacy.example.com</pre><p>
|
|
</p><p>In the preceding example, we are strangle the <span class="quote">“<span class="quote">legacy</span>”</span> application, which is mapped to all requests that do not match one of the other patterns.
|
|
Paths in <code class="literal">/first/**</code> have been extracted into a new service with an external URL.
|
|
Paths in <code class="literal">/second/**</code> are forwarded so that they can be handled locally (for example, with a normal Spring <code class="literal">@RequestMapping</code>).
|
|
Paths in <code class="literal">/third/**</code> are also forwarded but with a different prefix (<code class="literal">/third/foo</code> is forwarded to <code class="literal">/3rd/foo</code>).</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Note"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="images/note.png"></td><th align="left">Note</th></tr><tr><td align="left" valign="top"><p>The ignored patterns aren’t completely ignored, they just are not handled by the proxy (so they are also effectively forwarded locally).</p></td></tr></table></div></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_uploading_files_through_zuul" href="#_uploading_files_through_zuul"></a>8.8 Uploading Files through Zuul</h2></div></div></div><p>If you use <code class="literal">@EnableZuulProxy</code>, you can use the proxy paths to upload files and it should work, so long as the files are small.
|
|
For large files there is an alternative path that bypasses the Spring <code class="literal">DispatcherServlet</code> (to avoid multipart processing) in "/zuul/*".
|
|
In other words, if you have <code class="literal">zuul.routes.customers=/customers/**</code>, then you can <code class="literal">POST</code> large files to <code class="literal">/zuul/customers/*</code>.
|
|
The servlet path is externalized via <code class="literal">zuul.servletPath</code>.
|
|
If the proxy route takes you through a Ribbon load balancer, extremely large files also require elevated timeout settings, as shown in the following example:</p><p><b>application.yml. </b>
|
|
</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds</span>: <span class="hl-number">60000</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">ribbon</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> ConnectTimeout</span>: <span class="hl-number">3000</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> ReadTimeout</span>: <span class="hl-number">60000</span></pre><p>
|
|
</p><p>Note that, for streaming to work with large files, you need to use chunked encoding in the request (which some browsers do not do by default), as shown in the following example:</p><pre class="programlisting">$ curl -v -H <span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"Transfer-Encoding: chunked"</span> \
|
|
-F <span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"file=@mylarge.iso"</span> localhost:<span class="hl-number">9999</span>/zuul/simple/file</pre></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_query_string_encoding" href="#_query_string_encoding"></a>8.9 Query String Encoding</h2></div></div></div><p>When processing the incoming request, query params are decoded so that they can be available for possible modifications in Zuul filters.
|
|
They are then re-encoded the back end request is rebuilt in the route filters.
|
|
The result can be different than the original input if (for example) it was encoded with Javascript’s <code class="literal">encodeURIComponent()</code> method.
|
|
While this causes no issues in most cases, some web servers can be picky with the encoding of complex query string.</p><p>To force the original encoding of the query string, it is possible to pass a special flag to <code class="literal">ZuulProperties</code> so that the query string is taken as is with the <code class="literal">HttpServletRequest::getQueryString</code> method, as shown in the following example:</p><p><b>application.yml. </b>
|
|
</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> zuul</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> forceOriginalQueryStringEncoding</span>: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">true</span></pre><p>
|
|
</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Note"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="images/note.png"></td><th align="left">Note</th></tr><tr><td align="left" valign="top"><p>This special flag works only with <code class="literal">SimpleHostRoutingFilter</code>. Also, you loose the ability to easily override
|
|
query parameters with <code class="literal">RequestContext.getCurrentContext().setRequestQueryParams(someOverriddenParameters)</code>, because
|
|
the query string is now fetched directly on the original <code class="literal">HttpServletRequest</code>.</p></td></tr></table></div></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_plain_embedded_zuul" href="#_plain_embedded_zuul"></a>8.10 Plain Embedded Zuul</h2></div></div></div><p>If you use <code class="literal">@EnableZuulServer</code> (instead of <code class="literal">@EnableZuulProxy</code>), you can also run a Zuul server without proxying or selectively switch on parts of the proxying platform.
|
|
Any beans that you add to the application of type <code class="literal">ZuulFilter</code> are installed automatically (as they are with <code class="literal">@EnableZuulProxy</code>) but without any of the proxy filters being added automatically.</p><p>In that case, the routes into the Zuul server are still specified by configuring "zuul.routes.*", but there is no service discovery and no proxying. Consequently, the "serviceId" and "url" settings are ignored.
|
|
The following example maps all paths in "/api/**" to the Zuul filter chain:</p><p><b>application.yml. </b>
|
|
</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> zuul</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> routes</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> api</span>: /api/**</pre><p>
|
|
</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_disable_zuul_filters" href="#_disable_zuul_filters"></a>8.11 Disable Zuul Filters</h2></div></div></div><p>Zuul for Spring Cloud comes with a number of <code class="literal">ZuulFilter</code> beans enabled by default in both proxy and server mode.
|
|
See <a class="link" href="https://github.com/spring-cloud/spring-cloud-netflix/tree/master/spring-cloud-netflix-zuul/src/main/java/org/springframework/cloud/netflix/zuul/filters" target="_top">the Zuul filters package</a> for the list of filters that you can enable.
|
|
If you want to disable one, set <code class="literal">zuul.<SimpleClassName>.<filterType>.disable=true</code>.
|
|
By convention, the package after <code class="literal">filters</code> is the Zuul filter type.
|
|
For example to disable <code class="literal">org.springframework.cloud.netflix.zuul.filters.post.SendResponseFilter</code>, set <code class="literal">zuul.SendResponseFilter.post.disable=true</code>.</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="hystrix-fallbacks-for-routes" href="#hystrix-fallbacks-for-routes"></a>8.12 Providing Hystrix Fallbacks For Routes</h2></div></div></div><p>When a circuit for a given route in Zuul is tripped, you can provide a fallback response by creating a bean of type <code class="literal">FallbackProvider</code>.
|
|
Within this bean, you need to specify the route ID the fallback is for and provide a <code class="literal">ClientHttpResponse</code> to return as a fallback.
|
|
The following example shows a relatively simple <code class="literal">FallbackProvider</code> implementation:</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">class</span> MyFallbackProvider <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">implements</span> FallbackProvider {
|
|
|
|
<em><span class="hl-annotation" style="color: gray">@Override</span></em>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> String getRoute() {
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">return</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"customers"</span>;
|
|
}
|
|
|
|
<em><span class="hl-annotation" style="color: gray">@Override</span></em>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> ClientHttpResponse fallbackResponse(String route, <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">final</span> Throwable cause) {
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">if</span> (cause <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">instanceof</span> HystrixTimeoutException) {
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">return</span> response(HttpStatus.GATEWAY_TIMEOUT);
|
|
} <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">else</span> {
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">return</span> response(HttpStatus.INTERNAL_SERVER_ERROR);
|
|
}
|
|
}
|
|
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">private</span> ClientHttpResponse response(<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">final</span> HttpStatus status) {
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">return</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">new</span> ClientHttpResponse() {
|
|
<em><span class="hl-annotation" style="color: gray">@Override</span></em>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> HttpStatus getStatusCode() <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">throws</span> IOException {
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">return</span> status;
|
|
}
|
|
|
|
<em><span class="hl-annotation" style="color: gray">@Override</span></em>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">int</span> getRawStatusCode() <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">throws</span> IOException {
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">return</span> status.value();
|
|
}
|
|
|
|
<em><span class="hl-annotation" style="color: gray">@Override</span></em>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> String getStatusText() <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">throws</span> IOException {
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">return</span> status.getReasonPhrase();
|
|
}
|
|
|
|
<em><span class="hl-annotation" style="color: gray">@Override</span></em>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">void</span> close() {
|
|
}
|
|
|
|
<em><span class="hl-annotation" style="color: gray">@Override</span></em>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> InputStream getBody() <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">throws</span> IOException {
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">return</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">new</span> ByteArrayInputStream(<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"fallback"</span>.getBytes());
|
|
}
|
|
|
|
<em><span class="hl-annotation" style="color: gray">@Override</span></em>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> HttpHeaders getHeaders() {
|
|
HttpHeaders headers = <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">new</span> HttpHeaders();
|
|
headers.setContentType(MediaType.APPLICATION_JSON);
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">return</span> headers;
|
|
}
|
|
};
|
|
}
|
|
}</pre><p>The following example shows how the route configuration for the previous example might appear:</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">zuul</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> routes</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> customers</span>: /customers/**</pre><p>If you would like to provide a default fallback for all routes, you can create a bean of type <code class="literal">FallbackProvider</code> and have the <code class="literal">getRoute</code> method return <code class="literal">*</code> or <code class="literal">null</code>, as shown in the following example:</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">class</span> MyFallbackProvider <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">implements</span> FallbackProvider {
|
|
<em><span class="hl-annotation" style="color: gray">@Override</span></em>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> String getRoute() {
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">return</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"*"</span>;
|
|
}
|
|
|
|
<em><span class="hl-annotation" style="color: gray">@Override</span></em>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> ClientHttpResponse fallbackResponse(String route, Throwable throwable) {
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">return</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">new</span> ClientHttpResponse() {
|
|
<em><span class="hl-annotation" style="color: gray">@Override</span></em>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> HttpStatus getStatusCode() <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">throws</span> IOException {
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">return</span> HttpStatus.OK;
|
|
}
|
|
|
|
<em><span class="hl-annotation" style="color: gray">@Override</span></em>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">int</span> getRawStatusCode() <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">throws</span> IOException {
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">return</span> <span class="hl-number">200</span>;
|
|
}
|
|
|
|
<em><span class="hl-annotation" style="color: gray">@Override</span></em>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> String getStatusText() <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">throws</span> IOException {
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">return</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"OK"</span>;
|
|
}
|
|
|
|
<em><span class="hl-annotation" style="color: gray">@Override</span></em>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">void</span> close() {
|
|
|
|
}
|
|
|
|
<em><span class="hl-annotation" style="color: gray">@Override</span></em>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> InputStream getBody() <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">throws</span> IOException {
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">return</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">new</span> ByteArrayInputStream(<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"fallback"</span>.getBytes());
|
|
}
|
|
|
|
<em><span class="hl-annotation" style="color: gray">@Override</span></em>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> HttpHeaders getHeaders() {
|
|
HttpHeaders headers = <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">new</span> HttpHeaders();
|
|
headers.setContentType(MediaType.APPLICATION_JSON);
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">return</span> headers;
|
|
}
|
|
};
|
|
}
|
|
}</pre></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_zuul_timeouts" href="#_zuul_timeouts"></a>8.13 Zuul Timeouts</h2></div></div></div><p>If you want to configure the socket timeouts and read timeouts for requests proxied through Zuul, you have two options, based on your configuration:</p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">If Zuul uses service discovery, you need to configure these timeouts with the
|
|
<code class="literal">ribbon.ReadTimeout</code> and <code class="literal">ribbon.SocketTimeout</code> Ribbon properties.</li></ul></div><p>If you have configured Zuul routes by specifying URLs, you need to use
|
|
<code class="literal">zuul.host.connect-timeout-millis</code> and <code class="literal">zuul.host.socket-timeout-millis</code>.</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="zuul-redirect-location-rewrite" href="#zuul-redirect-location-rewrite"></a>8.14 Rewriting the <code class="literal">Location</code> header</h2></div></div></div><p>If Zuul is fronting a web application, you may need to re-write the <code class="literal">Location</code> header when the web application redirects through a HTTP status code of <code class="literal">3XX</code>.
|
|
Otherwise, the browser redirects to the web application’s URL instead of the Zuul URL.
|
|
You can configure a <code class="literal">LocationRewriteFilter</code> Zuul filter to re-write the <code class="literal">Location</code> header to the Zuul’s URL.
|
|
It also adds back the stripped global and route-specific prefixes.
|
|
The following example adds a filter by using a Spring Configuration file:</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">import</span> org.springframework.cloud.netflix.zuul.filters.post.LocationRewriteFilter;
|
|
...
|
|
|
|
<em><span class="hl-annotation" style="color: gray">@Configuration</span></em>
|
|
<em><span class="hl-annotation" style="color: gray">@EnableZuulProxy</span></em>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">class</span> ZuulConfig {
|
|
<em><span class="hl-annotation" style="color: gray">@Bean</span></em>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> LocationRewriteFilter locationRewriteFilter() {
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">return</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">new</span> LocationRewriteFilter();
|
|
}
|
|
}</pre><div class="caution" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Caution"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Caution]" src="images/caution.png"></td><th align="left">Caution</th></tr><tr><td align="left" valign="top"><p>Use this filter carefully. The filter acts on the <code class="literal">Location</code> header of ALL <code class="literal">3XX</code> response codes, which may not be appropriate in all scenarios, such as when redirecting the user to an external URL.</p></td></tr></table></div></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_metrics" href="#_metrics"></a>8.15 Metrics</h2></div></div></div><p>Zuul will provide metrics under the Actuator metrics endpoint for any failures that might occur when routing requests.
|
|
These metrics can be viewed by hitting <code class="literal">/actuator/metrics</code>. The metrics will have a name that has the format
|
|
<code class="literal">ZUUL::EXCEPTION:errorCause:statusCode</code>.</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="zuul-developer-guide" href="#zuul-developer-guide"></a>8.16 Zuul Developer Guide</h2></div></div></div><p>For a general overview of how Zuul works, see <a class="link" href="https://github.com/Netflix/zuul/wiki/How-it-Works" target="_top">the Zuul Wiki</a>.</p><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="_the_zuul_servlet" href="#_the_zuul_servlet"></a>8.16.1 The Zuul Servlet</h3></div></div></div><p>Zuul is implemented as a Servlet. For the general cases, Zuul is embedded into the Spring Dispatch mechanism. This lets Spring MVC be in control of the routing.
|
|
In this case, Zuul buffers requests.
|
|
If there is a need to go through Zuul without buffering requests (for example, for large file uploads), the Servlet is also installed outside of the Spring Dispatcher.
|
|
By default, the servlet has an address of <code class="literal">/zuul</code>.
|
|
This path can be changed with the <code class="literal">zuul.servlet-path</code> property.</p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="_zuul_requestcontext" href="#_zuul_requestcontext"></a>8.16.2 Zuul RequestContext</h3></div></div></div><p>To pass information between filters, Zuul uses a <a class="link" href="https://github.com/Netflix/zuul/blob/1.x/zuul-core/src/main/java/com/netflix/zuul/context/RequestContext.java" target="_top"><code class="literal">RequestContext</code></a>.
|
|
Its data is held in a <code class="literal">ThreadLocal</code> specific to each request.
|
|
Information about where to route requests, errors, and the actual <code class="literal">HttpServletRequest</code> and <code class="literal">HttpServletResponse</code> are stored there.
|
|
The <code class="literal">RequestContext</code> extends <code class="literal">ConcurrentHashMap</code>, so anything can be stored in the context. <a class="link" href="https://github.com/spring-cloud/spring-cloud-netflix/blob/master/spring-cloud-netflix-zuul/src/main/java/org/springframework/cloud/netflix/zuul/filters/support/FilterConstants.java" target="_top"><code class="literal">FilterConstants</code></a> contains the keys used by the filters installed by Spring Cloud Netflix (more on these <a class="link" href="#zuul-developer-guide-enable-filters" title="8.16.4 @EnableZuulServer Filters">later</a>).</p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="_enablezuulproxy_vs_enablezuulserver" href="#_enablezuulproxy_vs_enablezuulserver"></a>8.16.3 <code class="literal">@EnableZuulProxy</code> vs. <code class="literal">@EnableZuulServer</code></h3></div></div></div><p>Spring Cloud Netflix installs a number of filters, depending on which annotation was used to enable Zuul. <code class="literal">@EnableZuulProxy</code> is a superset of <code class="literal">@EnableZuulServer</code>. In other words, <code class="literal">@EnableZuulProxy</code> contains all the filters installed by <code class="literal">@EnableZuulServer</code>. The additional filters in the <span class="quote">“<span class="quote">proxy</span>”</span> enable routing functionality. If you want a <span class="quote">“<span class="quote">blank</span>”</span> Zuul, you should use <code class="literal">@EnableZuulServer</code>.</p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="zuul-developer-guide-enable-filters" href="#zuul-developer-guide-enable-filters"></a>8.16.4 <code class="literal">@EnableZuulServer</code> Filters</h3></div></div></div><p><code class="literal">@EnableZuulServer</code> creates a <code class="literal">SimpleRouteLocator</code> that loads route definitions from Spring Boot configuration files.</p><p>The following filters are installed (as normal Spring Beans):</p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p class="simpara">Pre filters:</p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: circle; "><li class="listitem"><code class="literal">ServletDetectionFilter</code>: Detects whether the request is through the Spring Dispatcher. Sets a boolean with a key of <code class="literal">FilterConstants.IS_DISPATCHER_SERVLET_REQUEST_KEY</code>.</li><li class="listitem"><code class="literal">FormBodyWrapperFilter</code>: Parses form data and re-encodes it for downstream requests.</li><li class="listitem"><code class="literal">DebugFilter</code>: If the <code class="literal">debug</code> request parameter is set, sets <code class="literal">RequestContext.setDebugRouting()</code> and <code class="literal">RequestContext.setDebugRequest()</code> to <code class="literal">true</code>.
|
|
*Route filters:</li><li class="listitem"><code class="literal">SendForwardFilter</code>: Forwards requests by using the Servlet <code class="literal">RequestDispatcher</code>. The forwarding location is stored in the <code class="literal">RequestContext</code> attribute, <code class="literal">FilterConstants.FORWARD_TO_KEY</code>. This is useful for forwarding to endpoints in the current application.</li></ul></div></li><li class="listitem"><p class="simpara">Post filters:</p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: circle; "><li class="listitem"><code class="literal">SendResponseFilter</code>: Writes responses from proxied requests to the current response.</li></ul></div></li><li class="listitem"><p class="simpara">Error filters:</p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: circle; "><li class="listitem"><code class="literal">SendErrorFilter</code>: Forwards to <code class="literal">/error</code> (by default) if <code class="literal">RequestContext.getThrowable()</code> is not null. You can change the default forwarding path (<code class="literal">/error</code>) by setting the <code class="literal">error.path</code> property.</li></ul></div></li></ul></div></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="_enablezuulproxy_filters" href="#_enablezuulproxy_filters"></a>8.16.5 <code class="literal">@EnableZuulProxy</code> Filters</h3></div></div></div><p>Creates a <code class="literal">DiscoveryClientRouteLocator</code> that loads route definitions from a <code class="literal">DiscoveryClient</code> (such as Eureka) as well as from properties. A route is created for each <code class="literal">serviceId</code> from the <code class="literal">DiscoveryClient</code>. As new services are added, the routes are refreshed.</p><p>In addition to the filters described earlier, the following filters are installed (as normal Spring Beans):</p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p class="simpara">Pre filters:</p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: circle; "><li class="listitem"><code class="literal">PreDecorationFilter</code>: Determines where and how to route, depending on the supplied <code class="literal">RouteLocator</code>. It also sets various proxy-related headers for downstream requests.</li></ul></div></li><li class="listitem"><p class="simpara">Route filters:</p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: circle; "><li class="listitem"><p class="simpara"><code class="literal">RibbonRoutingFilter</code>: Uses Ribbon, Hystrix, and pluggable HTTP clients to send requests. Service IDs are found in the <code class="literal">RequestContext</code> attribute, <code class="literal">FilterConstants.SERVICE_ID_KEY</code>. This filter can use different HTTP clients:</p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: square; "><li class="listitem">Apache <code class="literal">HttpClient</code>: The default client.</li><li class="listitem">Squareup <code class="literal">OkHttpClient</code> v3: Enabled by having the <code class="literal">com.squareup.okhttp3:okhttp</code> library on the classpath and setting <code class="literal">ribbon.okhttp.enabled=true</code>.</li><li class="listitem">Netflix Ribbon HTTP client: Enabled by setting <code class="literal">ribbon.restclient.enabled=true</code>. This client has limitations, including that it does not support the PATCH method, but it also has built-in retry.</li></ul></div></li><li class="listitem"><code class="literal">SimpleHostRoutingFilter</code>: Sends requests to predetermined URLs through an Apache HttpClient. URLs are found in <code class="literal">RequestContext.getRouteHost()</code>.</li></ul></div></li></ul></div></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="_custom_zuul_filter_examples" href="#_custom_zuul_filter_examples"></a>8.16.6 Custom Zuul Filter Examples</h3></div></div></div><p>Most of the following "How to Write" examples below are included <a class="link" href="https://github.com/spring-cloud-samples/sample-zuul-filters" target="_top">Sample Zuul Filters</a> project. There are also examples of manipulating the request or response body in that repository.</p><p>This section includes the following examples:</p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><a class="xref" href="#zuul-developer-guide-sample-pre-filter" title="How to Write a Pre Filter">the section called “How to Write a Pre Filter”</a></li><li class="listitem"><a class="xref" href="#zuul-developer-guide-sample-route-filter" title="How to Write a Route Filter">the section called “How to Write a Route Filter”</a></li><li class="listitem"><a class="xref" href="#zuul-developer-guide-sample-post-filter" title="How to Write a Post Filter">the section called “How to Write a Post Filter”</a></li></ul></div><div class="section"><div class="titlepage"><div><div><h4 class="title"><a name="zuul-developer-guide-sample-pre-filter" href="#zuul-developer-guide-sample-pre-filter"></a>How to Write a Pre Filter</h4></div></div></div><p>Pre filters set up data in the <code class="literal">RequestContext</code> for use in filters downstream.
|
|
The main use case is to set information required for route filters.
|
|
The following example shows a Zuul pre filter:</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">class</span> QueryParamPreFilter <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">extends</span> ZuulFilter {
|
|
<em><span class="hl-annotation" style="color: gray">@Override</span></em>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">int</span> filterOrder() {
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">return</span> PRE_DECORATION_FILTER_ORDER - <span class="hl-number">1</span>; <span xmlns:d="http://docbook.org/ns/docbook" class="hl-comment">// run before PreDecoration</span>
|
|
}
|
|
|
|
<em><span class="hl-annotation" style="color: gray">@Override</span></em>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> String filterType() {
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">return</span> PRE_TYPE;
|
|
}
|
|
|
|
<em><span class="hl-annotation" style="color: gray">@Override</span></em>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">boolean</span> shouldFilter() {
|
|
RequestContext ctx = RequestContext.getCurrentContext();
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">return</span> !ctx.containsKey(FORWARD_TO_KEY) <span xmlns:d="http://docbook.org/ns/docbook" class="hl-comment">// a filter has already forwarded</span>
|
|
&& !ctx.containsKey(SERVICE_ID_KEY); <span xmlns:d="http://docbook.org/ns/docbook" class="hl-comment">// a filter has already determined serviceId</span>
|
|
}
|
|
<em><span class="hl-annotation" style="color: gray">@Override</span></em>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> Object run() {
|
|
RequestContext ctx = RequestContext.getCurrentContext();
|
|
HttpServletRequest request = ctx.getRequest();
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">if</span> (request.getParameter(<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"sample"</span>) != null) {
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-comment">// put the serviceId in `RequestContext`</span>
|
|
ctx.put(SERVICE_ID_KEY, request.getParameter(<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"foo"</span>));
|
|
}
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">return</span> null;
|
|
}
|
|
}</pre><p>The preceding filter populates <code class="literal">SERVICE_ID_KEY</code> from the <code class="literal">sample</code> request parameter.
|
|
In practice, you should not do that kind of direct mapping. Instead, the service ID should be looked up from the value of <code class="literal">sample</code> instead.</p><p>Now that <code class="literal">SERVICE_ID_KEY</code> is populated, <code class="literal">PreDecorationFilter</code> does not run and <code class="literal">RibbonRoutingFilter</code> runs.</p><div class="tip" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Tip"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Tip]" src="images/tip.png"></td><th align="left">Tip</th></tr><tr><td align="left" valign="top"><p>If you want to route to a full URL, call <code class="literal">ctx.setRouteHost(url)</code> instead.</p></td></tr></table></div><p>To modify the path to which routing filters forward, set the <code class="literal">REQUEST_URI_KEY</code>.</p></div><div class="section"><div class="titlepage"><div><div><h4 class="title"><a name="zuul-developer-guide-sample-route-filter" href="#zuul-developer-guide-sample-route-filter"></a>How to Write a Route Filter</h4></div></div></div><p>Route filters run after pre filters and make requests to other services.
|
|
Much of the work here is to translate request and response data to and from the model required by the client.
|
|
The following example shows a Zuul route filter:</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">class</span> OkHttpRoutingFilter <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">extends</span> ZuulFilter {
|
|
<em><span class="hl-annotation" style="color: gray">@Autowired</span></em>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">private</span> ProxyRequestHelper helper;
|
|
|
|
<em><span class="hl-annotation" style="color: gray">@Override</span></em>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> String filterType() {
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">return</span> ROUTE_TYPE;
|
|
}
|
|
|
|
<em><span class="hl-annotation" style="color: gray">@Override</span></em>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">int</span> filterOrder() {
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">return</span> SIMPLE_HOST_ROUTING_FILTER_ORDER - <span class="hl-number">1</span>;
|
|
}
|
|
|
|
<em><span class="hl-annotation" style="color: gray">@Override</span></em>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">boolean</span> shouldFilter() {
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">return</span> RequestContext.getCurrentContext().getRouteHost() != null
|
|
&& RequestContext.getCurrentContext().sendZuulResponse();
|
|
}
|
|
|
|
<em><span class="hl-annotation" style="color: gray">@Override</span></em>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> Object run() {
|
|
OkHttpClient httpClient = <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">new</span> OkHttpClient.Builder()
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-comment">// customize</span>
|
|
.build();
|
|
|
|
RequestContext context = RequestContext.getCurrentContext();
|
|
HttpServletRequest request = context.getRequest();
|
|
|
|
String method = request.getMethod();
|
|
|
|
String uri = <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">this</span>.helper.buildZuulRequestURI(request);
|
|
|
|
Headers.Builder headers = <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">new</span> Headers.Builder();
|
|
Enumeration<String> headerNames = request.getHeaderNames();
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">while</span> (headerNames.hasMoreElements()) {
|
|
String name = headerNames.nextElement();
|
|
Enumeration<String> values = request.getHeaders(name);
|
|
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">while</span> (values.hasMoreElements()) {
|
|
String value = values.nextElement();
|
|
headers.add(name, value);
|
|
}
|
|
}
|
|
|
|
InputStream inputStream = request.getInputStream();
|
|
|
|
RequestBody requestBody = null;
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">if</span> (inputStream != null && HttpMethod.permitsRequestBody(method)) {
|
|
MediaType mediaType = null;
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">if</span> (headers.get(<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"Content-Type"</span>) != null) {
|
|
mediaType = MediaType.parse(headers.get(<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"Content-Type"</span>));
|
|
}
|
|
requestBody = RequestBody.create(mediaType, StreamUtils.copyToByteArray(inputStream));
|
|
}
|
|
|
|
Request.Builder builder = <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">new</span> Request.Builder()
|
|
.headers(headers.build())
|
|
.url(uri)
|
|
.method(method, requestBody);
|
|
|
|
Response response = httpClient.newCall(builder.build()).execute();
|
|
|
|
LinkedMultiValueMap<String, String> responseHeaders = <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">new</span> LinkedMultiValueMap<>();
|
|
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">for</span> (Map.Entry<String, List<String>> entry : response.headers().toMultimap().entrySet()) {
|
|
responseHeaders.put(entry.getKey(), entry.getValue());
|
|
}
|
|
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">this</span>.helper.setResponse(response.code(), response.body().byteStream(),
|
|
responseHeaders);
|
|
context.setRouteHost(null); <span xmlns:d="http://docbook.org/ns/docbook" class="hl-comment">// prevent SimpleHostRoutingFilter from running</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">return</span> null;
|
|
}
|
|
}</pre><p>The preceding filter translates Servlet request information into OkHttp3 request information, executes an HTTP request, and translates OkHttp3 response information to the Servlet response.</p></div><div class="section"><div class="titlepage"><div><div><h4 class="title"><a name="zuul-developer-guide-sample-post-filter" href="#zuul-developer-guide-sample-post-filter"></a>How to Write a Post Filter</h4></div></div></div><p>Post filters typically manipulate the response. The following filter adds a random <code class="literal">UUID</code> as the <code class="literal">X-Sample</code> header:</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">class</span> AddResponseHeaderFilter <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">extends</span> ZuulFilter {
|
|
<em><span class="hl-annotation" style="color: gray">@Override</span></em>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> String filterType() {
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">return</span> POST_TYPE;
|
|
}
|
|
|
|
<em><span class="hl-annotation" style="color: gray">@Override</span></em>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">int</span> filterOrder() {
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">return</span> SEND_RESPONSE_FILTER_ORDER - <span class="hl-number">1</span>;
|
|
}
|
|
|
|
<em><span class="hl-annotation" style="color: gray">@Override</span></em>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">boolean</span> shouldFilter() {
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">return</span> true;
|
|
}
|
|
|
|
<em><span class="hl-annotation" style="color: gray">@Override</span></em>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> Object run() {
|
|
RequestContext context = RequestContext.getCurrentContext();
|
|
HttpServletResponse servletResponse = context.getResponse();
|
|
servletResponse.addHeader(<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"X-Sample"</span>, UUID.randomUUID().toString());
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">return</span> null;
|
|
}
|
|
}</pre><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Note"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="images/note.png"></td><th align="left">Note</th></tr><tr><td align="left" valign="top"><p>Other manipulations, such as transforming the response body, are much more complex and computationally intensive.</p></td></tr></table></div></div></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="_how_zuul_errors_work" href="#_how_zuul_errors_work"></a>8.16.7 How Zuul Errors Work</h3></div></div></div><p>If an exception is thrown during any portion of the Zuul filter lifecycle, the error filters are executed.
|
|
The <code class="literal">SendErrorFilter</code> is only run if <code class="literal">RequestContext.getThrowable()</code> is not <code class="literal">null</code>.
|
|
It then sets specific <code class="literal">javax.servlet.error.*</code> attributes in the request and forwards the request to the Spring Boot error page.</p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="_zuul_eager_application_context_loading" href="#_zuul_eager_application_context_loading"></a>8.16.8 Zuul Eager Application Context Loading</h3></div></div></div><p>Zuul internally uses Ribbon for calling the remote URLs.
|
|
By default, Ribbon clients are lazily loaded by Spring Cloud on first call.
|
|
This behavior can be changed for Zuul by using the following configuration, which results eager loading of the child Ribbon related Application contexts at application startup time.
|
|
The following example shows how to enable eager loading:</p><p><b>application.yml. </b>
|
|
</p><pre class="screen">zuul:
|
|
ribbon:
|
|
eager-load:
|
|
enabled: true</pre><p>
|
|
</p></div></div></div><div class="chapter"><div class="titlepage"><div><div><h1 class="title"><a name="_polyglot_support_with_sidecar" href="#_polyglot_support_with_sidecar"></a>9. Polyglot support with Sidecar</h1></div></div></div><p>Do you have non-JVM languages with which you want to take advantage of Eureka, Ribbon, and Config Server?
|
|
The Spring Cloud Netflix Sidecar was inspired by <a class="link" href="https://github.com/Netflix/Prana" target="_top">Netflix Prana</a>.
|
|
It includes an HTTP API to get all of the instances (by host and port) for a given service.
|
|
You can also proxy service calls through an embedded Zuul proxy that gets its route entries from Eureka.
|
|
The Spring Cloud Config Server can be accessed directly through host lookup or through the Zuul Proxy.
|
|
The non-JVM application should implement a health check so the Sidecar can report to Eureka whether the app is up or down.</p><p>To include Sidecar in your project, use the dependency with a group ID of <code class="literal">org.springframework.cloud</code>
|
|
and artifact ID or <code class="literal">spring-cloud-netflix-sidecar</code>.</p><p>To enable the Sidecar, create a Spring Boot application with <code class="literal">@EnableSidecar</code>.
|
|
This annotation includes <code class="literal">@EnableCircuitBreaker</code>, <code class="literal">@EnableDiscoveryClient</code>, and <code class="literal">@EnableZuulProxy</code>.
|
|
Run the resulting application on the same host as the non-JVM application.</p><p>To configure the side car, add <code class="literal">sidecar.port</code> and <code class="literal">sidecar.health-uri</code> to <code class="literal">application.yml</code>.
|
|
The <code class="literal">sidecar.port</code> property is the port on which the non-JVM application listens.
|
|
This is so the Sidecar can properly register the application with Eureka.
|
|
The <code class="literal">sidecar.health-uri</code> is a URI accessible on the non-JVM application that mimics a Spring Boot health indicator.
|
|
It should return a JSON document that resembles the following:</p><p><b>health-uri-document. </b>
|
|
</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">{</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"status"</span>:<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"UP"</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">}</span></pre><p>
|
|
</p><p>The following application.yml example shows sample configuration for a Sidecar application:</p><p><b>application.yml. </b>
|
|
</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">server</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> port</span>: <span class="hl-number">5678</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">spring</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> application</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> name</span>: sidecar
|
|
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">sidecar</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> port</span>: <span class="hl-number">8000</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> health-uri</span>: http://localhost:<span class="hl-number">8000</span>/health.json</pre><p>
|
|
</p><p>The API for the <code class="literal">DiscoveryClient.getInstances()</code> method is <code class="literal">/hosts/{serviceId}</code>.
|
|
The following example response for <code class="literal">/hosts/customers</code> returns two instances on different hosts:</p><p><b>/hosts/customers. </b>
|
|
</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">[</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">{</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"host"</span>: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"myhost"</span><span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">,</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"port"</span>: <span class="hl-number">9000</span><span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">,</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"uri"</span>: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"http://myhost:9000"</span><span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">,</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"serviceId"</span>: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"CUSTOMERS"</span><span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">,</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"secure"</span>: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">false</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">},</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">{</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"host"</span>: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"myhost2"</span><span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">,</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"port"</span>: <span class="hl-number">9000</span><span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">,</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"uri"</span>: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"http://myhost2:9000"</span><span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">,</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"serviceId"</span>: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"CUSTOMERS"</span><span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">,</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"secure"</span>: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">false</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">}</span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">]</span></pre><p>
|
|
</p><p>This API is accessible to the non-JVM application (if the sidecar is on port 5678) at <code class="literal"><a class="link" href="http://localhost:5678/hosts/{serviceId}" target="_top">http://localhost:5678/hosts/{serviceId}</a></code>.</p><p>The Zuul proxy automatically adds routes for each service known in Eureka to <code class="literal">/<serviceId></code>, so the customers service is available at <code class="literal">/customers</code>.
|
|
The non-JVM application can access the customer service at <code class="literal"><a class="link" href="http://localhost:5678/customers" target="_top">http://localhost:5678/customers</a></code> (assuming the sidecar is listening on port 5678).</p><p>If the Config Server is registered with Eureka, the non-JVM application can access it through the Zuul proxy.
|
|
If the <code class="literal">serviceId</code> of the ConfigServer is <code class="literal">configserver</code> and the Sidecar is on port 5678, then it can be accessed at <a class="link" href="http://localhost:5678/configserver" target="_top">http://localhost:5678/configserver</a>.</p><p>Non-JVM applications can take advantage of the Config Server’s ability to return YAML documents.
|
|
For example, a call to <a class="link" href="http://sidecar.local.spring.io:5678/configserver/default-master.yml" target="_top">http://sidecar.local.spring.io:5678/configserver/default-master.yml</a>
|
|
might result in a YAML document resembling the following:</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">eureka</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> client</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> serviceUrl</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> defaultZone</span>: http://localhost:<span class="hl-number">8761</span>/eureka/
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> password</span>: password
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">info</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> description</span>: Spring Cloud Samples
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> url</span>: https://github.com/spring-cloud-samples</pre></div><div class="chapter"><div class="titlepage"><div><div><h1 class="title"><a name="retrying-failed-requests" href="#retrying-failed-requests"></a>10. Retrying Failed Requests</h1></div></div></div><p>Spring Cloud Netflix offers a variety of ways to make HTTP requests.
|
|
You can use a load balanced <code class="literal">RestTemplate</code>, Ribbon, or Feign.
|
|
No matter how you choose to create your HTTP requests, there is always a chance that a request may fail.
|
|
When a request fails, you may want to have the request be retried automatically.
|
|
To do so when using Sping Cloud Netflix, you need to include <a class="link" href="https://github.com/spring-projects/spring-retry" target="_top">Spring Retry</a> on your application’s classpath.
|
|
When Spring Retry is present, load-balanced <code class="literal">RestTemplates</code>, Feign, and Zuul automatically retry any failed requests (assuming your configuration allows doing so).</p><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_backoff_policies" href="#_backoff_policies"></a>10.1 BackOff Policies</h2></div></div></div><p>By default, no backoff policy is used when retrying requests.
|
|
If you would like to configure a backoff policy, you need to create a bean of type <code class="literal">LoadBalancedBackOffPolicyFactory</code>, which is used to create a <code class="literal">BackOffPolicy</code> for a given service, as shown in the following example:</p><pre class="programlisting"><em><span class="hl-annotation" style="color: gray">@Configuration</span></em>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">class</span> MyConfiguration {
|
|
<em><span class="hl-annotation" style="color: gray">@Bean</span></em>
|
|
LoadBalancedBackOffPolicyFactory backOffPolicyFactory() {
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">return</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">new</span> LoadBalancedBackOffPolicyFactory() {
|
|
<em><span class="hl-annotation" style="color: gray">@Override</span></em>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> BackOffPolicy createBackOffPolicy(String service) {
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">return</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">new</span> ExponentialBackOffPolicy();
|
|
}
|
|
};
|
|
}
|
|
}</pre></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_configuration" href="#_configuration"></a>10.2 Configuration</h2></div></div></div><p>When you use Ribbon with Spring Retry, you can control the retry functionality by configuring certain Ribbon properties.
|
|
To do so, set the <code class="literal">client.ribbon.MaxAutoRetries</code>, <code class="literal">client.ribbon.MaxAutoRetriesNextServer</code>, and <code class="literal">client.ribbon.OkToRetryOnAllOperations</code> properties.
|
|
See the <a class="link" href="https://github.com/Netflix/ribbon/wiki/Getting-Started#the-properties-file-sample-clientproperties" target="_top">Ribbon documentation</a> for a description of what these properties do.</p><div class="warning" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Warning"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Warning]" src="images/warning.png"></td><th align="left">Warning</th></tr><tr><td align="left" valign="top"><p>Enabling <code class="literal">client.ribbon.OkToRetryOnAllOperations</code> includes retrying POST requests, which can have an impact
|
|
on the server’s resources, due to the buffering of the request body.</p></td></tr></table></div><p>In addition, you may want to retry requests when certain status codes are returned in the response.
|
|
You can list the response codes you would like the Ribbon client to retry by setting the <code class="literal">clientName.ribbon.retryableStatusCodes</code> property, as shown in the following example:</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">clientName</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> ribbon</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> retryableStatusCodes</span>: <span class="hl-number">404</span>,<span class="hl-number">502</span></pre><p>You can also create a bean of type <code class="literal">LoadBalancedRetryPolicy</code> and implement the <code class="literal">retryableStatusCode</code> method to retry a request given the status code.</p><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="_zuul" href="#_zuul"></a>10.2.1 Zuul</h3></div></div></div><p>You can turn off Zuul’s retry functionality by setting <code class="literal">zuul.retryable</code> to <code class="literal">false</code>.
|
|
You can also disable retry functionality on a route-by-route basis by setting <code class="literal">zuul.routes.routename.retryable</code> to <code class="literal">false</code>.</p></div></div></div><div class="chapter"><div class="titlepage"><div><div><h1 class="title"><a name="_http_clients" href="#_http_clients"></a>11. HTTP Clients</h1></div></div></div><p>Spring Cloud Netflix automatically creates the HTTP client used by Ribbon, Feign, and Zuul for you.
|
|
However, you can also provide your own HTTP clients customized as you need them to be.
|
|
To do so, you can create a bean of type <code class="literal">ClosableHttpClient</code> if you
|
|
are using the Apache Http Cient or <code class="literal">OkHttpClient</code> if you are using OK HTTP.</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Note"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="images/note.png"></td><th align="left">Note</th></tr><tr><td align="left" valign="top"><p>When you create your own HTTP client, you are also responsible for implementing the correct connection management strategies for these clients.
|
|
Doing so improperly can result in resource management issues.</p></td></tr></table></div></div></div></body></html> |