This is the second part following this post: Creating a Service (Gateway) in CF
This post will summarize:
– how to deploy a custom service
– how to deploy the gateway that brokers that service
– how to deploy and bind an app that uses the service
The full source code for this blog post can be found here: cf-service-example
Deploying the service
Deploying the service is straight forward, we will do this like any CF app with cf push.
Navigate into the directory where your services lives, makes sure you are logged into CF, then `cf push`.
Respond to the prompts with the name you want for the service, I use random-string-service-service, for the rest we can use defaults.
Once the push was successful, `cf apps` should show something like this:
Getting applications in bootstrap-space... OK
name status usage url
random-string-service-service running 1 x 256M random-string-service-service.georg.cf-app.com
If you are using the example app, visiting the above url and adding `/health` should tell you that the service app is responding. You should see ‘The random string service is at your service!’ on the page.
Deploying the gateway
This is the part that was the most confusing to me. The gateway will play a broker role down the road and it is important to know that you use the gateway to create a service instance. From the directory where the gateway lives do a cf push.
$ cd [path_to]custom_gateway
$ bundle
$ cf create-service-auth-token custom-gateway pivotal --token 'random string service token'
$ cf push
Note that the service auth token gets created for the gateway, check the gateway.config.example to see details.. Next you can create the service that will be used by our app with:
$ cf create-service random-string-service
> random-string-service-sample
To double check, listing services should show our newly created service.
cf services
Getting services in bootstrap-space... OK
name service provider version plan bound apps
random-string-service-sample custom-gateway pivotal 1.0 10req none
Deploying the app that binds to the service
All that is needed now is to do a cf push from the directory the sample app lives in. In the push process select the option to bind a service and when prompted for a service, select the ‘random-string-service’, you can choose the defaults for everything else.
In order to use the service in our app, its needs to be getting the service info from the environment. Interesting to note here, the name that is uses in the VCAP_SERVICES environment is based on the service label, not the name you give your service instance.
def connector_url
unless ENV["VCAP_SERVICES"]
return "http://random-string-service.georg.cf-app.com/random_string"
end
vcap_services = JSON.parse(ENV["VCAP_SERVICES"])
vcap_services["random-string-service-1.0"][0]["credentials"]["url"]
end
The service itself could be much more extensive and wrapped into an object that the app uses.
When visiting the url that `cf apps` shows you for the sample app you should see a random string that gets generated by the service.
The full example for this post can be found here: cf-service-example
Also check out the CF documentation for services