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註冊中心的自我保護機制不太一樣。

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