首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計綜合問答

ZooKeeper 與 Spring 整合開發完整指南

     編輯:程式設計綜合問答

整合架構概覽

ZooKeeper 作為分散式協調服務,常與 Spring 整合實現服務發現、配置中心、分散式鎖等功能。整合核心在於 Spring 對 ZooKeeper 客戶端的封裝與生命週期管理。

核心依賴配置

<dependencies>
    <!-- Apache Curator:ZooKeeper 官方推薦高階客戶端 -->
    <dependency>
        <groupId>org.apache.curator</groupId>
        <artifactId>curator-framework</artifactId>
        <version>5.2.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.curator</groupId>
        <artifactId>curator-recipes</artifactId>
        <version>5.2.0</version>
    </dependency>
    <!-- Spring 整合支援 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.3.0</version>
    </dependency>
</dependencies>

Spring 配置類實作

@Configuration
public class ZookeeperConfig {
    
    @Value("${zookeeper.connectString:localhost:2181}")
    private String connectString;
    
    @Value("${zookeeper.sessionTimeoutMs:60000}")
    private int sessionTimeoutMs;
    
    @Value("${zookeeper.connectionTimeoutMs:15000}")
    private int connectionTimeoutMs;
    
    @Bean(initMethod = "start", destroyMethod = "close")
    public CuratorFramework curatorFramework() {
        return CuratorFrameworkFactory.newClient(
            connectString,
            sessionTimeoutMs,
            connectionTimeoutMs,
            new ExponentialBackoffRetry(1000, 3)
        );
    }
    
    @Bean
    public CuratorFrameworkClient curatorClient(CuratorFramework client) {
        return new CuratorFrameworkClient(client);
    }
}

常用功能封裝範例

服務註冊與發現

@Component
public class ServiceRegistry {
    
    @Autowired
    private CuratorFramework client;
    
    public void registerService(String serviceName, String address, int port) throws Exception {
        String path = "/services/" + serviceName + "/" + address + ":" + port;
        client.create().creatingParentsIfNeeded()
            .withMode(CreateMode.EPHEMERAL)
            .forPath(path, JSON.toJSONBytes(new ServiceMeta(address, port)));
    }
    
    public List<ServiceMeta> discoverServices(String serviceName) throws Exception {
        List<String> children = client.getChildren().forPath("/services/" + serviceName);
        return children.stream()
            .map(child -> {
                try {
                    byte[] data = client.getData().forPath("/services/" + serviceName + "/" + child);
                    return JSON.parseObject(data, ServiceMeta.class);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            })
            .collect(Collectors.toList());
    }
}

分散式鎖

@Component
public class DistributedLock {
    
    @Autowired
    private CuratorFramework client;
    
    public InterProcessMutex getLock(String lockName) {
        return new InterProcessMutex(client, "/locks/" + lockName);
    }
    
    // 使用範例
    public void executeWithLock(String lockName, Runnable task) {
        InterProcessMutex lock = getLock(lockName);
        try {
            if (lock.acquire(10, TimeUnit.SECONDS)) {
                try {
                    task.run();
                } finally {
                    lock.release();
                }
            }
        } catch (Exception e) {
            throw new RuntimeException("Lock acquire failed", e);
        }
    }
}

配置檔範例 (application.yml)

zookeeper:
  connectString: "zk1:2181,zk2:2181,zk3:2181"
  sessionTimeoutMs: 60000
  connectionTimeoutMs: 15000
  namespace: "myapp"

最佳實踐總結

  • 使用 Curator 而非原生 ZooKeeper API,自動處理重連、會話管理
  • 透過 Spring Bean 管理 CuratorFramework 生命週期
  • 關鍵路徑設計遵循:/namespace/type/identifier
  • 臨時節點用於服務註冊,持久節點用於配置存儲
  • 監聽器註冊注意避免重複註冊造成記憶體洩漏

參考資源

完整實戰範例可參考:ZooKeeper 與 Spring 整合開發實戰

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved