Skip to content

Spring Boot 2.0 application, backed by PostgreSQL, and designed for deployment to Pivotal Cloud Foundry (PCF)

License

Notifications You must be signed in to change notification settings

garystafford/spring-postgresql-demo

Repository files navigation

License Build Status

Spring Boot 2.0 PostgreSQL Application Demonstration

This project was originally used for the Programmatic Ponderings blog post, Developing Cloud-Native Data-Centric Spring Boot Applications for Pivotal Cloud Foundry, published March, 2018. Spring Boot 2.0 application, backed by PostgreSQL, and designed for deployment to Pivotal Cloud Foundry (PCF). Database changes are handled by Liquibase.

Also, this project was used for the two-part Programmatic Ponderings post, Managing Applications Across Multiple Kubernetes Environments with Istio, published April, 2018.

Docker Quick Start

The project now contains Dockerfile and docker-compose.yml files. If you have Docker and Docker Compose installed locally, you can preview project by creating Docker containers for both the PostgreSQL database and the Spring Boot application. To do so, execute the follow command from the root of the project

git clone --depth 1 --branch master \
  https://github.com/garystafford/spring-postgresql-demo.git
cd spring-postgresql-demo

docker-compose -p springdemo up -d

To follow the startup of the Spring Boot application, use the docker logs springdemo --follow command. When complete, browse to http://localhost:8080. See the list of available resources below.

To delete both Docker containers when done previewing, use the docker rm -f postgres springdemo command.

Build and Run Application with Gradle

The project assumes you have Docker and the Cloud Foundry Command Line Interface (cf CLI) installed locally.

First, provision the local PostgreSQL development database using Docker:

# create container
docker run --name postgres \
  -e POSTGRES_USERNAME=postgres \
  -e POSTGRES_PASSWORD=postgres1234 \
  -e POSTGRES_DB=elections \
  -p 5432:5432 \
  -d postgres

# view container
docker container ls

# trail container logs
docker logs postgres  --follow

Local database connection details are set in the src\main\resources\application.yml file.

The default Spring Profile, uses an h2 instance:

datasource:
  url: jdbc:h2:mem:elections
  username: sa
  password:
  driver-class-name: org.h2.Driver
  jpa:
    show-sql: true
h2:
  console:
    enabled: true

The dev Spring Profile, uses localhost PostgreSQL instance:

datasource:
  url: jdbc:postgresql://localhost:5432/elections
  username: postgres
  password: postgres1234
  driver-class-name: org.postgresql.Driver
jpa:
  show-sql: true

Optionally, you can override any of the setting in the src\main\resources\application.yml files, by setting local environment variables, such as:

# use 'set' on Windows
export SPRING_PROFILES_ACTIVE=<profile>
export SPRING_DATASOURCE_URL=<some_other_url>
export SPRING_DATASOURCE_USERNAME=<some_other_username>
export SPRING_DATASOURCE_PASSWORD=<some_other_password>

Next, build and run service locally, using Gradle, against the local Docker PostgreSQL database instance. This command will also execute the Liquibase change sets on the Docker PostgreSQL elections database.

SPRING_PROFILES_ACTIVE=dev ./gradlew clean bootRun

To view Liquibase database changelog:

SELECT * FROM databasechangelog;

To delete the local Docker-based PostgreSQL database:

docker rm -f postgres

Deploy to Pivotal Web Services

Purchase and provision an ElephantSQL PostgreSQL as a Service instance through the Pivotal Services Marketplace. Note the 'panda' service plan is NOT FREE! To purchase, you must have a Pivotal account with a credit card attached.

# view elephantsql service plans
cf marketplace -s elephantsql

# purchase elephantsql service plan
cf create-service elephantsql panda elections

# display details of running service
cf service elections

Deploy the Spring Boot service to Pivotal Web Services.

gradle build && cf push

Scale up instances:

# scale up to 2 instances
cf scale cf-spring -i 2

# review status of both instances
cf app pcf-postgresql-demo

Available Resources

Below is a partial list of the application's exposed resources. To see all resources, use the /actuator/mappings resource.

  • Actuator

    • /
    • /actuator/mappings (shows all resources!)
    • /actuator/metrics
    • /actuator/metrics/{metric}
    • /actuator/liquibase
    • /actuator/env
    • /actuator/configprops
    • /actuator/health
    • /actuator/info
    • /actuator/beans
  • Swagger

    • /swagger-ui.html
    • /v2/api-docs
  • h2 (default Spring Profile only)

    • /h2-console
  • Candidates (DB Table)

    • /candidates
    • /candidates/{id}
    • /profile/candidates
    • /candidates/search
    • /candidates/search/findByLastName?lastName=Obama
    • /candidates/search/findByPoliticalParty?politicalParty=Democratic%20Party
    • /candidates/summary
    • /candidates/summary/{politicalParty}
  • Elections (DB Table)

    • /elections
    • /elections/{id}
    • /profile/elections
    • /elections/search
    • /elections/search/findByTitle?title=2012%20Presidential%20Election
    • /elections/search/findByDescriptionContains?description=American
    • /elections/summary
  • Votes (DB Table)

    • /votes
    • /votes/{is}
    • /votes?page={page}}&size={size}
    • /profile/votes
  • Election Candidates (DB Table)

    • /electionCandidates
    • /profile/electionCandidates
  • Candidates, by Elections (DB View)

    • /election-candidates (GET only)
    • /profile/election-candidates
    • /election-candidates/search/findByElection?election=2016%20Presidential%20Election
  • Individual Votes, by Election (DB View)

    • /election-votes (GET only)
    • /election-votes?page={page}}&size={size} (GET only)
    • /profile/election-votes
    • /election-votes/search/findByElection?election=2012%20Presidential%20Election
    • /election-votes/summary
    • /election-votes/summary/{election}
  • Total Votes by Election and by Candidate (DB View)

    • /vote-totals (GET only)
    • /profile/vote-totals
    • /vote-totals/search/findByElection?election=2012%20Presidential%20Election
    • /election-votes/summary

References