最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - spring boot 2.7.1 send request by restTemplate take a lot of time - Stack Overflow

programmeradmin0浏览0评论

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=".0.0"
         xmlns:xsi=";
         xsi:schemaLocation=".0.0
         .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>.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>
     
         <mavenpiler.source>1.8</mavenpiler.source>
    </properties>

    <dependencies>
        <dependency>
            <groupId>.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>.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>.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
<plugin>
      <groupId>.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <configuration>
        <source>1.8</source>
        <target>1.8</target>
      </configuration>
    </plugin>
           <!-- <plugin>
                <groupId>.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 .springframework.boot.SpringApplication;
import .springframework.boot.autoconfigure.SpringBootApplication;
import .springframework.context.annotation.Bean;

import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

@SpringBootApplication
public class MainApplication {

    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class, args);
    }
}
package com.mkyong;

import .springframework.web.bind.annotation.RequestMapping;
import .springframework.web.bind.annotation.RestController;

@RestController
public 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=".0.0"
         xmlns:xsi=";
         xsi:schemaLocation=".0.0
         .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>.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>
     
         <mavenpiler.source>1.8</mavenpiler.source>
    </properties>

    <dependencies>
        <dependency>
            <groupId>.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>.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>.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
<plugin>
      <groupId>.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <configuration>
        <source>1.8</source>
        <target>1.8</target>
      </configuration>
    </plugin>
           <!-- <plugin>
                <groupId>.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 .springframework.boot.SpringApplication;
import .springframework.boot.autoconfigure.SpringBootApplication;
import .springframework.context.annotation.Bean;

import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

@SpringBootApplication
public class MainApplication {

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

}
package com.mkyong;

import java.util.concurrent.TimeUnit;

import .apache.logging.log4j.LogManager;
import .apache.logging.log4j.Logger;
import .springframework.boot.logging.log4j2.Log4J2LoggingSystem;
import .springframework.web.bind.annotation.RequestMapping;
import .springframework.web.bind.annotation.RestController;
import .springframework.web.client.RestTemplate;

import com.googlemon.base.Stopwatch;

@RestController
public 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;
    }

}

发布评论

评论列表(0)

  1. 暂无评论