Spring Cloud Netflix 微服務壓力測試

目的:對微服務的提供者和消費者組建的集合進行壓力測試,以發現可能的問題和解決的方法。

創建一個客戶端項目(Feign),提供http接口給JMeter調用,該接口使用Feign客戶端請求另外一個機器上的一個微服務:
JMeter --> 客戶端(Feign、Hystrix) --> 微服務(user-service)

客戶端上的代碼:
  1. @RestController  
  2. public class UserController {  
  3.     protected final Logger logger = LoggerFactory.getLogger(getClass());  
  4.       
  5.     @Autowired  
  6.     UserServiceClient userServiceClient;  
  7.   
  8.     /** 
  9.      * 根據用戶id獲取電話號碼 
  10.      * @param userId 
  11.      * @return 電話號碼 
  12.      * 
  13.      * 調用例子:http://localhost:12345/getPhoneNoByUserId?userId=263508 
  14.      */  
  15.     @RequestMapping(value = "/getPhoneNoByUserId", method = RequestMethod.GET)  
  16.     public String getPhoneNoByUserId(@RequestParam Integer userId) {  
  17.         logger.debug("getPhoneNoByUserId received. userId={}", userId);  
  18.           
  19.         return userServiceClient.getPhoneNoByUserId(userId);  
  20.     }  
  21. }  

客戶端調用user-service微服務,測試時去掉斷路器,已方便發現錯誤:
  1. /** 
  2.  * 調用user微服務的客戶端接口 
  3.  * @author XuJijun 
  4.  * 
  5.  */  
  6. //@FeignClient(value="user-service", fallback=UserServiceClientHystrix.class)  
  7. @FeignClient(value="user-service")  
  8. public interface UserServiceClient {  
  9.     /** 
  10.      * 根據userId獲取電話號碼 
  11.      */  
  12.     @RequestMapping(value = "/user/getPhoneNoByUserId", method = RequestMethod.GET)  
  13.     public String getPhoneNoByUserId(@RequestParam(value = "userId") Integer userId);  
  14. }  

錯誤情況1(Thread Group配置線程數爲10的時候,幾乎必現):
com.netflix.hystrix.exception.HystrixRuntimeException ... timed-out and no fallback available.
原因:
Hystrix缺省超時判斷爲1秒鐘,由於網絡問題,有些請求超過1秒鐘之後才接收到。
解決:
配置修改請求超時時長(application.yml):
[plain] view plain copy
  1. hystrix:  
  2.   command:  
  3.     default:  
  4.       execution:  
  5.         isolation:  
  6.           thread:  
  7.             timeoutInMilliseconds: 30000 #缺省爲1000  

錯誤情況2(Thread Group配置線程數爲1000的時候,幾乎必現):
com.netflix.hystrix.exception.HystrixRuntimeException ... could not be queued for execution...
原因:
Hystix請求線程池缺省爲最大10個線程,壓力測試情況下,很容易爆發超過10個請求。
解決:
配置修改線程池中的coreSize(application.yml)
[plain] view plain copy
  1. hystrix:  
  2.   threadpool:  
  3.     default:  
  4.       coreSize: 500 #缺省爲10  

解決以上兩個問題之後,配置線程數爲10000的時候,失敗率爲0。

附:這個參數的配置,有個基本得公式可以follow:
requests per second at peak when healthy × 99th percentile latency in seconds + some breathing room
每秒最大支撐的請求數 (99%平均響應時間 + 一個緩衝值)
比如:每秒能處理1000個請求,99%的請求響應時間是60ms,那麼公式是:1000*(0.060+0.012)

結論:
1、先保持timeout的默認值(1000ms),除非需要修改(其實通常會修改)
2、先保持threadpool的的線程數爲10個,除非需要更多
3、根據實際業務的請求併發數,配置壓力測試併發線程數,然後調整上面兩個參數,直到滿足需求爲止。

附:
Hystrix相關的常用配置信息:
  • 超時時間(默認1000ms,單位:ms)
    • hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds
    • hystrix.command.HystrixCommandKey.execution.isolation.thread.timeoutInMilliseconds
  • 線程池核心線程數
    • hystrix.threadpool.default.coreSize(默認爲10)
  • Queue
    • hystrix.threadpool.default.maxQueueSize(最大排隊長度。默認-1,使用SynchronousQueue。其他值則使用 LinkedBlockingQueue。如果要從-1換成其他值則需重啓,即該值不能動態調整,若要動態調整,需要使用到下邊這個配置)
    • hystrix.threadpool.default.queueSizeRejectionThreshold(排隊線程數量閾值,默認爲5,達到時拒絕,如果配置了該選項,隊列的大小是該隊列)
      • 注意:如果maxQueueSize=-1的話,則該選項不起作用
  • 斷路器
    • hystrix.command.default.circuitBreaker.requestVolumeThreshold(當在配置時間窗口內達到此數量的失敗後,進行短路。默認20個)
    • hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds(短路多久以後開始嘗試是否恢復,默認5s)
    • hystrix.command.default.circuitBreaker.errorThresholdPercentage(出錯百分比閾值,當達到此閾值後,開始短路。默認50%)
  • fallback
    • hystrix.command.default.fallback.isolation.semaphore.maxConcurrentRequests(調用線程允許請求HystrixCommand.GetFallback()的最大數量,默認10。超出時將會有異常拋出,注意:該項配置對於THREAD隔離模式也起作用)

轉載地址:http://blog.csdn.net/clementad/article/details/54315805

發佈了25 篇原創文章 · 獲贊 7 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章