Maven + Springboot + redis 配置
前言
刚进入到Java 开发的世界,对于小白Java的我来说,使用Maven + SpringBoot 的项目下启动redis;
第一步 本地安装Redis 服务
关于redis的教程链接 点击这里:https://www.runoob.com/redis/redis-install.html
由于我是Mac 上开发因此安装如下:
1. 下载redis 安装包
$ wget http://download.redis.io/releases/redis-2.8.17.tar.gz
$ tar xzf redis-2.8..tar.gz
$ cd redis-2.8.
$ make
备注:上面的下载后可以解压到你自己的人以目录
2. 启动redis 服务
make完后 redis-2.8.17目录下会出现编译后的redis服务程序redis-server,还有用于测试的客户端程序redis-cli,两个程序位于安装目录 src 目录下:
下面启动redis服务.
$ cd src
$ ./redis-server
3. 启动服务后进行客户端链接测试
$ cd src
$ ./redis-cli
127.0.0.1:> set foo bar
OK
127.0.0.1:> get foo
"bar"
这里可以看到本地的地址和端口分别为: 127.0.0.1 和 6379 基本端口是不会变的。
到这里为子,我们的redis 本地服务算是安装完成。
第二步 idea 配置以及代码处理
1. pom.xml 文件中引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2. application.yml(或applicationx.xml)之中 配置redis服务信息
由于我的工程是使用yml 那么这里就以yml 为主:
# Redis 配置
redis:
database: 0 #数据库索引(默认为0)
host: 127.0.0.1
port: 6379 #默认链接端口
password: #默认为空
lettuce:
pool:
max-active: 8 #最大链接池
max-wait: -1 #最大阻赛等待时间(使用负值没有限制)默认为-1
max-idle: 8 #连接池中的最大空闲连接 默认 8
min-idle: 0
3. 编写简单代码测试
package com.king.controller; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*; @RestController
@RequestMapping("/redis")
@ResponseBody
public class RedisStringController {
@Autowired
private StringRedisTemplate stringRedisTemplate; @PutMapping("/string/put")
public void put(String key , @RequestParam(required = false,defaultValue = "default") String value){
stringRedisTemplate.opsForValue().set(key, value);
} @GetMapping("/string/get")
public Object get(String key){
return stringRedisTemplate.opsForValue().get(key);
}
}
上面测试代码分别写了 一个 put 提交 和一个get请求
4. 运行测试(Run)
我这里代码是PUT 方式,所以不能直接在浏览器上进行访问,这里我就使用最简单的curl命令进行请求。
打开自己的终端,执行以下命令:
执行put 数据存储操作
$ curl -X PUT --data 'key=kingbo&value=ok' http://127.0.0.1:8080/redis/string/put
执行get 获取操做
$ curl http://127.0.0.1:8080/redis/string/get\?key\=kingbo
ok
第三部 实现redis进行Java对象模型存储
在上述使用中,是无法存储对象的,存储对象的话需要使用RedisTemplate并且要使用响应的序列化机制,下面我们就来测试下:
1. 引入序列化的jar包,这里我们是使用jackson
在pom.xml 文件中,添加依赖
<!-- redis 对象存储相关配置-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
2. 在Java工程中添加 RedisConfig文件
package com.king.config;
/*
* 文档地址
* http://www.ityouknow.com/springboot/2016/03/06/spring-boot-redis.html
*
*
* */ import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer; @Configuration
//@EnableCaching
public class RedisConfig {
@Bean
public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); //使用StringRedisSerializer来序列化和反序列化redis的ke
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer()); //开启事务
redisTemplate.setEnableTransactionSupport(true); redisTemplate.setConnectionFactory(redisConnectionFactory);
return redisTemplate;
} }
3. 添加测试controller
package com.king.controller; import com.king.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*; @RequestMapping("/redis/object")
@RestController
public class RedisObjectController {
@Autowired
private RedisTemplate redisTemplate; @GetMapping("/get/{username}")
public Object get(@PathVariable("username") String username){
return redisTemplate.opsForValue().get(username);
} @PutMapping("/put")
public void put(String username,Integer age,Integer id){
User user= new User();
user.setId(id);
user.setAge(age);
user.setUsername(username);
redisTemplate.opsForValue().set(username,user);
} }
其中我这里的User 模型代码如下
package com.king.pojo; import java.io.Serializable; public class User implements Serializable {
private Integer id;
private String username;
private Integer age; public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
}
}
4. 运行测试
数据上传
$ curl -X PUT --data 'username=jinlingbo&age=18&id=1' http://127.0.0.1:8080/redis/object/put
数据查询(get 操作可以直接在浏览器访问)
$ curl http://127.0.0.1:8080/redis/object/get/jinlingbo
{"id":,"username":"jinlingbo","age":}%
已经打印出结果
参考文献
https://segmentfault.com/a/1190000017805065
https://www.runoob.com/redis/redis-install.html
Maven + Springboot + redis 配置的更多相关文章
- springboot +redis配置
springboot +redis配置 pom依赖 <dependency> <groupId>org.springframework.boot</groupId> ...
- redis 安装使用 & SpringBoot Redis配置
1.安装 https://www.cnblogs.com/dingguofeng/p/8709476.html https://www.runoob.com/redis/redis-keys.html ...
- Springboot+Redis 配置和使用
pom.xml 引入redis 开启缓存 <!-- cache --> <dependency> <groupId>org.springframework.boot ...
- SpringBoot cache-control 配置静态资源缓存 (以及其中的思考经历)
昨天在部署项目时遇到一个问题,因为服务要部署到外网使用,中间经过了较多的网络传输限制,而且要加载arcgis等较大的文件,所以在部署后,发现页面loading需要很长时间,而且刷新也要重新从服务器下载 ...
- springboot中配置主从redis
测试redis的主从配置 redis实例 文件夹名称如下 redis_master_s redis_slaver1_s redis_slaver2_s redis.conf文件 master的redi ...
- springboot学习笔记-4 整合Druid数据源和使用@Cache简化redis配置
一.整合Druid数据源 Druid是一个关系型数据库连接池,是阿里巴巴的一个开源项目,Druid在监控,可扩展性,稳定性和性能方面具有比较明显的优势.通过Druid提供的监控功能,可以实时观察数据库 ...
- SpringBoot + Redis:基本配置及使用
注:本篇博客SpringBoot版本为2.1.5.RELEASE,SpringBoot1.0版本有些配置不适用 一.SpringBoot 配置Redis 1.1 pom 引入spring-boot-s ...
- SpringBoot Redis序列化配置
Redis配置 #Redis spring.redis.host= spring.redis.port=6379 spring.redis.database=0 # Redis服务器连接密码(默认为空 ...
- 从源码研究如何不重启Springboot项目实现redis配置动态切换
上一篇Websocket的续篇暂时还没有动手写,这篇算是插播吧.今天讲讲不重启项目动态切换redis服务. 背景 多个项目或微服务场景下,各个项目都需要配置redis数据源.但是,每当运维搞事时(修改 ...
随机推荐
- db2,用户名密码不对导致无法连接数据库: Reason: User ID or Password invalid. ERRORCODE=-4214, SQLSTATE=28000
文章目录 背景 解决 背景 qa需要db2的demo,运维给安装完db2,启动报错 com.ibm.db2.jcc.am.io: [jcc][t4][2013][11249][4.7.112] Con ...
- 8.1 图像API
8.1 图像API Routine Description Drawing related functions GUI_AddRect() 调整矩形框的大小 GUI_GetClientRect() R ...
- 使用CSS将图片转换成黑白(灰色、置灰) & 毛玻璃效果
法1⃣️: IE浏览器: filter: gray; 其他浏览器: .gray { -webkit-filter: grayscale(100%); -moz-filter: grayscale(10 ...
- JMeter轻松实现大数据量AI图像识别接口测试
****************************************************************************** 本文主要介绍利用Jmeter进行AI图像识 ...
- VC++ 2010 创建高级Ribbon界面详解(3)
3.工具栏 在传统的菜单式界面中,工具栏作为菜单的有益补充,被广泛使用.我们通过将一些常用命令放置到工具栏上,可以让用户直观而快速地访问到常用功能,提高了效率.在Ribbon界面中,工具栏得到了进一步 ...
- sanic连接mongo
方法一: #没有密码,就是没有用户和用户密码 settings={"MOTOR_URI":"mongodb://127.0.0.1:27017/zzy"} ap ...
- Web开发常规调试方法与常见问题分析
一.Web项目基本原理 现在的web项目大都已经前后端独立开发与部署. 前后端独立开发,一般是前端与后端通过web接口(常见的有RESTful与websocket)文档进行交流.前端开发人员先更具业务 ...
- PHP之数据连接方法(二)
首先API接口,无非就是通过该程序去处理数据的数据,及判断数据的准确性. 因此我们需要一个DBTool的操作方法. DBTool地址:https://github.com/gfarmhuang/DBT ...
- 微信app支付返回-1的问题
我也是被坑就当留个纪念 前两天查了各种关于微信app支付返回-1的都是ERR_COMM 问题然后各种 验证最后还是误解 第三天去验证了一下微信开放平台发现了问题 appid 不在同一个开放平台 项目之 ...
- jdk8中map新增的merge方法介绍
1.Map.merge方法介绍 jdk8对于许多常用的类都扩展了一些面向函数,lambda表达式,方法引用的功能,使得java面向函数编程更为方便.其中Map.merge方法就是其中一个,merge方 ...