### Minimal Eureka Server Setup Source: https://github.com/spring-cloud/spring-cloud-netflix/blob/main/docs/modules/ROOT/pages/spring-cloud-netflix.adoc A minimal Spring Boot application annotated with `@EnableEurekaServer` to run a Eureka server. ```java @SpringBootApplication @EnableEurekaServer public class Application { public static void main(String[] args) { SpringApplication.run(CustomerServiceTestApplication.class, args); } } ``` -------------------------------- ### Three Peer Aware Eureka Servers Configuration Source: https://github.com/spring-cloud/spring-cloud-netflix/blob/main/docs/modules/ROOT/pages/spring-cloud-netflix.adoc Configure three Eureka servers for peer awareness, enabling them to synchronize registrations and enhance availability. This example demonstrates a more complex peer-to-peer setup. ```yaml eureka: client: serviceUrl: defaultZone: https://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 ``` -------------------------------- ### Configure Eureka Instance for Cloud Foundry Source: https://github.com/spring-cloud/spring-cloud-netflix/blob/main/docs/modules/ROOT/pages/spring-cloud-netflix.adoc When deploying to Cloud Foundry, explicitly set the hostname and port numbers to use the Cloud Foundry router. This example configures the non-secure port and uses a Cloud Foundry environment variable for the hostname. ```yaml eureka: instance: hostname: ${vcap.application.uris[0]} nonSecurePort: 80 ``` -------------------------------- ### Two Peer Aware Eureka Servers Configuration Source: https://github.com/spring-cloud/spring-cloud-netflix/blob/main/docs/modules/ROOT/pages/spring-cloud-netflix.adoc Configure two Eureka servers to be peer-aware, allowing them to register with each other for increased resilience and availability. This setup uses Spring profiles to differentiate between the two peers. ```yaml --- spring: profiles: peer1 eureka: instance: hostname: peer1 client: serviceUrl: defaultZone: https://peer2/eureka/ --- spring: profiles: peer2 eureka: instance: hostname: peer2 client: serviceUrl: defaultZone: https://peer1/eureka/ ``` -------------------------------- ### Override Eureka Instance ID Source: https://github.com/spring-cloud/spring-cloud-netflix/blob/main/docs/modules/ROOT/pages/spring-cloud-netflix.adoc Override the default Eureka instance ID to provide a unique identifier. This example shows how to use the application name, Cloud Foundry instance ID (if available), or a random value to ensure uniqueness, especially when multiple instances run on the same host. ```yaml eureka: instance: instanceId: ${spring.application.name}:${vcap.application.instance_id:${spring.application.instance_id:${random.value}}} ``` -------------------------------- ### Using Native EurekaClient for Service Discovery Source: https://github.com/spring-cloud/spring-cloud-netflix/blob/main/docs/modules/ROOT/pages/spring-cloud-netflix.adoc Demonstrates how to inject and use the native EurekaClient to retrieve service instance information. Avoid using this in @PostConstruct or @Scheduled methods; prefer SmartLifecycle with a higher phase. ```java @Autowired private EurekaClient discoveryClient; public String serviceUrl() { InstanceInfo instance = discoveryClient.getNextServerFromEureka("STORES", false); return instance.getHomePageUrl(); } ``` -------------------------------- ### Using Spring Cloud DiscoveryClient for Service Discovery Source: https://github.com/spring-cloud/spring-cloud-netflix/blob/main/docs/modules/ROOT/pages/spring-cloud-netflix.adoc Shows how to use the Spring Cloud DiscoveryClient, a Netflix-agnostic API, to find service instances. This is generally more convenient than using the raw EurekaClient. ```java @Autowired private DiscoveryClient discoveryClient; public String serviceUrl() { List list = discoveryClient.getInstances("STORES"); if (list != null && list.size() > 0 ) { return list.get(0).getUri(); } return null; } ``` -------------------------------- ### Gradle Build Configuration for Eureka Server Source: https://github.com/spring-cloud/spring-cloud-netflix/blob/main/docs/modules/ROOT/pages/spring-cloud-netflix.adoc Configure Gradle to include the Spring Boot plugin and import the Spring Cloud starter parent BOM to resolve dependency issues with `spring-cloud-starter-netflix-eureka-server`. ```gradle 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}" } } ``` -------------------------------- ### Basic Spring Boot Application with Eureka Client Source: https://github.com/spring-cloud/spring-cloud-netflix/blob/main/docs/src/main/asciidoc/sagan-index.adoc This is a basic Spring Boot application that will automatically attempt to connect to a Eureka server at the default URL if the eureka-client starter is included. ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @RestController public class Application { @RequestMapping("/") public String home() { return "Hello World"; } public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` -------------------------------- ### Configure Eureka Instance for AWS Source: https://github.com/spring-cloud/spring-cloud-netflix/blob/main/docs/modules/ROOT/pages/spring-cloud-netflix.adoc Customize `EurekaInstanceConfigBean` to make the Eureka instance AWS-aware when deploying to an AWS cloud. This involves building `AmazonInfo` and setting it on the bean. ```java @Bean @Profile(!"default") public EurekaInstanceConfigBean eurekaInstanceConfig(InetUtils inetUtils) { EurekaInstanceConfigBean bean = new EurekaInstanceConfigBean(inetUtils); AmazonInfo info = AmazonInfo.Builder.newBuilder().autoBuild("eureka"); bean.setDataCenterInfo(info); return bean; } ``` -------------------------------- ### Configure Eureka Server Warmup Time Source: https://github.com/spring-cloud/spring-cloud-netflix/blob/main/docs/modules/ROOT/pages/spring-cloud-netflix.adoc Set `eureka.server.defaultOpenForTrafficCount=0` to enable the warmup time for the Eureka server, ensuring `waitTimeInMsWhenSyncEmpty` is considered. ```properties eureka.server.defaultOpenForTrafficCount=0 ``` -------------------------------- ### Minimal Eureka Client Application Source: https://github.com/spring-cloud/spring-cloud-netflix/blob/main/docs/modules/ROOT/pages/spring-cloud-netflix.adoc A basic Spring Boot application that automatically registers with Eureka when the Eureka client starter is included. Ensure 'spring.application.name' is set for the service ID. ```java @SpringBootApplication @RestController public class Application { @RequestMapping("/") public String home() { return "Hello world"; } public static void main(String[] args) { SpringApplication.run(CustomerServiceTestApplication.class, args); } } ``` -------------------------------- ### Configure Eureka Client TLS Source: https://github.com/spring-cloud/spring-cloud-netflix/blob/main/docs/modules/ROOT/pages/spring-cloud-netflix.adoc Set `eureka.client.tls.enabled` to true to enable client-side TLS. If `eureka.client.tls.trust-store` is omitted, the JVM default trust store is used. The default types for key-store and trust-store are PKCS12. Empty passwords are assumed if omitted. ```yaml eureka: client: tls: enabled: true key-store-type: PKCS12 key-store-password: key-password: trust-store: trust-store-type: PKCS12 trust-store-password: ``` -------------------------------- ### Configure Eureka Instance Status and Health URLs Source: https://github.com/spring-cloud/spring-cloud-netflix/blob/main/docs/modules/ROOT/pages/spring-cloud-netflix.adoc Set the `statusPageUrlPath` and `healthCheckUrlPath` for Eureka instances. These defaults to `/info` and `/health` respectively, aligning with Spring Boot Actuator defaults. Adjust if using a non-default server servlet path. ```yaml eureka: instance: statusPageUrlPath: ${server.servletPath}/info healthCheckUrlPath: ${server.servletPath}/health ``` -------------------------------- ### Prefer IP Address for Eureka Instance Registration Source: https://github.com/spring-cloud/spring-cloud-netflix/blob/main/docs/modules/ROOT/pages/spring-cloud-netflix.adoc Configure Eureka to advertise IP addresses instead of hostnames for service registration. Set `eureka.instance.preferIpAddress` to `true` to enable this behavior. ```properties eureka.instance.preferIpAddress=true ``` -------------------------------- ### Configure Eureka Server Template Loader Source: https://github.com/spring-cloud/spring-cloud-netflix/blob/main/docs/modules/ROOT/pages/spring-cloud-netflix.adoc If Thymeleaf is used, manually configure the Freemarker template loader for the Eureka server to prevent template loading issues. ```yaml spring: freemarker: template-loader-path: classpath:/templates/ prefer-file-system-access: false ``` -------------------------------- ### Configure Eureka Client Service URL Source: https://github.com/spring-cloud/spring-cloud-netflix/blob/main/docs/modules/ROOT/pages/spring-cloud-netflix.adoc Specifies the default zone for the Eureka client to connect to the Eureka server. Note that 'defaultZone' is case-sensitive and does not follow the Spring Boot snake-case convention. ```yaml eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ ``` -------------------------------- ### Dependencies for Jersey Client in EurekaClient Source: https://github.com/spring-cloud/spring-cloud-netflix/blob/main/docs/modules/ROOT/pages/spring-cloud-netflix.adoc Specifies the Maven dependencies required to use JerseyClient with EurekaClient. Ensure these are on the classpath if you intend to use Jersey. ```xml com.sun.jersey jersey-client com.sun.jersey jersey-core com.sun.jersey.contribs jersey-apache-client4 ``` -------------------------------- ### Configure Eureka Client TLS Authentication Source: https://github.com/spring-cloud/spring-cloud-netflix/blob/main/docs/modules/ROOT/pages/spring-cloud-netflix.adoc Enables TLS for Eureka client communication and specifies the keystore for client-side certificate authentication. This is used when the Eureka server requires client-side certificates. ```yaml eureka: client: tls: enabled: true key-store: ``` -------------------------------- ### Register Secure Eureka Application Source: https://github.com/spring-cloud/spring-cloud-netflix/blob/main/docs/modules/ROOT/pages/spring-cloud-netflix.adoc Configure Eureka to publish instance information preferring secure communication by disabling non-secure ports and enabling secure ports. This ensures the DiscoveryClient returns HTTPS URIs and the instance has a secure health check URL. ```yaml eureka: instance: statusPageUrl: https://${eureka.hostname}/info healthCheckUrl: https://${eureka.hostname}/health homePageUrl: https://${eureka.hostname}/ ``` -------------------------------- ### Standalone Eureka Server Configuration Source: https://github.com/spring-cloud/spring-cloud-netflix/blob/main/docs/modules/ROOT/pages/spring-cloud-netflix.adoc Configure Eureka Server to run in standalone mode by disabling client-side registration and registry fetching. This is useful when you don't want the server to act as a client to other Eureka instances. ```yaml server: port: 8761 eureka: instance: hostname: localhost client: registerWithEureka: false fetchRegistry: false serviceUrl: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ ``` -------------------------------- ### Add JAXB Runtime Dependency for JDK 11+ Eureka Server Source: https://github.com/spring-cloud/spring-cloud-netflix/blob/main/docs/modules/ROOT/pages/spring-cloud-netflix.adoc Include this dependency in your POM or Gradle file if you are running a Eureka server with JDK 11 or later, as JAXB modules were removed in JDK 11. ```xml org.glassfish.jaxb jaxb-runtime ``` -------------------------------- ### Enable Eureka Client Health Checks Source: https://github.com/spring-cloud/spring-cloud-netflix/blob/main/docs/modules/ROOT/pages/spring-cloud-netflix.adoc Enable health checks for the Eureka client to propagate application status to Eureka. This ensures that only applications in an 'UP' state receive traffic. Set this in `application.yml`, not `bootstrap.yml`, to avoid registration issues. ```yaml eureka: client: healthcheck: enabled: true ``` -------------------------------- ### Configure Eureka Client Refresh Disabled Source: https://github.com/spring-cloud/spring-cloud-netflix/blob/main/docs/modules/ROOT/pages/spring-cloud-netflix.adoc To disable Eureka client refresh, set `eureka.client.refresh.enable=false`. This prevents brief service unavailability during refreshes. ```properties eureka.client.refresh.enable=false ``` -------------------------------- ### Customize Eureka RestClient RequestConfig Source: https://github.com/spring-cloud/spring-cloud-netflix/blob/main/docs/modules/ROOT/pages/spring-cloud-netflix.adoc Provide a custom `EurekaClientHttpRequestFactorySupplier.RequestConfigCustomizer` bean to modify the `RequestConfig` for the underlying Apache HttpClient 5. ```java @Configuration public class RestClientConfiguration { @Bean EurekaClientHttpRequestFactorySupplier.RequestConfigCustomizer requestConfigCustomizer() { return builder -> builder.setProtocolUpgradeEnabled(false); } } ``` -------------------------------- ### Customize Eureka RestClient Timeouts Source: https://github.com/spring-cloud/spring-cloud-netflix/blob/main/docs/modules/ROOT/pages/spring-cloud-netflix.adoc Configure specific timeout values for the Eureka HTTP Client's RestClient. All timeout properties are in milliseconds. Default timeouts are set to 3 minutes. ```yaml eureka: client: restclient: timeout: connect-timeout: 5000 connect-request-timeout: 8000 socket-timeout: 10000 rest-template-timeout: connect-timeout: 5000 connect-request-timeout: 8000 socket-timeout: 10000 ``` -------------------------------- ### Securing Eureka Server with Spring Security Source: https://github.com/spring-cloud/spring-cloud-netflix/blob/main/docs/modules/ROOT/pages/spring-cloud-netflix.adoc Secure your Eureka server by adding Spring Security to the classpath and disabling CSRF protection for Eureka-specific endpoints. This ensures that only authenticated requests are processed by the Eureka server. ```java @Bean public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { http.authorizeHttpRequests((authz) -> authz .anyRequest().authenticated()) .httpBasic(withDefaults()); http.csrf().ignoringRequestMatchers("/eureka/**"); return http.build(); } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.