目录

spirngcloud服务注册与发现zookeeper

Eureka已停止更新,可以使用zk代替Eureka,zk是一个分布式协调工具,可以实现注册中心功能。

与Eureka不同的是,使用Eureka需要单独创建一个Eureka服务工程,使用zk则不需要单独创建注册中心服务工程。

Spring cloud整合zk工程如下

注册中心zookeeper

可以使用docker或者本地安装,启动服务

curator官网

版本问题:

20201108124830

注意zk客户端curator依赖的是3.5.3-beta版本,如果使用zk服务器3.4.x会不兼容,这里我们使用3.5.6版本的zk服务器或者将3.5.3-beta版本exclude掉,重新引入3.4.13版本,保持客户端与服务器的zk版本一致就行。

服务提供者

  1. 新建cloud-provider-payment8004

  2. 改pom

    引入zk客户端curator依赖

    1
    2
    3
    4
    5
    
    <!--SpringBoot整合zookeeper客户端-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
            </dependency>
    

    完整pom

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <parent>
            <artifactId>cloud2020</artifactId>
            <groupId>org.eh</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
       
        <artifactId>cloud-provider-payment8004</artifactId>
       
        <dependencies>
            <!--SpringBoot整合zookeeper客户端-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
            </dependency>
            <dependency>
                <groupId>org.eh</groupId>
                <artifactId>cloud-common</artifactId>
                <version>${project.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </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>
        </dependencies>
       
    </project>
    
  3. YML

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    
    server:
      port: 8004
       
    #服务别名----注册zookeeper到注册中心名称
    spring:
      application:
        name: cloud-payment-service
      # zookeeper服务地址
      cloud:
        zookeeper:
          connect-string: localhost:2181
    
  4. 主启动类

    增加注解@EnableDiscoveryClient

    1
    2
    3
    4
    5
    6
    7
    8
    
    @EnableDiscoveryClient // 该注解用于向consul或者zk作为注册中心时注册服务
    @MapperScan("com.eh.cloud2020.payment.dao")
    @SpringBootApplication
    public class PaymentMain8004 {
        public static void main(String[] args) {
            SpringApplication.run(PaymentMain8004.class, args);
        }
    }
    
  5. controller

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    
    package com.eh.cloud2020.payment.controller;
       
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
       
    import java.util.UUID;
       
    @RestController
    @Slf4j
    public class PaymentController {
       
       
        @Value("${server.port}")
        private String serverPort;
       
        @GetMapping("/paymentZK")
        public String paymentZK() {
            return "spring cloud with zk, serverPort:" + serverPort + "\t" + UUID.randomUUID().toString();
        }
    }
    
  6. 启动后访问GET http://localhost:8004/paymentZK

  7. zk客户端查看新增节点信息

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    
    # 根节点下新增了一个节点services
    [zk: localhost:2181(CONNECTED) 3] ls /
    [eh, helloZK, my, services, zookeeper]
    # services下新增了一个服务名cloud-payment-service,也就是在yml中配置的spring.application.name
    [zk: localhost:2181(CONNECTED) 4] ls /services
    [cloud-payment-service]
    # cloud-payment-service节点下也新增了一个节点
    [zk: localhost:2181(CONNECTED) 5] ls /services/cloud-payment-service
    [57a5983a-2d18-4836-8df5-914ed19395b6]
    # 查看节点57a5983a-2d18-4836-8df5-914ed19395b6存储数据
    [zk: localhost:2181(CONNECTED) 8] get /services/cloud-payment-service/57a5983a-2d18-4836-8df5-914ed19395b6
    

    json数据如下

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    
    {
        "name": "cloud-payment-service",
        "id": "57a5983a-2d18-4836-8df5-914ed19395b6",
        "address": "192.168.1.4",
        "port": 8004,
        "sslPort": null,
        "payload": {
            "@class": "org.springframework.cloud.zookeeper.discovery.ZookeeperInstance",
            "id": "application-1",
            "name": "cloud-payment-service",
            "metadata": {
                   
            }
        },
        "registrationTimeUTC": 1604811463163,
        "serviceType": "DYNAMIC",
        "uriSpec": {
            "parts": [
                {
                    "value": "scheme",
                    "variable": true
                },
                {
                    "value": "://",
                    "variable": false
                },
                {
                    "value": "address",
                    "variable": true
                },
                {
                    "value": ":",
                    "variable": false
                },
                {
                    "value": "port",
                    "variable": true
                }
            ]
        }
    }
    

zk创建的节点是临时节点,会话关闭则在心跳检测时间到了之后将临时节点57a5983a-2d18-4836-8df5-914ed19395b6删除

服务消费者

  1. 新建cloud-consumerzk-order80

  2. 改pom

    和服务提供者一模一样

  3. YML

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    
    server:
      port: 80
       
    #服务别名----注册zookeeper到注册中心名称
    spring:
      application:
        name: cloud-consumerzk-order
      # zookeeper服务地址
      cloud:
        zookeeper:
          connect-string: localhost:2181
    
  4. 主启动

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

    1. 注册RestTemplate,和之前一样

    2. OrderZkController,服务名称就是提供者的spring.application.name

       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      
      package com.eh.cloud2020.order.controller;
            
      import org.springframework.web.bind.annotation.GetMapping;
      import org.springframework.web.bind.annotation.RestController;
      import org.springframework.web.client.RestTemplate;
            
      @RestController
      public class OrderZkController {
            
          private final static String PAYMENT_URL = "http://cloud-payment-service";
            
          private final RestTemplate restTemplate;
            
          public OrderZkController(RestTemplate restTemplate) {
              this.restTemplate = restTemplate;
          }
            
          @GetMapping("/order/paymentZK")
          public String getPaymentById() {
              return restTemplate.getForObject(PAYMENT_URL + "/paymentZK/", String.class);
          }
      }
      
  6. 验证

    1. 服务调用

    2. zk节点变化情况

      1
      2
      
      [zk: localhost:2181(CONNECTED) 13] ls /services
      [cloud-consumerzk-order, cloud-payment-service]