The Workshop application was designed to illustrate the ease with which data services can be bound to and utilized by applications running on Cloud Foundry. In this lab, we’ll be binding the application to a MySQL databases.
Cloud Foundry services are managed through two primary types of operations:
- Create/Delete
These operations create or delete instances of a service. For a database this could mean creating/deleting a schema in an existing multitenant cluster or creating/deleting a dedicated database cluster.
- Bind/Unbind
These operations create or delete unique credential sets for an existing service instance that can then be injected into the environment of an application instance.
Your instance of the sample Workshop should not be running after the steps completed in lab 1. Make sure to change to the cf-spring-mvc-boot sample application directory. For this lab we will be using the Java application. Deploy the application:
$ cf push <some-unique-appname>
After the application deploys and starts, visit the application in your browser by hitting the route that was generated by the CLI. Currently, this data is being retrieved from an in-memory HSQL database running within the JVM. Let’s change that.
There are two ways to discover what services are available on Pivotal Cloudfoundry. The first is available on any instance of Cloud Foundry: the CLI. Just type:
$ cf marketplace
and you’ll get a list of services, their available plans, and descriptions. On Pivotal Cloudfoundry, the "free" tier of plans is normally the first one listed.
The second way is specific to Pivotal Cloudfoundry’s Apps Manager UI. If you haven’t already, login to it by visiting Apps Mgr
Click on the "Marketplace" link:
and you’ll see the same (or a similar if on PWS) service/plan/description listing in the browser:
-
Let’s begin by creating a MySQL service instance. From the Apps Manager UI service marketplace, select MySQL for Pivotal Cloudfoundry, select the free plan, and provide an instance name. In the drop-down list next to Bind to App select your workshop application.
-
Notice the admonition to
Use 'cf restage' to ensure your env variable changes take effect
. Let’s take a look at the environment variables for our application. We can do this by viewing to the homepage of the application. Here we will see a printout of the environment information. Take note of what is contained in the section Bound Services. It’s an empty JSON document! -
Now let’s restage the application, which cycles our application back through the staging/buildpack process before redeploying the application. In this case, we could accomplish the same goal by only restarting the application via
cf restart cf-spring-mvc-boot
. A restage is generally recommended because Cloud Foundry buildpacks also have access to injected environment variables and can install or configure things differently based on their values.]$ cf restage cf-spring-mvc-boot
Once the application is running again, revisit or refresh the browser tab where you have the Wokshop application loaded. You’ll notice now we have information that could be used to connect to a DB. In fact, our data is now being retrieved from that MySQL database!
Bound Services: { "p-mysql":[ { "name":"adam-db", "label":"p-mysql", "tags":[ "mysql", "relational" ], "plan":"100mb-dev", "credentials":{ "hostname":"10.68.105.55", "port":3306, "name":"cf_226845a3_1982_44ac_92b9_4b149af56bbc", "username":"n67hpBOnKlsPUjKn", "password":"hUa4WSrq2hPtBsuk", "uri":"mysql://n67hpBOnKlsPUjKn:[email protected]:3306/cf_226845a3_1982_44ac_92b9_4b149af56bbc?reconnect=true", "jdbcUrl":"jdbc:mysql://10.68.105.55:3306/cf_226845a3_1982_44ac_92b9_4b149af56bbc?user=n67hpBOnKlsPUjKn&password=hUa4WSrq2hPtBsuk" } } ] }
-
You may also verify your service was provisioned in the Apps Manager UI by clicking on your application and selecting the Services tab.
You should now see the service created in step 1 listed.
-
You may also verify your service was provisioned using the CLI:
$ cf services
You should now see the service created in step 1 listed and your application listed as a bound app.