上一篇:SpringBoot+zk+dubbo架构实践(一):本地部署zookeeper
前言
1 2 3 4
| 1这是第二篇了,本篇我们完成两件事情。
21、搭建SpringBoot 框架;
32、基于spring boot框架访问zookeeper。
4 |
搭建SpringBoot框架
其实之前“IT实战联盟”已经出了该系列文章,今天我们简单搭建一下(详细的步骤可以参考架构实战篇(一)-Spring Boot+MyBatis基础架构搭建)
基于开发工具(IntelliJ IDEA),创建名为zkboot的Maven工程
项目目录
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
| 1<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0"
2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4 <parent>
5 <groupId>org.springframework.boot</groupId>
6 <artifactId>spring-boot-starter-parent</artifactId>
7 <version>1.5.10.RELEASE</version>
8 </parent>
9 <modelVersion>4.0.0</modelVersion>
10
11 <groupId>com.itunion.zkboot</groupId>
12 <artifactId>zkboot</artifactId>
13 <version>1.0-SNAPSHOT</version>
14 <properties>
15 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
16 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
17 <java.version>1.8</java.version>
18 <maven.test.skip>true</maven.test.skip>
19 <aliyun.oss.version>3.1.0</aliyun.oss.version>
20 </properties>
21
22 <repositories>
23 <repository>
24 <id>clojars</id>
25 <url>http://clojars.org/repo/</url>
26 </repository>
27 </repositories>
28
29 <dependencies>
30 <dependency>
31 <groupId>org.springframework.boot</groupId>
32 <artifactId>spring-boot-devtools</artifactId>
33 <scope>runtime</scope>
34 </dependency>
35 <dependency>
36 <groupId>org.springframework.boot</groupId>
37 <artifactId>spring-boot-starter-web</artifactId>
38 </dependency>
39 <dependency>
40 <groupId>org.springframework.boot</groupId>
41 <artifactId>spring-boot-starter-test</artifactId>
42 <scope>test</scope>
43 </dependency>
44 <dependency>
45 <groupId>org.apache.zookeeper</groupId>
46 <artifactId>zookeeper</artifactId>
47 <version>3.4.12</version>
48 </dependency>
49 <dependency>
50 <groupId>org.apache.commons</groupId>
51 <artifactId>commons-io</artifactId>
52 <version>1.3.2</version>
53 </dependency>
54 <dependency>
55 <groupId>org.apache.commons</groupId>
56 <artifactId>commons-lang3</artifactId>
57 <version>3.4</version>
58 </dependency>
59 <dependency>
60 <groupId>org.springframework.boot</groupId>
61 <artifactId>spring-boot-starter-thymeleaf</artifactId>
62 </dependency>
63 <dependency>
64 <groupId>org.springframework.boot</groupId>
65 <artifactId>spring-boot-starter-web</artifactId>
66 </dependency>
67 </dependencies>
68
69 <build>
70 <finalName>zkboot</finalName>
71 <plugins>
72 <plugin>
73 <groupId>org.springframework.boot</groupId>
74 <artifactId>spring-boot-maven-plugin</artifactId>
75 </plugin>
76 <!-- 打war包用,maven打包的时候告诉maven不需要web.xml,否刚会报找不到web.xml错误 -->
77 <plugin>
78 <groupId>org.apache.maven.plugins</groupId>
79 <artifactId>maven-war-plugin</artifactId>
80 <version>2.1.1</version>
81 <configuration>
82 <failOnMissingWebXml>false</failOnMissingWebXml>
83 </configuration>
84 </plugin>
85 </plugins>
86 </build></project>
87 |
备注:groupId、artifactId 根据自己需要修改
application.properties
1 2 3 4
| 1#设置服务端口
2server.port=8089
3server.context-path=/zkboot
4 |
Application 启动入口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| 1package com.itunion.zkboot;
2import org.springframework.boot.SpringApplication;
3import org.springframework.boot.autoconfigure.SpringBootApplication;
4import org.springframework.boot.web.support.SpringBootServletInitializer;
5/**
6 * 启动入口
7 * @author lin
8 * @date 2018年06月05日14:21:49
9 */@SpringBootApplicationpublic class Application extends SpringBootServletInitializer {
10public static void main(String[] args) throws Exception {
11 SpringApplication.run(Application.class, args);
12 }
13
14}
15 |
RestZkController 获取设置的zookeeper node节点信息方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| 1package com.itunion.zkboot.web.controller;
2import org.apache.zookeeper.WatchedEvent;
3import org.apache.zookeeper.Watcher;
4import org.apache.zookeeper.ZooKeeper;
5import org.springframework.web.bind.annotation.RequestMapping;
6import org.springframework.web.bind.annotation.RequestMethod;
7import org.springframework.web.bind.annotation.RestController;/**
8 * Created by lin on 2018年06月05日14:23:36
9 */
10@RestControllerpublic class RestZkController {
11@RequestMapping(value = "/zkGet",method = RequestMethod.GET)
12public String zkGet(){
13 Watcher watcher= new Watcher(){
14 public void process(WatchedEvent event) {
15 System.out.println("receive event:"+event);
16 }
17 };
18
19 String value = null;
20 try {
21 final ZooKeeper zookeeper = new ZooKeeper("127.0.0.1:2181", 999999, watcher);
22 final byte[] data = zookeeper.getData("/node_1", watcher, null);
23 value = new String(data);
24 zookeeper.close();
25 }catch(Exception e){
26 e.printStackTrace();
27 }
28 return "获取 node_1 节点值为 [" + value + "]";
29 }
30}
31 |
启动项目并测试
1 2 3
| 1访问地址:http://127.0.0.1:8089/zkboot/zkGet
2备注:大家要保证 zookeeper 服务正常启动,并且创建了node_1 节点和值,如果没有可以参考上一篇文章!
3 |
执行结果
备注
简单的SpringBoot集成zookeeper已经完成了,如果可以深入了解可以继续自行深入学习。