Git Product home page Git Product logo

cna-workshop's Introduction

Pivotal Spring Data Demos

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.

Spring JDBC

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.

Deploying and Running the Demo Locally

  1. In a terminal window, change to the jdbcdemo directory, build, package, and run the application:

    mvn clean package spring-boot:run
  2. To use the service to insert data, you can use an application like Postman, or if you have curl installed, execute

    curl -X POST -H "Content-Type: application/json" -d '{"type":"Chequing","status":"Active","balance":"777"}' http://localhost:8080/account
  3. Retrieve a list of accounts:

    curl -X GET -H "Content-Type: application/json" http://localhost:8080/accounts
  4. Delete an account:

    curl -X DELETE -H "Content-Type: application/json" http://localhost:8080/account/6367736797661010002

Connecting to a Local MySQL Database

  1. Download and install a local instance of MySQL. By default, a test schema is created. Update the MySQL username and password as necessary in the jdbcdemo/src/main/resources/application.yml file.

  2. 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

Connecting to a Local DB2 Database

  1. Download and install a local instance of DB2. Create a sample schema. Update the DB2 username and password as necessary in the jdbcdemo/src/main/resources/application.yml file.

  2. 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

Deploying the Demo to Pivotal Cloud Foundry (PCF)

  1. Log into your PCF instance, and verify your Org and Space by running the cf target command.

  2. 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"
    }
  3. Create the user-provided service in PCF:

    cf cups jdbcdemo-db -p config.json
  4. Examine the manifest.yml file, note that it will automatically bind to the service. Deploy your application from the jdbcdemo 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.

  5. 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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.