Reputation: 13
I have created a user provided service as follows:
cf cups myservice -p '{"db": "text"}'
and I bind this service to my app, the service name is myservice
.
When i use cf env command, i can see the message:
"user-provided": [{
"credentials":{
"db":"text"
},
"name":"myservice"
}]
but when i access this variable with java
System.getenv("cloud.services.myservice.db")
is null. Why can't I access the db
value?
Upvotes: 1
Views: 5062
Reputation: 852
Services In CloudFoundry are presented JSON blob in the VCAP_SERVICES
environment variable.
In Java you will be able to get an object with all the services with:
JSONObject vcap = new JSONObject(System.getenv("VCAP_SERVICES"));
https://docs.run.pivotal.io/devguide/deploy-apps/environment-variable.html for more information on provided environment variables.
Upvotes: 0
Reputation: 5155
When you do cf env
on your app, you see an environment variable named VCAP_SERVICES
that contains a JSON data structure like you showed:
VCAP_SERVICES: {
"user-provided": [
{
"credentials":{ "db":"text" },
"name":"myservice"
}
]
}
Your application can retrieve this JSON structure with System.getenv("VCAP_SERVICES")
. You can then parse the JSON returned from that call into a Map
, for example, and retrieve the values needed.
There is no environment variable available to your app named cloud.services.myservice.db
, so System.getenv("cloud.services.myservice.db")
won't return anything useful.
Spring Boot parses the VCAP_SERVICES environment variable and creates Spring environment properties like cloud.services.myservice.credentials.db
and vcap.services.myservice.credentials.db
. These properties can't be retrieved with System.getenv()
because they exist only in the Spring environment abstraction, not in the OS environment. This is described nicely in a Spring blog post. More details are in the Spring Boot javadoc.
Upvotes: 2