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端口也是可以的