【问题标题】:How to high concurency in Spring BootSpring Boot 如何实现高并发
【发布时间】:2019-04-02 12:33:25
【问题描述】:

我需要创建一个每秒支持 40 个并发用户的产品(我是处理并发的新手)

为了实现这一点,我尝试开发了一个 hello world spring-boot 项目。 即,

弹簧启动 (1.5.9)

码头 9.4.15

具有获取端点的休息控制器

代码如下:

@GetMapping
public String index() {
    return "Greetings from Spring Boot!";
}

在 Gen10 DL360 机器上运行的应用

然后我尝试使用 apachebench 进行基准测试

75 个并发用户:

ab -t 120 -n 1000000 -c 75 http://10.93.243.87:9000/home/
Server Software:
Server Hostname:        10.93.243.87
Server Port:            9000

Document Path:          /home/
Document Length:        27 bytes

Concurrency Level:      75
Time taken for tests:   37.184 seconds
Complete requests:      1000000
Failed requests:        0
Write errors:           0
Total transferred:      143000000 bytes
HTML transferred:       27000000 bytes
Requests per second:    26893.28 [#/sec] (mean)
Time per request:       2.789 [ms] (mean)
Time per request:       0.037 [ms] (mean, across all concurrent requests)
Transfer rate:          3755.61 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1  23.5      0    3006
Processing:     0    2   7.8      1     404
Waiting:        0    2   7.8      1     404
Total:          0    3  24.9      2    3007

100 个并发用户:

ab -t 120 -n 1000000 -c 100 http://10.93.243.87:9000/home/
Server Software:
Server Hostname:        10.93.243.87
Server Port:            9000

Document Path:          /home/
Document Length:        27 bytes

Concurrency Level:      100
Time taken for tests:   36.708 seconds
Complete requests:      1000000
Failed requests:        0
Write errors:           0
Total transferred:      143000000 bytes
HTML transferred:       27000000 bytes
Requests per second:    27241.77 [#/sec] (mean)
Time per request:       3.671 [ms] (mean)
Time per request:       0.037 [ms] (mean, across all concurrent requests)
Transfer rate:          3804.27 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    2  35.7      1    3007
Processing:     0    2   9.4      1     405
Waiting:        0    2   9.4      1     405
Total:          0    4  37.0      2    3009

500 个并发用户:

ab -t 120 -n 1000000 -c 500 http://10.93.243.87:9000/home/
Server Software:
Server Hostname:        10.93.243.87
Server Port:            9000

Document Path:          /home/
Document Length:        27 bytes

Concurrency Level:      500
Time taken for tests:   36.222 seconds
Complete requests:      1000000
Failed requests:        0
Write errors:           0
Total transferred:      143000000 bytes
HTML transferred:       27000000 bytes
Requests per second:    27607.83 [#/sec] (mean)
Time per request:       18.111 [ms] (mean)
Time per request:       0.036 [ms] (mean, across all concurrent requests)
Transfer rate:          3855.39 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0   14 126.2      1    7015
Processing:     0    4  22.3      1     811
Waiting:        0    3  22.3      1     810
Total:          0   18 129.2      2    7018

1000 个并发用户:

ab -t 120 -n 1000000 -c 1000 http://10.93.243.87:9000/home/
Server Software:
Server Hostname:        10.93.243.87
Server Port:            9000

Document Path:          /home/
Document Length:        27 bytes

Concurrency Level:      1000
Time taken for tests:   36.534 seconds
Complete requests:      1000000
Failed requests:        0
Write errors:           0
Total transferred:      143000000 bytes
HTML transferred:       27000000 bytes
Requests per second:    27372.09 [#/sec] (mean)
Time per request:       36.534 [ms] (mean)
Time per request:       0.037 [ms] (mean, across all concurrent requests)
Transfer rate:          3822.47 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0   30 190.8      1    7015
Processing:     0    6  31.4      2    1613
Waiting:        0    5  31.4      1    1613
Total:          0   36 195.5      2    7018

从上面的测试运行中,我自己在 75 个用户的情况下达到了每秒约 27K,但它看起来增加了用户也增加了延迟。此外,我们可以清楚地注意到连接时间正在增加。

我要求我的应用程序支持 40k 并发用户(假设所有人都使用自己单独的浏览器)并且请求应在 250 毫秒内完成。

请帮帮我

【问题讨论】:

  • 您是否已经检测过您的代码?看看你大部分时间都花在了哪里?你的瓶颈在哪里? (cpu?内存?gc?磁盘?网络?)你的服务器配置是怎样的? (线程池?连接器?等)您针对高负载执行了哪些典型的操作系统调整? - 还要非常小心你的choice of benchmark
  • 是的,我已经通过应用eclipse.org/jetty/documentation/current/high-load.html 对我的操作系统进行了调整
  • 投入大量时间后,发现如下: 1. 在运行应用程序的虚拟机中启用了超线程。即,8 个物理核心在超线程后转换为 8 个物理核心 + 8 个逻辑核心 = 16 个核心。 2. 在负载测试期间,我可以看到 java 进程只使用了 800% 到 900% 的 CPU,即 java 进程使用了​​ 8 到 9 个内核,不超过这个。并且整个系统 cpu 百分比高达 40% 到 50% 现在的问题是,为什么 java 直到 cpu 周期的 1500% 到 1600% 才占用?

标签: spring-boot concurrency embedded-jetty apachebench c10k


【解决方案1】:

我自己也不是该主题的大巫师,但这里有一些建议:

  • 一个实例可以处理多少个请求是有硬性限制的,所以如果您想支持大量用户,则需要更多实例
  • 如果您使用多个实例,那么您必须以某种方式在实例之间分配请求。一种流行的解决方案是Netflix Eureka
  • 如果您不想维护额外资源并且产品将在云中运行,请使用提供的负载平衡服务(例如 AWS 上的 LoadBalancer)
  • 您还可以微调服务器的连接池设置

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-09
    • 2020-06-01
    • 2023-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-31
    • 1970-01-01
    相关资源
    最近更新 更多