Using Services in Apps

This topic provides usage guidance for binding all a9s Data Services to Cloud Foundry apps.

Binding Information

Binding information is delivered to apps via the VCAP_SERVICES environment variable. This variable is a JSON object string.

Structure

  • hash of services (e.g a9s-redis40, a9s-postgresql94...)
    • array of service instances
      • name
        • string
      • tags
        • array of tag strings
      • credentials
        • service specific credentials

VCAP_SERVICES Example

{
  "a9s-messaging36": [
    {
      "name": "example-rabbitdb",
      "tags": [ "rabbitmq" ],
      "credentials": {
        "host": "localhost",
        "password": "quest",
        "port": 5672,
        "uri": "amqp://guest:guest@localhost:5672",
        "username": "guest"
      }
    }
  ]
}

Best Practices

  1. Do not rely on order in VCAP_SERVICES
  2. Do not rely on service versions
  3. Do not hard code service instance names

Suggested Practices

Option A

Specify the service name via an environment viable with your app manifest. This is trivial to implement in an app, and follows the best practices making it easy to rebind an apps without changing code in your app.

Option B

Look for a service instance with a tag, that can be hard coded in an app, without compromising the best practices. This is trivial to implement, but you must clearly document the tag you have hard coded. The advantage of this option is not having to update a variable in an app manifest when rebinding.

RabbitMQ Go Example

Here you can find a RabbitMQ example of how to simultaneously implement suggested practices A and B in an app.