一.Redis持久化方式
1.1 RDB快照
說明:RDB快照主要以二進(jìn)制文件的形式進(jìn)行存儲數(shù)據(jù),主要以文件名dump.rdb進(jìn)行存儲,主要設(shè)置redis.conf里面設(shè)置’save 60 1000’命令可以開啟, 表示在60秒內(nèi)操作1000次進(jìn)行一次備份數(shù)據(jù)。在客戶端執(zhí)行save(同步)和bgsave(異步操作)。
redis.conf
#修改持久化文件存放的位置
dir /usr/local/redis-5.0.3/data/
#開啟RDB快照存儲
save 900 1 #表示在900秒中操作1次
save 30 10 #表示在30秒中操作10次
save 60 1000 #表示在60秒中操作100次
啟動redis相關(guān)命令
#RDB存放的文件名修改
dbfilename dump.db
#啟動redis服務(wù)
src/redis-server redis.conf
#查看redis服務(wù)進(jìn)程號id
ps -ef | grep redis
#結(jié)束redis服務(wù)
kill 2889(進(jìn)程號id)
#啟動redis編輯命令
src/redis-cli -p 6379
1.2 AOF重寫
說明:主要把文件生成為.aof文件,里面主要是<key,value>形式存儲。
開啟AOF在redis.conf配置
#開啟AOF
appendonly yes
#下面三項開啟一項
appendfsync always:每次有新命令追加到 AOF 文件時就執(zhí)行一次 fsync ,非常慢,也非常安全。
appendfsync everysec:每秒 fsync 一次,足夠快,并且在故障時只會丟失 1 秒鐘的數(shù)據(jù)。
appendfsync no:從不 fsync ,將數(shù)據(jù)交給操作系統(tǒng)來處理。更快,也更不安全的選擇。
#滿足60m后進(jìn)行從寫,下一次是這一次的一倍
auto‐aof‐rewrite‐min‐size 64mb
auto‐aof‐rewrite‐percentage 100
1.3 Redis 4.0混合持久化
說明:在AOF文件開啟的情況下,當(dāng)文件開始備份的時候?qū)贏OF備份文件中以二進(jìn)制文件形式進(jìn)行備份,當(dāng)時備份之后的值,還是以AOF<key,value>形式進(jìn)行備份。
開啟AOF在redis.conf配置
aof‐use‐rdb‐preamble yes
啟動redis混合模式重寫
#開啟混合模式存儲
bgrewriteaof
二.Redis搭建主從與哨兵架構(gòu)
2.配置主從架構(gòu)
1、復(fù)制一份redis.conf文件
2、將相關(guān)配置修改為如下值:
port 6380
pidfile /var/run/redis_6380.pid # 把pid進(jìn)程號寫入pidfile配置的文件
logfile "6380.log"
dir /usr/local/redis-5.0.3/data/6380 # 指定數(shù)據(jù)存放目錄
# 需要注釋掉bind
# bind 127.0.0.1(bind綁定的是自己機(jī)器網(wǎng)卡的ip,如果有多塊網(wǎng)卡可以配多個ip,代表允許客戶端通過機(jī)器的哪些網(wǎng)卡ip去訪問,內(nèi)網(wǎng)一般可以不配置bind,注釋掉即可)
3、配置主從復(fù)制
replicaof 192.168.0.60 6379 # 從本機(jī)6379的redis實例復(fù)制數(shù)據(jù),Redis 5.0之前使用slaveof
replica-read-only yes # 配置從節(jié)點只讀
4、啟動從節(jié)點
redis-server redis.conf
5、連接從節(jié)點
redis-cli -p 6380
6、測試在6379實例上寫數(shù)據(jù),6380實例是否能及時同步新修改數(shù)據(jù)
7、可以自己再配置一個6381的從節(jié)點
注意:在相關(guān)redis文件夾啟動,查看配置的文件夾是否存在,不存在,需要手工建立相關(guān)的文件夾
2.1 配置6380redis從服務(wù)器
1.在conf下復(fù)制redis6379改名為redis6380.conf
2.修改redis6380.conf配置文件
port 6380
pidfile /var/run/redis_6380.pid # 把pid進(jìn)程號寫入pidfile配置的文件
logfile "6380.log"
dir /usr/local/redis-5.0.3/data/6380 # 指定數(shù)據(jù)存放目錄
# 需要注釋掉bind
# bind 127.0.0.1
replicaof 192.168.2.66 6379 # 從本機(jī)6379的redis實例復(fù)制數(shù)據(jù),Redis 5.0之前使用slaveof
replica-read-only yes # 配置從節(jié)點只讀
2.2 配置6381redis從服務(wù)器
1.在conf下復(fù)制redis6379改名為redis6381.conf
2.修改redis6381.conf配置文件
port 6381
pidfile /var/run/redis_6381.pid # 把pid進(jìn)程號寫入pidfile配置的文件
logfile "6381.log"
dir /usr/local/redis-5.0.3/data/6381 # 指定數(shù)據(jù)存放目錄
# 需要注釋掉bind
# bind 127.0.0.1
replicaof 192.168.2.66 6379 # 從本機(jī)6379的redis實例復(fù)制數(shù)據(jù),Redis 5.0之前使用slaveof
replica-read-only yes # 配置從節(jié)點只讀
2.3 啟動6379、6380、6381服務(wù),看看是否配置成功
src/redis-service conf/redis6379.conf
src/redis-service conf/redis6380.conf
src/redis-service conf/redis6380.conf
src/redis-cli -p 6379
info
2.4 java代碼連接redis
2.4.1 關(guān)閉linux防火墻(在linux環(huán)境進(jìn)行操作)
systemctl stop firewalld # 臨時關(guān)閉防火墻
systemctl disable firewalld # 禁止開機(jī)啟動
2.4.2 導(dǎo)入相關(guān)的redis pomjar包
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
2.4.3 建立jave連接
package com.tuling.jedis;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.Pipeline;
import java.util.List;
public class redisTest {
public static void main(String[] args) {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(20);
jedisPoolConfig.setMaxIdle(10);
jedisPoolConfig.setMinIdle(5);
JedisPool jedisPool = new JedisPool(jedisPoolConfig, "192.168.1.66", 6379, 10000, null);
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
//單挑數(shù)據(jù)導(dǎo)入
System.out.println(jedis.set("single","Nickel"));
System.out.println(jedis.get("single"));
//******* 管道示例 ********
Pipeline pl = jedis.pipelined();
for(int i=0;i<10;i++){
pl.incr("pipeline");
pl.set("n"+i,"nickel");
//模擬管道報錯
//pl.setbit("nickel", -1, true);
}
List<Object> results=pl.syncAndReturnAll();
System.out.println(results);
}catch (Exception e){
e.printStackTrace();
}finally {
if (jedis != null)
jedis.close();
}
}
}
2.4 配置哨兵
2.4.1復(fù)制sentinel.conf到config下,分別名字叫sentinel-26379.conf、sentinel-26380.conf、sentinel-26381.conf
2.4.2配置26379哨兵
port 26379
daemonize yes
pidfile "/var/run/redis-sentinel-26379.pid"
logfile "26379.log"
dir "/usr/local/redis-5.0.3/data"
# sentinel monitor <master-redis-name> <master-redis-ip> <master-redis-port> <quorum>
# quorum是一個數(shù)字,指明當(dāng)有多少個sentinel認(rèn)為一個master失效時(值一般為:sentinel總數(shù)/2 + 1),master才算真正失效
sentinel monitor mymaster 192.168.0.60 6379 2 # mymaster這個名字隨便取,客戶端訪問時會用到
2.4.3配置26380哨兵
port 26380
daemonize yes
pidfile /var/run/redis-sentinel-26380.pid
logfile "log/26380.log"
dir /usr/local/redis-5.0.3/data/26380
sentinel monitor mymaster 192.168.1.66 6379 2 # mymaster這個名字隨便取,客戶端訪問時會用到
2.4.4配置26381哨兵
port 26381
daemonize yes
pidfile /var/run/redis-sentinel-26381.pid
logfile "log/26381.log"
dir /usr/local/redis-5.0.3/data/26381
sentinel monitor mymaster 192.168.1.66 6379 2 # mymaster這個名字隨便取,客戶端訪問時會用到
2.4.5啟動哨兵
src/redis-sentinel conf/company/sentinel-26379.conf
src/redis-sentinel conf/company/sentinel-26380.conf
src/redis-sentinel conf/company/sentinel-26381.conf
src/redis‐cli ‐p 26379
2.4.5啟動哨兵
2.4.6 java代碼連接哨兵
package com.tuling.jedis;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisSentinelPool;
import java.util.HashSet;
import java.util.Set;
public class SentinelTest {
public static void main(String[] args) {
JedisPoolConfig config=new JedisPoolConfig();
config.setMaxTotal(20);
config.setMaxIdle(10);
config.setMinIdle(5);
String masterName="mymaster";
Set<String> sentinels=new HashSet<String>();
sentinels.add(new HostAndPort("192.168.1.66",26379).toString());
sentinels.add(new HostAndPort("192.168.1.66",26380).toString());
sentinels.add(new HostAndPort("192.168.1.66",26381).toString());
JedisSentinelPool jedisSentinelPool=new JedisSentinelPool(masterName,sentinels,config,3000,null);
Jedis jedis=null;
try{
jedis=jedisSentinelPool.getResource();
System.out.println(jedis.set("age","18"));
System.out.println(jedis.get("age"));
}catch (Exception e){
e.printStackTrace();
}finally {
if(jedis!=null){
jedis.close();
}
}
}
}
2.5 Spring Boot整合哨兵
2.5.1引入jar
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
2.5.2配置application.yml文件
server:
port: 8080
spring:
redis:
sentinel:
master: mymaster
nodes: 192.168.1.66:26379,192.168.1.66:26380,192.168.1.66:26381
jedis:
pool:
max-idle: 50
min-idle: 10
max-active: 100
max-wait: 1000
datasource: 0
timeout: 3000
2.5.3啟動java代碼訪問
package com.redis;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class IndexController {
private static final Logger logger = LoggerFactory.getLogger(IndexController.class);
@Autowired
private StringRedisTemplate stringRedisTemplate;
/**
* 測試節(jié)點掛了哨兵重新選舉新的master節(jié)點,客戶端是否能動態(tài)感知到
*
* @throws InterruptedException
*/
@RequestMapping("/test_sentinel")
public void testSentinel() throws InterruptedException {
int i = 1;
while (true){
try {
stringRedisTemplate.opsForValue().set("zhuge"+i, i+""); //jedis.set(key,value);
System.out.println("設(shè)置key:"+ "zhuge" + i);
i++;
Thread.sleep(1000);
}catch (Exception e){
logger.error("錯誤:", e);
}
}
}
}
2.5.4 StringRedisTemplate與RedisTemplate詳解
spring 封裝了 RedisTemplate 對象來進(jìn)行對redis的各種操作,它支持所有的 redis 原生的 api。在RedisTemplate中提供了幾個常用的接口方法的使用,分別是:
- private ValueOperations<K, V> valueOps;
- private HashOperations<K, V> hashOps;
- private ListOperations<K, V> listOps;
- private SetOperations<K, V> setOps;
- private ZSetOperations<K, V> zSetOps;
RedisTemplate中定義了對5種數(shù)據(jù)結(jié)構(gòu)操作
- redisTemplate.opsForValue();//操作字符串
- redisTemplate.opsForHash();//操作hash
- redisTemplate.opsForList();//操作list
- redisTemplate.opsForSet();//操作set
- redisTemplate.opsForZSet();//操作有序set
三.Redis隊列與stream、Redis 6多線程
1.stream流程圖
2.redis中stream相關(guān)命令
1.生產(chǎn)端命令
xadd 追加消息
xdel 刪除消息
xrange 獲取消息列表,會自動過濾已經(jīng)刪除的消息
xlen 消息長度
//消息格式為
xadd streamtest * name mark age 18
**xadd表示stream流中的命令,streamtest表示流的名字,*表示自動生成唯一的ID號,name mark age 18表示消息內(nèi)容。 **文章來源:http://www.zghlxwxcb.cn/news/detail-497916.html
//streamtest 流中增加三條數(shù)據(jù)
xadd streamtest * name mark age 18
xadd streamtest * name james age 23
xadd streamtest * name king age 19
//獲取streamtest 流中數(shù)據(jù)的長度
xlen streamtest
//獲取streamtest 流中所有的數(shù)據(jù)
xrange streamtest - +
//獲取streamtest 流中獲取從開始到流ID的數(shù)據(jù)
xrange streamtest - 1700726396835-0
//獲取streamtest 流中數(shù)據(jù)從某個位置到某個位置
xrange streamtest 1700726302998-0 1700726396835-0
文章來源地址http://www.zghlxwxcb.cn/news/detail-497916.html
到了這里,關(guān)于Redis持久化說明及其單臺Linux服務(wù)器搭建Redis集群架構(gòu)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!