在springboot中使用redis很方便,只要在yml配置好reids的相关配置项就可以直接使用。

无论是单实例还是集群,都能配置:

spring:application:name: gateredis:timeout: 6000msdatabase: 10
#    host: localhost #单实例redis用这个配置
#    password:
#    port: 6379password: Redis@123cluster: #集群用这个配置nodes:- 127.0.0.1:7011- 127.0.0.1:7012- 127.0.0.1:7013- 127.0.0.1:7014- 127.0.0.1:7015- 127.0.0.1:7016max-redirects: 2 #获取失败 最大重定向次数lettuce:pool: max-active: 1000 #连接池最大连接数(使用负值表示没有限制)max-idle: 10 #连接池中的最大空闲连接min-idle: 3 #连接池中的最小空闲连接max-wait: -1 #连接池最大阻塞等待时间(使用负值表示没有限制)

springboot会帮我们初始化2个RedisTemplate。

一个是 redisTemplate,stringRedisTemplate

注意:

redisTemplate的类型是RedisTemplate<Object, Object>

stringRedisTemplate的类型是:RedisTemplate<String, String>

他们是有区别的,redisTemplate的序列化器是JdkSerializationRedisSerializer

stringRedisTemplate的序列化器是StringRedisSerializer。

在我们需要使用时,直接注入就可以:

 @Autowired     private RedisTemplate redisTemplate;@Autowired     private StringRedisTemplate  stringRedisTemplate;

我们在使用时,一般会修改序列化器,所以不会直接@Autowired这个对象就是用,而是重新定义下:

package com.example.gate.config;import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;@Configuration
@AutoConfigureAfter(RedisAutoConfiguration.class)
public class RedisConfig {//redisConnectionFactory配置信息由yml控制@Bean@ConditionalOnMissingBeanpublic RedisTemplate<String, Object> redisTemplateMy (LettuceConnectionFactory redisConnectionFactory) {RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();//配置序列化器template.setKeySerializer(new StringRedisSerializer());template.setValueSerializer(new GenericJackson2JsonRedisSerializer());template.setHashKeySerializer(new StringRedisSerializer());template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());template.setConnectionFactory(redisConnectionFactory);//      template.afterPropertiesSet();//断言,判断redisConnectionFactory是否不为空,实现InitializingBean接口会自动执行,所以这里不用写这行代码。return template;}}

redis连接池的配置pom依赖:

<dependency><groupId>org.apache.commons</groupId><artifactId>commons-pool2</artifactId></dependency>

这样在使用的时候就ok了,我们可以比较下我定义的redisTemplateMy和默认的是否是同一个对象,

package com.example.gate.controller;import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;import javax.annotation.Resource;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;/*** RedisAutoConfiguration * * @author lsy**/
@RestController
public class RedisTestController {@Resource(name="redisTemplateMy")private RedisTemplate redisTemplateMy;@Autowired     private RedisTemplate redisTemplate;@Autowired     private StringRedisTemplate  stringRedisTemplate;@RequestMapping(value = "/helloRedis",method = RequestMethod.GET)@ResponseBodypublic String helloRedis(@RequestParam(required=false) String param){System.out.println("helloRedis接收请求体为===\r\n"+param); DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");String dateStr=LocalDateTime.now().format(df);String value=param+"-------"+dateStr;String key="testkey";this.redisTemplate.opsForValue().set(key, value);String res=(String) this.redisTemplate.opsForValue().get(key);String print="查询redis结果===key=="+key+",值=="+res;System.out.println(print);判断springboot帮我创建的对象和我自己声明的对象是否是同一个System.out.println("判断是否相等===="+(redisTemplateMy.equals(redisTemplate)));String key22="testkey22";this.stringRedisTemplate.opsForValue().set(key22, value+"22222");String res222=(String) this.stringRedisTemplate.opsForValue().get(key22);String print22="查询redis结果===key22=="+key22+",值=="+res222;System.out.println(print22);return print;}}

结果是2个对象不相等。

所以我们保留一个就行

package com.example.gate.config;import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;@Configuration
@AutoConfigureAfter(RedisAutoConfiguration.class)
public class RedisConfig {//redisConnectionFactory配置信息由yml控制@Bean@ConditionalOnMissingBeanpublic RedisTemplate<String, Object> redisTemplate (LettuceConnectionFactory redisConnectionFactory) {RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();//配置序列化器template.setKeySerializer(new StringRedisSerializer());template.setValueSerializer(new GenericJackson2JsonRedisSerializer());template.setHashKeySerializer(new StringRedisSerializer());template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());template.setConnectionFactory(redisConnectionFactory);//        template.afterPropertiesSet();//断言,判断redisConnectionFactory是否不为空,实现InitializingBean接口会自动执行,所以这里不用写这行代码。return template;}}

redis集群yml配置lettuce相关推荐

  1. SpringBoot配置redis集群(Jedis and lettuce)

    SpringBoot2.x版本配置redis集群(Jedis and lettuce) 在SpringBoot1.x版本中,springboot默认使用集成jedis,在SpringBoot2.x版本 ...

  2. redis集群的配置

    1.修改主机的主机名称 [root@localhost ~]# vim/etc/sysconfig/network NETWORKING=yes HOSTNAME=localhost.localdom ...

  3. redis集群主从配置

    集群环境安装(ruby的环境云主机和虚拟机都不需要装) 因为集群命令文件需要ruby语言的支持(自行下载) http://www.ruby-lang.org/en/downloads/ 这个是下载地址 ...

  4. Redis集群的配置与使用

    redis集群 什么是redis集群? redis是指启动N个节点,将整个数据库分布在这N个节点中,每个节点存储总数据的1/N. redis集群实现了对redis的水平扩容 redis集群通过分区来提 ...

  5. [转]Redis集群的配置

    一:memcache 和 Redis 对比总结 [memecache 特点] 1:速度最快(没有自测,但网上有详细的测试用例) 2:支持水平扩展,可以任意添加节点 [redis 特点] 1:速度没有m ...

  6. Redis 集群密码配置

    >补充一下密码相关配置 在config配置文件中与密码有关的两个配置属性: 1)requirepass 为节点设置密码,访问该节点需要首先使用 auth xxxxxx 命令. 2)mastera ...

  7. redis集群版配置三种方式

    redis的三种集群方式 redis有三种集群方式:主从复制,哨兵模式和集群. 1.主从复制 主从复制原理: 从服务器连接主服务器,发送SYNC命令: 主服务器接收到SYNC命名后,开始执行BGSAV ...

  8. Redis集群——主从配置

    一主二从环境 host port role 192.168.186.200 6379 master 192.168.186.201 6379 slave 192.168.186.202 6379 sl ...

  9. Redis单例、主从模式、sentinel以及集群的配置方式及优缺点对比

    点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试文章 作者:爱宝贝丶 my.oschina.net/zhangxufen ...

最新文章

  1. python中的新式类与旧式类的一些基于descriptor的概念(上)
  2. 计算机书籍-R语言机器学习预测分析实战
  3. IDEA 真牛逼,900行 又臭又长 的类重构,几分钟搞定
  4. Mac brew安装maven
  5. 深入浅出Paxos分布式一致性算法
  6. 高手教你如何用香技巧香水达人教你用香省钱法 - 生活至上,美容至尚!
  7. 完美世界2020编程题-救雅典娜 英雄AB PK
  8. 2016年 CSS 库、框架和工具新生榜 TOP 50
  9. NYOJ 570欧拉函数求和(欧拉函数数论入门)
  10. JavaScript实现HTML导航栏下拉菜单[悬浮显示]
  11. 杰理之AUDIO_DAC【篇】
  12. linux美元符号切换为井号,struts2 (# % $)井号,百分号,美元符号的含义和使用方法举例...
  13. 运筹帷幄之中决胜千里之外 菜鸟初识代码编程规范二 命名规范
  14. LBS服务LevelUp推二维码支付产品
  15. 苹果手机html吊起拍照,一张好照片不是只按快门 iPhone手机拍照指南
  16. 合同节水服务认证国内怎么申请?
  17. google-hacking
  18. python扫描器_Python扫描器-HTTP协议
  19. 编写测试用例的基本方法之边界值
  20. 关注民生民情——华北水利水电大学“情艺”国情社情调查

热门文章

  1. apache poi斜边框线_apache poi合并单元格设置边框
  2. DotNetty完全教程(八)
  3. php积分夺宝,GitHub - irisroyaltyf/thinkphp5.0_shop: 基于thinkphp5,多商户商城。积分商城、团购、秒杀、拍卖、夺宝等多插件(持续开发中)...
  4. 在Cocos2dX游戏中使用Lua脚本进行游戏开发(基础篇)
  5. 前端vue+后端Django通信实例,传递json数据
  6. git删除与恢复错误删除、修改文件
  7. BLAS库不同厂商实现合集(2022)
  8. Python统计文件夹下文件的个数
  9. 23种设计模式——组合模式
  10. Java逻辑运算符异或