<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                [TOC] # 1. 節點的增刪改查 以SprintBook Web項目為例。 (1)添加依賴 *`pom.xml`* ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.5</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.21</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>RELEASE</version> <scope>compile</scope> </dependency> ``` (2)zookeeper服務器配置 *`application.properties`* ```properties # 如果是集群使用,隔開,如hadoop101:2181,hadoop102:2181 zookeeper.adrress=hadoop101:2181 zookeeper.timeout=5000 ``` (3)將Zookeeper組件注冊到IOC容器中 *`config/ZKConfig.java`* ```java import org.apache.zookeeper.WatchedEvent; import org.apache.zookeeper.Watcher; import org.apache.zookeeper.ZooKeeper; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.concurrent.CountDownLatch; @Configuration public class ZKConfig { @Value("${zookeeper.address}") private String address; @Value("${zookeeper.timeout}") private int timeout; @Bean(name = "zkClient") public ZooKeeper zkClient() { ZooKeeper zooKeeper = null; try { final CountDownLatch countDownLatch = new CountDownLatch(1); zooKeeper = new ZooKeeper(address, timeout, new Watcher() { @Override public void process(WatchedEvent watchedEvent) { if (Event.KeeperState.SyncConnected == watchedEvent.getState()) { countDownLatch.countDown(); } } }); countDownLatch.await(); System.out.println("程序初始化的時候,zookeeper連接成功了"); } catch (Exception e) { System.out.println("程序初始化的時候,zookeeper連接失敗" + e); } return zooKeeper; } } ``` (4)Zookeeper工具類 *`utils/ZKApi.java`* ```java import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.ZooDefs; import org.apache.zookeeper.ZooKeeper; import org.apache.zookeeper.data.Stat; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class ZKApi { @Autowired private ZooKeeper zooKeeper; /** * 判斷某一個節點是否存在 */ public Stat exists(String path) { try { return zooKeeper.exists(path, false); } catch (Exception e) { e.printStackTrace(); return null; } } /** * 創建一個新的節點 */ public boolean createNode(String path, String data) { try { zooKeeper.create(path, data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); return true; } catch (Exception e) { e.printStackTrace(); return false; } } /** * 更新一個節點的數據 */ public boolean updateNode(String path, String data) { try { zooKeeper.setData(path, data.getBytes(), -1); return true; } catch (Exception e) { e.printStackTrace(); return false; } } /** * 刪除一個節點 */ public boolean deleteNode(String path) { try { zooKeeper.delete(path, -1); return true; } catch (Exception e) { e.printStackTrace(); return false; } } } ``` (5)在controller層中測試 *`controller/ZKController.java`* ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import zookeeper.znode.utils.ZKApi; @RestController public class ZKController { @Autowired private ZKApi zkApi; @RequestMapping("/createNode") public String createNode() { String path = "/bd202/node001"; String data = "this is znode!"; boolean b = zkApi.createNode(path, data); return b + " "; } @RequestMapping("/exists") public String exists(String path) { path = "/" + path; return zkApi.exists(path).toString(); } @RequestMapping("/updateNode") public String updateNode(String path, String data) { path = "/bd202/" + path; boolean b = zkApi.updateNode(path, data); return b + " "; } @RequestMapping("/deleteNode") public String deleteNode(String path) { boolean b = zkApi.deleteNode(path); return b + " "; } } ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看