其他分享
首页 > 其他分享> > docker 安装 zookeeper

docker 安装 zookeeper

作者:互联网

1. 安装zookeeper

docker pull zookeeper:3.7.0
docker run --name zookeeper -p 2181:2181 --restart always -d zookeeper:3.7.0

 

springboot 引入zookeeper

1. pom

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.6.RELEASE</version>
</parent>

<dependencies>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!--引入zk客户端依赖-->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.zookeeper</groupId>
        <artifactId>zookeeper</artifactId>
        <version>3.4.8</version>
    </dependency>
    <dependency>
        <groupId>org.apache.curator</groupId>
        <artifactId>curator-framework</artifactId>
        <version>4.0.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.curator</groupId>
        <artifactId>curator-recipes</artifactId>
        <version>4.0.0</version>
    </dependency>
    <!--zk依赖结束-->

</dependencies>

2.  配置类


import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ZookeeperConfig {

    private String host = "122.9.143.126:2181";

    @Bean
    public CuratorFramework curatorFramework() {
        CuratorFramework curatorFramework = CuratorFrameworkFactory.builder()
                .connectString(host)
                .sessionTimeoutMs(5000)
                .retryPolicy(new ExponentialBackoffRetry(1000, 3))
                .build();
        curatorFramework.start();
        return curatorFramework;
    }
}

 

3. 使用


import org.apache.curator.framework.CuratorFramework;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping
public class Democontroller {

    @Autowired
    private CuratorFramework curatorFramework;

    @GetMapping("test01")
    public String test01() throws Exception {
        curatorFramework.create()
                // 如果有子节点会递归创建
                .creatingParentsIfNeeded()
                // 设置为持久节点
                .withMode(CreateMode.PERSISTENT).forPath("/abc");
        // 检查节点是否存在
        Stat stat = curatorFramework.checkExists().forPath("/abc");
        System.out.println("stat = " + stat);
        return "this is test01 method";
    }


}

 

标签:zookeeper,curator,springframework,import,apache,org,docker,安装
来源: https://www.cnblogs.com/txt1024/p/16695068.html