(window.webpackJsonp=window.webpackJsonp||[]).push([[76],{502:function(e,t,a){"use strict";a.r(t);var n=a(56),r=Object(n.a)({},(function(){var e=this,t=e.$createElement,a=e._self._c||t;return a("ContentSlotsDistributor",{attrs:{"slot-key":e.$parent.slotKey}},[a("h1",{attrs:{id:"spring-cloud-netflix"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#spring-cloud-netflix"}},[e._v("#")]),e._v(" Spring Cloud Netflix")]),e._v(" "),a("h2",{attrs:{id:"_1-service-discovery-eureka-clients"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_1-service-discovery-eureka-clients"}},[e._v("#")]),e._v(" 1. Service Discovery: Eureka Clients")]),e._v(" "),a("p",[e._v("Service Discovery is one of the key tenets of a microservice-based architecture.\nTrying to hand-configure each client or some form of convention can be difficult to do and can be brittle.\nEureka is the Netflix Service Discovery Server and Client.\nThe server can be configured and deployed to be highly available, with each server replicating state about the registered services to the others.")]),e._v(" "),a("h3",{attrs:{id:"_1-1-how-to-include-eureka-client"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_1-1-how-to-include-eureka-client"}},[e._v("#")]),e._v(" 1.1. How to Include Eureka Client")]),e._v(" "),a("p",[e._v("To include the Eureka Client in your project, use the starter with a group ID of "),a("code",[e._v("org.springframework.cloud")]),e._v(" and an artifact ID of "),a("code",[e._v("spring-cloud-starter-netflix-eureka-client")]),e._v(".\nSee the "),a("a",{attrs:{href:"https://projects.spring.io/spring-cloud/",target:"_blank",rel:"noopener noreferrer"}},[e._v("Spring Cloud Project page"),a("OutboundLink")],1),e._v(" for details on setting up your build system with the current Spring Cloud Release Train.")]),e._v(" "),a("h3",{attrs:{id:"_1-2-registering-with-eureka"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_1-2-registering-with-eureka"}},[e._v("#")]),e._v(" 1.2. Registering with Eureka")]),e._v(" "),a("p",[e._v("When a client registers with Eureka, it provides meta-data about itself — such as host, port, health indicator URL, home page, and other details.\nEureka receives heartbeat messages from each instance belonging to a service.\nIf the heartbeat fails over a configurable timetable, the instance is normally removed from the registry.")]),e._v(" "),a("p",[e._v("The following example shows a minimal Eureka client application:")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v('@SpringBootApplication\n@RestController\npublic class Application {\n\n @RequestMapping("/")\n public String home() {\n return "Hello world";\n }\n\n public static void main(String[] args) {\n new SpringApplicationBuilder(Application.class).web(true).run(args);\n }\n\n}\n')])])]),a("p",[e._v("Note that the preceding example shows a normal "),a("a",{attrs:{href:"https://projects.spring.io/spring-boot/",target:"_blank",rel:"noopener noreferrer"}},[e._v("Spring Boot"),a("OutboundLink")],1),e._v(" application.\nBy having "),a("code",[e._v("spring-cloud-starter-netflix-eureka-client")]),e._v(" 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:")]),e._v(" "),a("p",[e._v("application.yml")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("eureka:\n client:\n serviceUrl:\n defaultZone: http://localhost:8761/eureka/\n")])])]),a("p",[e._v("In the preceding example, "),a("code",[e._v("defaultZone")]),e._v(" 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).")]),e._v(" "),a("table",[a("thead",[a("tr",[a("th"),e._v(" "),a("th",[e._v("The "),a("code",[e._v("defaultZone")]),e._v(" property is case sensitive and requires camel case because the "),a("code",[e._v("serviceUrl")]),e._v(" property is a "),a("code",[e._v("Map")]),e._v(". Therefore, the "),a("code",[e._v("defaultZone")]),e._v(" property does not follow the normal Spring Boot snake-case convention of "),a("code",[e._v("default-zone")]),e._v(".")])])]),e._v(" "),a("tbody")]),e._v(" "),a("p",[e._v("The default application name (that is, the service ID), virtual host, and non-secure port (taken from the "),a("code",[e._v("Environment")]),e._v(") are "),a("code",[e._v("${spring.application.name}")]),e._v(", "),a("code",[e._v("${spring.application.name}")]),e._v(" and "),a("code",[e._v("${server.port}")]),e._v(", respectively.")]),e._v(" "),a("p",[e._v("Having "),a("code",[e._v("spring-cloud-starter-netflix-eureka-client")]),e._v(" on the classpath makes the app into both a Eureka “instance” (that is, it registers itself) and a “client” (it can query the registry to locate other services).\nThe instance behaviour is driven by "),a("code",[e._v("eureka.instance.*")]),e._v(" configuration keys, but the defaults are fine if you ensure that your application has a value for "),a("code",[e._v("spring.application.name")]),e._v(" (this is the default for the Eureka service ID or VIP).")]),e._v(" "),a("p",[e._v("See "),a("a",{attrs:{href:"https://github.com/spring-cloud/spring-cloud-netflix/tree/main/spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/EurekaInstanceConfigBean.java",target:"_blank",rel:"noopener noreferrer"}},[e._v("EurekaInstanceConfigBean"),a("OutboundLink")],1),e._v(" and "),a("a",{attrs:{href:"https://github.com/spring-cloud/spring-cloud-netflix/tree/main/spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/EurekaClientConfigBean.java",target:"_blank",rel:"noopener noreferrer"}},[e._v("EurekaClientConfigBean"),a("OutboundLink")],1),e._v(" for more details on the configurable options.")]),e._v(" "),a("p",[e._v("To disable the Eureka Discovery Client, you can set "),a("code",[e._v("eureka.client.enabled")]),e._v(" to "),a("code",[e._v("false")]),e._v(". Eureka Discovery Client will also be disabled when "),a("code",[e._v("spring.cloud.discovery.enabled")]),e._v(" is set to "),a("code",[e._v("false")]),e._v(".")]),e._v(" "),a("h3",{attrs:{id:"_1-3-authenticating-with-the-eureka-server"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_1-3-authenticating-with-the-eureka-server"}},[e._v("#")]),e._v(" 1.3. Authenticating with the Eureka Server")]),e._v(" "),a("p",[e._v("HTTP basic authentication is automatically added to your eureka client if one of the "),a("code",[e._v("eureka.client.serviceUrl.defaultZone")]),e._v(" URLs has credentials embedded in it (curl style, as follows: "),a("code",[e._v("[user:[email protected]:8761/eureka](https://user:password@localhost:8761/eureka)")]),e._v(").\nFor more complex needs, you can create a "),a("code",[e._v("@Bean")]),e._v(" of type "),a("code",[e._v("DiscoveryClientOptionalArgs")]),e._v(" and inject "),a("code",[e._v("ClientFilter")]),e._v(" instances into it, all of which is applied to the calls from the client to the server.")]),e._v(" "),a("p",[e._v("When Eureka server requires client side certificate for authentication, the client side certificate and trust store can be configured via properties, as shown in following example:")]),e._v(" "),a("p",[e._v("application.yml")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("eureka:\n client:\n tls:\n enabled: true\n key-store: \n key-store-type: PKCS12\n key-store-password: \n key-password: \n trust-store: \n trust-store-type: PKCS12\n trust-store-password: \n")])])]),a("p",[e._v("The "),a("code",[e._v("eureka.client.tls.enabled")]),e._v(" needs to be true to enable Eureka client side TLS. When "),a("code",[e._v("eureka.client.tls.trust-store")]),e._v(" is omitted, a JVM default trust store is used. The default value for "),a("code",[e._v("eureka.client.tls.key-store-type")]),e._v(" and "),a("code",[e._v("eureka.client.tls.trust-store-type")]),e._v(" is PKCS12. When password properties are omitted, empty password is assumed.")]),e._v(" "),a("table",[a("thead",[a("tr",[a("th"),e._v(" "),a("th",[e._v("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.")])])]),e._v(" "),a("tbody")]),e._v(" "),a("p",[e._v("If you want to customize the RestTemplate used by the Eureka HTTP Client you may want to create a bean of "),a("code",[e._v("EurekaClientHttpRequestFactorySupplier")]),e._v(" and provide your own logic for generating a "),a("code",[e._v("ClientHttpRequestFactory")]),e._v(" instance.")]),e._v(" "),a("h3",{attrs:{id:"_1-4-status-page-and-health-indicator"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_1-4-status-page-and-health-indicator"}},[e._v("#")]),e._v(" 1.4. Status Page and Health Indicator")]),e._v(" "),a("p",[e._v("The status page and health indicators for a Eureka instance default to "),a("code",[e._v("/info")]),e._v(" and "),a("code",[e._v("/health")]),e._v(" respectively, which are the default locations of useful endpoints in a Spring Boot Actuator application.\nYou need to change these, even for an Actuator application if you use a non-default context path or servlet path (such as "),a("code",[e._v("server.servletPath=/custom")]),e._v("). The following example shows the default values for the two settings:")]),e._v(" "),a("p",[e._v("application.yml")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("eureka:\n instance:\n statusPageUrlPath: ${server.servletPath}/info\n healthCheckUrlPath: ${server.servletPath}/health\n")])])]),a("p",[e._v("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.")]),e._v(" "),a("table",[a("thead",[a("tr",[a("th"),e._v(" "),a("th",[e._v("In Dalston it was also required to set the status and health check URLs when changing"),a("br"),e._v("that management context path. This requirement was removed beginning in Edgware.")])])]),e._v(" "),a("tbody")]),e._v(" "),a("h3",{attrs:{id:"_1-5-registering-a-secure-application"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_1-5-registering-a-secure-application"}},[e._v("#")]),e._v(" 1.5. Registering a Secure Application")]),e._v(" "),a("p",[e._v("If your app wants to be contacted over HTTPS, you can set two flags in the "),a("code",[e._v("EurekaInstanceConfigBean")]),e._v(":")]),e._v(" "),a("ul",[a("li",[a("p",[a("code",[e._v("eureka.instance.[nonSecurePortEnabled]=[false]")])])]),e._v(" "),a("li",[a("p",[a("code",[e._v("eureka.instance.[securePortEnabled]=[true]")])])])]),e._v(" "),a("p",[e._v("Doing so makes Eureka publish instance information that shows an explicit preference for secure communication.\nThe Spring Cloud "),a("code",[e._v("DiscoveryClient")]),e._v(" always returns a URI starting with "),a("code",[e._v("https")]),e._v(" for a service configured this way.\nSimilarly, when a service is configured this way, the Eureka (native) instance information has a secure health check URL.")]),e._v(" "),a("p",[e._v("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.\nYou can use placeholders to configure the eureka instance URLs, as shown in the following example:")]),e._v(" "),a("p",[e._v("application.yml")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("eureka:\n instance:\n statusPageUrl: https://${eureka.hostname}/info\n healthCheckUrl: https://${eureka.hostname}/health\n homePageUrl: https://${eureka.hostname}/\n")])])]),a("p",[e._v("(Note that "),a("code",[e._v("${eureka.hostname}")]),e._v(" is a native placeholder only available\nin later versions of Eureka. You could achieve the same thing with\nSpring placeholders as well — for example, by using "),a("code",[e._v("${eureka.instance.hostName}")]),e._v(".)")]),e._v(" "),a("table",[a("thead",[a("tr",[a("th"),e._v(" "),a("th",[e._v("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 “forwarded” headers are intercepted and handled by the application."),a("br"),e._v("If the Tomcat container embedded in a Spring Boot application has explicit configuration for the 'X-Forwarded-\\*` headers, this happens automatically."),a("br"),e._v("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.")])])]),e._v(" "),a("tbody")]),e._v(" "),a("h3",{attrs:{id:"_1-6-eureka-s-health-checks"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_1-6-eureka-s-health-checks"}},[e._v("#")]),e._v(" 1.6. Eureka’s Health Checks")]),e._v(" "),a("p",[e._v("By default, Eureka uses the client heartbeat to determine if a client is up.\nUnless specified otherwise, the Discovery Client does not propagate the current health check status of the application, per the Spring Boot Actuator.\nConsequently, 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.\nAs a consequence, every other application does not send traffic to applications in states other then 'UP'.\nThe following example shows how to enable health checks for the client:")]),e._v(" "),a("p",[e._v("application.yml")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("eureka:\n client:\n healthcheck:\n enabled: true\n")])])]),a("table",[a("thead",[a("tr",[a("th"),e._v(" "),a("th",[a("code",[e._v("eureka.client.healthcheck.enabled=true")]),e._v(" should only be set in "),a("code",[e._v("application.yml")]),e._v(". Setting the value in "),a("code",[e._v("bootstrap.yml")]),e._v(" causes undesirable side effects, such as registering in Eureka with an "),a("code",[e._v("UNKNOWN")]),e._v(" status.")])])]),e._v(" "),a("tbody")]),e._v(" "),a("p",[e._v("If you require more control over the health checks, consider implementing your own "),a("code",[e._v("com.netflix.appinfo.HealthCheckHandler")]),e._v(".")]),e._v(" "),a("h3",{attrs:{id:"_1-7-eureka-metadata-for-instances-and-clients"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_1-7-eureka-metadata-for-instances-and-clients"}},[e._v("#")]),e._v(" 1.7. Eureka Metadata for Instances and Clients")]),e._v(" "),a("p",[e._v("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.\nThere is standard metadata for information such as hostname, IP address, port numbers, the status page, and health check.\nThese are published in the service registry and used by clients to contact the services in a straightforward way.\nAdditional metadata can be added to the instance registration in the "),a("code",[e._v("eureka.instance.metadataMap")]),e._v(", and this metadata is accessible in the remote clients.\nIn general, additional metadata does not change the behavior of the client, unless the client is made aware of the meaning of the metadata.\nThere are a couple of special cases, described later in this document, where Spring Cloud already assigns meaning to the metadata map.")]),e._v(" "),a("h4",{attrs:{id:"_1-7-1-using-eureka-on-cloud-foundry"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_1-7-1-using-eureka-on-cloud-foundry"}},[e._v("#")]),e._v(" 1.7.1. Using Eureka on Cloud Foundry")]),e._v(" "),a("p",[e._v("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).\nThis is not necessarily a barrier to using Eureka.\nHowever, 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.\nYou 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).\nBy default, the "),a("code",[e._v("eureka.instance.instanceId")]),e._v(" is "),a("code",[e._v("vcap.application.instance_id")]),e._v(", as shown in the following example:")]),e._v(" "),a("p",[e._v("application.yml")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("eureka:\n instance:\n hostname: ${vcap.application.uris[0]}\n nonSecurePort: 80\n")])])]),a("p",[e._v("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.\nThis feature is not yet available on Pivotal Web Services ("),a("a",{attrs:{href:"https://run.pivotal.io",target:"_blank",rel:"noopener noreferrer"}},[e._v("PWS"),a("OutboundLink")],1),e._v(").")]),e._v(" "),a("h4",{attrs:{id:"_1-7-2-using-eureka-on-aws"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_1-7-2-using-eureka-on-aws"}},[e._v("#")]),e._v(" 1.7.2. Using Eureka on AWS")]),e._v(" "),a("p",[e._v("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("a",{attrs:{href:"https://github.com/spring-cloud/spring-cloud-netflix/tree/main/spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/EurekaInstanceConfigBean.java",target:"_blank",rel:"noopener noreferrer"}},[e._v("EurekaInstanceConfigBean"),a("OutboundLink")],1),e._v(" as follows:")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v('@Bean\n@Profile("!default")\npublic EurekaInstanceConfigBean eurekaInstanceConfig(InetUtils inetUtils) {\n EurekaInstanceConfigBean bean = new EurekaInstanceConfigBean(inetUtils);\n AmazonInfo info = AmazonInfo.Builder.newBuilder().autoBuild("eureka");\n bean.setDataCenterInfo(info);\n return bean;\n}\n')])])]),a("h4",{attrs:{id:"_1-7-3-changing-the-eureka-instance-id"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_1-7-3-changing-the-eureka-instance-id"}},[e._v("#")]),e._v(" 1.7.3. Changing the Eureka Instance ID")]),e._v(" "),a("p",[e._v("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).\nSpring Cloud Eureka provides a sensible default, which is defined as follows:")]),e._v(" "),a("p",[a("code",[e._v("${spring.cloud.client.hostname}:${spring.application.name}:${spring.application.instance_id:${server.port}}")])]),e._v(" "),a("p",[e._v("An example is "),a("code",[e._v("myhost:myappname:8080")]),e._v(".")]),e._v(" "),a("p",[e._v("By using Spring Cloud, you can override this value by providing a unique identifier in "),a("code",[e._v("eureka.instance.instanceId")]),e._v(", as shown in the following example:")]),e._v(" "),a("p",[e._v("application.yml")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("eureka:\n instance:\n instanceId: ${spring.application.name}:${vcap.application.instance_id:${spring.application.instance_id:${random.value}}}\n")])])]),a("p",[e._v("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.\nIn Cloud Foundry, the "),a("code",[e._v("vcap.application.instance_id")]),e._v(" is populated automatically in a Spring Boot application, so the random value is not needed.")]),e._v(" "),a("h3",{attrs:{id:"_1-8-using-the-eurekaclient"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_1-8-using-the-eurekaclient"}},[e._v("#")]),e._v(" 1.8. Using the EurekaClient")]),e._v(" "),a("p",[e._v("Once you have an application that is a discovery client, you can use it to discover service instances from the "),a("a",{attrs:{href:"#spring-cloud-eureka-server"}},[e._v("Eureka Server")]),e._v(".\nOne way to do so is to use the native "),a("code",[e._v("com.netflix.discovery.EurekaClient")]),e._v(" (as opposed to the Spring Cloud "),a("code",[e._v("DiscoveryClient")]),e._v("), as shown in the following example:")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v('@Autowired\nprivate EurekaClient discoveryClient;\n\npublic String serviceUrl() {\n InstanceInfo instance = discoveryClient.getNextServerFromEureka("STORES", false);\n return instance.getHomePageUrl();\n}\n')])])]),a("table",[a("thead",[a("tr",[a("th"),e._v(" "),a("th",[e._v("Do not use the "),a("code",[e._v("EurekaClient")]),e._v(" in a "),a("code",[e._v("@PostConstruct")]),e._v(" method or in a "),a("code",[e._v("@Scheduled")]),e._v(" method (or anywhere where the "),a("code",[e._v("ApplicationContext")]),e._v(" might not be started yet)."),a("br"),e._v("It is initialized in a "),a("code",[e._v("SmartLifecycle")]),e._v(" (with "),a("code",[e._v("phase=0")]),e._v("), so the earliest you can rely on it being available is in another "),a("code",[e._v("SmartLifecycle")]),e._v(" with a higher phase.")])])]),e._v(" "),a("tbody")]),e._v(" "),a("h4",{attrs:{id:"_1-8-1-eurekaclient-with-jersey"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_1-8-1-eurekaclient-with-jersey"}},[e._v("#")]),e._v(" 1.8.1. EurekaClient with Jersey")]),e._v(" "),a("p",[e._v("By default, EurekaClient uses Spring’s "),a("code",[e._v("RestTemplate")]),e._v(" for HTTP communication.\nIf you wish to use Jersey instead, you need to add the Jersey dependencies to your classpath.\nThe following example shows the dependencies you need to add:")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("\n com.sun.jersey\n jersey-client\n\n\n com.sun.jersey\n jersey-core\n\n\n com.sun.jersey.contribs\n jersey-apache-client4\n\n")])])]),a("h3",{attrs:{id:"_1-9-alternatives-to-the-native-netflix-eurekaclient"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_1-9-alternatives-to-the-native-netflix-eurekaclient"}},[e._v("#")]),e._v(" 1.9. Alternatives to the Native Netflix EurekaClient")]),e._v(" "),a("p",[e._v("You need not use the raw Netflix "),a("code",[e._v("EurekaClient")]),e._v(".\nAlso, it is usually more convenient to use it behind a wrapper of some sort.\nSpring Cloud has support for "),a("a",{attrs:{href:"#spring-cloud-feign"}},[e._v("Feign")]),e._v(" (a REST client builder) and "),a("a",{attrs:{href:"#spring-cloud-ribbon"}},[e._v("Spring "),a("code",[e._v("RestTemplate")])]),e._v(" through the logical Eureka service identifiers (VIPs) instead of physical URLs.")]),e._v(" "),a("p",[e._v("You can also use the "),a("code",[e._v("org.springframework.cloud.client.discovery.DiscoveryClient")]),e._v(", which provides a simple API (not specific to Netflix) for discovery clients, as shown in the following example:")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v('@Autowired\nprivate DiscoveryClient discoveryClient;\n\npublic String serviceUrl() {\n List list = discoveryClient.getInstances("STORES");\n if (list != null && list.size() > 0 ) {\n return list.get(0).getUri();\n }\n return null;\n}\n')])])]),a("h3",{attrs:{id:"_1-10-why-is-it-so-slow-to-register-a-service"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_1-10-why-is-it-so-slow-to-register-a-service"}},[e._v("#")]),e._v(" 1.10. Why Is It so Slow to Register a Service?")]),e._v(" "),a("p",[e._v("Being an instance also involves a periodic heartbeat to the registry\n(through the client’s "),a("code",[e._v("serviceUrl")]),e._v(") with a default duration of 30 seconds.\nA service is not available for discovery by clients until the instance, the server, and the client all have the same metadata in their local\ncache (so it could take 3 heartbeats).\nYou can change the period by setting "),a("code",[e._v("eureka.instance.leaseRenewalIntervalInSeconds")]),e._v(".\nSetting it to a value of less than 30 speeds up the process of getting clients connected to other services.\nIn 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.")]),e._v(" "),a("h3",{attrs:{id:"_1-11-zones"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_1-11-zones"}},[e._v("#")]),e._v(" 1.11. Zones")]),e._v(" "),a("p",[e._v("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.\nTo set that up, you need to configure your Eureka clients correctly.")]),e._v(" "),a("p",[e._v("First, you need to make sure you have Eureka servers deployed to each zone and that\nthey are peers of each other.\nSee the section on "),a("a",{attrs:{href:"#spring-cloud-eureka-server-zones-and-regions"}},[e._v("zones and regions")]),e._v("for more information.")]),e._v(" "),a("p",[e._v("Next, you need to tell Eureka which zone your service is in.\nYou can do so by using the "),a("code",[e._v("metadataMap")]),e._v(" property.\nFor example, if "),a("code",[e._v("service 1")]),e._v(" is deployed to both "),a("code",[e._v("zone 1")]),e._v(" and "),a("code",[e._v("zone 2")]),e._v(", you need to set the following Eureka properties in "),a("code",[e._v("service 1")]),e._v(":")]),e._v(" "),a("p",[a("strong",[e._v("Service 1 in Zone 1")])]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("eureka.instance.metadataMap.zone = zone1\neureka.client.preferSameZoneEureka = true\n")])])]),a("p",[a("strong",[e._v("Service 1 in Zone 2")])]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("eureka.instance.metadataMap.zone = zone2\neureka.client.preferSameZoneEureka = true\n")])])]),a("h3",{attrs:{id:"_1-12-refreshing-eureka-clients"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_1-12-refreshing-eureka-clients"}},[e._v("#")]),e._v(" 1.12. Refreshing Eureka Clients")]),e._v(" "),a("p",[e._v("By default, the "),a("code",[e._v("EurekaClient")]),e._v(" bean is refreshable, meaning the Eureka client properties can be changed and refreshed.\nWhen a refresh occurs clients will be unregistered from the Eureka server and there might be a brief moment of time\nwhere all instance of a given service are not available. One way to eliminate this from happening is to disable\nthe ability to refresh Eureka clients. To do this set "),a("code",[e._v("eureka.client.refresh.enable=false")]),e._v(".")]),e._v(" "),a("h3",{attrs:{id:"_1-13-using-eureka-with-spring-cloud-loadbalancer"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_1-13-using-eureka-with-spring-cloud-loadbalancer"}},[e._v("#")]),e._v(" 1.13. Using Eureka with Spring Cloud LoadBalancer")]),e._v(" "),a("p",[e._v("We offer support for the Spring Cloud LoadBalancer "),a("code",[e._v("ZonePreferenceServiceInstanceListSupplier")]),e._v(".\nThe "),a("code",[e._v("zone")]),e._v(" value from the Eureka instance metadata ("),a("code",[e._v("eureka.instance.metadataMap.zone")]),e._v(") is used for setting the\nvalue of "),a("code",[e._v("spring-cloud-loadbalancer-zone")]),e._v(" property that is used to filter service instances by zone.")]),e._v(" "),a("p",[e._v("If that is missing and if the "),a("code",[e._v("spring.cloud.loadbalancer.eureka.approximateZoneFromHostname")]),e._v(" flag is set to "),a("code",[e._v("true")]),e._v(",\nit can use the domain name from the server hostname as a proxy for the zone.")]),e._v(" "),a("p",[e._v("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).\nWe take "),a("code",[e._v("eureka.client.availabilityZones")]),e._v(", 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 "),a("code",[e._v("eureka.client.region")]),e._v(', which defaults to "us-east-1", for compatibility with native Netflix).')]),e._v(" "),a("h2",{attrs:{id:"_2-service-discovery-eureka-server"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_2-service-discovery-eureka-server"}},[e._v("#")]),e._v(" 2. Service Discovery: Eureka Server")]),e._v(" "),a("p",[e._v("This section describes how to set up a Eureka server.")]),e._v(" "),a("h3",{attrs:{id:"_2-1-how-to-include-eureka-server"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_2-1-how-to-include-eureka-server"}},[e._v("#")]),e._v(" 2.1. How to Include Eureka Server")]),e._v(" "),a("p",[e._v("To include Eureka Server in your project, use the starter with a group ID of "),a("code",[e._v("org.springframework.cloud")]),e._v(" and an artifact ID of "),a("code",[e._v("spring-cloud-starter-netflix-eureka-server")]),e._v(".\nSee the "),a("a",{attrs:{href:"https://projects.spring.io/spring-cloud/",target:"_blank",rel:"noopener noreferrer"}},[e._v("Spring Cloud Project page"),a("OutboundLink")],1),e._v(" for details on setting up your build system with the current Spring Cloud Release Train.")]),e._v(" "),a("table",[a("thead",[a("tr",[a("th"),e._v(" "),a("th",[e._v("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:")])])]),e._v(" "),a("tbody")]),e._v(" "),a("p",[e._v("application.yml")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("spring:\n freemarker:\n template-loader-path: classpath:/templates/\n prefer-file-system-access: false\n")])])]),a("h3",{attrs:{id:"_2-2-how-to-run-a-eureka-server"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_2-2-how-to-run-a-eureka-server"}},[e._v("#")]),e._v(" 2.2. How to Run a Eureka Server")]),e._v(" "),a("p",[e._v("The following example shows a minimal Eureka server:")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("@SpringBootApplication\n@EnableEurekaServer\npublic class Application {\n\n public static void main(String[] args) {\n new SpringApplicationBuilder(Application.class).web(true).run(args);\n }\n\n}\n")])])]),a("p",[e._v("The server has a home page with a UI and HTTP API endpoints for the normal Eureka functionality under "),a("code",[e._v("/eureka/*")]),e._v(".")]),e._v(" "),a("p",[e._v("The following links have some Eureka background reading: "),a("a",{attrs:{href:"https://github.com/cfregly/fluxcapacitor/wiki/NetflixOSS-FAQ#eureka-service-discovery-load-balancer",target:"_blank",rel:"noopener noreferrer"}},[e._v("flux capacitor"),a("OutboundLink")],1),e._v(" and "),a("a",{attrs:{href:"https://groups.google.com/forum/?fromgroups#!topic/eureka_netflix/g3p2r7gHnN0",target:"_blank",rel:"noopener noreferrer"}},[e._v("google group discussion"),a("OutboundLink")],1),e._v(".")]),e._v(" "),a("table",[a("thead",[a("tr",[a("th"),e._v(" "),a("th",[e._v("Due to Gradle’s dependency resolution rules and the lack of a parent bom feature, depending on "),a("code",[e._v("spring-cloud-starter-netflix-eureka-server")]),e._v(" can cause failures on application startup."),a("br"),e._v("To remedy this issue, add the Spring Boot Gradle plugin and import the Spring cloud starter parent bom as follows:"),a("br"),a("br"),e._v("build.gradle"),a("br"),a("br"),a("code",[e._v('
buildscript {
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:{spring-boot-docs-version}")
}
}

apply plugin: "spring-boot"

dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:{spring-cloud-version}"
}
}
')])])])]),e._v(" "),a("tbody")]),e._v(" "),a("h3",{attrs:{id:"_2-3-high-availability-zones-and-regions"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_2-3-high-availability-zones-and-regions"}},[e._v("#")]),e._v(" 2.3. High Availability, Zones and Regions")]),e._v(" "),a("p",[e._v("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).\nClients 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).")]),e._v(" "),a("p",[e._v("By default, every Eureka server is also a Eureka client and requires (at least one) service URL to locate a peer.\nIf 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.")]),e._v(" "),a("h3",{attrs:{id:"_2-4-standalone-mode"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_2-4-standalone-mode"}},[e._v("#")]),e._v(" 2.4. Standalone Mode")]),e._v(" "),a("p",[e._v("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.\nIn 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.\nThe following example shows how to switch off the client-side behavior:")]),e._v(" "),a("p",[e._v("application.yml (Standalone Eureka Server)")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("server:\n port: 8761\n\neureka:\n instance:\n hostname: localhost\n client:\n registerWithEureka: false\n fetchRegistry: false\n serviceUrl:\n defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/\n")])])]),a("p",[e._v("Notice that the "),a("code",[e._v("serviceUrl")]),e._v(" is pointing to the same host as the local instance.")]),e._v(" "),a("h3",{attrs:{id:"_2-5-peer-awareness"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_2-5-peer-awareness"}},[e._v("#")]),e._v(" 2.5. Peer Awareness")]),e._v(" "),a("p",[e._v("Eureka can be made even more resilient and available by running multiple instances and asking them to register with each other.\nIn fact, this is the default behavior, so all you need to do to make it work is add a valid "),a("code",[e._v("serviceUrl")]),e._v(" to a peer, as shown in the following example:")]),e._v(" "),a("p",[e._v("application.yml (Two Peer Aware Eureka Servers)")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("---\nspring:\n profiles: peer1\neureka:\n instance:\n hostname: peer1\n client:\n serviceUrl:\n defaultZone: https://peer2/eureka/\n\n---\nspring:\n profiles: peer2\neureka:\n instance:\n hostname: peer2\n client:\n serviceUrl:\n defaultZone: https://peer1/eureka/\n")])])]),a("p",[e._v("In the preceding example, we have a YAML file that can be used to run the same server on two hosts ("),a("code",[e._v("peer1")]),e._v(" and "),a("code",[e._v("peer2")]),e._v(") by running it in different Spring profiles.\nYou 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 "),a("code",[e._v("/etc/hosts")]),e._v(" to resolve the host names.\nIn fact, the "),a("code",[e._v("eureka.instance.hostname")]),e._v(" is not needed if you are running on a machine that knows its own hostname (by default, it is looked up by using "),a("code",[e._v("java.net.InetAddress")]),e._v(").")]),e._v(" "),a("p",[e._v("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\nthe registrations amongst themselves.\nIf the peers are physically separated (inside a data center or between multiple data centers), then the system can, in principle, survive “split-brain” type failures.\nYou can add multiple peers to a system, and as long as they are all\ndirectly connected to each other, they will synchronize\nthe registrations amongst themselves.")]),e._v(" "),a("p",[e._v("application.yml (Three Peer Aware Eureka Servers)")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("eureka:\n client:\n serviceUrl:\n defaultZone: https://peer1/eureka/,http://peer2/eureka/,http://peer3/eureka/\n\n---\nspring:\n profiles: peer1\neureka:\n instance:\n hostname: peer1\n\n---\nspring:\n profiles: peer2\neureka:\n instance:\n hostname: peer2\n\n---\nspring:\n profiles: peer3\neureka:\n instance:\n hostname: peer3\n")])])]),a("h3",{attrs:{id:"_2-6-when-to-prefer-ip-address"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_2-6-when-to-prefer-ip-address"}},[e._v("#")]),e._v(" 2.6. When to Prefer IP Address")]),e._v(" "),a("p",[e._v("In some cases, it is preferable for Eureka to advertise the IP addresses of services rather than the hostname.\nSet "),a("code",[e._v("eureka.instance.preferIpAddress")]),e._v(" to "),a("code",[e._v("true")]),e._v(" and, when the application registers with eureka, it uses its IP address rather than its hostname.")]),e._v(" "),a("table",[a("thead",[a("tr",[a("th"),e._v(" "),a("th",[e._v("If the hostname cannot be determined by Java, then the IP address is sent to Eureka."),a("br"),e._v("Only explict way of setting the hostname is by setting "),a("code",[e._v("eureka.instance.hostname")]),e._v(" property."),a("br"),e._v("You can set your hostname at the run-time by using an environment variable — for example, "),a("code",[e._v("eureka.instance.hostname=${HOST_NAME}")]),e._v(".")])])]),e._v(" "),a("tbody")]),e._v(" "),a("h3",{attrs:{id:"_2-7-securing-the-eureka-server"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_2-7-securing-the-eureka-server"}},[e._v("#")]),e._v(" 2.7. Securing The Eureka Server")]),e._v(" "),a("p",[e._v("You can secure your Eureka server simply by adding Spring Security to your\nserver’s classpath via "),a("code",[e._v("spring-boot-starter-security")]),e._v(". By default when Spring Security is on the classpath it will require that\na valid CSRF token be sent with every request to the app. Eureka clients will not generally possess a valid\ncross site request forgery (CSRF) token you will need to disable this requirement for the "),a("code",[e._v("/eureka/**")]),e._v(" endpoints.\nFor example:")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v('@EnableWebSecurity\nclass WebSecurityConfig extends WebSecurityConfigurerAdapter {\n\n @Override\n protected void configure(HttpSecurity http) throws Exception {\n http.csrf().ignoringAntMatchers("/eureka/**");\n super.configure(http);\n }\n}\n')])])]),a("p",[e._v("For more information on CSRF see the "),a("a",{attrs:{href:"https://docs.spring.io/spring-security/site/docs/current/reference/htmlsingle/#csrf",target:"_blank",rel:"noopener noreferrer"}},[e._v("Spring Security documentation"),a("OutboundLink")],1),e._v(".")]),e._v(" "),a("p",[e._v("A demo Eureka Server can be found in the Spring Cloud Samples "),a("a",{attrs:{href:"https://github.com/spring-cloud-samples/eureka/tree/Eureka-With-Security",target:"_blank",rel:"noopener noreferrer"}},[e._v("repo"),a("OutboundLink")],1),e._v(".")]),e._v(" "),a("h3",{attrs:{id:"_2-8-jdk-11-support"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_2-8-jdk-11-support"}},[e._v("#")]),e._v(" 2.8. JDK 11 Support")]),e._v(" "),a("p",[e._v("The JAXB modules which the Eureka server depends upon were removed in JDK 11. If you intend to use JDK 11\nwhen running a Eureka server you must include these dependencies in your POM or Gradle file.")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("\n org.glassfish.jaxb\n jaxb-runtime\n\n")])])]),a("h2",{attrs:{id:"_3-configuration-properties"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_3-configuration-properties"}},[e._v("#")]),e._v(" 3. Configuration properties")]),e._v(" "),a("p",[e._v("To see the list of all Spring Cloud Netflix related configuration properties please check "),a("RouterLink",{attrs:{to:"/en/spring-cloud/appendix.html"}},[e._v("the Appendix page")]),e._v(".")],1)])}),[],!1,null,null,null);t.default=r.exports}}]);