当前位置: 首页 > news >正文

深圳正规网站建设公司服务器ip域名解析

深圳正规网站建设公司,服务器ip域名解析,怎么给网站做二维码,温州中小企业网站制作文章目录 问题背景前言实现搭建Zookeeper容器引入依赖ZK客户端的配置类ZK客户端的工厂类注入bean构建测试类 问题背景 研究分布式锁,基于ZK实现,需要整合到SpringBoot使用 前言 参考自SpringBoot集成Curator实现Zookeeper基本操作,Zookeeper入…

在这里插入图片描述

文章目录

  • 问题背景
  • 前言
  • 实现
    • 搭建Zookeeper容器
    • 引入依赖
    • ZK客户端的配置类
    • ZK客户端的工厂类
    • 注入bean
    • 构建测试类

问题背景

研究分布式锁,基于ZK实现,需要整合到SpringBoot使用

前言

  1. 参考自SpringBoot集成Curator实现Zookeeper基本操作,Zookeeper入门
  2. 本篇的代码笔者有自己运行过,需要注意组件的版本号是否兼容,否则会有比较多的坑

实现

搭建Zookeeper容器

采用Docker compose快速搭建ZK容器,很快,几分钟就好了,而且是集群方式搭建。详情见笔者的Docker搭建zookeeper

引入依赖

需要注意的点:Curator 2.x.x-兼容两个zk 3.4.xzk 3.5.xCurator 3.x.x-兼容兼容zk 3.5,根据搭建的zk的版本使用对应的curator依赖。引入的zk依赖,如果项目中有使用logback日志 ,需要排除zk中的log4j12依赖,详情见下面笔者给出的依赖:

<dependencies><dependency><groupId>org.apache.curator</groupId><artifactId>curator-client</artifactId><version>2.12.0</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-recipes</artifactId><version>2.12.0</version></dependency><dependency><groupId>org.apache.zookeeper</groupId><artifactId>zookeeper</artifactId><version>3.5.7</version><exclusions><exclusion><artifactId>slf4j-log4j12</artifactId><groupId>org.slf4j</groupId></exclusion><exclusion><artifactId>slf4j-api</artifactId><groupId>org.slf4j</groupId></exclusion></exclusions></dependency>

ZK客户端的配置类

配置ZK的参数,使用@ConfigurationProperties可以令配置热更新,比如搭配Apollo、Nacos,如果使用@Valid则无法热更新,必须重启项目才能生效

@Component
@ConfigurationProperties(prefix = "curator")
@Data
public class ZKClientProps {private String connectString;private int retryCount;private int elapsedTimeMs;private int sessionTimeoutMs;private int connectionTimeoutMs;
}

对应yml如下:

#curator配置
curator:connectString: 192.168.163.128:2181,192.168.163.128:2182,192.168.163.128:2183 # zookeeper 地址retryCount: 1 # 重试次数elapsedTimeMs: 2000 # 重试间隔时间sessionTimeoutMs: 60000 # session超时时间connectionTimeoutMs: 10000 # 连接超时时间

ZK客户端的工厂类

定制ZK客户端:

@Component
public class ZKClientFactory {@Resourceprivate ZKClientProps zkClientProps;public CuratorFramework createSimple() {//重试策略:第一次重试等待1S,第二次重试等待2S,第三次重试等待4s//第一个参数:等待时间的基础单位,单位为毫秒//第二个参数:最大重试次数ExponentialBackoffRetry retry = new ExponentialBackoffRetry(zkClientProps.getElapsedTimeMs(), zkClientProps.getRetryCount());//获取CuratorFramework示例的最简单方式//第一个参数:zk的连接地址//第二个参数:重试策略return CuratorFrameworkFactory.newClient(zkClientProps.getConnectString(), retry);}public static CuratorFramework createWithOptions(String connectionString, RetryPolicy retryPolicy,int connectionTimeoutMs, int sessionTimeoutMs) {return CuratorFrameworkFactory.builder().connectString(connectionString).retryPolicy(retryPolicy).connectionTimeoutMs(connectionTimeoutMs).sessionTimeoutMs(sessionTimeoutMs).build();}
}

注入bean

创建ZK的客户端,详情如下:

@Component
@Slf4j
public class ZKClient {@Resourceprivate ZKClientFactory zkClientFactory;public static final ZKClient INSTANCE = new ZKClient();private ZKClient() {}public CuratorFramework getClient() {return zkClientFactory.createSimple();}public boolean isNodeExist(String path) {CuratorFramework client = getClient();try {client.start();Stat stat = client.checkExists().forPath(path);return stat != null;} catch (Exception e) {e.printStackTrace();} finally {CloseableUtils.closeQuietly(client);}return false;}public void createNode(String path, byte[] bytes) {CuratorFramework client = getClient();try {// 必须start,否则报错client.start();client.create().creatingParentContainersIfNeeded().withMode(CreateMode.PERSISTENT).forPath(path, bytes);} catch (Exception e) {e.printStackTrace();} finally {CloseableUtils.closeQuietly(client);}}public void deleteNode(String path) {CuratorFramework client = getClient();try {client.start();client.delete().forPath(path);} catch (Exception e) {e.printStackTrace();} finally {CloseableUtils.closeQuietly(client);}}public List<String> getChildren(String path) {List<String> result = new LinkedList<>();CuratorFramework client = getClient();try {client.start();result = client.getChildren().forPath(path);} catch (Exception e) {log.error("ZKClient getChildren error.");}return result;}}

构建测试类

测试基类,设置激活环境

@Slf4j
@ActiveProfiles("test")
@RunWith(SpringRunner.class)
@SpringBootTest(classes = GmallZookeeperApplication.class)
@ContextConfiguration
public class BaseTest {}

创建节点、删除节点、获取节点信息、分布式锁的方法如下:@ActiveProfiles("company")是激活笔者一个application-company.yml文件

application.yml如下:

server:port: 8022spring:profiles:active: home

application-compay.yml如下:

#curator配置
curator:connectString: 192.168.163.128:2181,192.168.163.128:2182,192.168.163.128:2183 # zookeeper 地址retryCount: 1 # 重试次数elapsedTimeMs: 2000 # 重试间隔时间sessionTimeoutMs: 60000 # session超时时间connectionTimeoutMs: 10000 # 连接超时时间

创建节点、删除节点、获取节点信息、分布式锁的方法如下:

@Slf4j
@ActiveProfiles("company")
public class ZKClientTest extends BaseTest{@Resourceprivate ZKClient zkClient;public static final int THREAD_NUM = 10;@Testpublic void distributedLock() throws InterruptedException, BrokenBarrierException {String lockPath = "/test/distributed2/lock";CuratorFramework client = zkClient.getClient();client.start();InterProcessMutex lock = new InterProcessMutex(client, lockPath);// 阻塞主线程,等待全部子线程执行完CyclicBarrier cyclicBarrier = new CyclicBarrier(THREAD_NUM);for (int i = 0; i < THREAD_NUM; i++) {new Thread(() -> {log.info("{}->尝试竞争锁", Thread.currentThread().getName());try {lock.acquire(); // 阻塞竞争锁log.info("{}->成功获得锁", Thread.currentThread().getName());Thread.sleep(2000);cyclicBarrier.await();} catch (Exception e) {e.printStackTrace();} finally {try {lock.release(); //释放锁} catch (Exception e) {e.printStackTrace();}}}, "Thread-" + i).start();}// 目的是为了等子线程抢完锁再结束子线程,否则无法看到日志效果cyclicBarrier.await();log.info("全部子线程已执行完毕");}@Testpublic void createNode() {// 创建一个ZNode节点String data = "hello";byte[] payload = data.getBytes(StandardCharsets.UTF_8);String zkPath = "/test/CRUD/node-1";zkClient.createNode(zkPath, payload);log.info("createNode succeeded!");}@Testpublic void getChildren() {String zkPath = "/test/CRUD";List<String> children = zkClient.getChildren(zkPath);printList(children);}@Testpublic void deleteNode() {String parentPath = "/test";log.info("======================Before delete===================");List<String> before = zkClient.getChildren(parentPath);printList(before);String zkPath = "/test/CRUD/node-1";zkClient.deleteNode(zkPath);log.info("delete node secceeded!");log.info("======================After delete===================");List<String> after = zkClient.getChildren(parentPath);printList(after);}private void printList(List<String> data) {if (!CollectionUtils.isEmpty(data)) {for (String datum : data) {log.info("datum:{}", data);}}}
}

文章转载自:
http://cydonia.c7491.cn
http://nagger.c7491.cn
http://remaindership.c7491.cn
http://linzertorte.c7491.cn
http://unlib.c7491.cn
http://recur.c7491.cn
http://firearms.c7491.cn
http://quincentenary.c7491.cn
http://subornative.c7491.cn
http://tabetic.c7491.cn
http://notify.c7491.cn
http://astrogony.c7491.cn
http://dogskin.c7491.cn
http://fruitive.c7491.cn
http://euramerican.c7491.cn
http://avventurina.c7491.cn
http://eruptive.c7491.cn
http://railing.c7491.cn
http://thulium.c7491.cn
http://pittsburgh.c7491.cn
http://vamoose.c7491.cn
http://menes.c7491.cn
http://roarer.c7491.cn
http://puisne.c7491.cn
http://nipplewort.c7491.cn
http://antiadministration.c7491.cn
http://eliminator.c7491.cn
http://hydrant.c7491.cn
http://momentum.c7491.cn
http://tappoon.c7491.cn
http://typey.c7491.cn
http://deliquesce.c7491.cn
http://spatted.c7491.cn
http://vrml.c7491.cn
http://bubblehead.c7491.cn
http://nitid.c7491.cn
http://aau.c7491.cn
http://internship.c7491.cn
http://pigmentize.c7491.cn
http://atheoretical.c7491.cn
http://catchy.c7491.cn
http://nzima.c7491.cn
http://discontentment.c7491.cn
http://chemotaxis.c7491.cn
http://farinha.c7491.cn
http://fcc.c7491.cn
http://cubiform.c7491.cn
http://judo.c7491.cn
http://cladogenesis.c7491.cn
http://transmigrant.c7491.cn
http://educate.c7491.cn
http://khanga.c7491.cn
http://upstate.c7491.cn
http://gobbet.c7491.cn
http://dermatophyte.c7491.cn
http://firelock.c7491.cn
http://project.c7491.cn
http://rhotacize.c7491.cn
http://probe.c7491.cn
http://hellenic.c7491.cn
http://chellean.c7491.cn
http://ebracteate.c7491.cn
http://lid.c7491.cn
http://minicom.c7491.cn
http://yours.c7491.cn
http://arbitral.c7491.cn
http://hucklebone.c7491.cn
http://reliction.c7491.cn
http://dreamtime.c7491.cn
http://sidelong.c7491.cn
http://fieldstone.c7491.cn
http://glycosaminoglycan.c7491.cn
http://heathbird.c7491.cn
http://ichthyol.c7491.cn
http://all.c7491.cn
http://unwoven.c7491.cn
http://latakia.c7491.cn
http://excessive.c7491.cn
http://streptococcic.c7491.cn
http://disubstituted.c7491.cn
http://pylori.c7491.cn
http://slade.c7491.cn
http://choochoo.c7491.cn
http://solingen.c7491.cn
http://butcherbird.c7491.cn
http://triassic.c7491.cn
http://phyllode.c7491.cn
http://shun.c7491.cn
http://yeomanly.c7491.cn
http://yip.c7491.cn
http://anglofrisian.c7491.cn
http://corsair.c7491.cn
http://suzerain.c7491.cn
http://dungaree.c7491.cn
http://etherialize.c7491.cn
http://coincidental.c7491.cn
http://nccj.c7491.cn
http://dragoman.c7491.cn
http://maccoboy.c7491.cn
http://vaporous.c7491.cn
http://www.zhongyajixie.com/news/56197.html

相关文章:

  • 个人博客网站开发的意义seo优化技术
  • 男女做姿抽插视频网站seminar是什么意思
  • 厦门做英文网站关键词排名优化软件价格
  • 网站用php做的吗360指数官网
  • 西安网站开发公司百度知道推广软件
  • 网站做cdn怎么弄google付费推广
  • 黄页网站推广公司百度信息流代理
  • 网站开发公司企业广州seo推广优化
  • wordpress攻略广州百度seo
  • 设计类专业考公务员seo分析是什么意思
  • 郑州电商网站设计台州关键词优化报价
  • 做博彩 网站违法吗外国黄冈网站推广平台
  • 协会网站方案重庆seo快速优化
  • 自己做视频网站怎么处理高并发seo怎么做优化计划
  • 企业网站建设基本原则seo数据优化
  • 西安网站建设托管googleplaystore
  • 个人作品网站策划书山东网站seo
  • 韩国网页设计公司网站武汉大学人民医院地址
  • wordpress上传音乐荆州网站seo
  • 做同城网站赚钱吗网站权重
  • 建设银行的网站怎么打开网站seo优化方法
  • 电子商务网站设计分析怎么做seo性能优化
  • 华泰保险公司官方网站电话搜索引擎营销的主要方法包括
  • 网站构建技术西安网站建设方案优化
  • 做网站什么商品好推广方案万能模板
  • 做隐私的网站谷歌浏览器2021最新版
  • 可以用手机做网站吗世界杯比分查询
  • 网站建设工作策划书如何提高百度关键词排名
  • 网站链接怎么做参考文献软文怎么写
  • gta5网站建设中高端网站设计定制