Skip to content

Instantly share code, notes, and snippets.

@roman
Created April 10, 2015 18:03
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roman/157c6c16f70eb46e50b1 to your computer and use it in GitHub Desktop.
Save roman/157c6c16f70eb46e50b1 to your computer and use it in GitHub Desktop.
Hystrix-clj hello world
(ns hystrix-hello-world.core
(:require
;; hystrix
[com.netflix.hystrix.core :as hystrix]
[hystrix-event-stream-clj.core :as hystrix-event]
;; http
[aleph.http :refer [start-http-server wrap-ring-handler]]
[clj-http.client :as http])
(:import [com.netflix.hystrix
HystrixCommandProperties]))
;; Instructions to run Hystrix Dashboard (on different JVM process):
;; https://github.com/Netflix/Hystrix/tree/master/hystrix-dashboard#run-via-gradle
;; (no need to install gradle yourself, the command in there does that for you)
;; This is an example of simple command that hits
;; a remote URL
(hystrix/defcommand fetch-request
{:hystrix/group-key "Web"
:hystrix/command-key "URLFetch"
:hystrix/init-fn
(fn fetch-request-init [_ setter]
(.andCommandPropertiesDefaults
setter
(.withExecutionTimeoutInMilliseconds
(HystrixCommandProperties/Setter)
(* 10 1000)))
setter)
;; :hystrix/fallback-fn
;; (fn fetch-request-fallback [url]
;; {:status 999
;; :headers {}
;; :body "shit just timed out"})
}
[url]
(http/get url))
;; DOCUMENTATION on how hystrix defcommand works:
;; https://github.com/Netflix/Hystrix/blob/master/hystrix-contrib/hystrix-clj/src/main/clojure/com/netflix/hystrix/core.clj#L17
(defn hystrix-stream-app [req]
(hystrix-event/stream))
(defn -main []
(start-http-server
(wrap-ring-handler hystrix-stream-app)
{:port 4500}))
(defproject hystrix-hello-world "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
;; TODO: Change this
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:repl-options { :host "0.0.0.0"
:port 56789 }
:plugins [[cider/cider-nrepl "0.9.0-SNAPSHOT"]]
:dependencies [[org.clojure/clojure "1.6.0"]
[clj-http "1.1.0"]
[aleph "0.3.3"]
[com.netflix.hystrix/hystrix-clj "1.4.4"]
[hystrix-event-stream-clj "0.1.3"]
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment