GithubHelp home page GithubHelp logo

ibm-garage / springboot-items-api Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 5.0 132 KB

A CRUD archetype using Spring Boot and Spring Data in an imperative (non-reactive) way. It is also intended to demonstrate the use of Aspect Oriented Programming (AOP) for trace logging, database exception conversion, and annotation based event processing.

License: Apache License 2.0

Java 98.41% Dockerfile 0.21% Mustache 1.38%
spring-boot springboot aspect-oriented-programming aop-aspectj spring-data rest-api kubernetes openshift archetype-java

springboot-items-api's Introduction

Spring Boot Items-API Archetype Microservice

This project is intended to serve as a CRUD archetype using Spring Boot and Spring Data in an imperative (non-reactive) way. It is also intended to demonstrate the use of Aspect Oriented Programming (AOP) for trace logging, database exception conversion, and annotation based event processing. More information about the use of AOP in this application can be found in this [accompanying Medium article] (https://medium.com/ibm-garage/improving-readability-of-java-code-with-aspect-oriented-programming-aop-167b05846190)

Note: To make it easier to use as a demonstration, a H2 in memory database is used with spring data, with the goal being for it to be swapped out for the database of your choice.

Building and packging the JAR into a container

This code is meant to be containerized. If you are deploying to Kubernetes you will need to build and containerize the application first.

NOTE: If deploying to OpenShift, how you build may be different depending on your needs and if you use the manifests/openshift/build.yaml as described in the "Deploying to Openshift" section.

NOTE: If you don't want to or can't build it yourself, you can use the container that is deployed in dockerhub at "michaelsteven/springboot-items-api:0.0.1"

Steps:

  1. clone down this git repository
  2. build the jar file (using maven locally, or maven in docker as shown below):
    docker run -v $PWD:/usr/src/springboot-items-api -w /usr/src/springboot-items-api maven:3-eclipse-temurin-17  mvn clean install
    
  3. Use the Dockerfile to create the container:
    docker build -t <<your-registry-namespace>>/springboot-items-api:0.0.1 .
    
  4. Push the container to your docker registry:
    docker login
    docker push <<your-registry-namespace>>/springboot-items-api:0.0.1
    

NOTE: Be sure to use your own namespace name instead of "<"

Deploying to Kubernetes

These instructions will walk you through using the helm chart found in the manifests folder of this repository to deploy the application from the command line.

PREREQUISITES:

  • kubectl cli
  • helm cli

How to deploy:

  1. clone down the code
  2. build and publish the docker container to your container registry using the steps in the "Building the code and packaging the JAR into a container" Alternatively, you could use my already published container.
  3. edit the 'manifests/helm/springboot-items-api/values.yaml with your values
  4. log into your kubernetes cluster from the CLI (this may vary depending on where your kubernetes cluster is deployed)
  5. verify you are connected to the cluster you expect
kubectl config current-context
  1. If needed, create your namespace
kubectl create namespace dev
  1. deploy the application.yaml into a ConfigMap in your namespace
kubectl create configmap springboot-items-api --from-file=application.yaml -n dev
  1. create a secret with your database credentials:
oc create secret generic mysql-credentials --from-literal=username=root --from-literal=password=password
  1. Add any other secrets you are using, including the ingress TLS secret. Steps for this may vary. Alternatively you could comment out the "tls" section in "manifests/helm/springboot-items-api/templates/ingress.yaml"
  2. Dry run the helm chart to look for problems
helm install -f manifests/helm/springboot-items-api/Chart.yaml springboot-items-api -n dev --dry-run ./manifests/helm/springboot-items-api
  1. Once happy with the output, run it for real without the --dry-run option
helm install -f manifests/helm/springboot-items-api/Chart.yaml springboot-items-api -n dev --dry-run ./manifests/helm/springboot-items-api

Deploying to Openshift

Explore running in OpenShift with a Developer Sandbox (free) account available in RedHat: https://developers.redhat.com/developer-sandbox.

Below is an step-by-step of a way to build and deploy this microservice using the CLI. They could also be used as part of an external CI/CD pipeline.

  1. clone down this git repository
  2. build the code with maven (mvn clean install)
  3. install the OC CLI
  4. log in to OpenShift with the OC CLI using a token generated from the OpenShift Console
  5. change to the right project (if needed)
  6. create a ConfigMap with your application settings:
oc create configmap springboot-items-api --from-file=application.yaml
  1. create a secret with your database credentials:
oc create secret generic mysql-credentials --from-literal=username=root --from-literal=password=password
  1. create a build configuration for the application using a template:
oc new-app --file=./manifests/openshift/build.yaml --param=APP=springboot-items-api
  1. initiate a build of the build configuration, uploading the current directory for use in the Dockerfile:
oc start-build springboot-items-api --from-dir .

This will build the Dockerfile and publish it into openshift's integrated container registry. 10. Use the deployment template to deploy the application:

oc process -f manifests/openshift/deployment.yaml --param=REGISTRY=image-registry.openshift-image-registry.svc:5000/michael-hepfer-dev | oc apply -f -

Copyright

© Copyright IBM Corporation [2021], [2022].

Licensing

Licensed under the Apache License, Version 2.0 (the "License"); you may not use these files except in compliance with the License. You may obtain a copy of the License at http://ww.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permission and limitations under the License.

springboot-items-api's People

Contributors

michaelsteven avatar

Stargazers

 avatar

Watchers

 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.