1.redis安装
进入redis官网下载.gz文件后上传至服务器,再解压,由于需要make编译源文件,因此服务器需要安装gcc
yum install gcc
安装完成后,执行make命令进行编译
2.redis.conf配置
- 使用守护进程启动redis
redis采用单进程多线程的模式,将daemonize修改为yes代表开启守护进程模式
daemonize no 修改为daemonize yes - bind 127.0.0.1 开发时注释掉这一行,以便其他机器访问
- requirepass 设置密码
3.使用redis-cli连接redis-server
./redis-cli -a 123456
关闭redis-server:
127.0.0.1:6379> shutdown
4.使用Spring Boot连接redis
配置依赖
<!--redis依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
配置application.yml
spring:
datasource:
username: root
password: 123456
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
driver-class-name: com.mysql.cj.jdbc.Driver
redis:
host: 192.168.2.100
port : 6379
password: 123456
编写单元测试
@SpringBootTest
class Springbootdemo1ApplicationTests {
@Autowired
private RedisTemplate<String,String> redisTemplate;
@Test
public void testRedis() {
ValueOperations<String,String> valueStr=this.redisTemplate.opsForValue();
//存储一条数据
valueStr.set("hi","world");
String str=valueStr.get("hello");
//打印
System.out.println(str);
}
}
在CentOS中登陆redis客户端查看是否添加成功:
127.0.0.1:6379> keys *
1) "hi"
2) "hello"
查看win控制台,打印信息: