I have a system consisting of several rest api projects.
Each rest api project is responsible for a specific topic in the project.
To my surprise, I discovered that accessing via rest api takes an unreasonable amount of time (up to 120 ms).
Does anyone have an idea why and how to create communication in a reasonable amount of time between those rest api services.
For the demonstration, I built two rest api projects in such a way that one calls the other and prints the duration of the operation.
the first project
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><artifactId>spring-boot-hello-world</artifactId><packaging>war</packaging><name>Spring Boot Hello World Example</name><version>1.0</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.7.1</version><relativePath/> <!-- lookup parent from repository, not local --></parent><properties><java.version>1.8</java.version><sprigframework.version>4.3.5.RELEASE</sprigframework.version><maven.compiler.source>1.8</maven.compiler.source></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>io.springfox</groupId><artifactId>springfox-boot-starter</artifactId><version>3.0.0</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>1.8</source><target>1.8</target></configuration></plugin><!-- <plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.1</version><configuration><source>${java.version}</source><target>${java.version}</target></configuration></plugin>--></plugins></build></project>
package com.mkyong;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.annotation.Bean;import springfox.documentation.builders.RequestHandlerSelectors;import springfox.documentation.spi.DocumentationType;import springfox.documentation.spring.web.plugins.Docket;@SpringBootApplicationpublic class MainApplication { public static void main(String[] args) { SpringApplication.run(MainApplication.class, args); }}
package com.mkyong;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class HelloController { @RequestMapping("/books") String hello() { return "Hello World, Spring Boot!"; }}
the next (the caller) project
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><artifactId>spring-boot-hello-world</artifactId><packaging>war</packaging><name>Spring Boot Hello World Example</name><version>1.0</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.7.1</version><relativePath/> <!-- lookup parent from repository, not local --></parent><properties><java.version>1.8</java.version><sprigframework.version>4.3.5.RELEASE</sprigframework.version><maven.compiler.source>1.8</maven.compiler.source></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>com.google.guava</groupId><artifactId>guava</artifactId><version>19.0</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>io.springfox</groupId><artifactId>springfox-boot-starter</artifactId><version>3.0.0</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>1.8</source><target>1.8</target></configuration></plugin><!-- <plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.1</version><configuration><source>${java.version}</source><target>${java.version}</target></configuration></plugin>--></plugins></build></project>
package com.mkyong;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.annotation.Bean;import springfox.documentation.builders.RequestHandlerSelectors;import springfox.documentation.spi.DocumentationType;import springfox.documentation.spring.web.plugins.Docket;@SpringBootApplicationpublic class MainApplication { public static void main(String[] args) { SpringApplication.run(MainApplication.class, args); }}
package com.mkyong;import java.util.concurrent.TimeUnit;import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;import org.springframework.boot.logging.log4j2.Log4J2LoggingSystem;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.client.RestTemplate;import com.google.common.base.Stopwatch;@RestControllerpublic class HelloController { private static final Logger logger = LogManager.getLogger(HelloController.class); @RequestMapping("/books") String hello() { // return "Hello World, Spring Boot!"; RestTemplate restTemplate = new RestTemplate(); String url = "http://localhost:8080/books"; // Example API Stopwatch timer = Stopwatch.createStarted(); // Make GET request and get response as a String String response = restTemplate.getForObject(url, String.class); timer.stop(); //System.out.println("elapse time restTempalte get:" + timer.elapsed(TimeUnit.MILLISECONDS)); // Print response logger.info("elapse time restTempalte get:" + timer.elapsed(TimeUnit.MILLISECONDS)); logger.info("response="+response); return response; }}