SpringBoot+zk+dubbo架构实践(二):SpringBoot 集成 zookeeper

SpringBoot+zk+dubbo架构实践(二):SpringBoot 集成 zookeeper_第1张图片
springboot+ zookeeper.jpeg

上一篇:SpringBoot+zk+dubbo架构实践(一):本地部署zookeeper

前言

这是第二篇了,本篇我们完成两件事情。
1、搭建SpringBoot 框架;
2、基于spring boot框架访问zookeeper。

搭建SpringBoot框架

其实之前“IT实战联盟”已经出了该系列文章,今天我们简单搭建一下(详细的步骤可以参考架构实战篇(一)-Spring Boot+MyBatis基础架构搭建)

基于开发工具(IntelliJ IDEA),创建名为zkboot的Maven工程
项目目录
SpringBoot+zk+dubbo架构实践(二):SpringBoot 集成 zookeeper_第2张图片
zkdemo项目目录.png
pom.xml


    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.10.RELEASE
    
    4.0.0

    com.itunion.zkboot
    zkboot
    1.0-SNAPSHOT
    
        UTF-8
        UTF-8
        1.8
        true
        3.1.0
    

    
        
            clojars
            http://clojars.org/repo/
        
    

    
        
            org.springframework.boot
            spring-boot-devtools
            runtime
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.apache.zookeeper
            zookeeper
            3.4.12
        
        
            org.apache.commons
            commons-io
            1.3.2
        
        
            org.apache.commons
            commons-lang3
            3.4
        
        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        
        
            org.springframework.boot
            spring-boot-starter-web
        
    

    
        zkboot
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
            
            
                org.apache.maven.plugins
                maven-war-plugin
                2.1.1
                
                    false
                
            
        
    

备注:groupId、artifactId 根据自己需要修改

application.properties
#设置服务端口
server.port=8089
server.context-path=/zkboot
Application 启动入口
package com.itunion.zkboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.support.SpringBootServletInitializer;

/**
 * 启动入口
 * @author lin
 * @date 2018年06月05日14:21:49
 */
@SpringBootApplication
public class Application extends SpringBootServletInitializer {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }

}
RestZkController 获取设置的zookeeper node节点信息方法
package com.itunion.zkboot.web.controller;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by lin on 2018年06月05日14:23:36
 */
@RestController
public class RestZkController {

    @RequestMapping(value = "/zkGet",method = RequestMethod.GET)
    public String zkGet(){
        Watcher watcher= new Watcher(){
            public void process(WatchedEvent event) {
                System.out.println("receive event:"+event);
            }
        };

        String value = null;
        try {
            final ZooKeeper zookeeper = new ZooKeeper("127.0.0.1:2181", 999999, watcher);
            final byte[] data = zookeeper.getData("/node_1", watcher, null);
            value = new String(data);
            zookeeper.close();
        }catch(Exception e){
            e.printStackTrace();
        }

        return "获取 node_1 节点值为 [" + value + "]";
    }
}
启动项目并测试
访问地址:http://127.0.0.1:8089/zkboot/zkGet
备注:大家要保证 zookeeper 服务正常启动,并且创建了node_1 节点和值,如果没有可以参考上一篇文章!
执行结果
SpringBoot+zk+dubbo架构实践(二):SpringBoot 集成 zookeeper_第3张图片
执行结果.png

备注

简单的SpringBoot集成zookeeper已经完成了,如果可以深入了解可以继续自行深入学习。

关注我们

更多精彩内容请关注“IT实战联盟”公众号,如果需要源码的话可以关注公众号留言(zkboot源码+邮箱),也可以加入交流群和作者互撩哦~~~


SpringBoot+zk+dubbo架构实践(二):SpringBoot 集成 zookeeper_第4张图片
IT实战联盟.jpg

你可能感兴趣的:(SpringBoot+zk+dubbo架构实践(二):SpringBoot 集成 zookeeper)