docker下安装zookeeper并实现springboot+zookeeper注册中心功能

一、docker下安装单zookeeper简易教程

1、docker search zookeeper 查询镜像

2、docker pull zookeeper 拉取镜像

3、查看镜像id,并启动镜像,映射端口号2181 

    docker run -d  -p 2181:2181 --name myzookeeper --restart always  镜像ID 

4、 查看运行状态的zookeeper镜像

5、可以通过 docker logs -f  myzookeeper 来查看zookeeper日志

6、通过命令进入镜像 docker exec -it  272378ab4887(容器id)   /bin/bash

7、此时zookeeper服务端已启动,需要启动客户端去查看注册进来的服务:进入bin目录,启动zkCli.sh

8、通过zookeeper客户端查看注册进来的服务

二、springboot注册zookeeper配置中心 代码部分

1、pom文件部分

<dependencies>
        <!-- SpringBoot整合Web组件 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency><!-- 引入自己定义的api通用包,可以使用Payment支付Entity -->
            <groupId>com.king.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</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>-->
    </dependencies>

2、yml文件

server:
  port: 8004

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

3、主启动类

package com.king.springcloud;

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

/**
 * created by king on 2020/4/14 2:02 下午
 */
@SpringBootApplication
@EnableDiscoveryClient
public class PaymentMain8004 {
    public static void main(String[] args) {
        SpringApplication.run(PaymentMain8004.class, args);
    }
}

4、controller测试类

package com.king.springcloud.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * created by king on 2020/4/14 2:03 下午
 */
@RestController
@Slf4j
public class PaymentController {
    @Value("${server.port}")
    private String serverPort;

    @RequestMapping(value="/payment/zk")
    public String paymentzk(){
        return "springboot with zk serverPort:"+serverPort+"\t" + UUID.randomUUID().toString();
    }

}

注意:zookeeper上的services节点是临时节点,不是持久节点,只要服务一停,节点就会被删除,重新启动微服务进行注册,会产生一个新的临时节点,和eureka注册中心的自我保护机制不太一样。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章