上一篇:SpringBoot+zk+dubbo架構實踐(一):本地部署zookeeper

前言

這是第二篇了,本篇我們完成兩件事情。
1、搭建SpringBoot 框架;
2、基於spring boot框架訪問zookeeper。

搭建SpringBoot框架

其實之前「IT實戰聯盟」已經出了該系列文章,今天我們簡單搭建一下(詳細的步驟可以參考架構實戰篇(一)-Spring Boot+MyBatis基礎架構搭建)

基於開發工具(IntelliJ IDEA),創建名為zkboot的Maven工程

項目目錄

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
*/@SpringBootApplicationpublic 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
*/

@RestControllerpublic 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集成zookeeper已經完成了,如果可以深入了解可以繼續自行深入學習。

關注我們

更多精彩內容請關注「IT實戰聯盟」,如果需要源碼的話可以留言(zkboot源碼+郵箱),也可以加入交流群和作者互撩哦~~~

推薦閱讀:

相关文章