Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable Eureka service discovery #3

Merged
merged 3 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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