x213212
    • 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
    • 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
    • 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 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
  • 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
    # zookeeper 搭建偽群集 ![](https://i.imgur.com/F77meWn.png) zookeeper 大概就是 目前只介紹這個東西的用途,可以得知我們的我們的服務都註冊到zookeeper 裡面的一個節點,然後我們可以透過去請求 zookeeper 先取得節點的可用伺服器清單,然後client 再去訪問可用的伺服器清單,這個就是 zookeeper 大概用途。 這邊文章後下回 spring cloud 來實作微服務(ps 不是這個文 # zookeeper 應用場景 * 分布式協調 * 分布式所 * 元数据/配置信息管理 * HA高可用 ![](https://i.imgur.com/fypQ7zX.png) 分布式协调这个其实是 zookeeper 很经典的一个用法,简单来说,就好比,你 A 系统发送个请求到 mq,然后 B 系统消息消费之后处理了。那 A 系统如何知道 B 系统的处理结果?用 zookeeper 就可以实现分布式系统之间的协调工作。A 系统发送请求之后可以在 zookeeper 上对某个节点的值注册个监听器,一旦 B 系统处理完了就修改 zookeeper 那个节点的值,A 系统立马就可以收到通知,完美解决。 ![](https://i.imgur.com/E28ekTT.png) 分布式锁举个栗子。对某一个数据连续发出两个修改操作,两台机器同时收到了请求,但是只能一台机器先执行完另外一个机器再执行。那么此时就可以使用 zookeeper 分布式锁,一个机器接收到了请求之后先获取 zookeeper 上的一把分布式锁,就是可以去创建一个 znode,接着执行操作;然后另外一个机器也尝试去创建那个 znode,结果发现自己创建不了,因为被别人创建了,那只能等着,等第一个机器执行完了自己再执行。 ![](https://i.imgur.com/N6pibSp.png) 元数据/配置信息管理zookeeper 可以用作很多系统的配置信息的管理,比如 kafka、storm 等等很多分布式系统都会选用 zookeeper 来做一些元数据、配置信息的管理,包括 dubbo 注册中心不也支持 zookeeper 么? ![](https://i.imgur.com/snPowy3.png) HA高可用性这个应该是很常见的,比如 hadoop、hdfs、yarn 等很多大数据系统,都选择基于 zookeeper 来开发 HA 高可用机制,就是一个重要进程一般会做主备两个,主进程挂了立马通过 zookeeper 感知到切换到备用进程。 # 環境設定 ```bash 下載 zookeeper 3.4.14 cd : /opt wget https://archive.apache.org/dist/zookeeper/zookeeper-3.4.14/zookeeper-3.4.14.tar.gz tar zxvf zookeeper-3.4.14.tar.gz 安裝目錄 : /opt/zookeeper mkdir /opt/zookeeper cd /opt/zookeeper tar -zxvf /opt/zookeeper-3.4.11.tar.gz cp -rf /opt/zookeeper/zookeeper-3.4.11 /opt/zookeeper/zk1 cp -rf /opt/zookeeper/zookeeper-3.4.11 /opt/zookeeper/zk2 cp -rf /opt/zookeeper/zookeeper-3.4.11 /opt/zookeeper/zk3 rm -rf /opt/zookeeper/zookeeper-3.4.11 到這邊已經產生了三個節點 /opt/zookeeper/zk1 /opt/zookeeper/zk2 /opt/zookeeper/zk3 ``` # zoo.cfg 配置 更多配置參數說明 https://kknews.cc/zh-tw/code/plmkpp8.html # 節點1 ```code #zookeeper时间单元,单位为毫秒 tickTime=2000 #集群中的follower服务器(F)与leader服务器(L)之间 初始连接 时能容忍的最多心跳数(tickTime的数量)。 initLimit=10 # 集群中的follower服务器(F)与leader服务器(L)之间 请求和应答 之间能容忍的最多心跳数(tickTime的数量)。 syncLimit=5 # data数据目录 dataDir=/opt/zookeeper/zk1/data # 客户端连接端口 clientPort=2181 # 客户端最大连接数 #maxClientCnxns=60 # 需要保留的快照数目 #autopurge.snapRetainCount=3 # 是否开启自动清理事务日志和快照功能 0 不开启,1表示开启 #autopurge.purgeInterval=1 #集群配置 server.1=127.0.0.1:2888:3888 server.2=127.0.0.1:2889:3889 server.3=127.0.0.1:2890:3890 ``` # 節點2 ```code #zookeeper时间单元,单位为毫秒 tickTime=2000 #集群中的follower服务器(F)与leader服务器(L)之间 初始连接 时能容忍的最多心跳数(tickTime的数量)。 initLimit=10 # 集群中的follower服务器(F)与leader服务器(L)之间 请求和应答 之间能容忍的最多心跳数(tickTime的数量)。 syncLimit=5 # data数据目录 dataDir=/opt/zookeeper/zk2/data # 客户端连接端口 clientPort=2182 # 客户端最大连接数 #maxClientCnxns=60 # 需要保留的快照数目 #autopurge.snapRetainCount=3 # 是否开启自动清理事务日志和快照功能 0 不开启,1表示开启 #autopurge.purgeInterval=1 #集群配置 server.1=127.0.0.1:2888:3888 server.2=127.0.0.1:2889:3889 server.3=127.0.0.1:2890:3890 ``` # 節點3 ```code #zookeeper时间单元,单位为毫秒 tickTime=2000 #集群中的follower服务器(F)与leader服务器(L)之间 初始连接 时能容忍的最多心跳数(tickTime的数量)。 initLimit=10 # 集群中的follower服务器(F)与leader服务器(L)之间 请求和应答 之间能容忍的最多心跳数(tickTime的数量)。 syncLimit=5 # data数据目录 dataDir=/opt/zookeeper/zk3/data # 客户端连接端口 clientPort=2183 # 客户端最大连接数 #maxClientCnxns=60 # 需要保留的快照数目 #autopurge.snapRetainCount=3 # 是否开启自动清理事务日志和快照功能 0 不开启,1表示开启 #autopurge.purgeInterval=1 #集群配置 server.1=127.0.0.1:2888:3888 server.2=127.0.0.1:2889:3889 server.3=127.0.0.1:2890:3890 ``` ```關閉防火牆 CentOS Linux开启和关闭防火墙命令有两种,一种是临时的,重启即复原;另外一种是永久性的,重启不会复原。 1) 临时生效,重启后复原 开启: service iptables start 关闭: service iptables stop 2) 永久性生效,重启后不会复原 开启: chkconfig iptables on 关闭: chkconfig iptables off ``` # myid 配置 需要在 data 裡面新增 一個 myid 文件各一個 ```code echo '1' > /opt/zookeeper/zk1/data/myid echo '2' > /opt/zookeeper/zk2/data/myid echo '3' > /opt/zookeeper/zk3/data/myid ``` # 日誌路徑 配置 修改 bin 目錄的 zkEnv.sh 把 ZOO_LOG_DIR 設為 ```code ZOO_LOG_DIR="../logs/" ``` # 啟動 ```code /opt/zookeeper/zk1/bin/zkServer.sh start /opt/zookeeper/zk2/bin/zkServer.sh start /opt/zookeeper/z3/bin/zkServer.sh start ``` # client 連接 隨機訪問任意server create /test 123 ![](https://i.imgur.com/I3O6gSQ.png) ``` /opt/zookerper/zk1/bin/zkCli.sh -server 127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183 ``` # client 創建一個 test znode 隨機訪問任意節點 ![](https://i.imgur.com/22Hfxn0.png) ``` create /test /123 ``` # client 查看 test znode ![](https://i.imgur.com/9s93U55.png) ``` get /test ``` # 查看 log 當有節點異動都可從這個 log 進行觀看 ![](https://i.imgur.com/MZ99vN6.png) ```code logs$ tail -f zookeeper.out ``` 參考 https://juejin.im/post/5db059046fb9a0204875e6c7

    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