差異處
這裏顯示兩個版本的差異處。
java:web:restapi:rpc-style_vs_rest [2017/09/18 23:59] tony [Level 3 - Hypermedia Controls] |
java:web:restapi:rpc-style_vs_rest [2023/06/25 09:48] |
||
---|---|---|---|
行 1: | 行 1: | ||
- | {{tag>rest}} | ||
- | ====== RPC-Style API VS REST API ====== | ||
- | ===== Problem ===== | ||
- | 幾年前聽到同事說:「另一個部門的XXX說我們的REST API都沒有動詞,這樣是對的嗎?」於是我們就直接把Best Practice丟給那位同事。當時做REST API只是因為別人有而要做,而不是為了某個Context而選擇REST;後來我就思考著一件事情: 「RPC-Style與REST API比起來,到底有哪些好處?」於是我花了些時間去找尋與整理資料。首先我會先說明這裡指的RPC-style API是什麼,接著我會透過Richardson Maturity Model(RMM)說明它們兩者的差異。這是我目前認為最好的比較方式。 | ||
- | ===== RPC-style API ===== | ||
- | 網路上許多比較,大都是針對RPC與REST;我所說的RPC-style API是基於HTTP,主要有兩點特徵: | ||
- | - 基於operation所設計出來的API,因此URI通常像/createAccount、/getAccounts、/deleteAccount與/updateAccount。 | ||
- | - HTTP method只會使用GET與POST,GET使用在取得資料,POST使用在新增、修改、刪除資料。 | ||
- | ===== 基於RMM的比較 ===== | ||
- | RMM不是定義REST達到了什麼級別,而是幫助我們層層思考的一種方式([[https://martinfowler.com/articles/richardsonMaturityModel.html|參考]])。Roy Fielding 2008年的[[http://roy.gbiv.com/untangled/2008/rest-apis-must-be-hypertext-driven|某篇文章]],給了RESTful API很嚴格的限制,也代表著Level 3只是Restful的前置條件。由於RPC-style API屬於Level 0,因此我認為用RMM來說明REST API比RPC-style好在哪裡是再適合不過的:\\ | ||
- | {{:java:web:restapi:rest_rmm.png|}}\\ | ||
- | (圖片來自於[[https://martinfowler.com/articles/richardsonMaturityModel.html|link]])\\ | ||
- | 我們可以從Level 1~Level3來分別討論: | ||
- | ==== Level 1 - Resource ==== | ||
- | REST API的操作是基於Resource的。這代表著Server的設計是基於Resource,而Client的操作也是基於Resource。這為Server與Client帶來一些好處: | ||
- | === Server === | ||
- | 物件導向程式設計的特性之一: Divide and Conquer,將一個複雜的問題分解成各個資源;把相關的操作放於同一資源當中,以達到high cohesion。 | ||
- | === Client === | ||
- | 第一個好處是由於Server的設計以資源為導向,這讓User很容易了解系統的Domain Concept;其次是由於這幾年REST風格的盛行,讓開發人員更容易熟悉API的使用([[https://www.quora.com/What-are-the-advantages-of-REST-over-a-more-RPC-style|參考link]]);最後是API document容易閱讀,這點從Slack的RPC-style API document使用resource的方式去分類就可以得知。\\ | ||
- | {{:java:web:restapi:slack_rpc_style_api.png|}} | ||
- | ==== Level 2 - HTTP Verb ==== | ||
- | 透過將資源的新增查詢修改刪除對應到POST、GET、PUT、DELETE,這讓大家有一致的作法,避免不必要的變化。也讓我們得知某一資源的URI後,就可以直接透過HTTP Verb去操作它,利於我們去閱讀Document。這有別於使用RPC-style時,因大家各自命名的方式而變成一定要閱讀document。 | ||
- | ==== Level 3 - Hypermedia Controls ==== | ||
- | Hypermedia使得Resource有了self-document的能力,包含描述Resource接下來可以做的事情,這讓API擁有了discoverability的特性。你可能會問,RPC-style API幹嘛需要什麼discoverability的特性呢?其實因為self-document的關係,讓我在使用REST API時,並不一定需要去查user guide;可以直接透過link點擊到與Resource相關的sub-resource,這是一個非常好用的功能! 除此之外,Hypermedia還增加了Server Scalability([[https://blog.toright.com/posts/725/representational-state-transfer-%E8%BB%9F%E9%AB%94%E6%9E%B6%E6%A7%8B%E9%A2%A8%E6%A0%BC%E4%BB%8B%E7%B4%B9-part-i-%E5%BE%9E%E4%BA%86%E8%A7%A3-rest-%E5%88%B0%E8%A8%AD%E8%A8%88-restful%EF%BC%81.html|參考]]、[[https://ihower.tw/blog/archives/1542|參考]])與API evolution([[http://slides.com/jcassee/hypermedia-evolution-2016|參考]])。\\ | ||
- | \\ | ||
- | 首先是Server Scalability。由於stateless與透過hypermeida的rel緣故,client與server是decoupling的;這也使得client除了root uri以外,根本不需要去管其它Resource對應到的server: | ||
- | <code> | ||
- | "links": [ | ||
- | { | ||
- | "rel": "search", | ||
- | "method": "POST", | ||
- | "href": "https://192.168.0.1/api/search" | ||
- | }, | ||
- | { | ||
- | "rel": "get_accounts", | ||
- | "method": "GET", | ||
- | "href": "https://192.168.0.2/api/accounts" | ||
- | } | ||
- | ] | ||
- | </code> | ||
- | 也因此resource名稱對於client來說,在runtime時就不是這麼重要了,重要的是rel的名稱;這應該是Roy Fielding在[[http://roy.gbiv.com/untangled/2008/rest-apis-must-be-hypertext-driven|這篇文章]]說以下內容的原因吧: | ||
- | <code> | ||
- | A REST API must not define fixed resource names or hierarchies (an obvious coupling of client and server). | ||
- | </code> | ||
- | 而API evolution也因為透過link ref與Resource互動的特性,使得你不用害怕改Resource的名稱。 | ||
- | ===== Summary ===== | ||
- | 我只針對REST比RPC-style好在哪裡做說明,這不代表著REST就一定比RPC-style好。下圖是我以前念書時候的一個作業,雖然server使用物件導向去設計model,但與client的溝通我還是選擇了RPC-style的方式:\\ | ||
- | {{:java:web:restapi:rpc_style_sample_ooad.png|}}\\ | ||
- | 假如是一個不是很大的專案,而且client只有我自己,我還是會選擇使用這樣的方式,因為簡單。所以要使用甚麼方式,還是要看當時所面對的Context,這也讓我思考hypermedia的特性到底對我們有沒有用。\\ | ||
- | \\ | ||
- | 這裡補充別人針對hypermedia API進行統計的資料給大家參考: | ||
- | * 2014年CA Technologies從180個API供應商中,得知有26.3%有實作hypermedia API,有28%要支援此功能。([[https://read01.com/zh-tw/aA2kjK.html#.Wb_o7rKg_Z4|link]]) | ||
- | * 2015年Akana舉辦一個線上研討會,從76人中得知有16%的人正在使用hypermedia API,而40%的人計畫要使用。([[https://blog.akana.com/hypermedia-apis/|link]]) | ||
- | 以上是目前的心得分享。 | ||
- | |||
- | ===== Reference ===== | ||
- | * [[https://apihandyman.io/do-you-really-know-why-you-prefer-rest-over-rpc/|Do you really know why you prefer REST over RPC?]] | ||
- | * [[http://roy.gbiv.com/untangled/2008/rest-apis-must-be-hypertext-driven|REST APIs must be hypertext-driven]] | ||
- | * [[https://martinfowler.com/articles/richardsonMaturityModel.html|Richardson Maturity Model]] | ||
- | * [[https://www.quora.com/What-are-the-advantages-of-REST-over-a-more-RPC-style|What are the advantages of REST over a more RPC style?]] | ||
- | * [[https://blog.toright.com/posts/725/representational-state-transfer-%E8%BB%9F%E9%AB%94%E6%9E%B6%E6%A7%8B%E9%A2%A8%E6%A0%BC%E4%BB%8B%E7%B4%B9-part-i-%E5%BE%9E%E4%BA%86%E8%A7%A3-rest-%E5%88%B0%E8%A8%AD%E8%A8%88-restful%EF%BC%81.html|淺談 REST 軟體架構風格 (Part.I) - 從了解 REST 到設計 RESTful!]] | ||
- | * [[https://ihower.tw/blog/archives/1542|什麼是REST跟RESTful?]] | ||
- | * [[http://slides.com/jcassee/hypermedia-evolution-2016|Hypermedia and API evolution]] | ||
- | * [[https://read01.com/zh-tw/aA2kjK.html#.Wb_qV7Kg_Z5|API設計如龍生九子,Tunneling、Hypermedia等更受開發者青睞]] | ||
- | * [[https://blog.akana.com/hypermedia-apis/|The Movement Towards Hypermedia APIs – Is it Happening?]] | ||
- | |||
- | ===== ===== | ||
- | ---- | ||
- | \\ | ||
- | ~~DISQUS~~ |