Skip to content

This project is a Summary of Spring boot App and it was split by branch in order to see the differences for example when create jpa, swagger,sonarqbe, jacoco or deploy in kubernetes.

Notifications You must be signed in to change notification settings

puitiza/product-microservice

Repository files navigation

product-microservice

This is a personal project in order to understand better microservices, and it split by section and commits.

Build Status Build Status Quality Gate Status Javadoc

H2 Browser console ! http://localhost:8080/h2-console

To Access is necessary to look in the application.yml file, we have the url, username and password

spring:
  datasource:
  url: jdbc:h2:~/test
  username: sa
  password: password
web intellij IDEA

Note: Remember to start up this project with H2 is necessary initialize it

spring:
  jpa:
    hibernate:
      ddl-auto: create-drop

JWT (JSON Web Token)

JWT is popular for Authentication and Information Exchange. Server encodes data into a JSON Web Token and send it to the Client. The Client saves the JWT, then every Request from Client to protected routes or resources should be attached that JWT (commonly at header). The Server will validate that JWT and return the Response.

There are three important parts of a JWT: Header, Payload, Signature. Together they are combined to a standard structure: header.payload.signature.

Spring Security Authentication process: receive HTTP request, filter, authenticate, store Authentication data, generate token, get User details, authorize, handle exception…

Dockerfile

‍💻 Installation

In your terminal

  • Run $ docker build -t product-microservice .

    Make sure to include . at the end

    Here, -t simply means tag followed by ‘ name:tag ’ format, for example: product-microservice:1.

  • Run Docker container using the image built

    $ docker run -d --name product-ms -p 8080:8080 product-microservice

    -d means that we will start the container in a detached mode. It exits when the root process used to run the container exits.

    -name assigns the name of the container.

    -p exposes the container’s internal port. The format is -p hostPort:containerPort. The exposed container’s port can be directed through the specified host’s port. Thus, -p 8080:8080 binds the host’s 8080 port to the container’s internal 8080 port.

    product-microservice is the Image name along with the tag.

    image
  • You can also have a look at the log file to see if my application ran successfully using

    $ docker logs -f product-ms

  • You can see ip from some container with these 2 cmds

    $ docker ps

    $ docker inspect <IdContainer> | grep "IPAddress"

‍Publish your image in Dockerhub (Optional)

  1. Run $ docker login and autenticathe with your credential, for password my suggestion is use token like in the image.

  2. tag your local docker image to remote docker image:

docker tag product-microservice puitiza/product-microservice
  1. Now run the command to push to remote docker image to DockerHub Repository:
docker image push puitiza/product-microservice

Unit Test Junit5 + Mockito - Jacoco

‍💻 Installation

if you want to see how much is the coverage verified, follow these steps

  1. Click in $ clean project
  2. Click in $ jacocoTestReport
  3. Click in $ jacocoTestCoverageVerification

Sonarqube/PostgreSQL in Apple M1 Chip or Windows

‍💻 Installation

In your terminal

  • Run $ docker pull davealdon/sonarqube-with-docker-and-m1-macs

    if you have on windonws OS:

    Run $ docker pull sonarqube:9.6-community

    In both case we are using version 9.x of sonarqube because this project is for Java 17

  • Run $ docker run -d --name sonarqube -p 9000:9000 -p 9092:9092 davealdon/sonarqube-with-docker-and-m1-macs

  • Check in your navegator the addres http://localhost:9000

  • User and Pasword are "admin"

‍💻 Quickstart

  • Is necesary to create a token and then configurate on your code inside build.gradle file

    sonarqube {
        properties {
            property "sonar.host.url", "http://localhost:9000"
            property "sonar.login", "5f542370ca13ec8a9ca37048d7f32809c01681c0"
        }
    }
    
  • Now you must to run a script for sonarqube in your terminal

    Run $ ./gradlew test jacocoTestReport This is to create coverage of your code and then

    Run $ ./gradlew sonarqube after this step you can see your project in sonarqube with sucessfull.

  • The easiest way is to do it with gradle, only press in sonarqube like you can see below, but remember before build/clean.

Expose your services with NGROK

‍💻 Installation

this is the easiest way to pull up ngrok service as image

 docker run --name ngrok_tmp --rm -it --net=host -e NGROK_AUTHTOKEN=<token_ngrok> ngrok/ngrok http 9000

--rm means that Remove the container what we created when this is stopped

--net=host means that we can use 'ngrok http <id_port>' example 'ngrok http 9000' otherwise we would have to use this form 'ngrok http host.docker.internal:9000'

if you want to see thus the inspect of ngrok , so needs to expose the port by default, after run cmd you can see http://localhost:3000/inspect/http

docker run --name ngrok_tmp --rm -it -p 3000:4040 -e NGROK_AUTHTOKEN=<token_ngrok> ngrok/ngrok http host.docker.internal:9000

The purpose of this is to expose locally running sonarqube to the internet and then configure it on cd/ci.

image

image

Deploy in Kubernetes

‍💻 Installation

  1. Enable kubernetes on your docker desktop and after that you can use Lens Desktop for Ide
  1. Run in your terminal inside your project $ kubectl apply -f deployment.yaml or you can do also by kubernetes plugin on Intellij Idea
  1. By default the metrics is not able in lens IDE with prometheus so you need to able it before. Run these commands in your terminal in this order
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo update
kubectl create namespace monitoring
helm install prometheus --namespace monitoring prometheus-community/kube-prometheus-stack --set nodeExporter.hostRootfs=false

In my case, The first time I ran only this cmd but Now I know that I have to add {--set ...}

helm install prometheus --namespace monitoring prometheus-community/kube-prometheus-stack

If it doesn't work follow the steps I put here node-exporter crashes at startup

image

List of comands:

  • $ kubectl get nodes List of nodes in your cluster

  • $ kubectl get pods List of pods in your cluster

  • $ kubectl logs <podname> If you want to see logs

  • $ kubectl get svc List of services in your cluster

  • $ kubectl get namespaces <name> get the summary of a specific namespace using

  • $ kubectl port-forward -n monitoring service/prometheus-grafana 3000:80 Expose this service http://localhost:3000

  • $ kubectl get secret -n monitoring prometheus-grafana -o jsonpath="{.data.admin-password}" | base64 --decode ; echo Expose password : prom-operator, and for user: admin. Also you can see this secret with your Lens Desktop

Contribution

🔗 wiki

Sonarqube

Jacoco

code Coverage (Sonarqube + Jacoco)

Junit 5 + Mockito

dockerfile

ngrok

Kubernetes

About

This project is a Summary of Spring boot App and it was split by branch in order to see the differences for example when create jpa, swagger,sonarqbe, jacoco or deploy in kubernetes.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published