由于http请求太多,Clojure Hystrix异常

问题描述 投票:1回答:1

我需要构建一个需要产生大量外部http请求的系统,我必须使用Netflix的Hystrix来创建回退和重新路由异常。现在,我有一个非常简单的设置:

(hystrix/defcommand fetch-request
    {:hystrix/group-key "c0"
     :hystrix/command-key "URLFetch"
     :hystrix/fallback-fn (fn [url]
                                                    {:status 419
                                                     :headers {}
                                                     :body "failed"})}
    [url]
    @(http/get url))


(defn test3 []
    (let [n 4000
        m (range 0 n)
        p (partition 300 m)]
        (doseq [t p]
            (thread
                (doseq [x t]
                    (let [res (fetch-request "http://localhost:3000/comments")]
                        (match (:status res)
                            200 (prn x)
                            :else (prn nil)))
                    )))
        ))

当我执行test3时,我不断得到nil。如果我减少n的值,我会得到200的状态(这是我需要的)。此外,如果我直接在http/get函数而不是test3命令上使用fetch-request,它的工作没有任何问题(即使n的值高于7000)。

注意:我使用分区+线程的原因是并行化http请求。如果你知道在clojure中尽可能快地执行大量http请求的更好方法,那真的很棒。

更新:

我玩了各种配置。他们中的大多数没有产生不同的结果。大多数req仍未执行,因此立即触发回退。我尝试禁用circuitBreaker(我不想这样做,这就是我使用hystrix的原因)看看它是否做了什么 - 而且确实如此。 80%的请求通过。

(hystrix/defcommand fetch-request
    {:hystrix/group-key "ct0"
     :hystrix/command-key "URLFetch"
     :hystrix/init-fn 
        (fn [_ ^com.netflix.hystrix.HystrixCommand$Setter setter]
            (.andCommandPropertiesDefaults
                setter
                ^com.netflix.hystrix.HystrixCommandProperties$Setter 
                (.withCircuitBreakerEnabled
                    (HystrixCommandProperties/Setter)
                    false)
            ;   ^com.netflix.hystrix.HystrixCommandProperties$Setter 
            ;   (.withExecutionTimeoutInMilliseconds
            ;       (HystrixCommandProperties/Setter)
            ;       1000))
            ; (.andCommandPropertiesDefaults
            ;   setter
            ;   (.withExecutionIsolationStrategy
            ;       (HystrixCommandProperties/Setter)
            ;        com.netflix.hystrix.HystrixCommandProperties$ExecutionIsolationStrategy/THREAD)
                ))
     :hystrix/fallback-fn 
        (fn fetch-req-fallback [url]
            {:status 419
             :headers {}
             :body "failed"})}
    [url]
    @(http/get url))

更新2:

删除thread块可以解决问题。但是,我确实需要跨多个线程执行这些请求,因此它不一定能解决我的问题。

http clojure clojurescript hystrix
1个回答
1
投票

固定它。我不得不修改线程池属性。

(hystrix/defcommand fetch-request
  {:hystrix/group-key "ct0"
   :hystrix/command-key "URLFetch"
   :hystrix/thread-pool-key "URLThread"
   :hystrix/init-fn
   (fn [_ ^com.netflix.hystrix.HystrixCommand$Setter setter]
     (.andThreadPoolPropertiesDefaults
      setter
      (doto (HystrixThreadPoolProperties/Setter)
        (.withMaxQueueSize 10)
        (.withQueueSizeRejectionThreshold 10)))

     (.andCommandPropertiesDefaults
      setter
      (doto (HystrixCommandProperties/Setter)
        (.withExecutionIsolationStrategy
         com.netflix.hystrix.HystrixCommandProperties$ExecutionIsolationStrategy/THREAD)
        (.withExecutionTimeoutInMilliseconds
         1500))))
   :hystrix/fallback-fn
   (fn fetch-req-fallback [url]
     {:status 419
      :headers {}
      :body "failed"})}
  [url]
  @(http/get url))
© www.soinside.com 2019 - 2024. All rights reserved.