50-51_Hystrix 支付微服务构建,JMeter 高并发压测后卡顿

2022年02月25日 14:32 · 阅读(128) ·

Hystrix 支付微服务构建

新建 cloud-provider-hystrix-payment8001

1.pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>msclound</artifactId>
  7. <groupId>com.atguigu.springcloud</groupId>
  8. <version>1.0-SNAPSHOT</version>
  9. </parent>
  10. <modelVersion>4.0.0</modelVersion>
  11. <artifactId>cloud-provider-hystrix-payment8001</artifactId>
  12. <dependencies>
  13. <!--hystrix-->
  14. <dependency>
  15. <groupId>org.springframework.cloud</groupId>
  16. <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
  17. </dependency>
  18. <!--eureka client-->
  19. <dependency>
  20. <groupId>org.springframework.cloud</groupId>
  21. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  22. </dependency>
  23. <!--web-->
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-web</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-starter-actuator</artifactId>
  31. </dependency>
  32. <dependency><!-- 引入自己定义的api通用包,可以使用Payment支付Entity -->
  33. <groupId>com.atguigu.springcloud</groupId>
  34. <artifactId>cloud-api-commons</artifactId>
  35. <version>${project.version}</version>
  36. </dependency>
  37. <dependency>
  38. <groupId>org.springframework.boot</groupId>
  39. <artifactId>spring-boot-devtools</artifactId>
  40. <scope>runtime</scope>
  41. <optional>true</optional>
  42. </dependency>
  43. <dependency>
  44. <groupId>org.projectlombok</groupId>
  45. <artifactId>lombok</artifactId>
  46. <optional>true</optional>
  47. </dependency>
  48. <dependency>
  49. <groupId>org.springframework.boot</groupId>
  50. <artifactId>spring-boot-starter-test</artifactId>
  51. <scope>test</scope>
  52. </dependency>
  53. </dependencies>
  54. <properties>
  55. <maven.compiler.source>8</maven.compiler.source>
  56. <maven.compiler.target>8</maven.compiler.target>
  57. </properties>
  58. </project>

2.application.yml

  1. server:
  2. port: 8003
  3. spring:
  4. application:
  5. name: cloud-provider-hystrix-payment
  6. eureka:
  7. client:
  8. register-with-eureka: true
  9. fetch-registry: true
  10. service-url:
  11. #defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
  12. defaultZone: http://eureka7001.com:7001/eureka

3.主启动类

  1. package com.atguigu.springcloud;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
  5. @EnableEurekaClient
  6. @SpringBootApplication
  7. public class PaymentHystrixMain8001 {
  8. public static void main(String[] args) {
  9. SpringApplication.run(PaymentHystrixMain8001.class,args);
  10. }
  11. }

4.业务类

(1)PaymentService

  1. package com.atguigu.springcloud.service;
  2. import org.springframework.stereotype.Service;
  3. import java.util.concurrent.TimeUnit;
  4. @Service
  5. public class PaymentService {
  6. /**
  7. * 正常访问,一切 OK
  8. * @param id
  9. * @return
  10. */
  11. public String paymentInfo_OK(Integer id){
  12. return "线程池:"+Thread.currentThread().getName()+"paymentInfo_OK,id:"+id+"\t"+":-)";
  13. }
  14. /**
  15. * 超时访问,演示降级
  16. * @param id
  17. * @return
  18. */
  19. public String paymentInfo_TimeOut(Integer id){
  20. try {
  21. TimeUnit.SECONDS.sleep(3);
  22. } catch (InterruptedException e) {
  23. e.printStackTrace();
  24. }
  25. return "线程池:"+Thread.currentThread().getName()+"paymentInfo_TimeOut,id:"+id+"\t"+":-(,耗费 3 秒";
  26. }
  27. }

(2)PaymentController

  1. package com.atguigu.springcloud.controller;
  2. import com.atguigu.springcloud.service.PaymentService;
  3. import lombok.extern.slf4j.Slf4j;
  4. import org.springframework.beans.factory.annotation.Value;
  5. import org.springframework.web.bind.annotation.GetMapping;
  6. import org.springframework.web.bind.annotation.PathVariable;
  7. import org.springframework.web.bind.annotation.RestController;
  8. import javax.annotation.Resource;
  9. @Slf4j
  10. @RestController
  11. public class PaymentController {
  12. @Resource
  13. private PaymentService paymentService;
  14. @Value("${server.port}")
  15. private String serverPort;
  16. @GetMapping("/payment/hystrix/ok/{id}")
  17. public String paymentInfo_OK(@PathVariable("id") Integer id){
  18. String result = paymentService.paymentInfo_OK(id);
  19. log.info("*******result:"+result);
  20. return result;
  21. }
  22. @GetMapping("/payment/hystrix/timeout/{id}")
  23. public String paymentInfo_TimeOut(@PathVariable("id") Integer id){
  24. String result = paymentService.paymentInfo_TimeOut(id);
  25. log.info("*******result:"+result);
  26. return result;
  27. }
  28. }

测试

● 启动 cloud-eureka-server7001
● 启动 cloud-provider-hystrix-payment8001

浏览器访问:http://localhost:8003/payment/hystrix/ok/31
返回结果:

  1. 线程池:http-nio-8003-exec-1paymentInfo_OKid31 :-)

浏览器访问:http://localhost:8003/payment/hystrix/timeout/31
返回结果:

  1. 线程池:http-nio-8003-exec-3paymentInfo_TimeOutid31 :-(,耗费 3

以上述为根基平台,从正确->错误->降级熔断->恢复

JMeter 高并发压测后卡顿

上述在非高并发情形下,还能勉强满足

下面使用 Jmeter 压测测试

开启 Jmeter,来 20000 个并发压死 8001,20000 个请求都去访问 paymentInfo_TimeOut 服务

1.测试计划中右键添加 - 线程 - 线程组(线程组 payment2020,线程数:200,线程数:100,其他参数默认)

2.刚刚新建线程组 payment2020,右键它 - 添加 - 取样器 - Http请求 - 路径,输入 http://localhost:8003/payment/hystrix/timeout/31

3.点击绿色三角形图标启动。

启动后,可以看到后端控制台开始疯狂打印日志

  1. *******result:线程池:http-nio-8003-exec-33paymentInfo_TimeOutid31 :-(,耗费 3
  2. *******result:线程池:http-nio-8003-exec-99paymentInfo_TimeOutid31 :-(,耗费 3
  3. *******result:线程池:http-nio-8003-exec-83paymentInfo_TimeOutid31 :-(,耗费 3
  4. *******result:线程池:http-nio-8003-exec-35paymentInfo_TimeOutid31 :-(,耗费 3
  5. *******result:线程池:http-nio-8003-exec-81paymentInfo_TimeOutid31 :-(,耗费 3

这个时候,浏览器输入 http://localhost:8003/payment/hystrix/ok/31

本来秒出的结果也需要几秒才出来

原因:tomcat 的默认的工作线程数被打满了,没有多余的线程来分解压力和处理。

Jmeter 压测结论

上面还是服务提供者 8001 自己测试,假如此时外部的消费者 80 也来访问,那消费者只能干等,最终导致消费端 80 不满意,服务端 8001 直接被拖慢。