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

《SpringBoot+zk+dubbo架构实践(二):SpringBoot 集成 zookeeper》 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》 zkdemo项目目录.png

pom.xml
<?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>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.RELEASE</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.itunion.zkboot</groupId>
    <artifactId>zkboot</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <maven.test.skip>true</maven.test.skip>
        <aliyun.oss.version>3.1.0</aliyun.oss.version>
    </properties>

    <repositories>
        <repository>
            <id>clojars</id>
            <url>http://clojars.org/repo/</url>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.12</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-io</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.4</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <build>
        <finalName>zkboot</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <!-- 打war包用,maven打包的时候告诉maven不需要web.xml,否刚会报找不到web.xml错误 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.1.1</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

备注: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》 执行结果.png

备注

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

关注我们

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

《SpringBoot+zk+dubbo架构实践(二):SpringBoot 集成 zookeeper》 IT实战联盟.jpg

    原文作者:IT实战联盟Lin
    原文地址: https://www.jianshu.com/p/cc983c8dc47a
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞