What product/components do you use and which version/fix level are you on?
Cumulocity IoT 10.10
Is your question related to the free trail, or to a production (customer) instance?
Customer License
What are you trying to achieve? Please describe in detail.
We want to call one of the endpoints of microservice hosted on c8y from another microservice which is hosted on the same c8y instance. We are trying to get the platform hostname using the class com.cumulocity.sdk.client.PlatformParameters
String hostname = platformParameters.getHost();
But its returning the hostname as http://cumulocity:8111/ in our test environments and that is not working while calling the microservice endpoint as we are getting HTTP code 422.
We wanted to know if there are any other option available?
Hi Robin,
I am not sure if the connection to http://cumulocity:8111/ should actually work and something else is missing there.
Can you share how you build the URL to the microservice API endpoint in detail?
Did you elaborate the usage of RestConnector, which helps in performing custom API calls to Cumulocity - like microservice API calls?
Alternatively below is a other solution, which I would only consider as an (ugly) work around to get the base URL.
You can read it from the system or spring environment:
private static final String SPRING_ENV_c8y_baseUrl = "C8Y.baseURL";
private static final String C8Y_BASEURL = "C8Y_BASEURL";
private final Environment environment;
private final MicroserviceSubscriptionsService subscriptions;
public String getC8yBaseUrl() {
String baseUrl = null;
if (System.getenv().containsKey(C8Y_BASEURL)) {
baseUrl = System.getenv(C8Y_BASEURL);
log.debug("Using sysenv C8Y_BASEURL:{}", baseUrl);
} else {
baseUrl = environment.getProperty(SPRING_ENV_c8y_baseUrl);
log.debug("Using spring environemnt C8y.baseUrl:{}", baseUrl);
}
if (!baseUrl.endsWith("/")) {
baseUrl = baseUrl + "/";
}
return baseUrl;
}
Accessing other microservices requires to go through the external loadbalancer.
you could try using the currentTenant endpoint and to use the domainName attribute of the tenant.
By using the external domain specified with domainName you should be able to access the other microservice from within your microservice.
Iām sadly not aware which classes you can use within java microservice SDK to do this.
Thank you very much for the reply.
So I tried below options to get c8y base url
PlatformParameters platformParameters;
String hostname = platformParameters.getHost();
This gave me URL as http://cumulocity:8111/ which did not work as I was getting 422 error code
I also tried getting the hostname from managedObject API using getSelf() method.
That gave me URL as https://t554656253.eu-latest.cumulocity.com which again did not work as it was timing out.
As @Tristan_Bastian mentioned you can use the currentTenant endpoint as well.
Which reminded me of the following:
Sadly the Microservice SDK does not support this, but it can be easily implemented, see below.
Be aware that there is already a TenantRepresentation.class existing in the SDK (com.cumulocity.rest.representation.tenant.TenantRepresentation), unfortunately this does not have the required domainName attribute.
package mypackage;
public class TenantRepresentation {
private String domainName;
public String getDomainName() {
return domainName;
}
}