知識庫 / Spring / Spring Cloud RSS 訂閱

Spring Cloud Zookeeper 入門指南

Spring Cloud
HongKong
6
02:35 PM · Dec 06 ,2025

1. 簡介

本文將介紹 Zookeeper 以及它在服務發現中的應用,服務發現是雲環境中服務知識的集中管理。

Spring Cloud Zookeeper 提供 Spring Boot 應用中對 Apache Zookeeper 的集成,通過自動配置和綁定到 Spring 環境。

2. 服務發現配置

我們將創建兩個應用程序:

  • 一個應用程序將提供服務(在本文檔中稱為 服務提供者
  • 另一個應用程序將消費該服務(稱為 服務消費者

Apache Zookeeper 將作為我們服務發現配置中的協調器。 Apache Zookeeper 的安裝説明可以在以下 鏈接 中找到。

3. 服務提供商註冊

我們將通過添加 spring-cloud-starter-zookeeper-discovery 依賴項並使用註解 @EnableDiscoveryClient 在主應用程序中來啓用服務註冊。

下面,我們將逐步演示用於返回“Hello World!”響應的GET請求的服務的註冊過程。

3.1. Maven 依賴

首先,我們需要添加以下 Maven 依賴:<a href="https://mvnrepository.com/search?q=spring-cloud-starter-zookeeper-discovery">spring-cloud-starter-zookeeper-discovery</a>, <a href="https://mvnrepository.com/artifact/org.springframework/spring-web">spring-web</a>, <a href="https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-dependencies">spring-cloud-dependencies</a>, 以及 <a href="https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter">spring-boot-starter</a> 到我們的 pom.xml 文件中:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter</artifactId>
	<version>2.2.6.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
	<artifactId>spring-web</artifactId>
        <version>5.1.14.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
     </dependency>
</dependencies>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Hoxton.SR4</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

3.2. 服務提供者註解

接下來,我們將主類註解為 @EnableDiscoveryClient。這將使 HelloWorld 應用程序具有發現能力:

@SpringBootApplication
@EnableDiscoveryClient
public class HelloWorldApplication {
    public static void main(String[] args) {
        SpringApplication.run(HelloWorldApplication.class, args);
    }
}

以及一個簡單的控制器:

@GetMapping("/helloworld")
public String helloWorld() {
    return "Hello World!";
}

3.3. YAML 配置

現在,讓我們創建一個名為 Application.yml 的 YAML 文件,用於配置應用程序的日誌級別並告知 Zookeeper 該應用程序啓用了發現功能。

應用程序名稱對於註冊到 Zookeeper 至關重要。稍後,在服務消費者中,一個 feign 客户端將在服務發現期間使用該名稱。

spring:
  application:
    name: HelloWorld
  cloud:
    zookeeper:
      discovery:
        enabled: true
logging:
  level:
    org.apache.zookeeper.ClientCnxn: WARN

Spring Boot 應用程序默認在 2181 端口查找 Zookeeper。如果 Zookeeper 位於其他位置,則需要添加以下配置:

spring:
  cloud:
    zookeeper:
      connect-string: localhost:2181

4. 服務消費者

現在我們將創建一個 REST 服務消費者,並使用 Spring Netflix Feign 客户端進行註冊。

4.1. Maven 依賴

首先,將以下 Maven 依賴添加到我們的 pom.xml 文件中:spring-cloud-starter-zookeeper-discovery, spring-web, spring-cloud-dependencies, spring-boot-starter-actuator, 以及 spring-cloud-starter-feign

<dependencies>
    <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>
        <version>2.2.6.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-feign</artifactId>
    </dependency>
</dependencies>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Hoxton.SR4</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

4.2. 服務消費者註解

與服務提供者類似,我們將使用 @EnableDiscoveryClient 標註主類,使其具有發現能力:

@SpringBootApplication
@EnableDiscoveryClient
public class GreetingApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(GreetingApplication.class, args);
    }
}

4.3. 使用 Feign 客户端發現服務

我們將使用 Spring Cloud Feign Integration,這是一個由 Netflix 開發的項目,它允許您定義聲明式的 REST 客户端。您只需聲明 URL 的格式,Feign 負責連接到 REST 服務。

Feign 客户端 通過 spring-cloud-starter-feign 包導入。 我們將通過註解一個 @Configuration 類,並使用 @EnableFeignClients 啓用 Feign 客户端。

最後,我們使用 @FeignClient(“service-name”) 註解一個接口,並將其自動注入到我們的應用程序中,以便我們能夠以編程方式訪問該服務。

在註解 @FeignClient(name = “HelloWorld”) 中,我們引用了之前創建的服務生產者所使用的 service-name

@Configuration
@EnableFeignClients
@EnableDiscoveryClient
public class HelloWorldClient {
 
    @Autowired
    private TheClient theClient;

    @FeignClient(name = "HelloWorld")
    interface TheClient {
 
        @RequestMapping(path = "/helloworld", method = RequestMethod.GET)
        @ResponseBody
	String helloWorld();
    }
    public String HelloWorld() {
        return theClient.HelloWorld();
    }
}

4.4. 控制器類

以下是一個簡單的服務控制器類,它將通過注入的接口 helloWorldClient 對象,調用服務提供者函數,並通過 Feign 客户端類消費服務(服務細節通過服務發現抽象),並顯示響應結果:

@RestController
public class GreetingController {
 
    @Autowired
    private HelloWorldClient helloWorldClient;

    @GetMapping("/get-greeting")
    public String greeting() {
        return helloWorldClient.helloWorld();
    }
}

4.5. YAML 配置

接下來,我們創建一個與之前類似的 YAML 文件 Application.yml,用於配置應用程序的日誌級別:

logging:
  level:
    org.apache.zookeeper.ClientCnxn: WARN

該應用程序默認在 2181 端口查找 Zookeeper。如果 Zookeeper 位於其他位置,則需要添加配置。

spring:
  cloud:
    zookeeper:
      connect-string: localhost:2181

5. 測試部署

部署後,HelloWorld REST 服務會向 Zookeeper 註冊自身。然後,作為服務消費者的 Greeting 服務使用 Feign 客户端調用 HelloWorld 服務。

現在,我們可以構建並運行這兩個服務。

最後,我們將瀏覽器指向 http://localhost:8083/get-greeting,它應該顯示:

Hello World!

6. 結論

在本文中,我們看到了如何使用 Spring Cloud Zookeeper 進行服務發現,並在 Zookeeper 服務器上註冊了名為 HelloWorld 的服務,以便 Greeting 服務可以使用 Feign Client 發現和消費,而無需知道其具體位置信息。

user avatar
0 位用戶收藏了這個故事!
收藏

發佈 評論

Some HTML is okay.