Http-kit 是主要由Java 和 Clojure开发,为Clojure定制的零依赖的HTTP lib,仅用3000多行代码,实现了高性能 Server,Client,Timer。代码量少,实现简单,对Hacker友好。
在普通的PC上进行性能测试时,http-kit server每秒能处理数万个请求,并能轻松保持数十万并发,每个连接消耗几k内存,并发数仅与内存大小有关,高并发不影响 latency。
http-kit 下载:http-kit
http-kit 官网:http-kit.org
1)HTTP Server:
2)HTTP Client:
2.0.0 版本修改纪录:
http-kit 力争做到API友好,性能强悍,实现简单,文档完善。
示例1:
(ns myClojure.syntax.httpkit2
(:use org.httpkit.server))
(defn app [req]
{:status 200
:headers {"Content-Type" "text/html"}
:body "hello ithomer.net"})
(run-server app {:ip "172.27.22.21" :port 8888 :thread 10})
(println "look at: http://172.27.22.21:8888")
控制台输出:
look at: http://172.27.22.21:8888 # 指定了IP地址后,访问 http://localhost:8888 将访问不到
访问地址:http://172.27.22.21:8888
示例 2:
代码:
; clojure connect to mysql
; ithomer.net
; 2013.12.10
(ns myClojure.syntax.httpkit3
(:use [compojure.route :only [files not-found]]
[compojure.handler :only [site]]
[compojure.core :only [defroutes GET POST DELETE ANY context]]
[org.httpkit.server])
(:require [compojure.route :as route]))
(defn show-landing-page [req] ;; ordinary clojure function, accepts a request map, returns a response map
;; return landing page's html string. possible template library:
;; mustache (https://github.com/shenfeng/mustache.clj, https://github.com/fhd/clostache...)
;; enlive (https://github.com/cgrand/enlive)
;; hiccup(https://github.com/weavejester/hiccup)
(println "<h1>Hello World</h1>")
)
(defn update-userinfo [req] ;; ordinary clojure function
(let [user-id (-> req :params :id) ; param from uri
password (-> req :params :password)] ; form param
(println user-id password)
))
(defn get-user-by-id [req] ;; ordinary clojure function
(let [user-id (-> req :params :id) ; param from uri
password (-> req :params :password)] ; form param
(println user-id password)
)
)
(defroutes all-routes
(GET "/" [] show-landing-page)
(context "/user/:id" []
(GET / [] get-user-by-id)
(POST / [] update-userinfo))
(route/not-found "<p>Page not found.</p>")) ;; all other, return 404
(run-server (site #'all-routes) {:port 8888})
运行后,访问网址: http://172.27.22.21:8888
控制台打印输出:
<h1>Hello World</h1>
参考推荐:
http-kit server(官方)
compojure(github)