swagger1st和friboo

swagger1st

swagger1st库提供了符合Ring规范的handler,能够根据Swagger定义解析、验证、路由HTTP请求。

使用swagger1st,不是在代码中定义API,而是利用Swagger官方的编辑器根据Swagger 2.0规范定义API。这样产生了一种API优先的开发流程,并且是使用独立于任何开发语言的方式定义API。

friboo

friboo提供了一些component,用于辅助Clojure的微服务应用开发。friboo鼓励采用swagger1st实行API优先的开发方式。

HTTP component

使用def-http-component宏创建HTTP component。http组件启动时,内部调用swagger1st创建handler,并使用jetty适配器运行之。

(ns myapi
  (:require [org.zalando.stups.friboo.system.http :refer [def-http-component]))

(def-http-component MyAPI "my-api.yaml" [db scheduler])

(defn my-api-function [parameters request db scheduler]
  ; 这里能够使用依赖的db和scheduler组件
  )

上面的代码创建了MyAPI组件,依赖于db和scheduler组件。my-api-function函数的第一个参数是一个扁平的map,对应的Swagger规范的parameters。

http组件使用:configuration存放配置项,在启动组件时用到它,例如

(map->MyAPI {:configuration {:port        8080
                             :cors-origin "*.zalando.de"}})

推荐采用下面的方式创建一个http系统。

(ns my-app.core
  (:require [org.zalando.stups.friboo.config :as config]
            [org.zalando.stups.friboo.system :as system]))

(defn run
  [default-configuration]
  (let [configuration (config/load-configuration
                        (system/default-http-namespaces-and :db)
                        [my-app.sql/default-db-configuration
                         my-app.api/default-http-configuration
                         default-configuration])
        system (system/http-system-map configuration
                                       my-app.api/map->API [:db]
                                       :db (my-app.sql/map->DB {:configuration (:db configuration)}))]

    (system/run configuration system)))

DB component

使用def-db-component宏创建。DB组件本质上是一个兼容db-spec的数据结构,使用:datasource存放数据源。

(ns mydb
  (:require [org.zalando.stups.friboo.system.db :refer [def-db-component]))

(def-db-component MyDB)

与http组件类似,db组件使用:configuration存放配置项,在启动组件时用到。

(map->MyDB {:configuration {:subprotocol "postgresql"
                            :subname     "localhost/mydb"}})
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,251评论 25 708
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,923评论 18 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,951评论 6 342
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,229评论 4 61
  • 这么多坍塌的谎言 这么多儿童在森林流浪 这么多空旷的坚持 这么多白云在无力支撑 你是个在冬天开花,春天结果的老骗子...
    苏苏诺阅读 221评论 5 11