1. 程式人生 > 實用技巧 >服務註冊之Zookeeper

服務註冊之Zookeeper

一、Zookeeper介紹

Zookeeper是什麼

ZooKeeper 是一個分散式的,開放原始碼的分散式應用程式協調服務,是 GoogleChubby 專案一個開源的實現,是 HadoopHbase 的重要元件。它是一個為分散式應用提供一致性服務的軟體,提供的功能包括:配置維護、域名服務、分散式同步、組服務等。

二、Zookeeper應用

2.1 安裝 Zookeeper

這裡我們採用 Docker 形式安裝:

# 拉取映象
docker pull zookeeper:3.5    
# 建立容器
docker run --name zookeeper -p 2181:2181 -d zookeeper:3.5
# 進入容器
docker exec -it zookeeper /bin/bash 
# 進入 zookeeper 目錄   
cd bin
# 通過命令列開啟 zookeeper
. zkCli.sh   
# 檢視當前服務列表,可以看到只有一個 zookeeper 服務
ls /
[zookeeper]    

2.2 建立 zookeeper-client 模組

  1. 新建 zookeeper-client 模組 新增相關依賴:
<properties>
    <java.version>1.8</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <spring-boot.version>2.3.0.RELEASE</spring-boot.version>
    <spring-cloud.version>Hoxton.SR4</spring-cloud.version>
</properties>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>${spring-boot.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>
  1. 在配置檔案 resources/application. yml 中新增相關配置:
server:
  # 指定執行埠
  port: 8800

spring:
  application:
    # 指定服務名稱
    name: zookeeper-client
  cloud:
    zookeeper:
      # zookeeper服務地址
      connect-string: 192.168.205.10:2181

management:
  endpoint:
    health:
      # 顯示應用健康資訊
      show-details: always
  endpoints:
    web:
      exposure:
        include: "*"
  1. 在啟動類上新增 @EnableDiscoveryClient 註解
@EnableDiscoveryClient
@SpringBootApplication
public class ZookeeperClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(ZookeeperClientApplication.class, args);
    }

}

4) 啟動專案,進入 Zookeeper 命令列檢視:

[zk: localhost:2181(CONNECTED) 1] ls /
[services, zookeeper]
[zk: localhost:2181(CONNECTED) 2] ls /services
[zookeeper-client]