Service Discovery
- Service Discovery Using Eureka
- Spring Cloud - Creating Eureka Server
- Spring Cloud - Creating Eureka Client
- Eureka Client Consumer Example
- Spring Cloud - Eureka Server API
- Spring Cloud - Eureka High Availablity
- Spring Cloud - Eureka Zone Awareness
Synchronous Communication
- Synchronous Communication with Feign Client
- Spring Cloud - Feign Client with Eureka
- Spring Cloud - Testing Feign Client
Synchronous Communication
- Spring Cloud - Gateway
- Spring Cloud - Creating Gateway Application
- Spring Cloud - Testing Gateway Application
- Spring Cloud - Monitoring Gateway Application
Miscellaneous
- Spring Cloud - Load Balancer
- Circuit Breaker using Hystrix
- Streams with Apache Kafka
- Distributed Logging using ELK and Sleuth
Spring Cloud Useful Resources
Spring Cloud - Eureka High Availability
We have been using Eureka server in standalone mode. However, in a Production environment, we should ideally have more than one instance of the Eureka server running. This ensures that even if one machine goes down, the machine with another Eureka server keeps on running.
Let us try to setup Eureka server in high-availability mode. For our example, we will use two instances. For this, we will use the following application-ha.yml to start the Eureka server.
spring:
application:
name: eureka-server
server:
port: ${app_port}
eureka:
client:
serviceURL:
defaultZone: ${eureka_other_server_url}
Points to note −
We have parameterized the port so that we can start multiple instances using same the config file.
We have added address, again parameterized, to pass the Eureka server address.
We are naming the app as Eureka-Server.
Let us now recompile our Eureka server project using maven install command
.[INFO] Scanning for projects... [INFO] [INFO] [1m------------------< [0;36mcom.tutorialspoint:eurekaserver[0;1m >-------------------[m [INFO] [1mBuilding eurekaserver 0.0.1-SNAPSHOT[m [INFO] from pom.xml [INFO] [1m--------------------------------[ jar ]---------------------------------[m [INFO] [INFO] [1m--- [0;32mresources:3.3.1:resources[m [1m(default-resources)[m @ [36meurekaserver[0;1m ---[m [INFO] Copying 2 resources from src\main\resources to target\classes [INFO] Copying 0 resource from src\main\resources to target\classes [INFO] [INFO] [1m--- [0;32mcompiler:3.14.0:compile[m [1m(default-compile)[m @ [36meurekaserver[0;1m ---[m [INFO] Recompiling the module because of [1madded or removed source files[m. [INFO] Compiling 1 source file with javac [debug parameters release 21] to target\classes [INFO] [INFO] [1m--- [0;32mresources:3.3.1:testResources[m [1m(default-testResources)[m @ [36meurekaserver[0;1m ---[m [INFO] skip non existing resourceDirectory D:\Projects\eurekaserver\src\test\resources [INFO] [INFO] [1m--- [0;32mcompiler:3.14.0:testCompile[m [1m(default-testCompile)[m @ [36meurekaserver[0;1m ---[m [INFO] Recompiling the module because of [1mchanged dependency[m. [INFO] Compiling 1 source file with javac [debug parameters release 21] to target\test-classes [INFO] [INFO] [1m--- [0;32msurefire:3.5.4:test[m [1m(default-test)[m @ [36meurekaserver[0;1m ---[m [INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] Running com.tutorialspoint.eurekaserver.[1mEurekaserverApplicationTests[m 17:10:29.063 [main] INFO org.springframework.test.context.support.AnnotationConfigContextLoaderUtils -- Could not detect default configuration classes for test class [com.tutorialspoint.eurekaserver.EurekaserverApplicationTests]: EurekaserverApplicationTests does not declare any static, non-private, non-final, nested classes annotated with @Configuration. 17:10:29.171 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper -- Found @SpringBootConfiguration com.tutorialspoint.eurekaserver.EurekaserverApplication for test class com.tutorialspoint.eurekaserver.EurekaserverApplicationTests . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v3.5.6) 2025-10-07T17:10:29.839+05:30 INFO 54892 --- [eurekaserver] [ main] c.t.e.EurekaserverApplicationTests : Starting EurekaserverApplicationTests using Java 21.0.6 with PID 54892 (started by mahes in D:\Projects\eurekaserver) 2025-10-07T17:10:29.840+05:30 INFO 54892 --- [eurekaserver] [ main] c.t.e.EurekaserverApplicationTests : No active profile set, falling back to 1 default profile: "default" 2025-10-07T17:10:30.916+05:30 INFO 54892 --- [eurekaserver] [ main] o.s.cloud.context.scope.GenericScope : BeanFactory id=a28ee115-cbd0-38a4-9689-454190eff47b 2025-10-07T17:10:31.442+05:30 INFO 54892 --- [eurekaserver] [ main] o.s.v.b.OptionalValidatorFactoryBean : Failed to set up a Bean Validation provider: jakarta.validation.NoProviderFoundException: Unable to create a Configuration, because no Jakarta Bean Validation provider could be found. Add a provider like Hibernate Validator (RI) to your classpath. 2025-10-07T17:10:32.440+05:30 WARN 54892 --- [eurekaserver] [ main] iguration$LoadBalancerCaffeineWarnLogger : Spring Cloud LoadBalancer is currently working with the default cache. While this cache implementation is useful for development and tests, it's recommended to use Caffeine cache in production.You can switch to using Caffeine cache, by adding it and org.springframework.cache.caffeine.CaffeineCacheManager to the classpath. 2025-10-07T17:10:32.702+05:30 INFO 54892 --- [eurekaserver] [ main] o.s.c.n.eureka.InstanceInfoFactory : Setting initial instance status as: STARTING 2025-10-07T17:10:32.723+05:30 INFO 54892 --- [eurekaserver] [ main] com.netflix.discovery.DiscoveryClient : Initializing Eureka in region us-east-1 2025-10-07T17:10:32.724+05:30 INFO 54892 --- [eurekaserver] [ main] com.netflix.discovery.DiscoveryClient : Client configured to neither register nor query for data. 2025-10-07T17:10:32.728+05:30 INFO 54892 --- [eurekaserver] [ main] com.netflix.discovery.DiscoveryClient : Discovery Client initialized at timestamp 1759837232725 with initial instances count: 0 2025-10-07T17:10:32.813+05:30 INFO 54892 --- [eurekaserver] [ main] c.n.eureka.DefaultEurekaServerContext : Initializing ... 2025-10-07T17:10:32.815+05:30 INFO 54892 --- [eurekaserver] [ main] c.n.eureka.cluster.PeerEurekaNodes : Adding new peer nodes [http://localhost:8761/eureka/] 2025-10-07T17:10:33.009+05:30 INFO 54892 --- [eurekaserver] [ main] c.n.d.provider.DiscoveryJerseyProvider : Using JSON encoding codec LegacyJacksonJson 2025-10-07T17:10:33.009+05:30 INFO 54892 --- [eurekaserver] [ main] c.n.d.provider.DiscoveryJerseyProvider : Using JSON decoding codec LegacyJacksonJson 2025-10-07T17:10:33.010+05:30 INFO 54892 --- [eurekaserver] [ main] c.n.d.provider.DiscoveryJerseyProvider : Using XML encoding codec XStreamXml 2025-10-07T17:10:33.010+05:30 INFO 54892 --- [eurekaserver] [ main] c.n.d.provider.DiscoveryJerseyProvider : Using XML decoding codec XStreamXml 2025-10-07T17:10:33.086+05:30 INFO 54892 --- [eurekaserver] [ main] c.n.eureka.cluster.PeerEurekaNodes : Replica node URL: http://localhost:8761/eureka/ 2025-10-07T17:10:33.095+05:30 INFO 54892 --- [eurekaserver] [ main] c.n.e.registry.AbstractInstanceRegistry : Finished initializing remote region registries. All known remote regions: [] 2025-10-07T17:10:33.095+05:30 INFO 54892 --- [eurekaserver] [ main] c.n.eureka.DefaultEurekaServerContext : Initialized 2025-10-07T17:10:33.193+05:30 INFO 54892 --- [eurekaserver] [ main] o.s.b.a.e.web.EndpointLinksResolver : Exposing 1 endpoint beneath base path '/actuator' 2025-10-07T17:10:33.254+05:30 INFO 54892 --- [eurekaserver] [ main] o.s.c.n.e.s.EurekaServiceRegistry : Registering application EUREKASERVER with eureka with status UP 2025-10-07T17:10:33.269+05:30 INFO 54892 --- [eurekaserver] [ main] c.t.e.EurekaserverApplicationTests : Started EurekaserverApplicationTests in 3.732 seconds (process running for 4.832) 2025-10-07T17:10:33.271+05:30 INFO 54892 --- [eurekaserver] [ Thread-10] o.s.c.n.e.server.EurekaServerBootstrap : isAws returned false 2025-10-07T17:10:33.273+05:30 INFO 54892 --- [eurekaserver] [ Thread-10] o.s.c.n.e.server.EurekaServerBootstrap : Initialized server context 2025-10-07T17:10:33.273+05:30 INFO 54892 --- [eurekaserver] [ Thread-10] c.n.e.r.PeerAwareInstanceRegistryImpl : Got 1 instances from neighboring DS node 2025-10-07T17:10:33.273+05:30 INFO 54892 --- [eurekaserver] [ Thread-10] c.n.e.r.PeerAwareInstanceRegistryImpl : Renew threshold is: 1 2025-10-07T17:10:33.273+05:30 INFO 54892 --- [eurekaserver] [ Thread-10] c.n.e.r.PeerAwareInstanceRegistryImpl : Changing status to UP 2025-10-07T17:10:33.275+05:30 INFO 54892 --- [eurekaserver] [ Thread-10] e.s.EurekaServerInitializerConfiguration : Started Eureka Server Mockito is currently self-attaching to enable the inline-mock-maker. This will no longer work in future releases of the JDK. Please add Mockito as an agent to your build as described in Mockito's documentation: https://javadoc.io/doc/org.mockito/mockito-core/latest/org.mockito/org/mockito/Mockito.html#0.3 OpenJDK 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended WARNING: A Java agent has been loaded dynamically (C:\Users\mahes\.m2\repository\net\bytebuddy\byte-buddy-agent\1.17.7\byte-buddy-agent-1.17.7.jar) WARNING: If a serviceability tool is in use, please run with -XX:+EnableDynamicAgentLoading to hide this warning WARNING: If a serviceability tool is not in use, please run with -Djdk.instrument.traceUsage for more information WARNING: Dynamic loading of agents will be disallowed by default in a future release [INFO] [1;32mTests run: [0;1;32m1[m, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 5.114 s -- in com.tutorialspoint.eurekaserver.[1mEurekaserverApplicationTests[m [INFO] [INFO] Results: [INFO] [INFO] [1;32mTests run: 1, Failures: 0, Errors: 0, Skipped: 0[m [INFO] [INFO] [INFO] [1m--- [0;32mjar:3.4.2:jar[m [1m(default-jar)[m @ [36meurekaserver[0;1m ---[m [INFO] Building jar: D:\Projects\eurekaserver\target\eurekaserver-0.0.1-SNAPSHOT.jar [INFO] [INFO] [1m--- [0;32mspring-boot:3.5.6:repackage[m [1m(repackage)[m @ [36meurekaserver[0;1m ---[m [INFO] Replacing main artifact D:\Projects\eurekaserver\target\eurekaserver-0.0.1-SNAPSHOT.jar with repackaged archive, adding nested dependencies in BOOT-INF/. [INFO] The original artifact has been renamed to D:\Projects\eurekaserver\target\eurekaserver-0.0.1-SNAPSHOT.jar.original [INFO] [INFO] [1m--- [0;32minstall:3.1.4:install[m [1m(default-install)[m @ [36meurekaserver[0;1m ---[m [INFO] Installing D:\Projects\eurekaserver\pom.xml to C:\Users\mahes\.m2\repository\com\tutorialspoint\eurekaserver\0.0.1-SNAPSHOT\eurekaserver-0.0.1-SNAPSHOT.pom [INFO] Installing D:\Projects\eurekaserver\target\eurekaserver-0.0.1-SNAPSHOT.jar to C:\Users\mahes\.m2\repository\com\tutorialspoint\eurekaserver\0.0.1-SNAPSHOT\eurekaserver-0.0.1-SNAPSHOT.jar [INFO] [1m------------------------------------------------------------------------[m [INFO] [1;32mBUILD SUCCESS[m [INFO] [1m------------------------------------------------------------------------[m [INFO] Total time: 13.445 s [INFO] Finished at: 2025-10-07T17:10:37+05:30 [INFO] [1m------------------------------------------------------------------------[m
For execution, we need to have two service instances running. To do that, let's open two shells and then execute the following command on one shell −
java -jar .\target\eurekaserver-0.0.1-SNAPSHOT.jar --app_port=8900 --eureka_other_server_url=http://localhost:8901/eureka' --spring.config.location=classpath:application-ha.yml
And execute the following on the other shell −
java -jar .\target\eurekaserver-0.0.1-SNAPSHOT.jar --app_port=8901 --eureka_other_server_url=http://localhost:8901/eureka' --spring.config.location=classpath:application-ha.yml
We can verify that the servers are up and running in high-availability mode by looking at the dashboard. For example, here is the dashboard on Eureka server 1 −
And here is the dashboard of Eureka server 2 −
So, as we see, we have two Eureka servers running and in sync. Even if one server goes down, the other server would keep functioning.
We can also update the service instance application to have addresses for both Eureka servers by having comma-separated server addresses.
spring:
application:
name: customer-service
server:
port: ${app_port}
eureka:
client:
serviceURL:
defaultZone: http://localhost:8900/eureka, http://localhost:8901/eureka