Skip to content

Commit

Permalink
use auto-injected Env. var CONTAINER_APP_ENV_DNS_SUFFIX corpManagedEn…
Browse files Browse the repository at this point in the history
…vironment.properties.defaultDomain as workaround for #1 & microsoft/azure-container-apps#473
  • Loading branch information
ezYakaEagle442 committed Nov 9, 2022
1 parent c3b4497 commit 8a507cb
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
package org.springframework.samples.petclinic.api.application;

import lombok.RequiredArgsConstructor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.samples.petclinic.api.dto.OwnerDetails;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
Expand All @@ -29,11 +33,43 @@
@RequiredArgsConstructor
public class CustomersServiceClient {

/*
see
https://github.com/ezYakaEagle442/aca-java-petclinic-mic-srv/issues/1
https://github.com/microsoft/azure-container-apps/issues/473
An alternate way of achieving this without getting the fqdn of the app:
An environment variable: **CONTAINER_APP_ENV_DNS_SUFFIX** is auto-injected for every container running on the environment which describes the environments default domain.
This environment variable can help formulate the Internal FQDN of the app. e.g.:
http://<containerapp-name>.internal.<CONTAINER_APP_ENV_DNS_SUFFIX>
ex: https://myinternalapp.internal.icyforest-6dcfec24.regionname.azurecontainerapps.io
https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.external-config
https://www.baeldung.com/spring-boot-properties-env-variables
*/

private final WebClient.Builder webClientBuilder;

@Value("${container.app.env.dns.suffix}")
private String acaEnvDnsSuffix;

@Value("${customers.service.url}")
private String customersServiceUrl;

@Autowired
private Environment environment;

String CONTAINER_APP_ENV_DNS_SUFFIX = environment.getProperty("container.app.env.dns.suffix");
String CUSTOMERS_SVC_URL = environment.getProperty("customers.service.url");

String internalK8Ssvc2svcRoute = "http://customers-service.internal." + acaEnvDnsSuffix;

public Mono<OwnerDetails> getOwner(final int ownerId) {
return webClientBuilder.build().get()
.uri("https://${CUSTOMERS_SVC_URL}/owners/{ownerId}", ownerId)
.uri(internalK8Ssvc2svcRoute + "/owners/{ownerId}", ownerId)
.retrieve()
.bodyToMono(OwnerDetails.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
package org.springframework.samples.petclinic.api.application;

import lombok.RequiredArgsConstructor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.samples.petclinic.api.dto.Visits;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;
Expand All @@ -32,8 +36,41 @@
@RequiredArgsConstructor
public class VisitsServiceClient {

/*
see
https://github.com/ezYakaEagle442/aca-java-petclinic-mic-srv/issues/1
https://github.com/microsoft/azure-container-apps/issues/473
An alternate way of achieving this without getting the fqdn of the app:
An environment variable: **CONTAINER_APP_ENV_DNS_SUFFIX** is auto-injected for every container running on the environment which describes the environments default domain.
This environment variable can help formulate the Internal FQDN of the app. e.g.:
http://<containerapp-name>.internal.<CONTAINER_APP_ENV_DNS_SUFFIX>
ex: https://myinternalapp.internal.icyforest-6dcfec24.regionname.azurecontainerapps.io
https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.external-config
https://www.baeldung.com/spring-boot-properties-env-variables
*/


@Value("${container.app.env.dns.suffix}")
private String acaEnvDnsSuffix;

@Value("${visits.service.url}")
private String visitsServiceUrl;

@Autowired
private Environment environment;

String CONTAINER_APP_ENV_DNS_SUFFIX = environment.getProperty("container.app.env.dns.suffix");
String VISITS_SVC_URL = environment.getProperty("visits.service.url");

String internalK8Ssvc2svcRoute = "http://visits-service.internal." + acaEnvDnsSuffix;

// Could be changed for testing purpose
private String hostname = "https://${VISITS_SVC_URL}/";
private String hostname = internalK8Ssvc2svcRoute ; // "https://${VISITS_SVC_URL}/";

private final WebClient.Builder webClientBuilder;

Expand Down

0 comments on commit 8a507cb

Please sign in to comment.