as the microservice is running inside a Docker container, you get the hostname the Docker host provides to the container. Can you explain what you want to achieve? I guess you do not really want the host name of the microservice in the first place.
Let me explain you my use case.
I am building one microservice, which will be deployed on different tenant. For the development phase i have provided the tenant details and other information through application-dev.properties file like below
cumulocity configuration for running localy and connecting to cumulocity
application.name=xyz-service
C8Y.bootstrap.register=true
C8Y.bootstrap.tenant=t12345
C8Y.baseURL=https://dev.iot.xyz.com
C8Y.bootstrap.user=servicebootstrap_xyz-service
C8Y.bootstrap.password=redacted
C8Y.microservice.isolation=MULTI_TENANT
C8Y.bootstrap.initialDelay=5000
device.id=123456789
calling class
@Autowired
private PlatformUtils platformUtils;
Optional<String> temp = platformUtils.getHost();
if (temp.isEmpty()) {
log.error(
"Unable to extract base url for the tenant. Will not be able to subscribe to notifications... exiting process initialization for tenant: {}. Resolve this issue and resubscribe the microservice",
tenantId);
return;
}
What is the purpose of knowing the host name within a microservice?
Within the microservice always the C8Y_baseURL should be used which is injected as an environment variable and resolved internally to http://cumulocity:8111/ which is intended.
I suggest to NOT implement any logic based on any public host name. This is against any microservice implementation principle. It should be host independent so it can be deployed anywhere.
If you want logic for all the subscribed tenants you should use the MicroserviceSubscriptionService which will inject you all details of the tenants where the microservice is subscribed to. Mainly the tenantID is used as a key.
In short: In most cases you don’t need the host name and, if implemented, it leads to bad design of your microservice e.g. establishing unnecessary new connections using the host name etc.