Redis的安装
首先从官网下载redis,目前的redis都是装在linux系统中,在官网中并没有提供windows版本
然后通过rz上传到服务器上的opt文件夹下,之后需要安装c语言环境,可以通过yum安装
也可以只安装gcc环境(可以通过gcc --version查看是否有gcc),如果显示没有则通过yum install gcc来进行安装
然后对刚刚上传的redis进行解压:tar -zxvf redis-6.2.6.tar.gz
解压完进入目录:redis-6.2.6
在redis-6.2.6目录下进行编译,通过make命令,最后通过make install进行安装,默认安装到 /usr/local/bin 目录下
然后进入目录,会发现redis安装成功
?redis-sentinel:redis集群使用
redis-server:redis服务器启动命令
redis-cli:客户端,操作入口
redis-benchmark:性能测试工具
redis-check-aof:修复有问题的aof文件
redis-check-dump:修复有问题的dump.rdb文件
?这个时候就安装完成了
启动方式
前台启动(不推荐)
命令行窗口不能关闭,否则服务器停止
redis-server
?此时这个窗口无法再进行其他操作,所以不推荐,关闭窗口则断开连接
使用ctrl C停止redis
后端启动
首先进入目录 redis-6.2.6 下,这里将redis.conf复制到etc下(也可以不复制)
?cp redis.conf /etc/redis.conf
?然后将后台启动设置daemonize no改成yes(修改etc下的redis.conf)
vi redis.conf
?完成修改后保存退出
然后就可以启动了,进入:cd /usr/local/bin 目录下启动
redis-server /etc/redis.conf
查看进程:
?ps -ef | grep redis
?客户端访问:redis-cli
?输入ping,如果显示pong则正常连接
关闭:通过shutdown或通过kill进程号
配置文件
此时只允许linux本地访问,将该行加注释,让他支持远程连接
将保护模式(防火墙)模式关闭,即将yes改为no
超时时间,0为永不超时
?然后可以设置密码
SpringBoot整合Redis
添加依赖
<!-- redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--连接池 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.6.0</version>
</dependency>
application.properties配置
spring.redis.host=101.200.239.97
spring.redis.port= 6379
#数据库索引
spring.redis.database=0
#连接超时时间
spring.redis.timeout=18000000
#连接池最大连接数(负数代表无限制)
spring.redis.lettuce.pool.max-active=20
#最大阻塞时间(负数代表无限制)
spring.redis.lettuce.pool.max-wait=-1
#连接池的最大空闲连接
spring.redis.lettuce.pool.max-idle=5
#连接池的最小空闲连接
spring.redis.lettuce.pool.min-idle=0
#密码
spring.redis.password=xxxxxx
package com.example.redistest.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.time.Duration;
/**
* 如果使用redis作为任务队列则启用这两个标注
*/
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
RedisSerializer<String> redisSerializer = new StringRedisSerializer();
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
template.setConnectionFactory(factory);
template.setKeySerializer(redisSerializer);
template.setValueSerializer(jackson2JsonRedisSerializer);
template.setHashValueSerializer(jackson2JsonRedisSerializer);
return template;
}
@Bean
public CacheManager cacheManager(RedisConnectionFactory factory){
RedisSerializer<String> redisSerializer = new StringRedisSerializer();
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofSeconds(600))
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer))
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))
.disableCachingNullValues();
RedisCacheManager cacheManager = RedisCacheManager.builder(factory)
.cacheDefaults(config).build();
return cacheManager;
}
}
测试
package com.example.redistest.controller;
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
@RequestMapping("test")
public class TestController {
@Autowired
private StringRedisTemplate redisTemplate;
@RequestMapping("redis")
public String testRedis(){
String test=redisTemplate.opsForValue().get("test");
System.out.println(test);
return test;
}
}
|