设为首页 加入收藏

TOP

Linux服务器使用Redis作为数据缓存,并用log4j2进行日志记录(一)
2023-09-23 15:44:11 】 浏览:177
Tags:Linux Redis 并用 log4j2

前言

个人网站使用Vue作为前端,SpringBoot作为后端,MySQL作为数据库,但前端每次请求都会从MySQL数据库中读取数据,而MySQL数据库的数据是存储于服务器磁盘中,所以响应速度有一定影响。之前了解过一点Redis数据库,该数据库数据存储于内存中(也可以持久化于磁盘中),数据读取速度就会比存储于磁盘中的MySQL快很多,故想把Redis数据库应用于该网站项目中。

一、安装Redis

Linux系统安装Redis比较简单,可以直接通过命令行安装,安装过程比较简单,在此就不赘述,下列出一些常用命令

# 更新apt
sudo apt update
# 安装Redis
sudo apt-get install redis-server
# 设置密码(在配置文件redis.conf中,位置在 /etc/redis/redis.conf)
requirepass ******

# 启动Redis服务
service redis-server start
# 停止Redis服务
service redis-server stop
# 重启Redis服务
service redis-server restart

# 启动Redis客户端
redis-cli
# 测试Redis是否连接
127.0.0.1:6379> ping

注意:需要修改Redis配置文件中的保护模式“protected-mode"为修改为no,否则会出现无法连接的情况

# 修改保护模式为no
# protected-mode yes
protected-mode no

# 若不修改可能无法连接Redis,出现以下错误
org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to 127.0.0.1:6379
        at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$ExceptionTranslatingConnectionProvider.translateException(LettuceConnectionFactory.java:1689)
        at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$ExceptionTranslatingConnectionProvider.getConnection(LettuceConnectionFactory.java:1597)
······
······
······
Caused by: io.lettuce.core.RedisConnectionException: Unable to connect to 127.0.0.1:6379
        at io.lettuce.core.RedisConnectionException.create(RedisConnectionException.java:78)
        at io.lettuce.core.RedisConnectionException.create(RedisConnectionException.java:56)
······
······
······
Caused by: java.lang.IllegalStateException: RedisHandshakeHandler not registered
        at io.lettuce.core.AbstractRedisClient.lambda$initializeChannelAsync0$6(AbstractRedisClient.java:431)
        at io.netty.util.concurrent.DefaultPromise.notifyListener0(DefaultPromise.java:590)
······
······
······

二、SpringBoot项目集成Redis

1、pom.xml添加依赖

<!-- Redis -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

2、Redis数据库连接配置 application.yml

# Redis
redis:
# 服务器地址
host: 127.0.0.1
# 服务器端口号
port: 6379
# 使用的数据库索引
database: 0
# 连接超时时间
timeout: 10000
# 设置密码
password: ******
lettuce:
  # 连接池
  pool:
    # 最大阻塞等待时间,负数表示没有限制
    max-wait: -1
    # 连接池中最大空闲
    max-idle: 5
    # 连接池中最小空闲
    min-idle: 0
    # 连接池最大连接数
    max-active: 20

3、Redis配置类RedisConfig

package cn.huskysir.Config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.spring
首页 上一页 1 2 3 4 5 6 7 下一页 尾页 1/9/9
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇MyBatis中#和$的区别 下一篇Spring Boot虚拟线程与Webflux在J..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目