yuze1995
  • NEW!
    NEW!  Connect Ideas Across Notes
    Save time and share insights. With Paragraph Citation, you can quote others’ work with source info built in. If someone cites your note, you’ll see a card showing where it’s used—bringing notes closer together.
    Got it
      • Create new note
      • Create a note from template
        • Sharing URL Link copied
        • /edit
        • View mode
          • Edit mode
          • View mode
          • Book mode
          • Slide mode
          Edit mode View mode Book mode Slide mode
        • Customize slides
        • Note Permission
        • Read
          • Only me
          • Signed-in users
          • Everyone
          Only me Signed-in users Everyone
        • Write
          • Only me
          • Signed-in users
          • Everyone
          Only me Signed-in users Everyone
        • Engagement control Commenting, Suggest edit, Emoji Reply
      • Invite by email
        Invitee

        This note has no invitees

      • Publish Note

        Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

        Your note will be visible on your profile and discoverable by anyone.
        Your note is now live.
        This note is visible on your profile and discoverable online.
        Everyone on the web can find and read all notes of this public team.

        Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

        Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

        Explore these features while you wait
        Complete general settings
        Bookmark and like published notes
        Write a few more notes
        Complete general settings
        Write a few more notes
        See published notes
        Unpublish note
        Please check the box to agree to the Community Guidelines.
        View profile
      • Commenting
        Permission
        Disabled Forbidden Owners Signed-in users Everyone
      • Enable
      • Permission
        • Forbidden
        • Owners
        • Signed-in users
        • Everyone
      • Suggest edit
        Permission
        Disabled Forbidden Owners Signed-in users Everyone
      • Enable
      • Permission
        • Forbidden
        • Owners
        • Signed-in users
      • Emoji Reply
      • Enable
      • Versions and GitHub Sync
      • Note settings
      • Note Insights New
      • Engagement control
      • Make a copy
      • Transfer ownership
      • Delete this note
      • Save as template
      • Insert from template
      • Import from
        • Dropbox
        • Google Drive
        • Gist
        • Clipboard
      • Export to
        • Dropbox
        • Google Drive
        • Gist
      • Download
        • Markdown
        • HTML
        • Raw HTML
    Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
    Create Create new note Create a note from template
    Menu
    Options
    Engagement control Make a copy Transfer ownership Delete this note
    Import from
    Dropbox Google Drive Gist Clipboard
    Export to
    Dropbox Google Drive Gist
    Download
    Markdown HTML Raw HTML
    Back
    Sharing URL Link copied
    /edit
    View mode
    • Edit mode
    • View mode
    • Book mode
    • Slide mode
    Edit mode View mode Book mode Slide mode
    Customize slides
    Note Permission
    Read
    Only me
    • Only me
    • Signed-in users
    • Everyone
    Only me Signed-in users Everyone
    Write
    Only me
    • Only me
    • Signed-in users
    • Everyone
    Only me Signed-in users Everyone
    Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # Ch 08. RPC 與 Query-Based API 設計 Restful 之外最常被用到的 API 風格是 RPC (Remote Procedure Call, 遠端程序呼叫) 及 Query-Based API (查詢式 API) ## 什麼是 RPC API RPC 的概念是執行遠端的程式碼,Server 給 Client 一個清單,讓 Client 知道 Server 有這些程式可以跑,以及每個程式各自的 Request 及 Response 結構。 RPC 架構下,Client 及 Server 的程式是緊密綁定的,一旦 Server 改變,Client 也會跟著改變 :::info 原本以為 Client / Server 是緊密綁定這件事蠻唬爛的,但搜尋了一下文件發現,在 .net 的範例中,可以透過 grpc tool 直接產生一個 cs file,後端需要透過繼承這個 cs 來實作這個 gRPC 的 endpoint 不過感覺跟 Client / Server 還是感覺可以分開實作,他頂多跟 Restful 一樣,可能異動 Server 後,Client 也要跟著異動 ::: #### gRPC vs Restful API ![](https://i.imgur.com/DVhvobJ.png) Ref. https://pjchender.dev/golang/grpc-getting-started/ ### gRPC gRPC 是 Google 發起的協議,Kubernetes 內部也是透過 gRPC 來做資訊的交換。gRPC 用 HTTP/2 作為底層的通訊協議,並且使用 Protocol Buffers (ProtoBuf) 來當作 Request 及 Response 的資料格式,並且利用 HTTP/2 的雙向串流特性來實現 Client/Server 的互動 #### gRPC 資料交換機制 ![](https://i.imgur.com/fPR3tXc.png) 通常 ProtoBuf 的格式如下: ```protobuf syntax = "proto3"; import "google/protobuf/timestamp.proto"; option csharp_namespace = "grpc_chat"; package chat; service Chatroom { rpc Join(stream SpeakRequest) returns (stream BroadcastMessage); } message SpeakRequest { string uid = 1; string name = 2; string message = 3; } message BroadcastMessage { string speaker = 1; google.protobuf.Timestamp time = 2; string message = 3; } ``` ### RPC 優缺點 優點 * RPC 效能較好 * 因為遵守 ProtoBuf 定義的格式來開發,因此不用特別思考 Endpoint 要如何設計,靠工具 Generate 出來後,直接實作內容就好了 缺點 * 格式難以閱讀 * 無法指定媒體類型 (media type) * 如果 Client 是瀏覽器的話,需要安裝其他的套件並強制使用他的指定的驗證機制才能與 API Endpont 進行互動 (無感) ### RPC API 規格 一樣先定義出來 Request 及 Response,並且照著 ProtoBuf 風格填寫 ![](https://i.imgur.com/scFu7ww.png) 最後就可以產生出一份 ProtoBuf 的文件 ![](https://i.imgur.com/VBKtwgh.png) ## 什麼是 Query-Based API Query-Based API 是以查詢為基礎的 API,讓 Client 可以自己決定要如何查詢資料,並且自己決定要拿那些資料,而且也有分頁及過濾的能力 Query-Based API 也可以一次把跟該資源有相關的資料一次性取回來,把他的父子資源都一起取回來,因此可以省下多次的查詢往返,而目前較流行的方案是 OData 及 GraphQL ### OData OData 是由 OASIS 管理的標準化協議,他是以 HTTP 及 Json 為基礎的 Query-Based API,跟 Restful 很接近,且也以 **"資源"** 為核心概念 通常 OData 也會實作 Hypermedia 的相關資源或連結 #### 書上提供的 OData 範例 ![](https://i.imgur.com/4tV7BBo.png) :::success 但我還是覺得 OData 不好維護,如果專案不夠大型,資料不夠多元,用 OData 會讓服務變得相對難維護 ::: ### GraphQL 由 Facebook 在 2012 年開發,2015 年公開,目標是讓 Client 可以自行決定回應的粒度及資料深度 GraphQL 只有用到 Get 及 Post 而已,具體的內容以 GraphQL 的查詢語言撰寫,也可以在查詢內對服務下某些運算或邏輯 #### 書上提供的 GraphQL 範例 ![](https://i.imgur.com/xg9oT8F.png) GraphQL 可以做為既有 Rest API 的前端,並且解析完 Request 後再轉給後端的 REST API 取得資料後,在彙整回去給前端 GraphQL 在設計上是以單一 Endpoint 與 Client 互動,因此 GraphQL 也難以用到 HTTP 的既有特性 ### Query-Based API 設計流程 1. 為所有資源設計圖譜結構 ![](https://i.imgur.com/aGlMZxj.png) 2. 設計 Query 及 Mutation (異動) 操作 Safe => Query Idempotent / Unsafe => Mutation 3. 撰寫 API 規格文件 ![](https://i.imgur.com/4YzKs3a.png) ###### tags: `Web API 設計原則:API與微服務傳遞價值之道` `Book`

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password
    or
    Sign in via Google Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully