对于最新稳定版,请使用Spring Cloud Zookeeper 5.0.0spring-doc.cadn.net.cn

快速入门

这份快速入门教程介绍如何使用Spring Cloud Zookeeper进行服务发现和分布式配置。spring-doc.cadn.net.cn

首先,在你的机器上运行Zookeeper。然后你可以访问它,并用Spring Cloud Zookeeper作为服务注册表和配置源使用。spring-doc.cadn.net.cn

发现客户端使用

要在应用中使用这些功能,你可以将其构建为依赖于春云-Zookeeper -核心春云-Zookeeper -发现. 添加依赖最方便的方法是使用 Spring Boot Starters:org.springframework.cloud:spring-cloud-starter-zookeeper-discovery. 我们建议使用依赖管理和Spring靴启动父. 以下示例展示了典型的Maven配置:spring-doc.cadn.net.cn

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-doc.cadn.net.cn

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-doc.cadn.net.cn

@SpringBootApplication
@RestController
public class Application {

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

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

}

当这个 HTTP 服务器运行时,它连接到 Zookeeper,而 Zookeeper 运行在默认的本地端口(2181)。 要修改启动行为,可以通过以下方式更改 Zookeeper 的位置application.properties如下例所示:spring-doc.cadn.net.cn

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

你现在可以使用发现客户端,@LoadBalanced 休息模板@LoadBalanced WebClient.Builder从Zookeeper获取服务和实例数据,如下示例所示:spring-doc.cadn.net.cn

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

分布式配置使用

要在应用中使用这些功能,你可以将其构建为依赖于春云-Zookeeper -核心Spring-cloud-Zookeeper-config. 添加依赖最方便的方法是使用 Spring Boot Starters:org.springframework.cloud:spring-cloud-starter-zookeeper-config. 我们建议使用依赖管理和Spring靴启动父. 以下示例展示了典型的Maven配置:spring-doc.cadn.net.cn

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-doc.cadn.net.cn

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-doc.cadn.net.cn

@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-doc.cadn.net.cn

如果你用的是 Spring Cloud Zookeeper 配置,你需要设置spring.config.import属性以绑定到 Zookeeper。你可以在 Spring Boot 配置数据导入部分了解更多。