CRUD operation using java springboot microservice hosted in kubernetes env, data stored in mongodb

Overview

springboot-mongodb-k8s-parth

Brief Introduction
Hello Friends,
I have created REST API using Springboot and Spring cloud application which performs CRUD operations (GET,POST,UPDATE,DELETE) , where data is saved into mongo db. Both Spring boot and Mongo db are hosted in kubernetes env(GCP-Google Cloud Provider) where Springboot is expose via LoadBalancer service to internet and Mongo is expose via Cluster IP service. Spring boot app will connect to mongo db via "mongo-service" and end user can connect to API using "springboot-service" Loadbalancer External IP.

Lets understand all moving parts of code and deployment in brief.

ClientController.java – It has code which performs GET,POST,UPDATE,DELETE.
Client.java- It is the Client document schema which we will store in mongo db. private Long id;
private String clientName;
private String clientType;
private Integer clientID;
private Integer gfcid;
private String clientRegion;
ClientRepo.java – It is repo which performs CRUD operations in mongo db.
application.yml- It has db connection information where all db details are fetch from kubernetes configmap and secretes.Pls make sure to set default mongo db port-27017.

#Once code is ready then login to GCP , follow below steps to make deployment up and running in GCP Kubernetes env,

a) Login to GCP(Google Cloud Provider),go to Kubernetes Engine menu-->Select Cluster-->Create New Cluster--> Connect (make sure kubernetes in up ,run command - "kubectl get nodes" to check)
b) Clone the git code using cmd-
   git clone https://github.com/parthec2/springboot-mongodb-k8s-parth.git , it will download whole code in kube cluster
c) Run - Cd springbootmongodb-k8s-parth, then Build code using cmd-
   mvn install -DskipTests
b) Once jar is created, then create image using DockerFile by command -
   docker build ./ –t parthec2/spring-mongo-service
c) Push jar to docker hub by cmd-
   docker push parthec2/spring-mongo-service OR docker push parthec2/spring-mongo-service:1.0 ( Login to docker hub, it should have push image to your docker hub)
d) Now run - cd src/main/resources and create secrets and configmap by using below commands
    kubectl create -f mongo-secret.yml
     kubectl create -f mongo-config.yml
e) Create mongo db persistent volumen and claim using below commands:
    kubectl create -f mongo-pv.yaml
    kubectl create -f mongo-pvc.yaml
g)Create mongo db deployment(pods) and service(type-ClusterIP) using below commands:
    kubectl create -f mongo-deployment.yaml (mongo db user name and pwd are set using env variables which are read from secrets)
    kubectl create -f mongo-service.yml
Check mongo pods health using beow command-
    kubectl exec -it pod/ -- /bin/sh
     #mongo -u test -p test@123 (where username is "test" and password is "test@123" which is stored in secret file in base64 encryption)
     #show dbs
     #use admin
     #show collections
     #db.client.find().pretty(); --It will show empty as we have not inserted any doc yet,


i) Create cluster role otherwise springboot-service will give Forbidden error(403) which accessing mongo service
#create rolebinding
    kubectl create rolebinding default-view --clusterrole=view --serviceaccount=default:default --namespace=default

j)Now,create spring boot deployment(2 Pods) and service(type-LoadBalancer) using below commands:
    kubectl create -f deployment.yml (It create 4 env variables which will be used by application.yml file)
    Get External IP of springboot-service by using command
   kubectl get svc/springboot-service
  HTTP GET
    The GET URL will be form as below- :
    Hit in Postman tool OR browser from your laptop/mobile device
    http://35.202.38.42:8080/api/getAllClients

It will return json with list of clients as below if we have inserted data
[
  {"id":1,"clientName":"ABC-BANK","clientType":"INTERNAL","clientID":44444,"gfcid":5000022,"clientRegion":"CCD"},
  {"id":2,"clientName":"XYZ-BANK","clientType":"INTERNAL","clientID":555555,"gfcid":7222,"clientRegion":"ASD"}
]
   
HTTP POST Go to Postman tool, select Post as option from drop down and add url- http://34.68.140.183:8080/addClient Go to body--select raw-type-JSON
  {
  "id": 4,
  "clientName":"EFG-Client",
  "clientType":"EXTERNAL",
  "clientID": 232323,
  "gfcid": 12345,
  "clientRegion": "SSB"
  }
 

Similary, you can try search by client id(getClient/{id}) and delete by client id (deleteClient/{id}).

Pls email me at [email protected] for any queries/feedback/suggestions,,,,
Keep coding,,Spread knowledge,,,Have a Beautiful day, :)

You might also like...

How to configure Replica Set with Embedded Mongo using Spring Boot and Flapdoodle for unit testing code that uses mongodb transactions

Spring Boot Embedded Mongo with Replica Set This project defines a basic rest service that allows users to update records of a Person (name and email)

Nov 1, 2022

Deploying Spring Boot and MongoDB as Containers Using Docker and Docker Compose

springboot-mongodb-docker Deploying Spring Boot and MongoDB as Containers Using Docker and Docker Compose Steps & Commands pull mongo image from docke

Nov 25, 2022

helm、kubernetes、spring boot、java

建立好 K8s 後需要再 /etc/default/kubelet 新增 --node-ip 的字段其值為當前主機的 IP 在 deployment/kubernetes 下使用 kubectl apply -f . 即可運行服務 Helm Initialize a Helm Chart Repos

Mar 5, 2022

Basic crud operations with json data, main focus is with tests

Spring Crud operations Basic crud operations with json data, main focus is with tests. For future reference Road Map Basic Crud on controllers (done)

Feb 1, 2022

Hi, Spring fans! We're going to learn how to build Kubernetes operators, CRDs, and controllers

Bootiful Kubernetes Operators Make sure youre in the default namespace of a Kubernetes cluster. Not sure fi this matters but I am, so it might help. T

Dec 29, 2022

Aye - an open source tool for scanning images on Kubernetes cluster

Aye - an open source tool for scanning images on Kubernetes cluster

Aye is an open source tool for scanning images on Kubernetes cluster. It uses Anchore CLI behind the scenes to get information about all images that are currently on the cluster. It also provides Prometheus metrics for each image, so teams are aware of different levels of vulnerabilities found inside them.

Sep 4, 2022

My solutions for the MongoDB for Java Developers course

Welcome to M220J Disclaimer: The dependencies and versions in this project are not maintained. This project is intended for educational purposes and i

Jun 26, 2022

DCL-350: Implementing MicroService Architecture using Spring Cloud

DCL-350: Implementing MicroService Architecture using Spring Cloud

DCL-350: Implementing MicroService Architecture using Spring Cloud

Sep 20, 2022

mall学习教程,架构、业务、技术要点全方位解析。mall项目(40k+star)是一套电商系统,使用现阶段主流技术实现。涵盖了SpringBoot 2.3.0、MyBatis 3.4.6、Elasticsearch 7.6.2、RabbitMQ 3.7.15、Redis 5.0、MongoDB 4.2.5、Mysql5.7等技术,采用Docker容器化部署。

mall学习教程,架构、业务、技术要点全方位解析。mall项目(40k+star)是一套电商系统,使用现阶段主流技术实现。涵盖了SpringBoot 2.3.0、MyBatis 3.4.6、Elasticsearch 7.6.2、RabbitMQ 3.7.15、Redis 5.0、MongoDB 4.2.5、Mysql5.7等技术,采用Docker容器化部署。

mall学习教程 简介 mall学习教程,架构、业务、技术要点全方位解析。mall项目(40k+star)是一套电商系统,使用现阶段主流技术实现。涵盖了SpringBoot 2.3.0、MyBatis 3.4.6、Elasticsearch 7.6.2、RabbitMQ 3.7.15、Redis 5

Jan 8, 2023
Owner
Parth Shah
Parth Shah
Spring Boot & MongoDB Login and Registration example with JWT, Spring Security, Spring Data MongoDB

Spring Boot Login and Registration example with MongoDB Build a Spring Boot Auth with HttpOnly Cookie, JWT, Spring Security and Spring Data MongoDB. Y

null 15 Dec 30, 2022
UMS is a CRUD based management system which uses File Handling to manipulate data and perform the CRUD operations

UMS is a CRUD (Create, Read, Update, Delete) based management system which uses File Handling to manipulate data and perform the CRUD operations. It is a group project made using Java procedural programming having both User and Admin sides.

Daoud-Hussain 9 Dec 20, 2022
Practice and testing with Java 11, Prometheus, and Spring-boot with MicroService Architecture. Designed to run on Kubernetes in minikube.

This application was written by Andrew Aslakson Built to run on minikube using kubernetes General race tracking system? Secure with Firebase Authentic

null 1 Feb 5, 2022
Mars - Object Relational Mapping Framework for MongoDB (MongoDB ORM)

Mars Object Relational Mapping Framework for MongoDB 致自己 造自己的轮子,让别人去说 ; What is Mars Mars is a unified driver platform product developed by Shanghai J

null 35 Nov 17, 2022
A simple program used to enter people into a file stored in memory, and the same saved data is displayed in a table

A simple program used to enter people (students or professors) into a file stored in memory, and the same saved data is displayed in a table. Persons have the appropriate attributes where name, surname, etc. are entered and identified by ID.

Ljubinko Stojanović 3 Apr 28, 2022
SpringBoot CRUD Employee Management System

Employee-Management-Springboot SpringBoot CRUD Employee Management System Tech it uses Java SpringBoot Hibernate MySQL To Run Download the zip file an

Abhinav Gupta 1 Jan 31, 2022
Spring Boot JdbcTemplate example with SQL Server: CRUD Rest API using Spring Data JDBC, Spring Web MVC

Spring Boot JdbcTemplate example with SQL Server: Build CRUD Rest API Build a Spring Boot CRUD Rest API example that uses Spring Data Jdbc to make CRU

null 7 Dec 20, 2022
TrashEmail is hosted Telegram bot that can save your private email address by offering disposable email address

TrashEmail is hosted Telegram bot that can save your private email address by offering disposable email address. It can create, manage, disposable email address and link them with your telegram bot chat.

Rohit Sehgal 493 Dec 28, 2022
This is a little self hosted shared planner application server.

VPR-Backend This project includes the backend for the VPR-project. Installation and Getting Started To run the server you first have to install a MySQ

Marc Beyer 2 Feb 5, 2022
Self-hosted Git Server with CI/CD and Kanban

NOTE: We develop OneDev at code.onedev.io for sake of dogfooding. All issues and pull requests should be submitted there Self-hosted Git Server with K

OneDev 10.5k Dec 31, 2022