快速上手
本快速入门教程将引导你了解如何使用 Spring Cloud Zookeeper 进行服务发现和分布式配置。
首先,在您的机器上运行 Zookeeper。然后,您可以使用 Spring Cloud Zookeeper 访问它,并将其用作服务注册中心和配置源。
Discovery Client 的使用方法
要在应用程序中使用这些功能,您可以将其构建为一个 Spring Boot 应用程序,该应用程序依赖于 spring-cloud-zookeeper-core 和 spring-cloud-zookeeper-discovery。添加依赖项最方便的方法是使用 Spring Boot starter:org.springframework.cloud:spring-cloud-starter-zookeeper-discovery。我们建议使用依赖管理和 spring-boot-starter-parent。以下示例显示了一个典型的 Maven 配置:
<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 设置:
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 服务器:
@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)上。要修改启动行为,你可以通过使用 application.properties 来更改 ZooKeeper 的位置,如下例所示:
spring:
cloud:
zookeeper:
connect-string: localhost:2181
现在,你可以使用 DiscoveryClient、@LoadBalanced RestTemplate 或 @LoadBalanced WebClient.Builder 从 ZooKeeper 中检索服务和实例数据,如以下示例所示:
@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;
}
分布式配置使用方法
为了在应用程序中使用这些功能,您可以将其构建为一个 Spring Boot 应用程序,该应用程序依赖于 spring-cloud-zookeeper-core 和 spring-cloud-zookeeper-config。添加依赖项最方便的方法是使用 Spring Boot starter:org.springframework.cloud:spring-cloud-starter-zookeeper-config。我们建议使用依赖管理和 spring-boot-starter-parent。以下示例显示了一个典型的 Maven 配置:
<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 设置:
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 服务器:
@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 Cloud Zookeeper Config,您需要设置 spring.config.import 属性才能绑定到 Zookeeper。您可以在 Spring Boot Config Data Import 部分 中了解更多信息。