Code Monkey home page Code Monkey logo

spring-postgresql-demo's Introduction

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

spring-postgresql-demo's People

Contributors

garystafford avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

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.