Spring RestTemplate 集成 ClientHttpRequestFactory 提升QPS

news/2025/2/26 18:55:50

1 工程概述

在这里插入图片描述

1.1 pom

 <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <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>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.71</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-collections4</artifactId>
            <version>4.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
        </dependency>

    </dependencies>

1.2 RestTemplateConfig 配置

@Configuration
public class RestTemplateConfig {

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

1.3 TestController

@RestController
@RequestMapping("/demo")
public class TestController {

    @Autowired
    private RestTemplate restTemplate;


    @GetMapping("/test")
    public String test() {
        ResponseEntity<String> forEntity = restTemplate.getForEntity("http://127.0.0.1:9000/demo/hello_world", String.class);
        return String.valueOf(forEntity.getStatusCodeValue());
    }


    @GetMapping("/hello_world")
    public String helloWorld()  {
        return "hello world";
    }

}

1.4 application.yml

server:
  port: 9000
  

1.5 主启动类

@SpringBootApplication
public class SpringDemoApplication {

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

}

1.6 测试

在这里插入图片描述

2 测试qps

jemeter: 60线程 5000次请求 10秒完成

在这里插入图片描述

结果: 700左右qps(并且持续下降),错误率还很高。在这里插入图片描述

在这里插入图片描述

原因分析:

每次请求都会创建一个ClientHttpRequest,未能达到request复用,及其耗资源:
在这里插入图片描述
在这里插入图片描述

3 集成 ClientHttpRequestFactory

@Configuration
public class RestTemplateConfig {

    @Bean
    public RestTemplate restTemplate(ClientHttpRequestFactory requestFactory) {
        return new RestTemplate(requestFactory);
    }


    @Bean
    public ClientHttpRequestFactory httpRequestFactory() {
        return new HttpComponentsClientHttpRequestFactory(httpClient());
    }

    @Bean
    public HttpClient httpClient() {

        Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
                .register("http", PlainConnectionSocketFactory.getSocketFactory())
                .register("https", SSLConnectionSocketFactory.getSocketFactory())
                .build();

        PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry);
        //设置连接池最大是100个连接
        connectionManager.setMaxTotal(1000);
        connectionManager.setDefaultMaxPerRoute(500);
        RequestConfig requestConfig = RequestConfig.custom()
                //返回数据的超时时间
                .setSocketTimeout(20000)
                //连接上服务器的超时时间
                .setConnectTimeout(10000)
                //从连接池中获取连接的超时时间
                .setConnectionRequestTimeout(1000).build();

        return HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).setConnectionManager(connectionManager).build();
    }
}

结果: 1100 qps 并且稳定,没有错误。

在这里插入图片描述


http://www.niftyadmin.cn/n/4556510.html

相关文章

洛谷 P4547 bzoj 5006 随机二分图 —— 状压DP+期望

题目&#xff1a;https://www.luogu.org/problemnew/show/P4547 https://www.lydsy.com/JudgeOnline/problem.php?id5006 参考博客&#xff1a;https://www.cnblogs.com/yanshannan/p/9452802.html 注意同一个点连出去的两条边本来就不能一起选&#xff01; 每次调用 map 会很…

在C语言中/n放先面和后面有什么不同啊

||| /n相当于换行 ||| /n是换行符号 ");}输出:can i help you main(){ printf("/n can i help you 放前面就是上一行换行..就是/n后面的显示从下一行开始... ||| 没什么区别..都是换行.. 放后面相当于先输出后回车 答案补充 printf("/n a")输出为&#xff…

Dokcer 桥接模式原理解析

1 Docker 网络模式 网络模式配置说明bridge模式–netbridge默认值。在Docker网桥docker0上为容器创建新的网络栈none模式–netnone不配置网络&#xff0c;用户可以稍后进入容器&#xff0c;自行配置container模式–netcontainer:name/id容器和另外一个容器共享Network namespa…

java起步问题 给我点忠告和建议

初学可能觉得很方便 JAVA是更高层次的编程语言 一时会让人觉得不知道从何下手 JAVA里有太多的类库 个人认为学C好一些 直接拿来用就行了 是目前中国最大的IT网站. 学习起来就轻松了不少.学习编程可以到WWW.CSDN.NET上去 可能说为你学JAVA奠定的良好的基础 是公认的学C的入门级最…

R语言学习1

最开始在学校只使用过Matlab&#xff0c;后来在工作中与同事交流才接触到R语言存在。 在网上查询了下&#xff0c;R语言主要是用于统计分析&#xff0c;是一门解释性语言&#xff0c;性能吗不要太期望很高&#xff0c;但开源且有很多的工具包。 但在真正开始学习时&#xff0c;…

Jenkins + Gitlab+ Docker + SpringBoot 构建流水线

1 环境 192.168.38.80 docker jenkins gitlab2 Spring Boot 2.1 HelloController RestController public class HelloController {GetMapping("/hello")public String hello() {return "Hello World";} }2.2 初始化Jenkinsfile pipeline {// 集群环境下…

0xf8 amp; 0x3f); C#的运算符问题 Console.WriteLine(0x{0:x}

||| 这个是不能被执行的有错误 ||| 是不是能输出汉字啊 ||| 结果&#xff1a;0x38原因不明- -|| 楼主如果觉得不好理解的话可以把十六进制的f8和3f转换为二进制进行按位相与 也就说后面两个常数的与结果是38 0x是预先打印的 是以十六进制的格式打印 答案补充 输出结果是0x38 把…

图解 IO 模型

1 应用程序如何读取数据&#xff1f; JVM上运行的JAVA程序无法从硬盘、网卡中直接读取数据。是通过内核来访问磁盘、网卡等硬件的。 2 IO模型 2.1 阻塞 阻塞&#xff1a;应用程序从内核读取数据&#xff0c;会切换至操作系统内核态来完成真正数据读取&#xff0c;而读取又…