1、spring配置,下面是一个完整redis配置,密码只需要设置password属性。
<!– jedis pool配置 –>
<bean id=”jedisPoolConfig” class=”redis.clients.jedis.JedisPoolConfig”>
<property name=”maxTotal” value=”${redis.maxActive}” />
<property name=”maxIdle” value=”${redis.maxIdle}” />
<property name=”maxWaitMillis” value=”${redis.maxWait}” />
<!– <property name=”testOnBorrow” value=”${redis.testOnBorrow}” /> –>
</bean>
<bean id=”jedisConnectionFactory”
class=”org.springframework.data.redis.connection.jedis.JedisConnectionFactory”>
<property name=”usePool” value=”true”></property>
<property name=”hostName” value=”${redis.host}” />
<property name=”port” value=”${redis.port}” />
<property name=”password” value=”${redis.pass}” />
<property name=”timeout” value=”${redis.timeout}” />
<!– <property name=”database” value=”${redis.default.db}”></property> –>
<constructor-arg index=”0″ ref=”jedisPoolConfig” />
</bean>
<!– Redis Template –>
<bean id=”redisTemplate” class=”org.springframework.data.redis.core.StringRedisTemplate”>
<property name=”connectionFactory” ref=”jedisConnectionFactory” />
<!– 如果不配置Serializer,那么存储的时候智能使用String,如果用User类型存储,那么会提示错误User can’t cast
to String!!! –>
<property name=”keySerializer”>
<bean
class=”org.springframework.data.redis.serializer.StringRedisSerializer” />
</property>
<property name=”valueSerializer”>
<bean
class=”org.springframework.data.redis.serializer.StringRedisSerializer” />
</property>
<property name=”hashValueSerializer”>
<bean
class=”com.datatang.api.commonmodule.common.serializer.GsonSerializer” />
</property>
</bean>
2、java操作redis,只需要加上jedis.auth(passwd)即可
public static Jedis getRedisConnection(String configFlag){ String host; int port; Jedis jedis = null; InputStream is = RedisUtil.class.getClassLoader().getResourceAsStream("config/redis.properties"); Properties pro = new Properties(); try { pro.load(is); host = pro.getProperty("redis.host" + configFlag); port = Integer.parseInt(pro.getProperty("redis.port" + configFlag)); jedis = new Jedis(host, port); String passwd = pro.getProperty("redis.pass"); jedis.auth(passwd); } catch (IOException e) { e.printStackTrace(); }finally{ try { if(is != null) is.close(); } catch (IOException e) { e.printStackTrace(); } } return jedis; }
相关推荐
博客文章示例代码:spring boot 自学笔记(四) Redis集成—Jedis
基于spring的子项目spring-data-redis写的一个基于hash类型的用户CRUD,经过简单的封装,可以实现通用CRUD,请事先安装好redis,配置文件redis.properties请做相应修改,希望对你有帮助。
主要介绍了Redis框架Jedis及Redisson对比解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
主要介绍了spring boot+redis 监听过期Key,本文通过示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
java客户端不是很好支持redis cluster,spring-date-redis和jedis批量提交还不支持,单个提交都是可以的。 为了批量解决批量提交 网上有几个方案,本示例使用了其中一种,demo里的JedisClusterPipeline类是网上找的...
主要介绍了SpringBoot+Redis执行lua脚本的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
#Spring-Session 项目 spring-session httpsession 示例 安装 redis 本地 redis-server redis-cli ... Config 类:创建 JedisConnectionFactory 并使用 Jedis SessionServlet 连接到 Redis 类:使用 ge
Spring启动演示 SpringBoot + SpringCloud + SpringSecurity学习过程中的二进制汇总,沉淀记录下学习历程 1.知识点图谱 所有博文集中发布在个人博客网站: 大致规划的内容包括以下章节,希望能用半年到一年(严重...
对mybaits的简单封装,方便简单的调用和多数据源的配置:使用atomikos来处理多数据源的一致性事务:封装对redis的增删改查以及对象的操作:利用spring封装了对redis集群的操作:Spring 和 FastDFS集群操作的示例。...
主要介绍了Java Jedis NOAUTH Authentication required问题解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
2,maven主要依赖:org.springframework.data spring-data-redis 2.3.2.RELEASE redis.clients jedis 3.3.0 3,哨兵配置: #哨兵的配置列表 spring.redis.sentinel.master =我的主人 spring.redis.sentinel.nodes...