spring boot + redis4.0.2 详细案例
来源:互联网 发布:金融网络销售怎么做的 编辑:程序博客网 时间:2024/06/11 04:13
本文大纲
1.从无到有。在centos7 中安装redis4.0
2.spring boot 配置 redis 案例
3.使用总结
一:从无到有。在centos7 中安装redis4.0
1.安装 oracle vm virtualBox 虚拟机。
下载地址:http://download.virtualbox.org/virtualbox/5.2.0/VirtualBox-5.2.0-118431-Win.exe。运行安装即可
配置项:网络链接方式---桥接网卡
2.安装centos7
2.1下载地址:http://mirrors.aliyun.com/centos/7/isos/x86_64/CentOS-7-x86_64-Minimal-1708.iso。
2.2添加虚拟系统。选择刚下载的centos7系统
2.3装机完成后需要配置静态IP地址,参考博客 http://blog.csdn.net/johnnycode/article/details/50184073 。里面有详细的配置方式。
2.4关闭防火墙 iptables
3.安装redis4.0
3.1 wget http://download.redis.io/releases/redis-4.0.2.tar.gz /usr/local/redis 将redis4下载到指定位置,我个人习惯放再user/local下面
3.2 解压
# cd /usr/local/redis
# tar xzvf redis-4.0.2.tar.gz
3.3 然后执行make编译源码
# cd redis-4.0.2
# make
3.4.make命令执行完成后,会在src目录下生成6个可执行文件,
分别是redis-server、redis-cli、redis-benchmark、redis-check-aof、redis-check-dump、redis-sentinel。我们只需要将redis-server,redis-cli 拷贝到/usr/local/bin/下面即可
3.5 因为我们需要使用spring boot 去操作redis。所以要修改redis.conf 配置文件。
# vi /usr/local/redis-4.0.2/redis.conf
- 1.将 bind 127.0.0.1 注释掉
- 2.设置访问密码 requirepass 123456
3.6 启动 redis
# redis-server /usr/local/redis-4.0.2/redis.conf
3.7 测试-需要另开一个客户端
# redis-cli
127.0.0.1:6379> auth 123456 -- 上面配置的密码
127.0.0.1:6379> set name csdn
OK
127.0.0.1:6379> get name
"csdn"
二。创建spring boot (我是用的eclipse 版本是 Neon 4.6 RC3)
1.新建spring boot 项目。选择redis
2.修改application.yml 配置文件
spring:
redis:
host: 192.168.1.xx
port: 6379
password: 123456
3.添加redis组件
@Component
public class RedisComponent {
@Autowired
private StringRedisTemplate stringRedisTemplate;
public void set(String key, String value) {
ValueOperations<String, String> ops = this.stringRedisTemplate.opsForValue();
if (!this.stringRedisTemplate.hasKey(key)) {
ops.set(key, value);
System.out.println("set key success");
} else {
// 存在则打印之前的value值
System.out.println("this key = " + ops.get(key));
}
}
public String get(String key) {
return this.stringRedisTemplate.opsForValue().get(key);
}
public void del(String key) {
this.stringRedisTemplate.delete(key);
}
}
4.测试类
private RedisComponent redisComponent;
public void set() {
roncooRedisComponent.set("name", "今天是个好日子");
}
public void get() {
System.out.println(roncooRedisComponent.get("name"));
}
@Test
public void del() {
roncooRedisComponent.del("name");
}
测试 get
测试 del
三:使用总结
这次做boot+redis配置遇到了一个小问题。就是没有配置redis密码。导致boot访问被拒。算是一个小小的经验教训吧。
- spring boot + redis4.0.2 详细案例
- maven打包案例-spring boot
- Spring boot项目搭建案例
- CentOS7安装Redis4.0.2
- Redis4.0.2安装
- linux 安装redis4.0.2
- redis4.0.2集群配置
- Spring Boot 详细日志配置
- Spring Boot Properties详细清单
- Spring Boot 1--新建简单Hello Spring Boot案例
- redis4.0.0 集群安装详细步骤
- spring boot 源码分析(一) 案例
- Spring Boot 2--项目属性配置案例
- Spring Boot 3---Controller使用案例
- Spring Boot 4--连接oracle数据库案例
- Spring Boot 集成 FreeMarker 详解案例
- Spring Boot 集成 FreeMarker 详解案例
- spring boot 返回json案例demo
- Kubernetes部署记录
- Tensorflow实战学习(八)【机器学习基础 线性回归】
- java多线程之Phaser
- Tensorflow实战学习(九)【对数几率回归】
- Tensorflow实战学习(十四)【卷积层、激活函数、池化层、归一化层、高级层】
- spring boot + redis4.0.2 详细案例
- Tensorflow实战学习(十)【softmax分类】
- Tensorflow实战学习(十一)【多层神经网络】
- LVM
- C和指针之字符串strspn、strcspn函数源代码的实现
- Tensorflow实战学习(十二)【卷积网络简述】
- 从零开始搞懂Android网络请求——异步加载ListView
- dubbo找不到dubbo.xsd报错,也可以应用其他场景
- HDU 2050 折线分割平面(根据直线递推)