# zjc-springboot **Repository Path**: zhangjunchao09/zjcspringboot ## Basic Information - **Project Name**: zjc-springboot - **Description**: spring boot实列 1. zookeeper:curator-recipes实现动态分片 2. redis: 监听过期key. 3. 利用redis监听过期key,利用zk分片解决多实例重复消费,实现延迟消息队列,延迟时间可修改; 4 tcp长连接负载均衡 - **Primary Language**: Java - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2022-03-02 - **Last Updated**: 2023-10-27 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # zjcspringboot ## 介绍 spring boot实例: 1. zookeeper:curator-recipes实现动态分片 2. redis: 监听过期key,实现RedisExpiredListener即可 3. zjc-springboot-delayqueue : 利用redis监听过期key,zk实现动态分片实现消息延迟队列,延迟时间可修改; 实现ApplicationListener可接收消息。 ## 使用教程 ### redis 过期key监听 - 安装redis,配置 notify-keyspace-events Ex - maven添加依赖 ``` com.zjc.springboot zjc-springboot-redis-watch 1.0.0 ``` - 实现RedisExpiredListener 继承MessageListener ``` package com.zjc.springboot.redis.watch.listener; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.connection.Message; import org.springframework.data.redis.connection.MessageListener; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.stereotype.Component; @Component public class RedisExpiredListener implements MessageListener { @Autowired StringRedisTemplate stringRedisTemplate; @Override public void onMessage(Message message, byte[] pattern) { String expiredKey = message.toString(); String patt = new String(pattern); System.out.println(patt); if (expiredKey.startsWith("ex:event")) { //TODO } } } ``` ### zk动态分片 - 安装zk - maven添加依赖 ``` com.zjc.springboot zjc-springboot-zk-slice 1.0.0 ``` zookeeperClient可以获取节点信息 NodeInfo 当前节点id和总节点数size,根据 Math.abs(key.hashCode()) % size == id 做简单的任务分解 ### 延迟消息队列 - 安装zk - 安装redis,配置 notify-keyspace-events Ex - maven添加依赖 ``` com.zjc.springboot zjc-springboot-delayqueue 1.0.0 ``` - 发送消息: ``` @Autowired RedisClient redisClient; public void send(String key, String value, int time) { Msg msg = new Msg(); msg.setKey(key); msg.setValue(value); msg.setTime(time); redisClient.sendMsg(msg); } ``` - 接收消息: ``` @Component public class MsgEventLisenter implements ApplicationListener { private static Gson gson = new GsonBuilder().create(); @Override public void onApplicationEvent(MsgEvent event) { Msg msg = event.getMsg(); System.out.println(gson.toJson(msg)); //TODO: 对监听到的事件进行处理 } } ``` ## 参与贡献 1. Fork 本仓库 2. 新建 Feat_xxx 分支 3. 提交代码 4. 新建 Pull Request