Code Monkey home page Code Monkey logo

luismr / docker-for-devs-5-java-springboot-with-local-test-and-prod-envs Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 14 KB

Spring Boot 2.7 using Rest Repository for JPA using H2 in memory database - NO CONTROLLER - it is automatically generated/handled by Spring Data Rest. This application (Challenge #2) will run in local (H2 persistent), test (H2 transient - for CI/CD) and Prod (MariaDB)

Java 97.62% Dockerfile 2.38%
java jpa jpa-entities rest-api rest-api-framework spring-boot springboot

docker-for-devs-5-java-springboot-with-local-test-and-prod-envs's Introduction

Getting Started

Requirements

You can use SDKMAN to manage your Java and Maven runtimes, and there is a very good tutorial about how to use SDKMAN here.

  • Java 17
sdk install java 17.0.3.6.1-amzn
  • Maven 3.8.5
sdk install maven 3.8.5

Build and Run

Using Maven

Build

mvn clean install

Running only the Tests

mvn test

Running the application

Test Environment

Test environment will use H2SQL in memory mode to persist your test data

mvn spring-boot:run

or using just Java

mvn clean install
java -jar target/api-crud-1.0-SNAPSHOT.jar 
Local Environment

Local environment will use H2SQL in file to persist your changes

mvn spring-boot:run -Dspring-boot.run.profiles=local

or using just Java

mvn clean install
java -jar -Dspring.profiles.active=local target/api-crud-1.0-SNAPSHOT.jar 
Prod Environment

Running Docker MariaDB to provide Database support and run the application using your own Machine (for test and debug)

  • To create the MariaDB container, if it is not created
docker run --name db -d -e MARIADB_ROOT_PASSWORD=password -e MARIADB_DATABASE=mydb mariadb:latest
  • To stop the MariaDB container, if the container is already running
docker stop db
  • To restart the MariaDB container, if the container is already created do
docker start db
  • To remove the MariaDB container, stop the container first and then
docker rm db

Then, start the application using maven

mvn spring-boot:run -Dspring-boot.run.profiles=deploy

... or execute directly from the JAR file, after you build it usinbg maven

mvn clean install
java -jar -Dspring.profiles.active=deploy target/api-crud-1.0-SNAPSHOT.jar 

To customize the deploy to connect in another MYSQL database, please set these environment variables:

  • MYSQL_HOST: connects to the defined host (default localhost)
  • MYSQL_PORT: connects to the defined port (default 3306)
  • MYSQL_DATABASE: select the database as default for opened connection (default mydb)
  • MYSQL_USERNAME: connects to database using the username (default root)
  • MYSQL_PASSWORD: connects to database using the username (default password)
  • SQL_QUERIES_SHOW: show all SQL statements generated by JPA (default false)
  • SQL_QUERIES_FORMAT: pretty format all SQL statements generated by JPA (default false)

Example:

export MYSQL_HOST=mydb.internal.net
export MYSQL_DATABASE=customer_data
export MYSQL_USERNAME=customers
export MYSQL_PASSWORD=str0ngp@ssw0rd
export SQL_QUERIES_SHOW=true
export SQL_QUERIES_FORMAT=true 
java -jar -Dspring.profiles.active=deploy target/api-crud-1.0-SNAPSHOT.jar 

It works because the application-deploy.yml profile has placeholders to find the value from the environment variables or use a default value. Please realize when you create your local MariaDB container you set the all parameters to match default profile values.

Using IDE

I used IntelliJ Idea from JetBrains to build this application but you also can use Eclipse, or Netbeans to run this application.

Documentation

References

For further reference, please consider the following sections:

Guides

The following guides illustrate how to use some features concretely:

docker-for-devs-5-java-springboot-with-local-test-and-prod-envs's People

Contributors

luismr avatar

Watchers

 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.