暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

golang源码分析:etcd(3)

        搭完集群后,我开始体验下etcd如何使用:

1,设置key,进行前缀查询操作

    ENDPOINTS=http://127.0.0.1:22379
    etcdctl --endpoints=$ENDPOINTS put web1 value1
    etcdctl --endpoints=$ENDPOINTS put web2 value2
    OK
    etcdctl --endpoints=$ENDPOINTS put web3 value3

    查询

      etcdctl --endpoints=$ENDPOINTS get web --prefix
      web1
      value1
      web2
      value2
      web3
      value3

      2,前缀删除

        etcdctl --endpoints=$ENDPOINTS put key myvalue
        etcdctl --endpoints=$ENDPOINTS del key


        etcdctl --endpoints=$ENDPOINTS put k1 value1
        etcdctl --endpoints=$ENDPOINTS put k2 value2
        etcdctl --endpoints=$ENDPOINTS del k --prefix
        2


        3,事务操作txn

          etcdctl --endpoints=$ENDPOINTS put user1 bad
          etcdctl --endpoints=$ENDPOINTS txn --interactive
          OK
          compares:
          value("user1") = "bad"


          success requests (get, put, del):
          del user1


          failure requests (get, put, del):
          put user1 good


          SUCCESS


          1

          4,watch操作

          在一个console进行watch,另一个console进行key的操作

            etcdctl --endpoints=$ENDPOINTS watch stock1
              etcdctl --endpoints=$ENDPOINTS put stock1 1000

              当然也可以watch前缀

                etcdctl --endpoints=$ENDPOINTS watch stock --prefix
                etcdctl --endpoints=$ENDPOINTS put stock1 10
                etcdctl --endpoints=$ENDPOINTS put stock2 20

                5,租期

                  % etcdctl --endpoints=$ENDPOINTS lease grant 300
                  lease 414688a849143a15 granted with TTL(300s)
                     % etcdctl --endpoints=$ENDPOINTS put sample value --lease=414688a849143a15
                    etcdctl --endpoints=$ENDPOINTS get sample


                    OK
                    sample
                    value

                    操作的时候需要指定返回的租期id

                      % etcdctl --endpoints=$ENDPOINTS get sample                               
                      sample
                      value


                      6,分布式锁

                         % etcdctl --endpoints=$ENDPOINTS lock mutex1
                        mutex1/414688a849143a1d

                        在另一个console尝试加锁,会一直阻塞,直到前者释放锁

                          % ENDPOINTS=http://127.0.0.1:22379
                          % etcdctl --endpoints=$ENDPOINTS lock mutex1
                          mutex1/414688a849143a21

                          7,选举

                            % etcdctl --endpoints=$ENDPOINTS elect one p1
                            one/414688a849143a2a
                            p1
                              % etcdctl --endpoints=$ENDPOINTS elect one p2
                              p2

                              8,查看数据状态



                                % etcdctl --write-out=table --endpoints=$ENDPOINTS endpoint status


                                +------------------------+------------------+---------------+-----------------+---------+----------------+-----------+------------+-----------+------------+--------------------+--------+
                                | ENDPOINT | ID | VERSION | STORAGE VERSION | DB SIZE | DB SIZE IN USE | IS LEADER | IS LEARNER | RAFT TERM | RAFT INDEX | RAFT APPLIED INDEX | ERRORS |
                                +------------------------+------------------+---------------+-----------------+---------+----------------+-----------+------------+-----------+------------+--------------------+--------+
                                | http://127.0.0.1:22379 | 91bc3c398fb3c146 | 3.6.0-alpha.0 | 3.6.0 | 33 kB | 33 kB | false | false | 2 | 37 | 37 | |
                                +------------------------+------------------+---------------+-----------------+---------+----------------+-----------+------------+-----------+------------+--------------------+--------+


                                  % etcdctl --endpoints=$ENDPOINTS endpoint health
                                  http://127.0.0.1:22379 is healthy: successfully committed proposal: took = 15.993027ms




                                  9,数据持久化备份

                                    % etcdctl --endpoints=$ENDPOINTS snapshot save my.db


                                    {"level":"info","ts":"2023-06-11T16:48:51.176005+0800","caller":"snapshot/v3_snapshot.go:67","msg":"created temporary db file","path":"my.db.part"}
                                    {"level":"info","ts":"2023-06-11T16:48:51.178707+0800","logger":"client","caller":"v3@v3.6.0-alpha.0/maintenance.go:237","msg":"opened snapshot stream; downloading"}
                                    {"level":"info","ts":"2023-06-11T16:48:51.214419+0800","caller":"snapshot/v3_snapshot.go:75","msg":"fetching snapshot","endpoint":"http://127.0.0.1:22379"}
                                    {"level":"info","ts":"2023-06-11T16:48:51.230865+0800","logger":"client","caller":"v3@v3.6.0-alpha.0/maintenance.go:302","msg":"completed snapshot read; closing"}
                                    {"level":"info","ts":"2023-06-11T16:48:51.245575+0800","caller":"snapshot/v3_snapshot.go:90","msg":"fetched snapshot","endpoint":"http://127.0.0.1:22379","size":"33 kB","took":"68.257765ms","etcd-version":"3.6.0"}
                                    {"level":"info","ts":"2023-06-11T16:48:51.247532+0800","caller":"snapshot/v3_snapshot.go:100","msg":"saved","path":"my.db"}
                                    Snapshot saved at my.db
                                    Server version 3.6.0
                                      % etcdctl --write-out=table --endpoints=$ENDPOINTS snapshot status my.db

                                      10,版本v2和v3之间迁移



                                        # write key in etcd version 2 store
                                        export ETCDCTL_API=2
                                        etcdctl --endpoints=http://$ENDPOINT set foo bar


                                        # read key in etcd v2
                                        etcdctl --endpoints=$ENDPOINTS --output="json" get foo


                                        # stop etcd node to migrate, one by one


                                        # migrate v2 data
                                        export ETCDCTL_API=3
                                        etcdctl --endpoints=$ENDPOINT migrate --data-dir="default.etcd" --wal-dir="default.etcd/member/wal"


                                        # restart etcd node after migrate, one by one


                                        # confirm that the key got migrated
                                        etcdctl --endpoints=$ENDPOINTS get foo




                                        文章转载自golang算法架构leetcode技术php,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

                                        评论