The purpose of this workshop is to demonstrate Spring Boot, and compare Spring JPA with Spring JDBC to connect to an existing database when deployed to Cloud Foundry.
This application will use the Spring Data JDBC Template to demonstrate accessing an external database from a Spring Boot application running first locally, then on Pivotal Cloud Foundry.
Source code for this demo application can be found in the jdbcdemo
directory.
When deployed locally and not specifying any profile, the app will by default use an H2 in-memory database. Other profiles have been set up to access MySQL and DB2 databases. When the app is deployed to PCF you can create a user-provided service to connect to an external database.
-
In a terminal window, change to the
jdbcdemo
directory, build, package, and run the application:mvn clean package spring-boot:run
-
To use the service to insert data, you can use an application like Postman, or if you have
curl
installed, executecurl -X POST -H "Content-Type: application/json" -d '{"type":"Chequing","status":"Active","balance":"777"}' http://localhost:8080/account
-
Retrieve a list of accounts:
curl -X GET -H "Content-Type: application/json" http://localhost:8080/accounts
-
Delete an account:
curl -X DELETE -H "Content-Type: application/json" http://localhost:8080/account/6367736797661010002
-
Download and install a local instance of MySQL. By default, a
test
schema is created. Update the MySQL username and password as necessary in thejdbcdemo/src/main/resources/application.yml
file. -
Run the application again using the
dev
profile to connect to the local MySQL instance:mvn clean package spring-boot:run -Dspring.profiles.active=dev
-
Download and install a local instance of DB2. Create a
sample
schema. Update the DB2 username and password as necessary in thejdbcdemo/src/main/resources/application.yml
file. -
Run the application again using the
test
profile to connect to the local MySQL instance:mvn clean package spring-boot:run -Dspring.profiles.active=test
-
Log into your PCF instance, and verify your Org and Space by running the
cf target
command. -
Create a user-provided service JSON configuration file with the following content, substituting your own values for each
DB2_
parameter:config.json{ "hostname": "DB2_HOST", "jdbcUrl": "jdbc:db2://DB2_HOST/DB2_SCHEMA?user=DB2_USER\u0026password=DB2_PASSWORD", "name": "DB2_SCHEMA", "password": "DB2_PASSWORD", "port": "DB2_PORT", "uri": "db2://DB2_HOST:DB2_PORT/DB2_SCHEMA?reconnect=true", "username": "DB2_USER" }
-
Create the user-provided service in PCF:
cf cups jdbcdemo-db -p config.json
-
Examine the
manifest.yml
file, note that it will automatically bind to the service. Deploy your application from thejdbcdemo
root directory:cf push
Spring will automatically look for the credentials and connect to the external DB2 instance specified when you created the user provided service.
-
Test the application using the same
curl
commands (or Postman) used for the local deployment above, just changing the app URL endpoint to match your application route.