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 Zone Awareness
Eureka also supports the concept of zone awareness. Zone awareness as a concept is very useful when we have a cluster across different geographies. Say, we get an incoming request for a service and we need to choose the server which should service the request. Instead of sending and processing that request on a server which is located far, it is more fruitful to choose a server which is in the same zone. This is because, network bottleneck is very common in a distributed application and thus we should avoid it.
Let us now try to setup Eureka clients and make them Zone aware. For doing that, let us add application-za.yml
spring:
application:
name: customer-service
server:
port: ${app_port}
eureka:
instance:
metadataMap:
zone: ${zoneName}
client:
serviceURL:
defaultZone: http://localhost:8900/eureka
Let us now recompile our Eureka client project.
[INFO] Scanning for projects... [INFO] [INFO] [1m------------------< [0;36mcom.tutorialspoint:eurekaclient[0;1m >-------------------[m [INFO] [1mBuilding eurekaclient 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 @ [36meurekaclient[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 @ [36meurekaclient[0;1m ---[m [INFO] Recompiling the module because of [1madded or removed source files[m. [INFO] Compiling 2 source files with javac [debug parameters release 21] to target\classes [INFO] [INFO] [1m--- [0;32mresources:3.3.1:testResources[m [1m(default-testResources)[m @ [36meurekaclient[0;1m ---[m [INFO] skip non existing resourceDirectory D:\Projects\eurekaclient\src\test\resources [INFO] [INFO] [1m--- [0;32mcompiler:3.14.0:testCompile[m [1m(default-testCompile)[m @ [36meurekaclient[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 @ [36meurekaclient[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.eurekaclient.[1mEurekaclientApplicationTests[m 17:45:05.033 [main] INFO org.springframework.test.context.support.AnnotationConfigContextLoaderUtils -- Could not detect default configuration classes for test class [com.tutorialspoint.eurekaclient.EurekaclientApplicationTests]: EurekaclientApplicationTests does not declare any static, non-private, non-final, nested classes annotated with @Configuration. 17:45:05.150 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper -- Found @SpringBootConfiguration com.tutorialspoint.eurekaclient.EurekaclientApplication for test class com.tutorialspoint.eurekaclient.EurekaclientApplicationTests . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v3.5.6) 2025-10-07T17:45:05.775+05:30 INFO 32456 --- [eurekaclient] [ main] c.t.e.EurekaclientApplicationTests : Starting EurekaclientApplicationTests using Java 21.0.6 with PID 32456 (started by mahes in D:\Projects\eurekaclient) 2025-10-07T17:45:05.776+05:30 INFO 32456 --- [eurekaclient] [ main] c.t.e.EurekaclientApplicationTests : No active profile set, falling back to 1 default profile: "default" 2025-10-07T17:45:06.558+05:30 INFO 32456 --- [eurekaclient] [ main] o.s.cloud.context.scope.GenericScope : BeanFactory id=61598357-3a65-347a-b2c3-05819b0e284d 2025-10-07T17:45:07.313+05:30 INFO 32456 --- [eurekaclient] [ main] DiscoveryClientOptionalArgsConfiguration : Eureka HTTP Client uses RestTemplate. 2025-10-07T17:45:07.348+05:30 WARN 32456 --- [eurekaclient] [ 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:45:07.377+05:30 INFO 32456 --- [eurekaclient] [ main] o.s.c.n.eureka.InstanceInfoFactory : Setting initial instance status as: STARTING 2025-10-07T17:45:07.425+05:30 INFO 32456 --- [eurekaclient] [ main] com.netflix.discovery.DiscoveryClient : Initializing Eureka in region us-east-1 2025-10-07T17:45:07.425+05:30 INFO 32456 --- [eurekaclient] [ main] c.n.d.s.r.aws.ConfigClusterResolver : Resolving eureka endpoints via configuration 2025-10-07T17:45:07.440+05:30 INFO 32456 --- [eurekaclient] [ main] com.netflix.discovery.DiscoveryClient : Disable delta property : false 2025-10-07T17:45:07.452+05:30 INFO 32456 --- [eurekaclient] [ main] com.netflix.discovery.DiscoveryClient : Single vip registry refresh property : null 2025-10-07T17:45:07.452+05:30 INFO 32456 --- [eurekaclient] [ main] com.netflix.discovery.DiscoveryClient : Force full registry fetch : false 2025-10-07T17:45:07.452+05:30 INFO 32456 --- [eurekaclient] [ main] com.netflix.discovery.DiscoveryClient : Application is null : false 2025-10-07T17:45:07.452+05:30 INFO 32456 --- [eurekaclient] [ main] com.netflix.discovery.DiscoveryClient : Registered Applications size is zero : true 2025-10-07T17:45:07.452+05:30 INFO 32456 --- [eurekaclient] [ main] com.netflix.discovery.DiscoveryClient : Application version is -1: true 2025-10-07T17:45:07.452+05:30 INFO 32456 --- [eurekaclient] [ main] com.netflix.discovery.DiscoveryClient : Getting all instance registry info from the eureka server 2025-10-07T17:45:08.103+05:30 INFO 32456 --- [eurekaclient] [ main] com.netflix.discovery.DiscoveryClient : The response status is 200 2025-10-07T17:45:08.106+05:30 INFO 32456 --- [eurekaclient] [ main] com.netflix.discovery.DiscoveryClient : Starting heartbeat executor: renew interval is: 30 2025-10-07T17:45:08.108+05:30 INFO 32456 --- [eurekaclient] [ main] c.n.discovery.InstanceInfoReplicator : InstanceInfoReplicator onDemand update allowed rate per min is 4 2025-10-07T17:45:08.108+05:30 INFO 32456 --- [eurekaclient] [ main] com.netflix.discovery.DiscoveryClient : Discovery Client initialized at timestamp 1759839308108 with initial instances count: 0 2025-10-07T17:45:08.108+05:30 INFO 32456 --- [eurekaclient] [ main] o.s.c.n.e.s.EurekaServiceRegistry : Registering application EUREKACLIENT with eureka with status UP 2025-10-07T17:45:08.108+05:30 INFO 32456 --- [eurekaclient] [ main] com.netflix.discovery.DiscoveryClient : Saw local status change event StatusChangeEvent [timestamp=1759839308108, current=UP, previous=STARTING] 2025-10-07T17:45:08.108+05:30 INFO 32456 --- [eurekaclient] [foReplicator-%d] com.netflix.discovery.DiscoveryClient : DiscoveryClient_EUREKACLIENT/Home:eurekaclient: registering service... 2025-10-07T17:45:08.122+05:30 INFO 32456 --- [eurekaclient] [ main] c.t.e.EurekaclientApplicationTests : Started EurekaclientApplicationTests in 2.678 seconds (process running for 3.698) 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 2025-10-07T17:45:08.272+05:30 INFO 32456 --- [eurekaclient] [foReplicator-%d] com.netflix.discovery.DiscoveryClient : DiscoveryClient_EUREKACLIENT/Home:eurekaclient - registration status: 204 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: 3.790 s -- in com.tutorialspoint.eurekaclient.[1mEurekaclientApplicationTests[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 @ [36meurekaclient[0;1m ---[m [INFO] Building jar: D:\Projects\eurekaclient\target\eurekaclient-0.0.1-SNAPSHOT.jar [INFO] [INFO] [1m--- [0;32mspring-boot:3.5.6:repackage[m [1m(repackage)[m @ [36meurekaclient[0;1m ---[m [INFO] Replacing main artifact D:\Projects\eurekaclient\target\eurekaclient-0.0.1-SNAPSHOT.jar with repackaged archive, adding nested dependencies in BOOT-INF/. [INFO] The original artifact has been renamed to D:\Projects\eurekaclient\target\eurekaclient-0.0.1-SNAPSHOT.jar.original [INFO] [INFO] [1m--- [0;32minstall:3.1.4:install[m [1m(default-install)[m @ [36meurekaclient[0;1m ---[m [INFO] Installing D:\Projects\eurekaclient\pom.xml to C:\Users\mahes\.m2\repository\com\tutorialspoint\eurekaclient\0.0.1-SNAPSHOT\eurekaclient-0.0.1-SNAPSHOT.pom [INFO] Installing D:\Projects\eurekaclient\target\eurekaclient-0.0.1-SNAPSHOT.jar to C:\Users\mahes\.m2\repository\com\tutorialspoint\eurekaclient\0.0.1-SNAPSHOT\eurekaclient-0.0.1-SNAPSHOT.jar [INFO] [1m------------------------------------------------------------------------[m [INFO] [1;32mBUILD SUCCESS[m [INFO] [1m------------------------------------------------------------------------[m [INFO] Total time: 9.863 s [INFO] Finished at: 2025-10-07T17:45:11+05:30 [INFO] [1m------------------------------------------------------------------------[m
For execution, we will 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\eurekaclient-0.0.1-SNAPSHOT.jar --app_port=8080 --zone_name=USA --spring.config.location=classpath:application-za.yml
And execute the following on the other shell −
java -jar .\target\eurekaclient-0.0.1-SNAPSHOT.jar --app_port=8081 --zone_name=EU --spring.config.location=classpath:application-za.yml
We can go back to the dashboard to verify that the Eureka Server registers the zone of the services. As seen in the following image, we have two availability zones instead of 1, which we have been seeing till now.
Now, any client can look at the zone it is present in. Say the client is located in USA, it would prefer the service instance of USA. And it can get the zone information from the Eureka Server.