快速入門

本快速入門將引導您使用 Spring Cloud Zookeeper 進行服務發現和分散式組態。

首先,在您的機器上執行 Zookeeper。然後您可以存取它並將其用作服務註冊中心和組態來源,搭配 Spring Cloud Zookeeper。

服務發現用戶端使用方式

若要在應用程式中使用這些功能,您可以將其建置為 Spring Boot 應用程式,該應用程式依賴 spring-cloud-zookeeper-corespring-cloud-zookeeper-discovery。新增相依性的最方便方法是使用 Spring Boot Starter:org.springframework.cloud:spring-cloud-starter-zookeeper-discovery。我們建議使用相依性管理和 spring-boot-starter-parent。以下範例顯示典型的 Maven 組態

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 設定

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 伺服器

@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 的位置,如下列範例所示

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-corespring-cloud-zookeeper-config。新增相依性的最方便方法是使用 Spring Boot Starter:org.springframework.cloud:spring-cloud-starter-zookeeper-config。我們建議使用相依性管理和 spring-boot-starter-parent。以下範例顯示典型的 Maven 組態

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 設定

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 伺服器

@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 組態資料匯入章節中閱讀更多相關資訊。