Zookeeper入門操作

語言: CN / TW / HK

服務註冊

根據上一講Eureka基礎繼續講解使用,其實原理都是一樣都是註冊服務。

zookepper

Eureka停止更新了你怎麼辦?

只要技術有用處就不怕他會消失

zookeeper是一個分佈式協調工具,可以實現註冊中心功能

我們用zookeeper服務器取代Eureka服務器,zk作為服務註冊中心

首先應該在虛擬機下載zk,關閉防火牆可以嘗試ping地址查看是否能相互ping通即可
創建支付模塊cloud-provider-payment8004
  • 配置pom

xml <dependencies> <!-- SpringBoot整合Web組件 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency><!-- 引入自己定義的api通用包,可以使用Payment支付Entity --> <groupId>com.cloud</groupId> <artifactId>cloud-api-commons</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <!-- SpringBoot整合zookeeper客户端 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId> <!--先排除自帶的zookeeper3.5.3--> <exclusions> <exclusion> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> </exclusion> </exclusions> </dependency> <!--添加zookeeper3.4.9版本--> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.9</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.7</version> </dependency> </dependencies>

  • 配置yml,這裏我們發現其實註冊器配置基本相同

```yaml server: port: 8004

spring: application: name: cloud-provider-payment cloud: zookeeper: connect-string: 116.62.12.158:2181 ```

  • 啟動類註解 @EnableDiscoveryClient

java @SpringBootApplication @EnableDiscoveryClient public class PaymentMain8004 { public static void main(String[] args) { SpringApplication.run(PaymentMain8004.class,args); } }

  • 寫Controller

```java @RestController public class PayMentController { @Value("${server.port}") private String port;

  @GetMapping("payment/zk")
  public String payment(){
      return "zookeeper" + port+ UUID.randomUUID().toString();
  }

}

```

創建客服端模塊cloud-consumer-zk-order80
  • 還是按步驟來 pom配置

xml <dependencies> <!-- SpringBoot整合Web組件 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency><!-- 引入自己定義的api通用包,可以使用Payment支付Entity --> <groupId>com.cloud</groupId> <artifactId>cloud-api-commons</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <!-- SpringBoot整合zookeeper客户端 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId> <!--先排除自帶的zookeeper3.5.3--> <exclusions> <exclusion> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> </exclusion> </exclusions> </dependency> <!--添加zookeeper3.4.9版本--> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.9</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.7</version> </dependency> </dependencies>

  • 配置yml

```yml server: port: 80

spring: application: name: cloud-order-service cloud: zookeeper: connect-string: 116.62.12.158:2181 ```

  • 啟動類註解

```java

@SpringBootApplication @EnableDiscoveryClient public class ZkOrder80 { public static void main(String[] args) { SpringApplication.run(ZkOrder80.class,args); } } ```

  • 配置RestTemplate

```java @Configuration public class Application {

  @Bean
  @LoadBalanced
  public RestTemplate getRestTemplate()
  {
      RestTemplate restTemplate = new RestTemplate();
      restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
      return restTemplate;
  }

} ```

  • controller

```java @RestController public class OrderController {

  @Resource
  private RestTemplate restTemplate;

  private static final String   PAYMENT_URL = "http://cloud-provider-payment";
  @GetMapping("order/zk")
  public String orderzk(){
      String result = restTemplate.getForObject(PAYMENT_URL+"/payment/zk", String.class);
      return result;
  }

} ```

啟動測試

zookeeper操作

​ 進入根目錄下cd /usr/local/zookeeper/bin

​ 開啟服務./zkServer.sh start

​ 連接./zkCli.sh

​ 顯示內容 ls / 這裏如果你不開啟80和8001是隻有zookeeper一個的

zookeeper1.jpg 查看services裏面有什麼ls / 這裏就看到了我們所註冊的服務

zookeeper2.jpg

    在進入一層ls /services/cloud-order-service   看到一串流水號

zookeeper.jpg 我們get一下獲取流水號信息get /services/cloud-order-service/32717337-c70c-4653-9cd0-1c875bc4a9ad

zookeeper3.jpg ​ 就獲得的一串json,我們找個人工具解析一下更容易看

zookeeper4.jpg

這是就可以清楚看到我們所註冊的服務了,測試80端口也是可以的