springboot2.x整合redis
pom文件
<!--springboot中的redis依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
配置
# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=39.96.162.54
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=qweqwe
# 连接超时时间(毫秒)
spring.redis.timeout=0
配置类
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
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.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* Redis的配置类
*/
@Configuration
public class RedisConfig {
@Bean
@SuppressWarnings("all")
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
template.setConnectionFactory(factory);
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);
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
// key采用String的序列化方式
template.setKeySerializer(stringRedisSerializer);
// hash的key也采用String的序列化方式
template.setHashKeySerializer(stringRedisSerializer);
// value序列化方式采用jackson
template.setValueSerializer(jackson2JsonRedisSerializer);
// hash的value序列化方式采用jackson
template.setHashValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
}
springboot2.x整合redis的更多相关文章
- SpringBoot2.x整合Redis实战 4节课
1.分布式缓存Redis介绍 简介:讲解为什么要用缓存和介绍什么是Redis,新手练习工具 1.redis官网 https://redis.io/download 2.新手 ...
- 小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_39、SpringBoot2.x整合redis实战讲解
笔记 3.SpringBoot2.x整合redis实战讲解 简介:使用springboot-starter整合reids实战 1.官网:https://docs.spring.io/spring-bo ...
- SpringBoot2.0 整合 Redis集群 ,实现消息队列场景
本文源码:GitHub·点这里 || GitEE·点这里 一.Redis集群简介 1.RedisCluster概念 Redis的分布式解决方案,在3.0版本后推出的方案,有效地解决了Redis分布式的 ...
- Springboot2.0整合Redis(注解开发)
一. pom.xm文件引入对应jar包 <dependency> <groupId>org.springframework.boot</groupId> <a ...
- SpringBoot2.0整合Redis
Spring Boot2.0在2018年3月份正式发布,相比1.0还是有比较多的改动,例如SpringBoot 自2.0起支持jdk1.8及以上的版本.第三方类库升级.响应式 Spring 编程支持等 ...
- Springboot2.x整合Redis(一)
备注: springboto整合redis依赖于spring-boot-starter-data-redis这个jar 一,项目环境和依赖 1.POM.xml配置 <parent> < ...
- 【SpringBoot】Springboot2.x整合Redis(一)
备注: springboto整合redis依赖于spring-boot-starter-data-redis这个jar 一,项目环境和依赖 1.POM.xml配置 <parent> < ...
- springboot2.x整合redis实现缓存(附github链接)
本文代码已提交github: https://github.com/LCABC777/Springboot-redis(1)Springboot中使用redis操作的两种方式:lettuce和j ...
- Springboot2.x整合Redis以及连接哨兵模式/集群模式
依赖: <!--spirngboot版本为2.x--><!-- 加载spring boot redis包,springboot2.0中直接使用jedis或者lettuce配置连接池, ...
- springboot2.0整合redis作为缓存以json格式存储对象
步骤1 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spr ...
随机推荐
- 用阿里fastJson解析Json字符串
一下总结来自工作代码: 1.第一种情况: 通过服务器端发送http请求获取的接送字符串. String jsonStr = HttpRequestUtil.sendGet(config.getAddr ...
- EditPlus等编辑器选中列(块)的方法
EditPlus 1)菜单:编辑 -> 选择 -> 列选择2)先按下 Alt + C ,释放,然后移动鼠标或键盘上下左右键进行选择注意:在自动换行的模式下是不行的,改为不自动换行就行了. ...
- 2.9 log4j 在测试过程中打印执行日志
Apache Log4j配置说明/log4j的分层打印 http://blog.csdn.net/zzy7075/article/details/53763728
- 2019HDU多校第一场1001 BLANK (DP)(HDU6578)
2019HDU多校第一场1001 BLANK (DP) 题意:构造一个长度为n(n<=10)的序列,其中的值域为{0,1,2,3}存在m个限制条件,表示为 l r x意义为[L,R]区间里最多能 ...
- linux php 扩展安装
phpize./configure --with-php-config=/usr/bin/php-config make && make install
- 将 master 节点服务器从 k8s 集群中移除并重新加入
背景 1 台 master 加入集群后发现忘了修改主机名,而在 k8s 集群中修改节点主机名非常麻烦,不如将 master 退出集群改名并重新加入集群(前提是用的是高可用集群). 操作步骤 ssh 登 ...
- Java - 字符串操作
字符串常用操作如下 public static void main(String[] args) { /** * 创建字符串 */ String s1="zifuchuan123" ...
- pikachu平台搭建
1.将pikachu转移至htdocs 2.然后打开pikachu文件夹里的inc文件夹 3.里面对应的内容该成之前刚刚设置好的数据库服务器地址,用户名,密码和端口号 4.打开浏览器,输入http:/ ...
- 减轻集群负载、三种k8s 替代openstack的解决方案
减轻集群负载.三种k8s 替代openstack的解决方案 待办 https://news.ycombinator.com/item?id=17013779 kubevirt https://host ...
- 安卓按键:读取txt开头出现未知字符的问题
很多时候 我们读取txt 用traceprint输出后 最头上会莫名其妙多出一个问号 但是你用问号匹配他 却匹配不到 就是1个未知字符 这个到底是什么 怎么避免出现这个东西呢 这个主要是txt文件 ...