Spring Boot with Java MicroServices

Example 1: Java Spring Boot with Micro Services using REST Call

Here we have two services like address_service and employee_service. In employee_service we have called to address_service. Let’s see a demo application for better understanding.

Project Structure:

img

First we will develop address_service:


pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.1.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.tpoints</groupId>
    <artifactId>address_service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>address_service</name>
    <description>This is service example</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-devtools</artifactId>
                    <scope>runtime</scope>
                    <optional>true</optional>
                </dependency>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-test</artifactId>
                    <scope>test</scope>
                </dependency>
            </dependencies>

                    <build>
                        <plugins>
                            <plugin>
                                <groupId>org.springframework.boot</groupId>
                                <artifactId>spring-boot-maven-plugin</artifactId>
                            </plugin>
                        </plugins>
                    </build>

               </project>
       
   

application.properties
server.port=8081

AddressServiceApplication.java
package com.tpoints;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class AddressServiceApplication {

	public static void main(String[] args) {
		SpringApplication.run(AddressServiceApplication.class, args);
	}

}

AddressController.java
package com.tpoints.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class AddressController {
	
	
	@GetMapping("/address")
	public String getAddress()
	{
		return "Jayadev Vihar, BBSR";
	}

}

Now we will run as Spring Boot App

img

Then we will open a browser and put url along with endpoint then we will get this output.

img

Now we will develop employee_service and inside this service we will call to address service.

img
EmpServiceApplication.java
package com.tpoints;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class EmployeeServiceApplication {

	public static void main(String[] args) {
		SpringApplication.run(EmployeeServiceApplication.class, args);
	}

}

EmployeeController.java
package com.tpoints.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class EmployeeController {
	
	@Autowired
	private RestTemplate restTemplate;
	
	@GetMapping("/employee")
	public String getEmployee()
	{
		//return the address data along with the employee data
		String address=restTemplate.getForObject
				("http://localhost:8081/address", String.class);
		
return "Name:Trilochan Tarai,Email:javabytrilochan@gmail.com"+" "+address;
	}

}

EmployeeAppConfig.java
package com.tpoints;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class EmployeeAppConfig {
	
	
	@Bean
	public RestTemplate restTemplate()
	{
		return new RestTemplate();
	}

}

Now we will run employee_service :

img img

About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 Previous