本篇文章為大家展示了windows下如何把搭建redis cluster集群及配置springboot2.3.x,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
成都創(chuàng)新互聯(lián)是一家專注于網(wǎng)站設(shè)計(jì)、成都網(wǎng)站設(shè)計(jì)與策劃設(shè)計(jì),路南網(wǎng)站建設(shè)哪家好?成都創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)10多年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:路南等地區(qū)。路南做網(wǎng)站價(jià)格咨詢:028-86922220
1.軟件環(huán)境
Redis-x64-3.2.100.zip
Redis-trib.rb
rubyinstaller-2.3.3-x64.exe
2.解壓redis
把下載的redis解壓到D盤redis-cluster目錄,然后在復(fù)制出來五份,端口分別是
6379,6380,6381,6382,6383,6384
3.修改每個(gè)redis文件夾下的redis.windows.conf配置文件
進(jìn)入到每個(gè)文件夾下,找到redis.windows.conf,然后改動(dòng)這些參數(shù)
port 6379 cluster-enabled yes cluster-config-file nodes-6379.conf cluster-node-timeout 15000 appendonly yes
在每個(gè)文件夾下新建start.bat文件
title redis-6379 redis-server.exe redis.windows.conf
4.安裝 Ruby
redis的集群使用 ruby腳本編寫,所以系統(tǒng)需要有 Ruby 環(huán)境
5.打開cmd窗口執(zhí)行
gem install redis
6.啟動(dòng)每個(gè)redis,安裝集群腳本
把 redis-trib.rb拷貝到redis-cluster文件夾
redis-trib.rb create --replicas 1 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384
--replicas 1 表示每個(gè)主數(shù)據(jù)庫擁有從數(shù)據(jù)庫個(gè)數(shù)為1。master節(jié)點(diǎn)不能少于3個(gè),所以我們用了6個(gè)redis
集群啟動(dòng)腳本.bat中的命令如下
start /d "D:\redis-cluster\Redis-6379" start.bat start /d "D:\redis-cluster\Redis-6380" start.bat start /d "D:\redis-cluster\Redis-6381" start.bat start /d "D:\redis-cluster\Redis-6382" start.bat start /d "D:\redis-cluster\Redis-6383" start.bat start /d "D:\redis-cluster\Redis-6384" start.bat ping /n 3 127.1>nul ruby redis-trib.rb create --replicas 1 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384
此操作要在第6步執(zhí)行完畢,集群創(chuàng)建好之后執(zhí)行,不然集群創(chuàng)建失敗,給集群設(shè)置密碼,密碼需要一致不然會(huì)失敗
masterauth redispassword requirepass redispassword
7.和springboot2.3.x集成 POM.XML
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.3.RELEASE</version> <relativePath/> </parent> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
application.yml配置
spring: redis: cluster: nodes: 127.0.0.1:6379,127.0.0.1:6380,127.0.0.1:6381,127.0.0.1:6382,127.0.0.1:6383,127.0.0.1:6384
8.RedisUtil.java
package com.example.elasticsearchdemo.util; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.*; import org.springframework.stereotype.Component; import java.io.Serializable; import java.util.List; import java.util.Set; import java.util.concurrent.TimeUnit; @Component public class RedisUtils { @Autowired private RedisTemplate redisTemplate; /** * 寫入緩存 * * @param key * @param value * @return */ public boolean set(final String key, Object value) { boolean result = false; try { ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue(); operations.set(key, value); result = true; } catch (Exception e) { e.printStackTrace(); } return result; } /** * 寫入緩存設(shè)置時(shí)效時(shí)間 * * @param key * @param value * @return */ public boolean set(final String key, Object value, Long expireTime) { boolean result = false; try { ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue(); operations.set(key, value); redisTemplate.expire(key, expireTime, TimeUnit.SECONDS); result = true; } catch (Exception e) { e.printStackTrace(); } return result; } /** * 批量刪除對應(yīng)的value * * @param keys */ public void remove(final String... keys) { for (String key : keys) { remove(key); } } /** * 批量刪除對應(yīng)的value (帶事務(wù),業(yè)務(wù)代碼中用到事務(wù),則需用此方法) * * @param keys */ public void removeTransactional(final String... keys) { for (String key : keys) { removeTransactional(key); } } /** * 批量刪除key * * @param pattern */ public void removePattern(final String pattern) { Set<Serializable> keys = redisTemplate.keys(pattern); if (keys.size() > 0) redisTemplate.delete(keys); } /** * 刪除對應(yīng)的value * * @param key */ public void remove(final String key) { if (exists(key)) { redisTemplate.delete(key); } } /** * 判斷緩存中是否有對應(yīng)的value * * @param key * @return */ public boolean exists(final String key) { return redisTemplate.hasKey(key); } /** * 讀取緩存 * * @param key * @return */ public Object get(final String key) { ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue(); return operations.get(key); } /** * 哈希 添加 * * @param key * @param hashKey * @param value */ public void hmSet(String key, Object hashKey, Object value) { HashOperations<String, Object, Object> hash = redisTemplate.opsForHash(); hash.put(key, hashKey, value); } /** * 哈希獲取數(shù)據(jù) * * @param key * @param hashKey * @return */ public Object hmGet(String key, Object hashKey) { HashOperations<String, Object, Object> hash = redisTemplate.opsForHash(); return hash.get(key, hashKey); } /** * 列表添加 * * @param k * @param v */ public void lPush(String k, Object v) { ListOperations<String, Object> list = redisTemplate.opsForList(); list.rightPush(k, v); } /** * 列表獲取 * * @param k * @param l * @param l1 * @return */ public List<Object> lRange(String k, long l, long l1) { ListOperations<String, Object> list = redisTemplate.opsForList(); return list.range(k, l, l1); } /** * 集合添加 * * @param key * @param value */ public void add(String key, Object value) { SetOperations<String, Object> set = redisTemplate.opsForSet(); set.add(key, value); } /** * 集合獲取 * * @param key * @return */ public Set<Object> setMembers(String key) { SetOperations<String, Object> set = redisTemplate.opsForSet(); return set.members(key); } /** * 有序集合添加 * * @param key * @param value * @param scoure */ public void zAdd(String key, Object value, double scoure) { ZSetOperations<String, Object> zset = redisTemplate.opsForZSet(); zset.add(key, value, scoure); } /** * 有序集合獲取 * * @param key * @param scoure * @param scoure1 * @return */ public Set<Object> rangeByScore(String key, double scoure, double scoure1) { ZSetOperations<String, Object> zset = redisTemplate.opsForZSet(); return zset.rangeByScore(key, scoure, scoure1); } /** * 加鎖 * * @param key * @return */ public boolean tryLock(String key) { try { long currTime = System.currentTimeMillis(); //加鎖成功 return redisTemplate.opsForValue().setIfAbsent(key, currTime); } finally { redisTemplate.expire(key, 5, TimeUnit.SECONDS); } } }
9.RedisController
package com.test; import com.test.RedisUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class RedisController { @Autowired private RedisUtils redisUtils; @RequestMapping("/redis") public String redis(String key){ redisUtils.set(key,"sdfsdfsdf"); return "OK-"+key; } }
用工具連接每個(gè)redis,可以看到每個(gè)redis里面都有key和value了。redis cluster環(huán)境搭建成功
【設(shè)置redis最大可使用內(nèi)存】
maxmemory 100mb
【達(dá)到最大內(nèi)存的策略】
maxmemory-policy noeviction 拒絕寫入
【可能出現(xiàn)的坑】
在執(zhí)行set操作的時(shí)候 【error】CLUSTERDOWN Hash slot not served
沒有分配槽,因?yàn)閞edis集群要分配16384個(gè)槽來儲(chǔ)存數(shù)據(jù),那么沒有分配槽則報(bào)如上錯(cuò)誤
什么原因呢?
原因是最后使用ruby來搭建集群的時(shí)候錯(cuò)誤操作
redis-trib.rb create --replicas 1 127.0.0.1:6379,127.0.0.1:6380,127.0.0.1:6381,127.0.0.1:6382,127.0.0.1:6383,127.0.0.1:6384
上面執(zhí)行完時(shí)會(huì)出現(xiàn)提示
Can I set the above configuration? (type 'yes' to accept):
你需要輸入yes,而并非縮寫 y
就是這個(gè)錯(cuò)誤引起的分配槽失敗。
上述內(nèi)容就是windows下如何把搭建redis cluster集群及配置springboot2.3.x,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)頁題目:windows下如何把搭建rediscluster集群及配置springboot2.3.x
URL網(wǎng)址:http://aaarwkj.com/article18/iidcdp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、標(biāo)簽優(yōu)化、手機(jī)網(wǎng)站建設(shè)、面包屑導(dǎo)航、全網(wǎng)營銷推廣、外貿(mào)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)