redis下载 ,看里面的教程
走完这里面的命令就好了,然后
到redis目录下
cd redis-5.0.5
编辑redis.conf配置
vim redis.conf
开启密码:把# requirepass foobared改为# requirepass 123456
开启AOF持久化:把appendonly no改为appendonly yes
连接远程:吧 # bind 127.0.0.1
注释掉,要不然你就一直只能连本地
最后一步,很多人掉坑里
# 启动 记得要带你的配置文件一起运行哟
./redis-server ../redis.conf &
# 关闭
redis-cli SHUTDOWN
#查看redis服务
ps -ef | grep -i redis
# 砍掉某个端口服务
kill -9 xxxxx
好,现在拿出你的代码测一测
/**
* service
**/
public interface MsgService {
String setMsg(String key,String msg);
String getMsg(String key);
}
@Service
public class MsgServiceImp implements MsgService {
@Autowired
private RedisTemplate redisTemplate;
@Override
public String setMsg(String key, String msg) {
redisTemplate.opsForValue().set(key,msg);
return "success";
}
@Override
public String getMsg(String key) {
return (String) redisTemplate.opsForValue().get(key);
}
}
@RestController
@Api(value = "redis 测试")
@RequestMapping("/msg")
public class MsgController {
@Autowired
private MsgService msgService;
@GetMapping("/set")
@ApiOperation(value = "赋值")
public String setMsg(@RequestParam(value = "key")String key,@RequestParam(value = "msg")String msg){
return msgService.setMsg(key, msg);
}
@GetMapping("/get")
@ApiOperation(value = "用key取值")
public String getMsg(@RequestParam(value = "ket")String key){
return msgService.getMsg(key);
}
}
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
application.yml
spring:
redis:
host: 49.235.112.142
port: 6379
最简单的SpringBoot+redis
最后可能还会出现一个问题就是
解读Redis报错:“MISCONF Redis is configured to save RDB snapshots”
这个错发生在celery中用redis 作为backend 传输返回结果时发生的错误,以前也遇过这个错,但是没有放在心上,今天偶然发现所有节点都出现了该报错,并且影响了队列的正常消费,所以时候对这个错误一探究竟了。
将配置项stop-writes-on-bgsave-error 设置为no,可以在redis命令行里配置,也可以在redis.conf配置文件里改。