新聞中心
本篇文章為大家展示了windows下如何把搭建redis cluster集群及配置springboot2.3.x,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
成都創(chuàng)新互聯(lián)是一家專注于網(wǎng)站設(shè)計、成都網(wǎng)站設(shè)計與策劃設(shè)計,路南網(wǎng)站建設(shè)哪家好?成都創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)10多年,網(wǎng)設(shè)計領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:路南等地區(qū)。路南做網(wǎng)站價格咨詢: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.修改每個redis文件夾下的redis.windows.conf配置文件
進(jìn)入到每個文件夾下,找到redis.windows.conf,然后改動這些參數(shù)
port 6379 cluster-enabled yes cluster-config-file nodes-6379.conf cluster-node-timeout 15000 appendonly yes
在每個文件夾下新建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.啟動每個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 表示每個主數(shù)據(jù)庫擁有從數(shù)據(jù)庫個數(shù)為1。master節(jié)點不能少于3個,所以我們用了6個redis
集群啟動腳本.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è)置密碼,密碼需要一致不然會失敗
masterauth redispassword requirepass redispassword
7.和springboot2.3.x集成 POM.XML
org.springframework.boot spring-boot-starter-parent 2.3.3.RELEASE org.springframework.boot spring-boot-starter-data-redis
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 { ValueOperationsoperations = redisTemplate.opsForValue(); operations.set(key, value); result = true; } catch (Exception e) { e.printStackTrace(); } return result; } /** * 寫入緩存設(shè)置時效時間 * * @param key * @param value * @return */ public boolean set(final String key, Object value, Long expireTime) { boolean result = false; try { ValueOperations 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 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 operations = redisTemplate.opsForValue(); return operations.get(key); } /** * 哈希 添加 * * @param key * @param hashKey * @param value */ public void hmSet(String key, Object hashKey, Object value) { HashOperations hash = redisTemplate.opsForHash(); hash.put(key, hashKey, value); } /** * 哈希獲取數(shù)據(jù) * * @param key * @param hashKey * @return */ public Object hmGet(String key, Object hashKey) { HashOperations hash = redisTemplate.opsForHash(); return hash.get(key, hashKey); } /** * 列表添加 * * @param k * @param v */ public void lPush(String k, Object v) { ListOperations list = redisTemplate.opsForList(); list.rightPush(k, v); } /** * 列表獲取 * * @param k * @param l * @param l1 * @return */ public List