Java CFEnv
Java CFEnv is a library for easily accessing the environment variables set when deploying an application to Cloud Foundry. It is modeled after the design of the node library node-cfenv and other -cfenv
libraries in the Cloud Foundry ecosystem.
The class CfEnv
is the entry point to the API for accessing Cloud Foundry environment variables. In a Spring application, you can use the Spring Expression Language to invoke methods on bean of type CfEnv
to set properties. CFEnv’s Boot support sets common application properties so that Java objects such as the DataSource
or the RabbitConnectionFactory
are created using Spring Boot autoconfiguration.
The 1.0 M1 blog provides some additional background information.
Dependency Info
You can access the stable release from maven central using the coordinates
<dependency>
<groupId>io.pivotal.cfenv</groupId>
<artifactId>java-cfenv-boot</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
SCS and SSO modules
If you’re attempting to connect a client app to a Spring Cloud Services or Single Sign-On service instance, you should follow their respective docs.
Introduction
The entry point is the class CfEnv
which parses Cloud Foundry environment variables such as VCAP_SERVICES
. The information in VCAP_SERVICES
is a JSON string that contains credential information to access bound services, such as a database. For example, here is the value of VCAP_SERVICES
for an application bound to the mysql and redis services.
{
"p-mysql": [
{
"credentials": {
"hostname": "10.0.4.35",
"port": 3306,
"name": "cf_2e23d10a_8738_8c3c_66cf_13e44422698c",
"username": "8McHri7aKbuTEGCR",
"password": "J2BNJYkeXAH9idkG",
"uri": "mysql://8McHri7aKbuTEGCR:[email protected]:3306/cf_2e23d10a_8738_8c3c_66cf_13e44422698c?reconnect=true",
"jdbcUrl": "jdbc:mysql://10.0.4.35:3306/cf_2e23d10a_8738_8c3c_66cf_13e44422698c?user=8McHri7aKbuTEGCR&password=J2BNJYkeXAH9idkG"
},
"syslog_drain_url": null,
"volume_mounts": [],
"label": "p-mysql",
"provider": null,
"plan": "100mb",
"name": "mysql",
"tags": [
"mysql",
"relational"
]
}
],
"p-redis": [
{
"credentials": {
"host": "10.0.4.30",
"password": "291452d8-d4a1-3bec-90d9-g50503138248",
"port": 45470
},
"syslog_drain_url": null,
"volume_mounts": [],
"label": "p-redis",
"provider": null,
"plan": "shared-vm",
"name": "redis",
"tags": [
"pivotal",
"redis"
]
}
],
"nfs": [
{
"binding_name": null,
"credentials": {},
"instance_name": "nfs1",
"label": "nfs",
"name": "nfs1",
"plan": "Existing",
"provider": null,
"syslog_drain_url": null,
"tags": [
"nfs"
],
"volume_mounts": [
{
"container_dir": "/var/vcap/data/78525ee7-196c-4ed4-8ac6-857d15334631",
"device_type": "shared",
"mode": "rw"
}
]
}
]
}
The keys in the JSON are not always identical across services, for example in the above JSON the mysql service has added the key named hostname
to the credentials while the redis service has contributed the key named host
. Most relational database services do not expose a key named jdbcUrl
so the URL needs to be created by extracting individual fields and building up the URL string. Since this is such a common case, the CfEnv library provides support to retrieve the URL string for several databases.
To get access to a specific service’s credentials, the keys tag
, name
, or label
can be specified as a criteria for selecting one of the services in the array.
Basic usage
Using the JSON from the previous section, here are some simple API calls to extract credential information.
CfEnv cfEnv = new CfEnv();
String redisHost = cfEnv.findCredentialsByTag("redis").getHost();
String redisPort = cfEnv.findCredentialsByTag("redis").getPort();
String redisPassword = cfEnv.findCredentialsByTag("redis").getPassword();
Multiple strings can be passed to match against more than one tag. There are additional finder methods to search by name
and label
and the finder method support passing a regex string for pattern matching.
The classes CfService
and CfCredentials
are returned from the following API calls and have methods for accessing common fields in addition to a generic get(String)
map API.
CfEnv cfEnv = new CfEnv();
List<CfService> cfService = cfEnv.findAllServices();
CfService redisService = cfEnv.findServiceByTag("redis");
List<String> redisServiceTags = redisService.getTags();
String redisPlan = redisService.getPlan();
redisPlan = redisService.get("plan")
CfCredentials redisCredentials = cfEnv.findCredentialsByTag("redis");
String redisPort = redisCredentials.getPort();
Integer redisPort = redisCredentials.getMap().get("port");
cfService = cfEnv.findServiceByName("redis");
cfService = cfEnv.findServiceByLabel("p-redis");
cfService = cfEnv.findServiceByLabel(".*-redis");
The class CfVolume
contains information for a shared disk provided by Cloud Foundry volume services. You can access it using the getVolumes
method on CfService
as shown below.
CfEnv cfEnv = new CfEnv();
List<CfVolume> cfVolumes = cfEnv.findServiceByName("nfs1").getVolumes();
String path = cfVolumes.get(0).getPath();
JDBC Support
There is additional support for getting the JDBC URL contained in the module spring-cfenv-jdbc
. The entry point to the API is the class CfJdbcEnv
which is a subclass of CfEnv
and adds a few methods. The method findJdbcService
will heuristically look at all services for known tags, labels and names of common database services to create the URL.
CfJdbcEnv cfJdbcEnv = new CfJdbcEnv()
CfJdbcService cfJdbcService = cfJdbcEnv.findJdbcService();
String jdbcUrl = cfJdbcService.getJdbcUrl();
String username = cfJdbcService.getUsername();
String password = cfJdbcService.getPassword();
String driverClassName = cfJdbcService.getDriverClassName();
If there is more than one database bound to the application, an exception will be thrown and you should use the findJdbcServiceByName
method to locate a unique database service.
String jdbcUrl1 = cfJdbcEnv.findJdbcServiceByName("mysqlA").getUrl();
String jdbcUrl2 = cfJdbcEnv.findJdbcServiceByName("mysqlB").getUrl();
If you are creating a user-provided-service in order to connect to a database by jdbc, provide a uri
property along with username
and password
in the credentials block.
cf cups sqlserver-ups -p '{ "uri": "sqlserver://hostname:1433;database=test_db", "username": "someuser", "password": "whatmeworry?" }'
Alternatively, you can simply provide a fully qualified jdbcUrl
.
cf cups sqlserver-ups -p '{ "jdbcUrl": "jdbc:sqlserver://hostname:1433;database=test_db;user=someuse;password=whatmeworry?" }'
In either case, consult your database provider’s documentation on the correct format of a connection uri. This library will preserve any platform specific uri elements.
Use with Spring
If you register the CfJdbcEnv
class as a bean, then you can use the Spring Expression Language to set properties.
@Bean
public CfJdbcEnv cfJdbcEnv() {
return new CfJdbcEnv();
}
Then in a property file imported by Spring, refer to the CfJdbcEnv bean using the following syntax.
myDatasourceUrl=#{ cfJdbcEnv.findJdbcService().getUrl() }
Or say for cassandra, you can use the CfEnv
class registered as a bean.
@Bean
public CfEnv cfEnv() {
return new CfEnv();
}
cassandra.contact-points=#{ cfEnv.findCredentialsByTag('cassandra').get('node_ips') }
cassandra.username=#{ cfEnv.findCredentialsByTag('cassandra').getUserName() }
cassandra.password=#{ cfEnv.findCredentialsByTag('cassandra').getPassword() }
cassandra.port=#{ cfEnv.findCredentialsByTag('cassandra').get('cqlsh_port') }
Similar for setting a custom application property to access the disk mounted by Volume Services.
myapp.config.path=#{ cfEnv.findServiceByName("nfs1").getVolumes().get(0).getPath() }
Using Spring Boot
The module java-cfenv-boot
provides several EnvironmentPostProcessor
implementations that set well known Boot properties so that Boot’s auto-configuration can kick in. For example, the CfDataSourceEnvironmentPostProcessor
sets the Boot property spring.datasource.url
. Just add a dependency on java-cfenv-boot
.
The list of supported services are:
-
Databases - DB2, MySQL, Oracle, Postgresl, SqlServer
-
RabbitMQ
-
Cassandara
-
MongoDB
-
Redis
-
CredHub
-
Hashicorp Vault
If, for any reason, you need to disable processing of a specific service instance, you can do so by setting the following flag in your application properties:
cfenv.service.{serviceName}.enabled=false
Connecting Multiple Instances of a Single Service Type
If you need to configure connections to multiple instances of a given service type, or do anything more than setting application properties for spring-boot to pick up and use in auto-configuration, you will need to follow the approach layed out in the sections above (Basic Usage, JDBC Support, Use with Spring) to access the binding credentials. Then follow the same procedure that would be used to connect to your services in any other deployment environment.
Pushing your application to Cloud Foundry
You must disable the java buildpack’s auto-reconfiguration so that you always delegate to Boot to create beans.
cf set-env <APP> JBP_CONFIG_SPRING_AUTO_RECONFIGURATION '{enabled: false}'
Since the auto-reconfiguration also set the cloud profile, you will have to do that explicitly
cf set-env <APP> SPRING_PROFILES_ACTIVE cloud
Supporting other Services
The interface CfEnvProcesor
simplifies what you need to write in most cases. The environment post processor, CfEnvPostProcessor
delegates to all CfEnvProcessors that are discovered using Spring’s SpringFactoriesLoader
. Here is the implementation for MongoDB
public class MongoCfEnvProcessor implements CfEnvProcessor {
private static String mongoScheme = "mongodb";
@Override
public boolean accept(CfService service) { (1)
return service.existsByTagIgnoreCase("mongodb") ||
service.existsByLabelStartsWith("mongolab") ||
service.existsByUriSchemeStartsWith(mongoScheme) ||
service.existsByCredentialsContainsUriField(mongoScheme);
}
@Override
public void process(CfCredentials cfCredentials, Map<String, Object> properties) { (2)
properties.put("spring.data.mongodb.uri", cfCredentials.getUri(mongoScheme));
}
@Override
public CfEnvProcessorProperties getProperties() { (3)
return CfEnvProcessorProperties.builder()
.propertyPrefixes("spring.data.mongodb")
.serviceName("MongoDB")
.build();
}
}
-
In the
accept
method is where you put determine how to identify your service type. TheCfService
class has several methods to help make this as easy as possible. -
Copy over necessary values from the
CfCredentails
object to Spring Boot auto-configuration keys. -
Create a
CfEnvProcessorProperties
instance so that logging which is done inCfEnvPostProcessor
reflects your specific service.
Remember to add an entry in spring.factories
so that your processor can be discovered.
io.pivotal.cfenv.spring.boot.CfEnvProcessor=com.example.MyCoolServiceCfEnvProcessor
Building
Clone the repo and type
$ ./mvnw clean install
which will run the tests as well.