本快速入门将介绍如何使用 Spring Cloud Zookeeper 进行服务发现和分布式配置。Spring中文文档

首先,在您的计算机上运行 Zookeeper。然后,您可以访问它并将其用作 Spring Cloud Zookeeper 的服务注册表和配置源。Spring中文文档

发现客户端使用情况

若要在应用程序中使用这些功能,可以将其构建为依赖于 和 的 Spring Boot 应用程序。 添加依赖项的最便捷方法是使用 Spring Boot 启动器:。 建议使用依赖项管理和 . 以下示例显示了典型的 Maven 配置:spring-cloud-zookeeper-corespring-cloud-zookeeper-discoveryorg.springframework.cloud:spring-cloud-starter-zookeeper-discoveryspring-boot-starter-parentSpring中文文档

pom.xml
<project>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>{spring-boot-version}</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>

  <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-test</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <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>
    </dependencies>
  </dependencyManagement>
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>

以下示例显示了典型的 Gradle 设置:Spring中文文档

build.gradle
plugins {
  id 'org.springframework.boot' version ${spring-boot-version}
  id 'io.spring.dependency-management' version ${spring-dependency-management-version}
  id 'java'
}

repositories {
  mavenCentral()
}

dependencies {
  implementation 'org.springframework.cloud:spring-cloud-starter-zookeeper-discovery'
  testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
dependencyManagement {
  imports {
    mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
  }
}
根据您使用的版本,您可能需要调整项目中使用的 Apache Zookeeper 版本。 您可以在安装 Zookeeper 部分阅读有关它的更多信息。

现在,您可以创建标准的 Spring Boot 应用程序,例如以下 HTTP 服务器:Spring中文文档

@SpringBootApplication
@RestController
public class Application {

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

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

}

当此 HTTP 服务器运行时,它将连接到在默认本地端口 (2181) 上运行的 Zookeeper。 若要修改启动行为,可以使用 更改 Zookeeper 的位置,如以下示例所示:application.propertiesSpring中文文档

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

现在,您可以使用 、 或 从 Zookeeper 检索服务和实例数据,如以下示例所示:DiscoveryClient@LoadBalanced RestTemplate@LoadBalanced WebClient.BuilderSpring中文文档

@Autowired
private DiscoveryClient discoveryClient;

public String serviceUrl() {
    List<ServiceInstance> list = discoveryClient.getInstances("STORES");
    if (list != null && list.size() > 0 ) {
        return list.get(0).getUri().toString();
    }
    return null;
}
根据您使用的版本,您可能需要调整项目中使用的 Apache Zookeeper 版本。 您可以在安装 Zookeeper 部分阅读有关它的更多信息。

分布式配置用法

若要在应用程序中使用这些功能,可以将其构建为依赖于 和 的 Spring Boot 应用程序。 添加依赖项的最便捷方法是使用 Spring Boot 启动器:。 建议使用依赖项管理和 . 以下示例显示了典型的 Maven 配置:spring-cloud-zookeeper-corespring-cloud-zookeeper-configorg.springframework.cloud:spring-cloud-starter-zookeeper-configspring-boot-starter-parentSpring中文文档

pom.xml
<project>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>{spring-boot-version}</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>

  <dependencies>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-zookeeper-config</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <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>
    </dependencies>
  </dependencyManagement>
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>

以下示例显示了典型的 Gradle 设置:Spring中文文档

build.gradle
plugins {
  id 'org.springframework.boot' version ${spring-boot-version}
  id 'io.spring.dependency-management' version ${spring-dependency-management-version}
  id 'java'
}

repositories {
  mavenCentral()
}

dependencies {
  implementation 'org.springframework.cloud:spring-cloud-starter-zookeeper-config'
  testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
dependencyManagement {
  imports {
    mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
  }
}
根据您使用的版本,您可能需要调整项目中使用的 Apache Zookeeper 版本。 您可以在安装 Zookeeper 部分阅读有关它的更多信息。

现在,您可以创建标准的 Spring Boot 应用程序,例如以下 HTTP 服务器:Spring中文文档

@SpringBootApplication
@RestController
public class Application {

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

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

}

应用程序从 Zookeeper 检索配置数据。Spring中文文档

如果使用 Spring Cloud Zookeeper Config,则需要设置属性才能绑定到 Zookeeper。 您可以在 Spring Boot Config Data Import 部分阅读有关它的更多信息。spring.config.import
根据您使用的版本,您可能需要调整项目中使用的 Apache Zookeeper 版本。 您可以在安装 Zookeeper 部分阅读有关它的更多信息。
如果使用 Spring Cloud Zookeeper Config,则需要设置属性才能绑定到 Zookeeper。 您可以在 Spring Boot Config Data Import 部分阅读有关它的更多信息。spring.config.import