From 4c873f9d235938d38cd9120763741bd9e73cf82b Mon Sep 17 00:00:00 2001 From: Srikanta Nagaraja Date: Sun, 7 Jul 2024 01:22:11 -0700 Subject: [PATCH] Enable Eureka service discovery --- .../resources/AzureStorageChildResource.java | 2 +- .../resources/AzureStorageResource.java | 4 +++- .../templates/eureka/application.yaml | 2 ++ .../main/resources/templates/eureka/pom.xml | 6 +++++- samples/storage-explorer/date-service/pom.xml | 21 +++++++++++++++++++ .../src/main/resources/application.yml | 9 ++++++++ .../StorageExplorerAppHost.java | 8 +++---- .../storage-explorer/storage-explorer/pom.xml | 13 ++++++++++++ .../StorageServiceController.java | 13 ++++++------ .../service/AzureBlobStorageService.java | 11 +++++++--- .../src/main/resources/application.yml | 9 ++++++++ 11 files changed, 82 insertions(+), 16 deletions(-) create mode 100644 samples/storage-explorer/date-service/src/main/resources/application.yml create mode 100644 samples/storage-explorer/storage-explorer/src/main/resources/application.yml diff --git a/aspire4j/aspire4j-extensions-azure-storage/src/main/java/com/microsoft/aspire/extensions/azure/storage/resources/AzureStorageChildResource.java b/aspire4j/aspire4j-extensions-azure-storage/src/main/java/com/microsoft/aspire/extensions/azure/storage/resources/AzureStorageChildResource.java index e23dfec..8aacc27 100644 --- a/aspire4j/aspire4j-extensions-azure-storage/src/main/java/com/microsoft/aspire/extensions/azure/storage/resources/AzureStorageChildResource.java +++ b/aspire4j/aspire4j-extensions-azure-storage/src/main/java/com/microsoft/aspire/extensions/azure/storage/resources/AzureStorageChildResource.java @@ -29,6 +29,6 @@ public String getConnectionStringEnvironmentVariable() { @Override public String getValue() { - return storageResource.getName() + ".outputs." + endpointSuffix; + return "{" + storageResource.getName() + ".outputs." + endpointSuffix + "}"; } } diff --git a/aspire4j/aspire4j-extensions-azure-storage/src/main/java/com/microsoft/aspire/extensions/azure/storage/resources/AzureStorageResource.java b/aspire4j/aspire4j-extensions-azure-storage/src/main/java/com/microsoft/aspire/extensions/azure/storage/resources/AzureStorageResource.java index 9d90a96..4a80776 100644 --- a/aspire4j/aspire4j-extensions-azure-storage/src/main/java/com/microsoft/aspire/extensions/azure/storage/resources/AzureStorageResource.java +++ b/aspire4j/aspire4j-extensions-azure-storage/src/main/java/com/microsoft/aspire/extensions/azure/storage/resources/AzureStorageResource.java @@ -13,10 +13,12 @@ public class AzureStorageResource extends AzureBicepResource implements ResourceWithEndpoints { - private static final ResourceType AZURE_STORAGE = ResourceType.fromString("azure.storage.v0"); + private static final ResourceType AZURE_STORAGE = ResourceType.fromString("azure.bicep.v0"); public AzureStorageResource(String name) { super(AZURE_STORAGE, name); + withParameter("principalId", ""); + withParameter("principalType", ""); } public AzureStorageBlobsResource addBlobs(String name) { diff --git a/aspire4j/aspire4j-extensions-spring/src/main/resources/templates/eureka/application.yaml b/aspire4j/aspire4j-extensions-spring/src/main/resources/templates/eureka/application.yaml index a670117..f114c9a 100644 --- a/aspire4j/aspire4j-extensions-spring/src/main/resources/templates/eureka/application.yaml +++ b/aspire4j/aspire4j-extensions-spring/src/main/resources/templates/eureka/application.yaml @@ -4,6 +4,8 @@ spring: server: port: ${port?c} eureka: + instance: + hostname: localhost client: register-with-eureka: ${registerWithEureka?string("true", "false")} fetch-registry: ${fetchRegistry?string("true", "false")} \ No newline at end of file diff --git a/aspire4j/aspire4j-extensions-spring/src/main/resources/templates/eureka/pom.xml b/aspire4j/aspire4j-extensions-spring/src/main/resources/templates/eureka/pom.xml index 960b98c..f07f922 100644 --- a/aspire4j/aspire4j-extensions-spring/src/main/resources/templates/eureka/pom.xml +++ b/aspire4j/aspire4j-extensions-spring/src/main/resources/templates/eureka/pom.xml @@ -14,7 +14,7 @@ 0.0.1-SNAPSHOT - 17 + 21 2023.0.2 @@ -23,6 +23,10 @@ org.springframework.cloud spring-cloud-starter-netflix-eureka-server + + org.springframework.boot + spring-boot-starter-web + org.springframework.boot diff --git a/samples/storage-explorer/date-service/pom.xml b/samples/storage-explorer/date-service/pom.xml index 417dc0e..3fe5eec 100644 --- a/samples/storage-explorer/date-service/pom.xml +++ b/samples/storage-explorer/date-service/pom.xml @@ -28,6 +28,7 @@ 21 + 2023.0.2 @@ -49,6 +50,14 @@ runtime true + + + + org.springframework.cloud + spring-cloud-starter-netflix-eureka-client + + + org.springframework.boot spring-boot-starter-test @@ -56,6 +65,18 @@ + + + + org.springframework.cloud + spring-cloud-dependencies + ${spring-cloud.version} + pom + import + + + + diff --git a/samples/storage-explorer/date-service/src/main/resources/application.yml b/samples/storage-explorer/date-service/src/main/resources/application.yml new file mode 100644 index 0000000..e2977af --- /dev/null +++ b/samples/storage-explorer/date-service/src/main/resources/application.yml @@ -0,0 +1,9 @@ +spring: + application: + name: date-service-spring +eureka: + client: + serviceUrl: + defaultZone: ${services__eureka__https__0}/eureka/ + instance: + preferIpAddress: true \ No newline at end of file diff --git a/samples/storage-explorer/storage-explorer-apphost/src/main/java/com/microsoft/aspire/storageexplorer/StorageExplorerAppHost.java b/samples/storage-explorer/storage-explorer-apphost/src/main/java/com/microsoft/aspire/storageexplorer/StorageExplorerAppHost.java index 31399cb..9ba4d2e 100644 --- a/samples/storage-explorer/storage-explorer-apphost/src/main/java/com/microsoft/aspire/storageexplorer/StorageExplorerAppHost.java +++ b/samples/storage-explorer/storage-explorer-apphost/src/main/java/com/microsoft/aspire/storageexplorer/StorageExplorerAppHost.java @@ -19,16 +19,16 @@ public class StorageExplorerAppHost implements AppHost { .addEurekaServiceDiscovery("eureka"); var dateService = app.withExtension(SpringExtension.class) - .addSpringProject("date-service-spring") - .withPath("date-service") - .withExternalHttpEndpoints(); + .addSpringProject("date-service-spring") + .withPath("date-service") + .withReference(eurekaServiceDiscovery) + .withExternalHttpEndpoints(); var storageExplorer = app.withExtension(SpringExtension.class) .addSpringProject("storage-explorer-spring") .withPath("storage-explorer") .withExternalHttpEndpoints() .withReference(blobStorage) - .withReference(dateService) .withReference(eurekaServiceDiscovery); // Old style, with direct reference to dockerfiles diff --git a/samples/storage-explorer/storage-explorer/pom.xml b/samples/storage-explorer/storage-explorer/pom.xml index 1fe1043..81129a1 100644 --- a/samples/storage-explorer/storage-explorer/pom.xml +++ b/samples/storage-explorer/storage-explorer/pom.xml @@ -28,6 +28,7 @@ 21 + 2023.0.2 @@ -39,6 +40,13 @@ pom import + + org.springframework.cloud + spring-cloud-dependencies + ${spring-cloud.version} + pom + import + @@ -77,6 +85,11 @@ org.springframework.boot spring-boot-starter + + + org.springframework.cloud + spring-cloud-starter-netflix-eureka-client + org.springframework.boot spring-boot-docker-compose diff --git a/samples/storage-explorer/storage-explorer/src/main/java/com/microsoft/aspire/storageexplorer/StorageServiceController.java b/samples/storage-explorer/storage-explorer/src/main/java/com/microsoft/aspire/storageexplorer/StorageServiceController.java index bc1e106..fadf752 100644 --- a/samples/storage-explorer/storage-explorer/src/main/java/com/microsoft/aspire/storageexplorer/StorageServiceController.java +++ b/samples/storage-explorer/storage-explorer/src/main/java/com/microsoft/aspire/storageexplorer/StorageServiceController.java @@ -2,6 +2,7 @@ import com.microsoft.aspire.storageexplorer.service.StorageItem; import com.microsoft.aspire.storageexplorer.service.StorageService; +import com.netflix.discovery.EurekaClient; import jakarta.servlet.http.HttpServletResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -32,13 +33,13 @@ public class StorageServiceController { private static final Logger LOGGER = LoggerFactory.getLogger(StorageServiceController.class); private final StorageService storageService; - - @Value("${services__dateservice__https__0}") - private String dateServiceEndpoint; + private final String dateServiceUrl; @Autowired - public StorageServiceController(final StorageService storageService) { + public StorageServiceController(final StorageService storageService, final EurekaClient discoveryClient) { this.storageService = storageService; + this.dateServiceUrl = discoveryClient.getNextServerFromEureka("date-service-spring", false).getHomePageUrl(); + LOGGER.info("The discovered date service URL is " + dateServiceUrl); } @GetMapping("/") @@ -51,12 +52,12 @@ public String listUploadedFiles(final Model model, } private void updateTime(Model model) { - LOGGER.info("The datetime service endpoint is " + dateServiceEndpoint); String lastUpdated = ""; try { RestTemplate restTemplate = new RestTemplate(); - OffsetDateTime time = restTemplate.getForObject(dateServiceEndpoint + "/time", OffsetDateTime.class); + OffsetDateTime time = restTemplate.getForObject(dateServiceUrl + "/time", OffsetDateTime.class); if (time != null) { + LOGGER.info("The date service time is " + time.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); lastUpdated = time.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); } } catch (Exception exception) { diff --git a/samples/storage-explorer/storage-explorer/src/main/java/com/microsoft/aspire/storageexplorer/service/AzureBlobStorageService.java b/samples/storage-explorer/storage-explorer/src/main/java/com/microsoft/aspire/storageexplorer/service/AzureBlobStorageService.java index bc1e516..d7e7659 100644 --- a/samples/storage-explorer/storage-explorer/src/main/java/com/microsoft/aspire/storageexplorer/service/AzureBlobStorageService.java +++ b/samples/storage-explorer/storage-explorer/src/main/java/com/microsoft/aspire/storageexplorer/service/AzureBlobStorageService.java @@ -8,6 +8,8 @@ import com.azure.storage.blob.BlobServiceClient; import com.azure.storage.blob.BlobServiceClientBuilder; import com.azure.storage.blob.models.BlobHttpHeaders; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; @@ -21,15 +23,18 @@ */ @Service public class AzureBlobStorageService implements StorageService { + private static final Logger LOGGER = LoggerFactory.getLogger(AzureBlobStorageService.class); + private String blobStorageContainerName = "mycontainer"; - @Value("${ENDPOINT}") + @Value("${ConnectionStrings__storage-explorer-blobs}") private String storageEndpoint; private BlobContainerClient blobContainerClient; @Override public void init() { + LOGGER.info("Using Azure Blob Storage endpoint {}", storageEndpoint); if (blobContainerClient != null) { return; } @@ -37,7 +42,7 @@ public void init() { boolean doInit = true; if ((storageEndpoint == null || storageEndpoint.isEmpty())) { - System.err.println("Error: Please set the ENDPOINT property"); + System.err.println("Error: Please set the ConnectionStrings__storage-explorer-blobs property"); doInit = false; } @@ -64,7 +69,7 @@ public void store(final String filename, final InputStream inputStream, final lo final String mimeType = URLConnection.guessContentTypeFromName(filename); blobClient.setHttpHeaders(new BlobHttpHeaders() - .setContentType(mimeType)); + .setContentType(mimeType)); } @Override diff --git a/samples/storage-explorer/storage-explorer/src/main/resources/application.yml b/samples/storage-explorer/storage-explorer/src/main/resources/application.yml new file mode 100644 index 0000000..b97be26 --- /dev/null +++ b/samples/storage-explorer/storage-explorer/src/main/resources/application.yml @@ -0,0 +1,9 @@ +spring: + application: + name: storage-explorer-spring +eureka: + client: + serviceUrl: + defaultZone: ${services__eureka__https__0}/eureka/ + instance: + preferIpAddress: true \ No newline at end of file