From 389a3116f7b91b80a3b28d380b935af953b25f3c Mon Sep 17 00:00:00 2001 From: Michael Rasch Date: Sat, 10 May 2025 18:01:40 -0400 Subject: [PATCH] Store leader value to the app base url --- .../postman-docker-compose.yml | 1 + .../src/main/resources/application.properties | 3 ++- .../standalone-docker-compose.yml | 1 + .../election/config/EtcdConfiguration.java | 17 +++++++++-------- .../election/service/EtcdElectionProcess.java | 4 +--- .../container/IntegrationTestExtension.java | 2 +- 6 files changed, 15 insertions(+), 13 deletions(-) diff --git a/registry-server-runner/postman-docker-compose.yml b/registry-server-runner/postman-docker-compose.yml index 24f6181..b42a670 100644 --- a/registry-server-runner/postman-docker-compose.yml +++ b/registry-server-runner/postman-docker-compose.yml @@ -44,6 +44,7 @@ services: - REDIS_HOST=redis - REDIS_PORT=6379 - spring.profiles.active=automation + - APP_BASE_URL=https://postman-docker:8080 build: context: .. dockerfile: ./registry-server-runner/Dockerfile diff --git a/registry-server-runner/src/main/resources/application.properties b/registry-server-runner/src/main/resources/application.properties index 4a0f580..140b6f0 100644 --- a/registry-server-runner/src/main/resources/application.properties +++ b/registry-server-runner/src/main/resources/application.properties @@ -5,4 +5,5 @@ spring.redis.host=${REDIS_HOST} spring.redis.port=${REDIS_PORT} etcd.leader.key=/leader service.registry.logging.aspectJ.enabled=true -service.registry.logging.aspectJ.timing.enabled=true \ No newline at end of file +service.registry.logging.aspectJ.timing.enabled=true +app.base.url=${APP_BASE_URL} \ No newline at end of file diff --git a/registry-server-runner/standalone-docker-compose.yml b/registry-server-runner/standalone-docker-compose.yml index 1d49d1d..32ff0d9 100644 --- a/registry-server-runner/standalone-docker-compose.yml +++ b/registry-server-runner/standalone-docker-compose.yml @@ -47,6 +47,7 @@ services: - ETCD_URLS=http://etcd:2379 - REDIS_HOST=redis - REDIS_PORT=6379 + - APP_BASE_URL=https://standalone-docker:8080 build: context: .. dockerfile: ./registry-server-runner/Dockerfile diff --git a/server/src/main/java/com/michael/container/distributed/election/config/EtcdConfiguration.java b/server/src/main/java/com/michael/container/distributed/election/config/EtcdConfiguration.java index fb6cf43..88d1e10 100644 --- a/server/src/main/java/com/michael/container/distributed/election/config/EtcdConfiguration.java +++ b/server/src/main/java/com/michael/container/distributed/election/config/EtcdConfiguration.java @@ -4,20 +4,21 @@ import java.net.URI; import java.net.URISyntaxException; import java.util.Arrays; -import java.util.UUID; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class EtcdConfiguration { private final String etcdLeaderKey; - private final UUID serviceUniqueIdentifier; + private final String baseUrl; private final URI[] etcdEndpoints; public EtcdConfiguration( - @Value("${etcd.leader.key}") String etcLeaderKey, @Value("${etcd.urls}") String etcdUrls) { + @Value("${etcd.leader.key}") String etcLeaderKey, + @Value("${app.base.url}") String baseUrl, + @Value("${etcd.urls}") String etcdUrls) { this.etcdLeaderKey = etcLeaderKey; - serviceUniqueIdentifier = UUID.randomUUID(); + this.baseUrl = baseUrl; etcdEndpoints = Arrays.stream(etcdUrls.split(",")) .map( @@ -36,11 +37,11 @@ public String getEtcdLeaderKey() { return etcdLeaderKey; } - public UUID getServiceUniqueIdentifier() { - return serviceUniqueIdentifier; - } - public URI[] getEtcdEndpoints() { return etcdEndpoints; } + + public String getBaseUrl() { + return baseUrl; + } } diff --git a/server/src/main/java/com/michael/container/distributed/election/service/EtcdElectionProcess.java b/server/src/main/java/com/michael/container/distributed/election/service/EtcdElectionProcess.java index 21e0072..22be7e4 100644 --- a/server/src/main/java/com/michael/container/distributed/election/service/EtcdElectionProcess.java +++ b/server/src/main/java/com/michael/container/distributed/election/service/EtcdElectionProcess.java @@ -76,9 +76,7 @@ public void startLeaderElection() { // Try and be efficient, we wrap all the computations into one request LockResult lockResult = lockProcess.lock( - etcdConfiguration.getEtcdLeaderKey(), - etcdConfiguration.getServiceUniqueIdentifier().toString(), - 5L); + etcdConfiguration.getEtcdLeaderKey(), etcdConfiguration.getBaseUrl(), 5L); electionState.setLeaseId(lockResult.leaseId()); if (lockResult.txnResponse().isSucceeded()) { acquiredLeadership = true; diff --git a/server/src/test/java/com/michael/container/IntegrationTestExtension.java b/server/src/test/java/com/michael/container/IntegrationTestExtension.java index 69c1136..df6c7ee 100644 --- a/server/src/test/java/com/michael/container/IntegrationTestExtension.java +++ b/server/src/test/java/com/michael/container/IntegrationTestExtension.java @@ -9,7 +9,7 @@ import org.springframework.data.redis.core.RedisTemplate; import org.springframework.test.context.TestPropertySource; -@TestPropertySource(properties = {"etcd.leader.key=/leader"}) +@TestPropertySource(properties = {"etcd.leader.key=/leader", "app.base.url=https://localhost:8080"}) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @ExtendWith(DockerExtension.class) public class IntegrationTestExtension {