一、Redis安装
1、下载压缩包传到虚拟机中
2、 解压文件,在文件夹中可以看到redis.conf
3、基本的环境安装
yum install gcc-c++
make
make install
4、redis的默认安装路径 /usr/local/bin下
5、在该文件夹下新建目录xconfig并把redis.conf复制过来
6、redis默认不是后台启动的,修改配置文件!
添加密码
7、查看防火墙并关闭,方便以后springboot连接
systemctl status firewalld
systemctl stop firewalld.service
8、启动Redis服务
redis-server xconfig/redis.conf
9、启动客户端测试
redis-cli -p 6379
二、Springboot整合Jedis
1、引入Jedis依赖
<!--jedis-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>${jedis.version}</version>
</dependency>
2、编写application.yml
spring:
redis:
port: 6379 #端口号
password: #输入redis数据库密码
host: #输入ip地址
jedis:
pool:
max-idle: 6 #最大空闲数
max-active: 10 #最大连接数
min-idle: 2 #最小空闲数
timeout: 2000 #连接超时
3、编写JedisConfig
package com.atieslab.czxt.cz.common.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
/**
* @ClassName: JedisConfig
* @DescriptionL Jedis配置
* @author: xzt
* @date: 2023年07月31日15:26
* @Version: 1.0
*/
@Configuration
@Slf4j
public class JedisConfig {
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private int port;
@Value("${spring.redis.password}")
private String password;
@Value("${spring.redis.timeout}")
private int timeout;
@Value("${spring.redis.jedis.pool.max-active}")
private int maxActive;
@Value("${spring.redis.jedis.pool.max-idle}")
private int maxIdle;
@Value("${spring.redis.jedis.pool.min-idle}")
private int minIdle;
@Bean
public JedisPool jedisPool(){
JedisPoolConfig jedisPoolConfig=new JedisPoolConfig();
jedisPoolConfig.setMaxIdle(maxIdle);
jedisPoolConfig.setMinIdle(minIdle);
jedisPoolConfig.setMaxTotal(maxActive);
JedisPool jedisPool=new JedisPool(jedisPoolConfig,host,port,timeout,password);
log.info("JedisPool连接成功:"+host+"\t"+port);
return jedisPool;
}
}
4、封装工具类JedisUtils
package com.atieslab.czxt.cz.common.util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
/**
* @ClassName: JedisUtils
* @DescriptionL Jedis工具类
* @author: xzt
* @date: 2023年07月31日15:31
* @Version: 1.0
*/
@Component
public class JedisUtils {
@Autowired
private JedisPool jedisPool;
/**
* 获取Jedis资源
*/
public Jedis getJedis(){
return jedisPool.getResource();
}
/**
* 释放Jedis连接
*/
public void close(Jedis jedis){
if(jedis!=null){
jedis.close();
}
}
}
5、测试
package com.atieslab.czxt.cz.service.impl;
import com.atieslab.czxt.cz.common.util.JedisUtils;
import com.atieslab.czxt.cz.mapper.CzxxMapper;
import com.atieslab.czxt.cz.model.vo.CzxxQueryInfoVo;
import com.atieslab.czxt.cz.service.CzxxService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import java.util.List;
/**
* @ClassName: CzxxServiceImpl
* @DescriptionL CzxxService实现类
* @author: xzt
* @date: 2023年07月21日14:02
* @Version: 1.0
*/
@Service
@Slf4j
public class CzxxServiceImpl implements CzxxService {
@Autowired
CzxxMapper czxxMapper;
@Autowired
JedisUtils jedisUtils;
@Override
public List<CzxxQueryInfoVo> getAllInfoList() {
List<CzxxQueryInfoVo> allInfoList = czxxMapper.getAllInfoList();
Jedis jedis = jedisUtils.getJedis();
String name = jedis.get("name");
log.info("redis得到的数据:{}",name);
jedisUtils.close(jedis);
return allInfoList;
}
}