
👍Java Spring Boot & Microservice Realtime Project development CT👍
June 11, 2025 at 08:42 AM
*5 Tasks for today: 11Jun*
✅ 1. Experiment with spring boot app: JDK17, port change, dns & LB understanding, 2 process same port, nslookup, ipconfig.
✅ 2. Add Devtool dependency, so for every change, spring-boot app will restart by itself.
✅ 3. Use SLF4J with Logback for logging. ANd remove System.out.println
✅ 4. Understand the core spring concept, object creation & management will happen by spring framework. Not by developer. Check how this relates to Controller that we created.
✅ 5. Create layered application for Add functionality. Create service interface & impl, and controller => service talk based on interface.
========================
1. Experiment with spring boot app: JDK17, port change, dns & LB understanding, 2 process same port, nslookup, ipconfig.
- Since we are using java 17 while setting up spring-boot app.
Ensure, IDE level also Java 17 is used.
In installed JRE & compiler point to java 17. Refer to recording for Eclipse STS, for other IDEs, explore on internet.
- Understand default spring boot port, and change port to 8081
server.port=8081
Restart the service.
Tomcat started on port 8081
http://localhost:8081/api/add?num1=40&num2=20
- While existing spring boot app is already running, try to start it one more time, and check unable to occupy the same port.
Solve it by stopping previous service, and then restarting new service.
- Modify your port to 80 & test
http://localhost:80/api/add?num1=40&num2=20
http://localhost/api/add?num1=40&num2=20
- Know default http & https ports.
- Understand in realworld how the mapping happens. LB, APIGateway, NginX runs on 80 & 443, and internally services can have different port.
- Understand how DNS mapping happens, where domain is mapped to IP of LB, APIGateway, NginX - A Record
refer to recording.
- Use commands like below to find google IP, and try to access based on IP.
nslookup google.com
http://
- Find IP of your laptop using ipconfig. And use below
http:///api/add?num1=40&num2=20
Also
http://127.0.0.1/api/add?num1=40&num2=20
Get strong understanding of how IP & Port relates while the flow is happening.
==================================
2. Add Devtool dependency, so for every change, spring-boot app will restart by itself.
search for: devtool maven dependency
from mvnrespository.com, get the dependency tag & add to your pom.xml
org.springframework.boot
spring-boot-devtools
By default try to remove the , so the compatible version to spring-boot will be picked up. If after removing , if you are facing any errors, then add version.
Whenever you change pom.xml
stop previous process
clean package
clean spring-boot:run
=====================
3. Use SLF4J with Logback for logging. ANd remove System.out.println
Note: SLF4J with Logback is default for spring-boot, and applicable for latest applications. Jars are already in your classpath. Just we need to use it.
private static final Logger logger = LoggerFactory.getLogger(AdditionController.class);
logger.info("Adding numbers: num1:{} | num2:{}", num1, num2);
========================
4. Understand the core spring concept, object creation & management will happen by spring framework. Not by developer. Check how this relates to Controller that we created.
Add constructor in controller, & check on service startup, object is created by framework.
- throw Exception in Controller & check the stack trace
try {
// Simulate a delay for demonstration purposes
throw new RuntimeException("Simulated delay for demonstration purposes");
} catch (RuntimeException e) {
e.printStackTrace();
logger.error("Thread interrupted: {}", e.getMessage());
}
Analyse stack trace, and check how tomcat - spring works together, to further invoke our controller method.
So we can understand that Spring is creating & managing Controller object. THis is core principle of spring framework. "Objects will be created & managed by spring. Developer is supposed to use it."
====================================
5. Create layered application for Add functionality. Create service interface & impl, and controller => service talk based on interface.
- Rename AddController to MathController
- Add service interface
package com.mycomp.firstapp.service.interfaces;
public interface MathService
- Add service impl
package com.mycomp.firstapp.service.impl;
public class MathServiceImpl implements MathService
- In controller use reference of MathService & invoke the method. Ensure you are not having reference of MathServiceImpl.
Code below
MathService mathService = new MathServiceImpl();
int sum = mathService.add(num1, num2);
Wrong - you should not code below. Impl reference not be used.
MathServiceImpl mathService = new MathServiceImpl();
int sum = mathService.add(num1, num2);