This is a personal project in order to understand better microservices, and it split by section and commits.
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 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…
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. -
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"
-
Run $
docker login
and autenticathe with your credential, for password my suggestion is use token like in the image. -
tag your local docker image to remote docker image:
docker tag product-microservice puitiza/product-microservice
- Now run the command to push to remote docker image to DockerHub Repository:
docker image push puitiza/product-microservice
if you want to see how much is the coverage verified, follow these steps
- Click in $
clean project
- Click in $
jacocoTestReport
- Click in $
jacocoTestCoverageVerification
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"
-
Is necesary to create a token and then configurate on your code inside
build.gradle
filesonarqube { 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 thenRun $
./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.
- You can download Ngrok by exec or image on Docker. https://hub.docker.com/r/ngrok/ngrok
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.
- Enable kubernetes on your docker desktop and after that you can use Lens Desktop for Ide
- Run in your terminal inside your project $
kubectl apply -f deployment.yaml
or you can do also by kubernetes plugin on Intellij Idea
- 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
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
- Report issues
- Open pull request with improvements
- Spread the word
- Reach out to me directly at anthony.puitiza.02@gmail.com
🔗 wiki
Sonarqube
Jacoco
code Coverage (Sonarqube + Jacoco)
Junit 5 + Mockito
- https://www.freecodecamp.org/news/unit-testing-services-endpoints-and-repositories-in-spring-boot-4b7d9dc2b772/
- https://www.javaguides.net/2022/03/spring-boot-unit-testing-service-layer.html
- https://howtodoinjava.com/spring-boot2/testing/spring-boot-mockito-junit-example/
- https://stackabuse.com/guide-to-unit-testing-spring-boot-rest-apis/
- https://www.baeldung.com/spring-mvc-test-exceptions
dockerfile
- https://medium.com/geekculture/docker-basics-and-easy-steps-to-dockerize-spring-boot-application-17608a65f657
- https://www.baeldung.com/dockerizing-spring-boot-application
ngrok
- https://chriskirby.net/blog/using-ngrok-through-docker-for-local-service-development-on-mac
- https://tech.osteel.me/posts/docker-for-local-web-development-part-6-expose-a-local-container-to-the-internet
Kubernetes
- https://dzone.com/articles/spring-boot-with-kubernetes
- https://andrewlock.net/running-kubernetes-and-the-dashboard-with-docker-desktop/
- https://medium.com/@ahmedyosry963/kubernetes-in-your-local-environment-d63e62c3b5f
- https://medium.com/@dijin123/kubernetes-and-the-ui-dashboard-with-docker-desktop-5ad4799b3b61
- https://kubernetes.io/docs/tasks/administer-cluster/namespaces/
- https://www.youtube.com/watch?v=zW-E8THfvPY