【SpringCloud】08 Zookeeper02 节点概念 & 订单注册

Zookeeper的节点概念:

总体两种:持久节点 & 临时节点

细分则为:无序号的持久节点与临时节点,带序号的持久节点与临时节点

 

那么,我们上一篇把支付模块服务注册的ZK节点,是算持久节点还是临时节点呢?

现在我们关闭服务,再查看节点:

这里经过大概50秒的样子,才销毁了节点:

服务关闭之后并不会马上移除节点,ZK会持续再给服务发包,检查是否有回应,持续到一定时间始终没有回应才会移除

所以结论是,服务节点是一个临时节点

 

现在重启服务,再次查看:

节点也不再是原有的节点了

 

订单模块注册ZK:

1、新建模块,模块名称: Consumer-Order-Zookeeper-Port-80 

2、导入ZK模块,其他和之前80模块一致

被这个starter坑死了,Maven的提示就是没给Starter

<?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>SpringCloud-ATGG-2020</artifactId>
        <groupId>cn.dzz.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>Consumer-Order-Zookeeper-Port-80</artifactId>

    <dependencies>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
        </dependency>

        <dependency>
            <groupId>cn.dzz.springcloud</groupId>
            <artifactId>API-Commons</artifactId>
            <version>${project.version}</version> <!--<version>1.0-SNAPSHOT</version>-->
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </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>
    </dependencies>

</project>

3、编写Application.yml

server:
  port: 80

spring:
  application:
    name: consumer-order-service
  cloud:
    zookeeper:
      connect-string: zk所在IP地址:2181

4、编写启动类:

发现@EnableDiscoveryClient注解还打不上,需要加一个依赖:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-commons</artifactId>
</dependency>

然后再编写启动类:

package cn.dzz.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/**
 * @author DaiZhiZhou
 * @file SpringCloud-ATGG-2020
 * @create 2020-08-26 11:16
 */
@SpringBootApplication
@EnableDiscoveryClient
public class ConsumerOrderZookeeperPort80Application {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerOrderZookeeperPort80Application.class, args);
    }
}

5、编写ZK控制器:

package cn.dzz.springcloud.controller;

import cn.dzz.springcloud.component.JsonResult;
import cn.dzz.springcloud.entity.Payment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * @author ArkD42
 * @file SpringCloud-ATGG-2020
 * @create 2020年08月24日 22:47
 */

@Slf4j
@RestController
@RequestMapping("zk/order")
public class ZookeeperOrderController {

    @Resource
    private RestTemplate restTemplate;

    public static final String PAYMENT_URL = "http://consumer-order-service";

    @RequestMapping("consumer/payment")  // zk/order/consumer/payment
    public String zookeeperPaymentInfo(){
        return restTemplate.getForObject(PAYMENT_URL + "/payment", String.class);
    }

}

6、启动服务:

访问接口:  http://localhost/zk/order/consumer/payment 

 

posted @ 2020-08-26 16:57  emdzz  阅读(175)  评论(0)    收藏  举报