Giter Club home page Giter Club logo

customer-cloudrun's Introduction

This is code for java springboot microservcies with mongodb as backend and explains how to override application properties externally i.e how to override proeprties set and packaged in the application jar. This make sure that when we containerise an application, we can update a[[lication properties without need to modify packaged Jars.

MongoDB data

The data required is sample mysql database - here

  • Data needs to be in database named classicmodels
  • Following collections are needed, data for these is in /data directory
  • You can use mongoimport of compass to import this data
  • For Customer collection, below is the type for columns for import. Rest of all columns are string.
_id int32
creditLimit double
postalCode int32
saleRepEmployeeNumber int32
  • Below is the schema for orders collection
_id int32
comments string
customerNumber int32
orderDate date
requiredDate date
shipperDate date
status string

Create two instances for mongo DB

  • One local, this is default in application.properties
  • One on MongoAtlas, this we will use to override application properties t oconnect to cloud without compiling / building the code again.

Local Execution

cd customer
mvn package
java -jar target/customer-0.0.1-SNAPSHOT.jar
  • Once the code is running open swagger-ui
    • Now you can run any of the api available on the screen.

Remote connect to MongoAtlas - Non-Container

There are multiple ways to override application properties without changing the application jar. We will look at two of these:-

  • Override using ENV variables

    • Set environment variable SPRING_DATA_MONGODB_URI=<Atlas URI>
      This variable corresponds to spring.data.mongodb.uri set in application.properties in the jar. See how it is capitalized and dot are replaced by underscore.

    • There is no need to rebuild the code, just execute the existing jar. This time code will connect to Atlas.

  • Override using external proeprties files

    • Create config directory in current working directory and place application.properties file with overridden values.
    • place this line in the properties file
      spring.data.mongodb.uri=<Atlas URI>
    • Make sure you have unset the variable and then execute the jar again. (No need to recompile the code)

Now that we know how to externalize configuration for spring boot application, we can move to next step of containerizing it.

Containerized execution

Dockerfile

The dockerfile should be multi-staged to keep it lean by ensuring no unnecessary files make into the image. Read here, and review Dockerfile in this repository in customer folder

Create Image

Run below command to create docker image. You should be in customer directory i.e directory where Dockerfile is present

docker build -t customer .

Run container

docker run -p 8080:8080 --name customer customer

This will error out, there is no localhost mongodb inside container so application cannot connect

Run container with ENV set to Atlas

docker rm customer
docker run -p 8080:8080  -e "SPRING_DATA_MONGODB_URI=<Your Atlas URI>" --name customer customer

If you were able to connect to atlas earlier, this should connect as well and you will be able to launch swagger-ui page.

Run container with external config file

Local path is path to config dir you created above.

docker rm customer
docker run -p 8080:8080  -v "<local path to config dir>:/home/customer/config:ro" --name customer customer

Again you will see that this works and connects to Mongo Atlas.

Now that we have seen how to execute this in docker, our application is finally ready for Kubernetes.

Kubernetes Execution

Now that this application is container ready, we can look at how to deploy this to Cloud Run and Kubernetes

Push image to GCR

gcloud artifacts repositories create customer --repository-format docker --location <region> --description "spring boot service image"
docker tag customer <region>-docker.pkg.dev/<projectid>/customer/customer:v1
docker push <region>-docker.pkg.dev/<projectid>/customer/customer:v1
  • Update image name in deployment.yaml

Create Cluster and deploy service

gcloud container clusters create gke-mongo --zone asia-south1-a
kubectl apply -f customer/kustomize/deployment.yaml
kubectl expose deployment customer-dep  --name customer --port 8080 --target-port 8080

Test it

kubectl port-forward  customer 8080:8080
  • Open swagger-ui.html on localhost - localhost:8080/swagger-ui.html

Cloud Run execution (WIP)

To run this on GCP Cloud Run, associated infrastructure needs to bve created. Terraform template for the same is provided here

The setup created by this template is described in image below. Components which are created by terraform are highlighted with TF logo. Cloud Run Setup

customer-cloudrun's People

Stargazers

Tahir T. avatar  avatar Mayur Duduka avatar

Watchers

James Cloos avatar Kamal avatar

Forkers

sukhcheema

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.