Quick Start

本快速入门指南介绍了如何将 Spring Cloud Zookeeper 用于服务发现和分布式配置。

This quick start walks through using Spring Cloud Zookeeper for Service Discovery and Distributed Configuration.

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

First, run Zookeeper on your machine. Then you can access it and use it as a Service Registry and Configuration source with Spring Cloud Zookeeper.

Discovery Client Usage

要使用应用程序中的这些特性,你可以将其构建为一个 Spring Boot 应用程序,该应用程序依赖于 spring-cloud-zookeeper-corespring-cloud-zookeeper-discovery。添加依赖项的最便捷方法是使用 Spring Boot 启动器: org.springframework.cloud:spring-cloud-starter-zookeeper-discovery。我们建议使用依赖管理和 spring-boot-starter-parent。以下示例展示了一个典型的 Maven 配置:

To use these features in an application, you can build it as a Spring Boot application that depends on spring-cloud-zookeeper-core and spring-cloud-zookeeper-discovery. The most convenient way to add the dependency is with a Spring Boot starter: org.springframework.cloud:spring-cloud-starter-zookeeper-discovery. We recommend using dependency management and spring-boot-starter-parent. The following example shows a typical Maven configuration:

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 设置:

The following example shows a typical Gradle setup:

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 版本。您可以在 Install Zookeeper section 中了解有关它的更多信息。

Depending on the version you are using, you might need to adjust Apache Zookeeper version used in your project. You can read more about it in the Install Zookeeper section.

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

Now you can create a standard Spring Boot application, such as the following HTTP server:

@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。要修改启动行为,你可以使用 application.properties 更改 Zookeeper 的位置,如下例所示:

When this HTTP server runs, it connects to Zookeeper, which runs on the default local port (2181). To modify the startup behavior, you can change the location of Zookeeper by using application.properties, as shown in the following example:

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

现在你可以使用 DiscoveryClient@LoadBalanced RestTemplate@LoadBalanced WebClient.Builder 从 Zookeeper 检索服务和实例数据,如下例所示:

You can now use DiscoveryClient, @LoadBalanced RestTemplate, or @LoadBalanced WebClient.Builder to retrieve services and instances data from Zookeeper, as shown in the following example:

@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;
}

Distributed Configuration Usage

要使用应用程序中的这些特性,你可以将其构建为一个 Spring Boot 应用程序,该应用程序依赖于 spring-cloud-zookeeper-corespring-cloud-zookeeper-config。添加依赖项的最便捷方法是使用 Spring Boot 启动器: org.springframework.cloud:spring-cloud-starter-zookeeper-config。我们建议使用依赖管理和 spring-boot-starter-parent。以下示例展示了一个典型的 Maven 配置:

To use these features in an application, you can build it as a Spring Boot application that depends on spring-cloud-zookeeper-core and spring-cloud-zookeeper-config. The most convenient way to add the dependency is with a Spring Boot starter: org.springframework.cloud:spring-cloud-starter-zookeeper-config. We recommend using dependency management and spring-boot-starter-parent. The following example shows a typical Maven configuration:

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 设置:

The following example shows a typical Gradle setup:

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 版本。您可以在 Install Zookeeper section 中了解有关它的更多信息。

Depending on the version you are using, you might need to adjust Apache Zookeeper version used in your project. You can read more about it in the Install Zookeeper section.

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

Now you can create a standard Spring Boot application, such as the following HTTP server:

@SpringBootApplication
@RestController
public class Application {

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

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

}

该应用程序从 Zookeeper 检索配置数据。

The application retrieves configuration data from Zookeeper.

如果您使用 Spring Cloud Zookeeper Config,则需要设置 spring.config.import 属性才能绑定到 Zookeeper。您可以在 Spring Boot Config Data Import section 中了解有关它的更多信息。

If you use Spring Cloud Zookeeper Config, you need to set the spring.config.import property in order to bind to Zookeeper. You can read more about it in the Spring Boot Config Data Import section.