【问题标题】:How to set a timeout on a Hystrix command in Clojure?如何在 Clojure 中为 Hystrix 命令设置超时?
【发布时间】:2023-03-22 14:03:01
【问题描述】:

我正在学习 Hystrix 和 Clojure,但不明白如何(正确)在 Clojure 中为 Hystrix 命令设置超时。

我更广泛地搜索了 StackOverflow 和网络。我查看了 Hystrix 的 Clojure 包装器源代码 (https://github.com/Netflix/Hystrix/blob/master/hystrix-contrib/hystrix-clj/src/main/clojure/com/netflix/hystrix/core.clj)。有一个 init-fn 函数参数看起来很有希望,但 cmets 似乎表明这不是一个可持续的解决方案。但这只是一个简单的开始吗?

我在 Clojure 中运行了一个非常简单的 Hystrix 命令,希望能帮助我将其扩展为设置 200 毫秒超时:

(ns hystrix-timeout.core
  (:require [clojure.string :as str])
  (:require [com.netflix.hystrix.core :as hystrix])
  (:gen-class))


(defn my-primary [a]
  (if (= a true) (throw (Exception. "Primary failed")) (str "primary: " a)))

(defn my-fallback [a]
  (str "fallback: " a))

(hystrix/defcommand my-command
  {:hystrix/fallback-fn my-fallback}
  [a]
  (my-primary a))


(defn -main
  "Executes a simple Hystrix command. Will use a timeout when I know how to do this in Clojure."
  [& args]

  (println (my-command false))
  (println (my-command true))

  (System/exit 0)   ; Exit explicitly as Hystrix threads are still running.
)

我已将我的 lein 项目提交到 https://github.com/oliverbaier/learning/tree/master/hystrix-timeout,以防万一这让回答更容易。

非常感谢, 奥利弗

【问题讨论】:

标签: clojure timeout


【解决方案1】:

最简单的方法是只使用系统属性。您可以设置全局默认值:

(System/setProperty "hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds" "200")

或命令特定值:

(System/setProperty "hystrix.command.my-command.execution.isolation.thread.timeoutInMilliseconds" "200")

您可以在您的 -main 方法中执行此操作。如果你正在运行一个真正的 web-app sans -main,你可以在 project.clj 中添加一个 ring init:ring {:handler your-handler :init your-config-method} your-config-method 将在启动时被调用。

【讨论】:

    猜你喜欢
    • 2012-05-17
    • 2014-08-13
    • 1970-01-01
    • 1970-01-01
    • 2016-12-27
    • 2012-01-16
    • 1970-01-01
    • 1970-01-01
    • 2015-08-27
    相关资源
    最近更新 更多