Kai Chen
    • 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

      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.
      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
    • Engagement control
    • 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 Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control 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

    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.
    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
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # 六角鼠年鐵人賽 Week 23 - Spring Boot - Redis Pub/Sub 功能介紹 ==大家好,我是 "為了拿到金角獎盃而努力著" 的文毅青年 - Kai== ## 范蠡 :::info 飛鳥盡,良弓藏;狡兔死,走狗烹 ::: 趁著端午連假休息了一週後,繼續程式的自學之路。 ## 主題 前幾篇講了 Redis 的基本功能介紹與使用 Java 相連的方式 (後續很多應用就看個人的方式),現在來講點特別的功能 PUB / SUB。 雖然這並非 Redis 的強項,但也是好用的服務,作為一個輕量級的 NoSQL 來說,Redis 真的能說該有的都有了~ ## Pub/Sub PUB SUB 又稱作訂閱功能,是目前不論交友平台、影音平台、甚至特色APP都會有的標配,其目的是為了避免在被訂閱方做出了什麼事情或回應時,訂閱方無法第一時間得知,為了減少中間落差的時間所實現的功能。 透過一方訂閱、一方被訂閱的方式,讓其雖然不能永遠保持同步,但可以在一方做出反應時,另外一方可以得到反應為目標。 而這功能在 Redis 中也很容易做到,先來看看以下的語法 | 指令 | 描述 | 備註 | | ---- | ---- | ---- | | SUBSCRIBE [channel_name1] [channel_name2]... | 訂閱一個或多個 channel | 若在 redis 上操作會進入監聽模式,在 Java 的操作上需要使用一個 Thread 持續監聽其回應| | PSUBSCRIBE [pattern] | 訂閱一個或多個 channel 名稱符合 pattern 的 channel | pattern 目前支援方式: * 多個字元符、? 單個字元符、\[abc\] 限定字元符 | | PUBLISH [channel_name] [message] | 發布一條訊息到指定的 channel 上 || | UNSUBSCRIBE [channel_name1] [channel_name2]... | 取消訂閱一個或多個 channel || | PUNSUBSCRIBE [pattern] | 取消訂閱 channel 名稱符合該 pattern 的 channel || | PUBSUB [command] | 檢視目前 Redis pub/sub 子系統的狀態 | 分別有三種命令可以使用: CHANNELS 顯示當前有多少使用中的 channel;NUMPAT 顯示當前有多少種 SUBSCRIBE Pattern;NUMSUB [channel or patter] 顯示當前 channel 或 pattern 有多少個訂閱者 | ## 實務操作 在 docker Redis 的操作上也很簡單,Kai 這邊會訂閱一個 channel 和一個 pattern,並對兩個 channel 發布訊息來看看成果。 ### 訂閱一個 channel 請先用 docker 開啟一個負責訂閱的 Redis 操作畫面 ``` subscribe twitter_channel_0001 ``` ![](https://i.imgur.com/g6Lz0U4.png) ### 對該 chanel 發布訊息 再開啟一個 docker Redis 操作畫面,對上面訂閱的 channel 發布訊息 ``` publish twitter_channel_0001 "hello world" ``` > 如果不想使用雙引號,請注意訊息中間不能有空格 ![](https://i.imgur.com/2k0SSkp.png) ### 回第一個開啟的 docker Redis 操作畫面 可以看到已經收到了回來的訊息! ![](https://i.imgur.com/g8PbGsq.png) ### 這次訂閱一個 pattern ``` psubscribe twitter_channel_* ``` ![](https://i.imgur.com/Cb5jkZz.png) ### 對好幾個類似的 channel 發布訊息 ``` publish twitter_channel_0002 "hello Kai!" publish twitter_channel_0003 "hello Mary!" publish twitter_channel_0004 "hello John!" ``` ![](https://i.imgur.com/9CIQ3qb.png) ### 回到訂閱 pattern 的操作畫面 發現剛剛傳送的三筆資料都過來了! ![](https://i.imgur.com/2vf8G8t.png) ## 結語 :::danger PUBSUB 的功能很可惜無法透過 docker Redis 的操作作呈現,或者是我個人還有不懂的地方,若有了解的朋友也歡迎提出方式讓我學習! 結尾跟大家分享一篇不錯的文章,是 Java 實現 Pub/Sub 功能的範例,可以參考學習~ [Java Jedis操作Redis示例(一)——pub/sub模式实现消息队列](https://blog.csdn.net/KingCat666/article/details/77883813) 簡單帶過 PUB/SUB 的功能,下一篇也簡單介紹幾個 Redis 常用的功能後,就可以先將 Redis 單原告個段落了~ Redis 深度還有許多可以學習的部分,Kai 的文章也只是初碰皮毛,有興趣的朋友可以再多加深入研究,Kai 目前寫文的方向為廣度優先,等到真的沒什麼可以寫的時候就會回顧並深入每個主題。(如果東西真的學得完...) ::: 首頁 [Kai 個人技術 Hackmd](/2G-RoB0QTrKzkftH2uLueA) ###### tags: `Redis`,`w3HexSchool`

    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

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    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