Redis安装配置及整合SpringBoot的方法

发布时间:2022-03-29 13:58:12 作者:iii
来源:亿速云 阅读:202

这篇文章主要介绍“Redis安装配置及整合SpringBoot的方法”,在日常操作中,相信很多人在Redis安装配置及整合SpringBoot的方法问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Redis安装配置及整合SpringBoot的方法”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

安装 Redis

# 下载Redis
wget https://download.redis.io/releases/redis-6.0.9.tar.gz

# 解压 redis
tar -zxvf redis-6.0.9.tar.gz

# 安装 gcc 环境, 安装过 忽略
yum -y install gcc-c++

cd redis-5.0.5 

# 安装 
make && make install

Redis 配置启动脚本

# 拷贝 utils 目录下的 redis_init_script 到 /etc/init.d 目录下  redis_init_script 是启动脚本
cp utils/redis_init_script /etc/init.d

cd /etc/init.d

vim /etc/init.d/redis_init_script

# ----------------- redis_init_script start ---------------
#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.

### BEGIN INIT INFO
# Provides:     redis_6379
# Default-Start:        2 3 4 5
# Default-Stop:         0 1 6
# Short-Description:    Redis data structure server
# Description:          Redis data structure server. See https://redis.io
### END INIT INFO

# redis 默认端口
REDISPORT=6379
# redis 默认启动 redis-server 位置
EXEC=/usr/local/bin/redis-server
# redis redis-cli 位置
CLIEXEC=/usr/local/bin/redis-cli

# redis pid 位置 拼接了默认端口参数。 
PIDFILE=/var/run/redis_${REDISPORT}.pid
# redis 默认配置的conf 配置文件
CONF="/usr/local/redis/conf/redis.conf"

# $1 参数 为 start 或者 stop 
case "$1" in
    start)
        if [ -f $PIDFILE ]
        then
                echo "$PIDFILE exists, process is already running or crashed"
        else
                echo "Starting Redis server..."
				$EXEC $CONF
        fi
        ;;
    stop)
        if [ ! -f $PIDFILE ]
        then
                echo "$PIDFILE does not exist, process is not running"
        else
                PID=$(cat $PIDFILE)
                echo "Stopping ..."
                $CLIEXEC -p $REDISPORT shutdown
                while [ -x /proc/${PID} ]
                do
                    echo "Waiting for Redis to shutdown ..."
                    sleep 1
                done
                echo "Redis stopped"
        fi
        ;;
    *)
        echo "Please use start or stop as first argument"
        ;;
esac

# ----------------- redis_init_script end ---------------

# 保存之后启动redis 
./redis_init_script start 

# 停止 redis 
./redis_init_script stop

配置Redis 开启自启动

# 在以下位置加上一段注释
#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.

### BEGIN INIT INFO
# Provides:     redis_6379
# Default-Start:        2 3 4 5
# Default-Stop:         0 1 6
# Short-Description:    Redis data structure server
# Description:          Redis data structure server. See https://redis.io
### END INIT INFO

#chkconfig: 22345 10 90
#desccription: Start and Stop redis

:wq! # 保存 

# 注册redis 到开机自启动
chkconfig redis_init_script on

Redis 配置文件解析

# 设置后台运行  yes 后台运行, no 前台运行
daemonize yes 

# pidfile pid 目录文件
pidfile /var/run/redis_6379.pid

# dir redis 的工作空间。必须写一个目录。不能写一个文件名
dir /usr/local/redis/working

# bind 哪些ip地址可以访问 redis-server 0.0.0.0 任何地址都可以访问
bind 0.0.0.0

# requirepass 设置 redis 链接密码
requirepass 584521

SpringBoot 整合 Redis

<dependency>
	<goupId>org.springframework.boot</goupId>
    <artifactId>spring-boot-starter-data-redis</aartifactId>
</dependency>
spring:
	redis:
		database: 0 				# 数据库
		host: 127.0.0.1				# redis 地址
		port: 6379					# redis 端口
		password: 584521			# redis 密码		
@ApiIgnore
@RestController
@RequestMapping("redis")
public class RedisController {
    
    @Autowired
    private RedisTemplate redisTemplate;
    
    @GetMapping("/set")
    public Object set(String key, String value) {
        redisTemplate.opsForValue().set(key, value);
        return "ok";
    }
    
    @GetMapping("get")
    public Object get(String key) {
		Object value = redisTemplate.opsForValue().get(key);
        return value;
    }
    
    @GetMapping("delete")
    public Object delete(String key) {
        redisTemplate.delete(key);
        return "ok";
    }
}

Redis 持久化机制

Redis RDB 机制 Redis DataBase

# 打开 redis.conf 文件
vim redis.conf 

# redis 工作空间
dir /usr/local/redis/working 

# rdb 持久化文件名
dbfilename dump.rdb

# save 保存到硬盘。多少时间内发生了多少次改变
save 900 1
save 300 10 
save 60 10000

# stop-writes-on-bgsave-error 保存时,发生错误停止写入操作
stop-writes-on-bgsave-error yes

# rdbcompression 压缩 rdb 文件 如果像节省cpu性能开销。 就可以关闭 no 
rdbcompression yes 

# rdbchecksum 压缩 rdb 文件以后。 是否要检验 rdb 文件。 会有百分之 10 的性能损耗
rdbchecksum yes

Redis AOF 机制 Append Only File

Redis 主从架构

Redis 缓存过期处理和内存淘汰机制

Redis 哨兵模式

原来一主二从里面, Master 节点一旦宕机。 我们就无法写入数据了。因为主节点宕机。从节点无法写入数据。只可以读取数据。

# 开启保护模式后 绑定 ip 哪个 ip 才能够连接
# bind 127.0.0.1
# yes 开启绑定模式 ,。 no 不开启
protected-mode no 
# 端口号
port 26379

# daemonize 是否开启后台
daemonize yes

# pid 文件位置。 和 redis 不是同一个进程 
pidfile /var/run/redis-sentinel.pid 

# 配置 sentinel 的日志文件
logfile /usr/local/redis/logs/sentinel/redis-sentinel.log

# dir sentinel 的工作空间
dir /usr/local/redis/sentinel

# sentinel monitor <master-group-name> <ip> <port> <quorum> 配置监听的 master 名称、 以及 ip 地址, 端口号。 
sentinel monitor xh-master 192.168.1.191 6379 2

# sentinel auth-pass mymaster MySUPER--secret-0123passw0rd
sentinel auth-pass xh-master 584521

# sentinel down-after-milliseconds <master-name> <milliseconds> master 名称。 哨兵认为master 失败的时间段
sentinel down-after-milliseconds xh-master 10000 

# sentinel parallel-syncs <master-name> <numslaves> master 名称 以及同时需要同步几个 slave 节点的数据。
sentinel parallel-syncs xh-master 1

# sentinel failover-timeout <master-name> <milliseconds> master 名称。 故障转移超时时间。 
sentinel failover-timeout xh-master 180000
scp ./sentinel.conf root@192.168.1.192:/usr/local/redis/

scp ./sentinel.conf root@192.168.1.193:/usr/local/redis/
# 启动时会报一个错误 
redis-sentinel  
# 说 Sentinel 没有指定配置文件
6031:X 08 Nov 21:12:20.727 # Sentinel started without a config file. Exiting...

# 指定配置文件启动
redis-sentinel /usr/local/redis/sentinel.conf 

安装此方式启动 slave 1  和  slave 2
4-4 解决原Master恢复后不同步问题
在本节课中,相信细心的同学会发现原来的Master(191)恢复成Slave后,他的同步状态不OK,状态为 master_link_status:down ,这是为什么呢?
这是因为我们只设置了192和193的 masterauth ,这是用于同步master的数据,但是191一开始是master是不受影响的,当master转变为slave后,由于他没有
auth ,所以他不能从新的master同步数据,随之导致 info replication 的时候,同步状态为 down ,所以只需要修改 redis.conf 中的 masterauth 为 584521
一般master数据无法同步给slave的方案检查为如下:
1. 网络通信问题,要保证互相ping通,内网互通。
2. 关闭防火墙,对应的端口开发(虚拟机中建议永久关闭防火墙,云服务器的话需要保证内网互通)。
3. 统一所有的密码,不要漏了某个节点没有设置。

# 查看xh-master下的master节点信息
sentinel master xh-master
# 查看xh-master下的slaves节点信息
sentinel slaves xh-master
# 查看xh-master下的哨兵节点信息
sentinel sentinels xh-master

Redis 使用 哨兵模式整合 SpringBoot 中

spring:
	redis:
		database: 1
		password: 584521
		sentinel: 
			master: xh-master  	# 配置 master 的名称
			nodes: 192.168.1.191:26379,192.168.1.192:26379,192.168.1.193:26379	# 配置 redis 哨兵的 端口号以及 ip

Redis Cluster集群

搭建 Redis-cluster 集群

# 开启 cluster 集群  yes 开启, no 关闭
cluster-enabled yes

# cluster-config-file nodes-6379.conf cluster-config-file cluster 节点配置文件
cluster-config-file nodes-6379.conf

# cluster-node-timeout 15000 配置 redis-cluster 超时时间
cluster-node-timeout 1500
# 开启 aof 持久化
appendonly yes

# 修改万配置文件后。删除 aof 和 rbd 文件 如果不删除可能就会报错
rm -rf *.aof 
rm -rf *.rdb 

# 停止 redis 
/etc/init.d/redis_init_script stop 

# 启动 redis
/etc/init.d/redis_init_script start
# 如果设置密码了记得设置密码
redis-cli -a 584521 --cluster create 192.168.1.201:6379 192.168.1.202:6379 192.168.1.203:6379 192.168.1.204:6379 192.168.1.205:6379 192.168.1.206:6379 --cluster-replicas 1

Redis安装配置及整合SpringBoot的方法

# 上面最后会询问你是否要配置该集群了 
Can I set the above configuration? (type 'yes' to accept) : yes
redis-cli -a 584521 --cluster check 192.168.1.201:6379

Redis Slots 概念

[OK] All 16384 slots covered

Redis安装配置及整合SpringBoot的方法

# 查看集群的信息
redis-cli-c -a 584521 -h 192.168.1.202 -p6379 
# 查看节点的信息
cluster nodes

Redis SpringBoot 中整合 Cluster 集群

spring:
	redis:
		password: 584521
		cluster: 
			nodes: 192.168.1.201:6379 192.168.1.202:6379 192.168.1.203:6379 192.168.1.204:6379 192.168.1.205:6379 192.168.1.206:6379

缓存穿透

缓存击穿

缓存雪崩

到此,关于“Redis安装配置及整合SpringBoot的方法”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

推荐阅读:
  1. SpringBoot怎么整合Redis
  2. SpringBoot怎样整合redis的缓存?

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

redis springboot

上一篇:JavaScript如何提取页面代码中所有网址

下一篇:JavaScript如何动态加载脚本文件

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》