121 lines
15 KiB
HTML
121 lines
15 KiB
HTML
<html><head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
|
<title>12. Service Discovery: Eureka Server</title><link rel="stylesheet" type="text/css" href="css/manual-multipage.css"><meta name="generator" content="DocBook XSL Stylesheets V1.79.1"><link rel="home" href="multi_spring-cloud.html" title="Spring Cloud"><link rel="up" href="multi__spring_cloud_netflix.html" title="Part III. Spring Cloud Netflix"><link rel="prev" href="multi__service_discovery_eureka_clients.html" title="11. Service Discovery: Eureka Clients"><link rel="next" href="multi__circuit_breaker_hystrix_clients.html" title="13. Circuit Breaker: Hystrix Clients"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">12. Service Discovery: Eureka Server</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="multi__service_discovery_eureka_clients.html">Prev</a> </td><th width="60%" align="center">Part III. Spring Cloud Netflix</th><td width="20%" align="right"> <a accesskey="n" href="multi__circuit_breaker_hystrix_clients.html">Next</a></td></tr></table><hr></div><div class="chapter"><div class="titlepage"><div><div><h2 class="title"><a name="spring-cloud-eureka-server" href="#spring-cloud-eureka-server"></a>12. Service Discovery: Eureka Server</h2></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>12.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="https://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 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 project already uses Thymeleaf as its template engine, the Freemarker templates of the Eureka server may not be loaded correctly. In this case it is necessary to configure the template loader manually:</p></td></tr></table></div><p><b>application.yml. </b>
|
|
</p><pre class="screen">spring:
|
|
freemarker:
|
|
template-loader-path: classpath:/templates/
|
|
prefer-file-system-access: false</pre><p>
|
|
</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>12.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>12.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="multi_spring-cloud-ribbon.html" title="16. 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>12.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>12.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>12.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>12.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 class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_jdk_11_support" href="#_jdk_11_support"></a>12.8 JDK 11 Support</h2></div></div></div><p>The JAXB modules which the Eureka server depends upon were removed in JDK 11. If you intend to use JDK 11
|
|
when running a Eureka server you must include these dependencies in your POM or Gradle file.</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.glassfish.jaxb<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>jaxb-runtime<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="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="multi__service_discovery_eureka_clients.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="multi__spring_cloud_netflix.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="multi__circuit_breaker_hystrix_clients.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">11. Service Discovery: Eureka Clients </td><td width="20%" align="center"><a accesskey="h" href="multi_spring-cloud.html">Home</a></td><td width="40%" align="right" valign="top"> 13. Circuit Breaker: Hystrix Clients</td></tr></table></div></body></html> |