Spring Boot—20Zookeeper
https://docs.spring.io/spring-boot/docs/2.0.1.RELEASE/reference/htmlsingle/
pom.xml
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.12.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-x-discovery</artifactId>
<version>2.12.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-x-discovery-server</artifactId>
<version>2.12.0</version>
</dependency>
application.properties
#
server.address=0.0.0.0
server.port=8080
server.servlet.context-path=/test
server.session.timeout=300
server.error.path=/error
#
server.tomcat.accesslog.enabled=true
server.tomcat.accesslog.buffered=true
server.tomcat.accesslog.directory=D:/Project/JavaWeb/SpringBoot/04JPASpringBoot/logs
#
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=Asia/Shanghai
#
spring.thymeleaf.cache=true
spring.thymeleaf.enabled=true file.upload.path=D:/Project/JavaWeb/SpringBoot/04JPASpringBoot/fileUpLoad spring.servlet.multipart.enabled=true
spring.servlet.multipart.file-size-threshold=0
spring.servlet.multipart.location=D:/Project/JavaWeb/SpringBoot/04JPASpringBoot/temp
spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=10MB
spring.servlet.multipart.resolve-lazily=false #
# debug=true # Enable debug logs.
# trace=true # Enable trace logs. # LOGGING
logging.config=classpath:logback.xml # zookeeper
zk.url=127.0.0.1:2181
zk.retry-time-init = 1000
zk.retry-count = 3
启动类
package com.smartmap.sample.test; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class TestZookeeperApplication { public static void main(String[] args) {
SpringApplication.run(TestZookeeperApplication.class, args); }
}
Zookeeper配置类
package com.smartmap.sample.test.conf; import java.util.ArrayList;
import java.util.Collection; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.api.CuratorEvent;
import org.apache.curator.framework.api.CuratorEventType;
import org.apache.curator.framework.api.CuratorListener;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.curator.x.discovery.ServiceDiscovery;
import org.apache.curator.x.discovery.ServiceDiscoveryBuilder;
import org.apache.curator.x.discovery.ServiceInstance;
import org.apache.curator.x.discovery.ServiceInstanceBuilder;
import org.apache.curator.x.discovery.details.JsonInstanceSerializer;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher.Event.EventType;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; @Configuration
public class ZookeeperConfiguration {
Log log = LogFactory.getLog(ZookeeperConfiguration.class); @Value("${zk.url}")
private String zkUrl; @Value("${zk.retry-time-init}")
private int retryTimeInit; @Value("${zk.retry-count}")
private int retryCount; @Bean
public CuratorFramework getCuratorFramework() throws Exception {
RetryPolicy retryPolicy = new ExponentialBackoffRetry(retryTimeInit, retryCount);
CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient(zkUrl, retryPolicy);
addListener(curatorFramework);
curatorFramework.start();
registerService(curatorFramework);
ServiceInstance<ServerPayload> service = findService(curatorFramework, "book");
log.info(service);
return curatorFramework;
} /**
* 添加监听
* @param curatorFramework
*/
private void addListener(CuratorFramework curatorFramework) {
curatorFramework.getCuratorListenable().addListener(new CuratorListener() { @Override
public void eventReceived(CuratorFramework curatorFramework, CuratorEvent curatorEvent) throws Exception {
CuratorEventType type = curatorEvent.getType();
if(type==CuratorEventType.WATCHED) {
WatchedEvent we = curatorEvent.getWatchedEvent();
EventType et = we.getType();
if(we.getPath() != null) {
log.info(et + ":" + we.getPath());
// 重新注册监听
curatorFramework.checkExists().watched().forPath(we.getPath());
}
}
}
});
} /**
* 服务注册
* @param client
* @throws Exception
*/
protected void registerService(CuratorFramework client) throws Exception {
// 构造一个服务描述
ServiceInstanceBuilder<ServerPayload> serviceInstanceBuilder = ServiceInstance.builder();
serviceInstanceBuilder.address("127.0.0.1");
serviceInstanceBuilder.port(8080);
serviceInstanceBuilder.name("book");
ServerPayload serverPayload = new ServerPayload();
serverPayload.setUrl("/api/v1.1/system/book");
serviceInstanceBuilder.payload(serverPayload);
ServiceInstance<ServerPayload> serviceInstance = serviceInstanceBuilder.build();
// 服务注册
/*JsonInstanceSerializer<ServerPayload> jsonInstanceSerializer = new JsonInstanceSerializer(ServerPayload.class);
ServiceDiscovery<ServerPayload> serviceDiscovery = ServiceDiscoveryBuilder.builder(ServerPayload.class).client(client)
.serializer(jsonInstanceSerializer).basePath("/service").build();
serviceDiscovery.registerService(serviceInstance);
serviceDiscovery.start();*/ ServiceDiscovery<ServerPayload> serviceDiscovery = getServiceDiscovery(client);
serviceDiscovery.registerService(serviceInstance);
} /**
* 服务发现
* @param client
* @param serviceName
* @return
* @throws Exception
*/
protected ServiceInstance<ServerPayload> findService(CuratorFramework client, String serviceName) throws Exception {
/*JsonInstanceSerializer<ServerPayload> jsonInstanceSerializer = new JsonInstanceSerializer(ServerPayload.class);
ServiceDiscovery<ServerPayload> serviceDiscovery = ServiceDiscoveryBuilder.builder(ServerPayload.class).client(client)
.serializer(jsonInstanceSerializer).basePath("/service").build();
serviceDiscovery.start();*/
ServiceDiscovery<ServerPayload> serviceDiscovery = getServiceDiscovery(client);
// 查找服务
Collection<ServiceInstance<ServerPayload>> all = serviceDiscovery.queryForInstances(serviceName);
if(all.size() == 0) {
return null;
}
else {
// 取第一个服务
ServiceInstance<ServerPayload> service = new ArrayList<ServiceInstance<ServerPayload>>(all).get(0);
log.info(service.getAddress());
log.info(service.getPayload());
return service;
}
} public ServiceDiscovery<ServerPayload> getServiceDiscovery(CuratorFramework client) throws Exception {
JsonInstanceSerializer<ServerPayload> jsonInstanceSerializer = new JsonInstanceSerializer<ServerPayload>(ServerPayload.class);
ServiceDiscovery<ServerPayload> serviceDiscovery = ServiceDiscoveryBuilder.builder(ServerPayload.class).client(client)
.serializer(jsonInstanceSerializer).basePath("/service").build();
serviceDiscovery.start();
return serviceDiscovery;
} public static class ServerPayload {
private String url; public String getUrl() {
return url;
} public void setUrl(String url) {
this.url = url;
} }
}
CuratorFrameWork调用Zookeeper
package com.smartmap.sample.test.service.impl; import java.util.List;
import java.util.concurrent.TimeUnit; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import com.smartmap.sample.test.service.OrderService; @Service
public class OrderServiceImpl implements OrderService { final Log log = LogFactory.getLog(OrderServiceImpl.class);
final String lockPath = "/lock/order"; @Autowired
CuratorFramework curatorFramework; /**
* 创建节点
*/
public String createPath(String path) throws Exception {
curatorFramework.create().withMode(CreateMode.PERSISTENT).forPath(path, new byte[0]);
return "create " + path;
} /**
* 删除节点
*/
public String delete(String path) throws Exception {
curatorFramework.delete().forPath(path);
return "delete " + path;
} /**
* 获取节点数据
*/
public String getData(String path) throws Exception {
byte[] bs = curatorFramework.getData().forPath(path);
String result = new String(bs);
return "get data: " + result;
} /**
* 设置节点数据
*/
public String setData(String path, String data) throws Exception {
curatorFramework.setData().forPath(path, data.getBytes());
return "set data " + data;
} /**
* 检查节点是否存在
*/
public String check(String path) throws Exception {
Stat stat = curatorFramework.checkExists().forPath(path);
return "stat " + stat;
} /**
* 获取子节点
*/
public String children(String path) throws Exception {
List<String> children = curatorFramework.getChildren().forPath(path);
return "children " + children;
} /**
* 监控节点
*/
public String watch(String path) throws Exception {
Stat stat = curatorFramework.checkExists().watched().forPath(path);
return "watch: < " + path + " > stat: " + stat;
} /**
* 分布式锁
*/
public void makeOrderType(String type) {
String path = lockPath + "/" + type;
log.info("try do job for " + type);
try {
InterProcessMutex lock = new InterProcessMutex(curatorFramework, path);
if (lock.acquire(10, TimeUnit.HOURS)) {
try {
Thread.sleep(1000 * 5);
log.info("do job " + type + " done");
} finally {
lock.release();
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
} }
Controller类
package com.smartmap.sample.test.controller.rest; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import com.smartmap.sample.test.service.OrderService; @RestController
@RequestMapping("/api/v1.1/system/book")
public class ZookeeperTestCrontroller { @Autowired
OrderService orderService; /**
* 创建节点
*
* curl -XPOST 'http://127.0.0.1:8080/test/api/v1.1/system/book/create?path=/testNode'
* @param path
* @return
* @throws Exception
*/
@PostMapping("/create")
public String create(@RequestParam("path") String path) throws Exception {
return orderService.createPath(path);
} /**
* 删除节点
* curl -XPOST 'http://127.0.0.1:8080/test/api/v1.1/system/book/delete?path=/testNode'
* @param path
* @return
* @throws Exception
*/
@PostMapping("/delete")
public String delete(@RequestParam("path") String path) throws Exception {
return orderService.delete(path);
} /**
* 获取节点数据
* curl -XGET 'http://127.0.0.1:8080/test/api/v1.1/system/book/data?path=/testNode'
* @param path
* @return
* @throws Exception
*/
@GetMapping("/data")
public String getData(@RequestParam("path") String path) throws Exception {
return orderService.getData(path);
} /**
* 设置节点数据
* curl -XPOST 'http://127.0.0.1:8080/test/api/v1.1/system/book/data?path=/testNode&data=987654321'
* @param path
* @param data
* @return
* @throws Exception
*/
@PostMapping("/data")
public String setData(@RequestParam("path") String path, @RequestParam("data") String data) throws Exception {
return orderService.setData(path, data);
} /**
* 检查节点是否存在
* curl -XPOST 'http://127.0.0.1:8080/test/api/v1.1/system/book/check?path=/testNode'
* @param path
* @return
* @throws Exception
*/
@PostMapping("/check")
public String check(@RequestParam("path") String path) throws Exception {
return orderService.check(path);
} /**
* 获取子节点
* curl -XPOST 'http://127.0.0.1:8080/test/api/v1.1/system/book/children?path=/testNode'
* @param path
* @return
* @throws Exception
*/
@PostMapping("/children")
public String getChildren(@RequestParam("path") String path) throws Exception {
return orderService.children(path);
} /**
* 监控节点
*
* curl -XPOST 'http://127.0.0.1:8080/test/api/v1.1/system/book/watch?path=/testNode'
* @param path
* @return
* @throws Exception
*/
@PostMapping("/watch")
public String watch(@RequestParam("path") String path) throws Exception {
return orderService.watch(path);
} /**
* 订单--分布式锁
* curl -XPOST 'http://127.0.0.1:8080/test/api/v1.1/system/book/order?type=orderType'
* @param type
* @return
* @throws Exception
*/
@PostMapping("/order")
public String makeOrder(@RequestParam("type") String type) throws Exception {
orderService.makeOrderType(type);
return "success";
} }
Spring Boot—20Zookeeper的更多相关文章
- 玩转spring boot——快速开始
开发环境: IED环境:Eclipse JDK版本:1.8 maven版本:3.3.9 一.创建一个spring boot的mcv web应用程序 打开Eclipse,新建Maven项目 选择quic ...
- 【微框架】之一:从零开始,轻松搞定SpringCloud微框架系列--开山篇(spring boot 小demo)
Spring顶级框架有众多,那么接下的篇幅,我将重点讲解SpringCloud微框架的实现 Spring 顶级项目,包含众多,我们重点学习一下,SpringCloud项目以及SpringBoot项目 ...
- 玩转spring boot——开篇
很久没写博客了,而这一转眼就是7年.这段时间并不是我没学习东西,而是园友们的技术提高的非常快,这反而让我不知道该写些什么.我做程序已经有十几年之久了,可以说是彻彻底底的“程序老炮”,至于技术怎么样?我 ...
- 玩转spring boot——结合redis
一.准备工作 下载redis的windows版zip包:https://github.com/MSOpenTech/redis/releases 运行redis-server.exe程序 出现黑色窗口 ...
- 玩转spring boot——AOP与表单验证
AOP在大多数的情况下的应用场景是:日志和验证.至于AOP的理论知识我就不做赘述.而AOP的通知类型有好几种,今天的例子我只选一个有代表意义的“环绕通知”来演示. 一.AOP入门 修改“pom.xml ...
- 玩转spring boot——结合JPA入门
参考官方例子:https://spring.io/guides/gs/accessing-data-jpa/ 接着上篇内容 一.小试牛刀 创建maven项目后,修改pom.xml文件 <proj ...
- 玩转spring boot——结合JPA事务
接着上篇 一.准备工作 修改pom.xml文件 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=&q ...
- 玩转spring boot——结合AngularJs和JDBC
参考官方例子:http://spring.io/guides/gs/relational-data-access/ 一.项目准备 在建立mysql数据库后新建表“t_order” ; -- ----- ...
- 玩转spring boot——结合jQuery和AngularJs
在上篇的基础上 准备工作: 修改pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=&q ...
随机推荐
- iOS完全自学手册——[二]Hello World工程
1.前言 写第二篇文章之前,我在想第二篇应该怎么写?后来觉得与其写Objective-C语言的相关语法,不如直接开始写个小项目.语法简单入门即可.因为,即便把语法看的很深到最后还是不一定能做项目,运用 ...
- 【xsy1172】 染色 dp
题目大意:现有$n$条排成一行的木板,每个木板有一个目标颜色.你每次能将一个区间内的木板分别染成它们的目标颜色,而这次染色的代价为这个区间内不同目标颜色的木板的数量的平方.问将全部木板染成目标颜色的最 ...
- easyui toopTip,鼠标划过悬浮,显示一个小提示框的方法
easyui toopTip,鼠标划过悬浮,显示一个小提示框的方法 /*easyui,鼠标划过悬浮,显示一个小提示框的方法*/ function toopTip(idOrClass,showText) ...
- 2 new出的对象 prototype与__proto__
对象没有原型对象,函数才有 new出的对象,this的会重新创建,二prototype并不会重新创建,而是追溯原型链的方式进行继承 var Book=function(id,bookname,pric ...
- KBEngine 安装
其实这篇的内容官方文档都有, 但是既然打算记录一下学习笔记, 也就先从安装开始了. 一 下载源代码 进入github下载最新release的源码压缩包. windows选择zip, 下载完成之后右键解 ...
- 剑指offer四十之数组中只出现一次的数字
一.题目 一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字. 二.思路 建一个hashMap,统计各数字出现的次数,然后遍历hashMap,输出出现一次的数字 ...
- (转) mysqldumpslow使用说明总结
原文:http://blog.csdn.net/langkeziju/article/details/49301993 mysqldumpslow使用说明mysqldumpslow --helpUsa ...
- 《Mysql技术内幕,Innodb存储引擎》——锁
lock与latch 在数据库中lock与latch分别指不同的所. latch:可分为互斥量(mutex)和读写锁(rwlock),目的在于保证数据库内部的结构中共享资源并发时能够正确操作,其对象主 ...
- vue2.0实现购物车功能
购物车功能是一件比较繁琐的事情,逻辑功能太多,今天就用vue2.0实现一个简单的购物车功能,数据都本地自己写的假数据 功能列表: 1.全选和单选结算 2.减少和增加数量 3.商品的删除 界面搭建以及布 ...
- java学习--Reflection反射机制
JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意方法和属性:这种动态获取信息以及动态调用对象方法的功能称为java语言的反射机制. ...