【发布时间】: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,以防万一这让回答更容易。
非常感谢, 奥利弗
【问题讨论】:
-
这应该会引导您朝着正确的方向前进:stackoverflow.com/questions/6694530/…