Redis+Hbase+RocketMQ?實際使用問題案例講解
更新時間:2023年01月23日 09:53:59 作者:王德發(fā)!
這篇文章主要介紹了Redis+Hbase+RocketMQ?實際使用問題案例分享,本文結(jié)合示例代碼給大家講解的非常詳細,需要的朋友可以參考下
需求
- 將Hbase數(shù)據(jù),解析后推送到RocketMQ。
- redis使用list數(shù)據(jù)類型,存儲了需要推送的數(shù)據(jù)的RowKey及表名。
簡單畫個流程圖就是:
分析及確定方案
Redis
- 明確list中元素結(jié)構(gòu)
{"rowkey":rowkey,"table":table}
解析出rowkey; - 一次取多個元素加快效率;取了之后放入重試隊列,并刪除原來的元素;
- 處理數(shù)據(jù)永遠是重試隊列里的,成功之后刪除,失敗就加上重試次數(shù)并重新放回;
- 明確從list中取值所使用的redis命令;
- 范圍獲取
LRANGE
; - 范圍刪除(留下指定范圍的數(shù)據(jù))
LTRIM
; - 判斷l(xiāng)ist長度
LLEN
; - 加入list
RPUSH
;刪除LREM
等等; - 從Hbase獲取數(shù)據(jù)失敗和發(fā)送到mq失敗都令重試次數(shù)加一;
- 每次碰到重試次數(shù)不為0的數(shù)據(jù)都休眠1s;
- 設(shè)置最大重試次數(shù),達到限制后丟棄;
- 考慮客戶redis部署方式,單機、主從、集群、哨兵等;
- 選擇合適的客戶端,Jedis、Redisson、Lettuce等;
- 編寫不同的操作代碼,也可以利用配置文件、環(huán)境變量、工廠模式等適配各種部署模式;
Hbase
- 基本理論知識學(xué)習(xí)(原來沒接觸過),rowkey是沒條數(shù)據(jù)的主鍵,限定符是字段名,列族是多個限定名的集合等;
- 當(dāng)時看這個覺得不錯http://www.dbjr.com.cn/article/230731.htm因為是不停讀取數(shù)據(jù)、鏈接、Table不用close,可以緩存起來,沒必要每次都創(chuàng)建;
- 確定批量獲取數(shù)據(jù)方式為批量
Get
,沒用scan
; - 了解解析方式,一些網(wǎng)上的解析試了之后會亂碼,這邊用的是它自帶的
CellUtil.clone
相關(guān)方法; - 考慮所有都沒數(shù)據(jù)時休眠10s;
RocketMQ
- 有現(xiàn)成的發(fā)送代碼,公司封裝好的;
- 調(diào)整發(fā)送的速度、太快了服務(wù)端會吃不消(獲取Hbase數(shù)據(jù)速度太快了,最開始沒限制一會兒就入了百萬數(shù)據(jù)),設(shè)置超時時間(默認3s);
- 調(diào)整服務(wù)端的內(nèi)存、線程數(shù)等參數(shù);
實現(xiàn)
配置
#server configuration server.port=8896 #log config logging.file.path=./logs #redis-standalone redis.standalone.host= redis.standalone.port=6379 redis.standalone.password= redis.standalone.enable=true #redis-cluster redis.cluster.nodes= redis.cluster.password= redis.cluster.timeout=30000 redis.cluster.enable=false # Zookeeper 集群地址,逗號分隔 hbase.zookeeper.quorum= # Zookeeper 端口 hbase.zookeeper.property.clientPort=2181 # 消息目的rocketmq地址 rocketmq.server.host= # 發(fā)送消息間隔時間,防止發(fā)送過快mq受不了 rocketmq.send.interval.millisec=10 # 每次從redis讀取數(shù)據(jù)量限制。 data.access.redisDataSize=100 # 失敗數(shù)據(jù)重試次數(shù),超過的直接丟棄 data.access.retryNum=10 # 需要接入的表,需要發(fā)送到rocketmq的topic和在redis中的key的映射。xxx.xxx.xxx[topic]=redisKey data.access.topicKeyMap[weibo_hbase]=data:sync:notice:suanzi:weibo:back data.access.topicKeyMap[wechat_hbase]=data:sync:notice:suanzi:wechat:back
部分代碼
獲取配置,其余的直接@Value("${}")
:
@Setter @Getter @Configuration @ConfigurationProperties(prefix = "data.access") public class AccessRedisMqConfig { /** * key:topic; value:redis的key */ private Map<String, String> topicKeyMap = new HashMap<>(); /** * 一次從redis中讀取數(shù)據(jù)量限制 */ private long redisDataSize = 50; /** * 失敗數(shù)據(jù)重試次數(shù) */ private int retryNum = 10; }
開啟接入:
@Component public class AdapterRunner implements ApplicationRunner { @Resource private DataAccessService dataAccessService; @Override public void run(ApplicationArguments args) { System.out.println("項目已啟動,開始接入數(shù)據(jù)到RocketMQ……"); dataAccessService.accessData2Mq(); } }
其他代碼其實也在分析里了。
踩坑
mq發(fā)送問題
org.apache.rocketmq.remoting.exception.RemotingTooMuchRequestException: invokeAsync call timeout at org.apache.rocketmq.remoting.netty.NettyRemotingClient.invokeAsync(NettyRemotingClient.java:525) at org.apache.rocketmq.client.impl.MQClientAPIImpl.sendMessageAsync(MQClientAPIImpl.java:523) at org.apache.rocketmq.client.impl.MQClientAPIImpl.onExceptionImpl(MQClientAPIImpl.java:610) at org.apache.rocketmq.client.impl.MQClientAPIImpl.access$100(MQClientAPIImpl.java:167) at org.apache.rocketmq.client.impl.MQClientAPIImpl$1.operationComplete(MQClientAPIImpl.java:572) at org.apache.rocketmq.remoting.netty.ResponseFuture.executeInvokeCallback(ResponseFuture.java:54) at org.apache.rocketmq.remoting.netty.NettyRemotingAbstract$2.run(NettyRemotingAbstract.java:319) at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515) at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) at java.base/java.util.concurrent.ThreadPoolExecutor$Wo
上面分析也說了,注意發(fā)送速度,有多少資源就接入多快。還有注意相關(guān)三個端口是否開放。
總結(jié)
程序很簡單,主要涉及方案的是,獲取redis的list數(shù)據(jù)時,是考慮效率,及加入重試策略,保證數(shù)據(jù)不丟失等。
相關(guān)文章
Redis實現(xiàn)每日簽到功能(大數(shù)據(jù)量)
在面對百萬級用戶簽到情況下,傳統(tǒng)數(shù)據(jù)庫存儲和判斷會遇到瓶頸,使用Redis的二進制數(shù)據(jù)類型可實現(xiàn)高效的簽到功能,示例代碼展示了如何調(diào)用這些功能,包括當(dāng)天簽到、補簽以及查詢簽到記錄,PHP結(jié)合Redis二進制數(shù)據(jù)類型可有效處理大數(shù)據(jù)量下的簽到問題2024-10-10redis+mysql+quartz 一種紅包發(fā)送功能的實現(xiàn)
這篇文章主要介紹了redis+mysql+quartz 一種紅包發(fā)送功能的實現(xiàn)的相關(guān)資料,需要的朋友可以參考下2017-01-01