1、注册中心Zookeeper

1、zookeeper是一个分布式协调工具,可以实现注册中心功能

2、

2、服务提供者

1、pom

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>cloud2021</artifactId>
  7. <groupId>com.tfjy.springcloud</groupId>
  8. <version>1.0-SNAPSHOT</version>
  9. </parent>
  10. <modelVersion>4.0.0</modelVersion>
  11. <artifactId>cloud-provider-payment8004</artifactId>
  12. <dependencies>
  13. <!-- SpringBoot整合Web组件 -->
  14. <dependency>
  15. <groupId>org.springframework.boot</groupId>
  16. <artifactId>spring-boot-starter-web</artifactId>
  17. </dependency>
  18. <!-- SpringBoot整合zookeeper客户端 -->
  19. <dependency>
  20. <groupId>org.springframework.cloud</groupId>
  21. <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
  22. <!--先排除自带的zookeeper3.5.3-->
  23. <exclusions>
  24. <exclusion>
  25. <groupId>org.apache.zookeeper</groupId>
  26. <artifactId>zookeeper</artifactId>
  27. </exclusion>
  28. </exclusions>
  29. </dependency>
  30. <!--添加zookeeper3.4.9版本-->
  31. <dependency>
  32. <groupId>org.apache.zookeeper</groupId>
  33. <artifactId>zookeeper</artifactId>
  34. <version>3.4.9</version>
  35. </dependency>
  36. <dependency>
  37. <groupId>org.springframework.boot</groupId>
  38. <artifactId>spring-boot-devtools</artifactId>
  39. <scope>runtime</scope>
  40. <optional>true</optional>
  41. </dependency>
  42. <dependency>
  43. <groupId>org.projectlombok</groupId>
  44. <artifactId>lombok</artifactId>
  45. <optional>true</optional>
  46. </dependency>
  47. <dependency>
  48. <groupId>org.springframework.boot</groupId>
  49. <artifactId>spring-boot-starter-test</artifactId>
  50. <scope>test</scope>
  51. </dependency>
  52. </dependencies>
  53. </project>

2、YML

  1. #8004表示注册到zookeeper服务器的支付服务提供者端口号
  2. server:
  3. port: 8004
  4. #服务别名----注册zookeeper到注册中心名称
  5. spring:
  6. application:
  7. name: cloud-provider-payment
  8. cloud:
  9. zookeeper:
  10. connect-string: 121.5.175.59:2181

3、主启动

  1. package com.atguigu.springcloud;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  5. @SpringBootApplication
  6. @EnableDiscoveryClient //该注解用于向使用consul或者zookeeper作为注册中心时,注册服务。
  7. public class PaymentMain8004 {
  8. public static void main(String[] args) {
  9. SpringApplication.run(PaymentMain8004.class,args);
  10. }
  11. }

4、测试

  1. http://localhost:8004/payment/zk

输入上面的网址

  1. springCloud with zookeeper:8004 a152a333-2bd1-4b26-a22b-bc2fe63b0d68

5、从zookeeper中查看

  1. [root@VM-0-16-centos bin]# ./zkCli.sh

然后查看根目录

  1. [zk: localhost:2181(CONNECTED) 0] ls /
  1. [services, zookeeper]

image.png

6、服务节点是临时节点还是持久节点

image.png
可以进行检测,当我们停掉了服务之后
image.png
立马检测不到了。

所以说就是临时节点

3、服务消费者