Skip to content

Commit

Permalink
Merge pull request #3 from JonathanGiles/eureka-changes
Browse files Browse the repository at this point in the history
Enable Eureka service discovery
  • Loading branch information
JonathanGiles authored Jul 9, 2024
2 parents 1f39d28 + eb76106 commit 6b3d25b
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ spring:
server:
port: ${targetPort?c}
eureka:
instance:
hostname: localhost
client:
register-with-eureka: ${registerWithEureka?string("true", "false")}
fetch-registry: ${fetchRegistry?string("true", "false")}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<version>0.0.1-SNAPSHOT</version>

<properties>
<java.version>17</java.version>
<java.version>21</java.version>
<spring-cloud.version>2023.0.2</spring-cloud.version>
</properties>

Expand All @@ -23,6 +23,10 @@
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
21 changes: 21 additions & 0 deletions samples/storage-explorer/date-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
</scm>
<properties>
<java.version>21</java.version>
<spring-cloud.version>2023.0.2</spring-cloud.version>
</properties>

<dependencies>
Expand All @@ -49,13 +50,33 @@
<scope>runtime</scope>
<optional>true</optional>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
spring:
application:
name: date-service-spring
eureka:
client:
serviceUrl:
defaultZone: ${services__eureka__https__0}/eureka/
instance:
preferIpAddress: true
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,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);
// .withReference(openAI);

Expand Down
13 changes: 13 additions & 0 deletions samples/storage-explorer/storage-explorer/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
</scm>
<properties>
<java.version>21</java.version>
<spring-cloud.version>2023.0.2</spring-cloud.version>
</properties>

<dependencyManagement>
Expand All @@ -39,6 +40,13 @@
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down Expand Up @@ -77,6 +85,11 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-docker-compose</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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("/")
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -21,23 +23,26 @@
*/
@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;
}

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;
}

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
spring:
application:
name: storage-explorer-spring
eureka:
client:
serviceUrl:
defaultZone: ${services__eureka__https__0}/eureka/
instance:
preferIpAddress: true

0 comments on commit 6b3d25b

Please sign in to comment.