关于redis在spring上的密码配置问题
1.在项目中的spring配置文件中,为每一个redis服务都配置上密码,由于我是使用的maven,
因此下面的参数都是从profile文件中加载进来的,这里不去详细介绍。
<!-- redis 配置 begin ====================== -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="50" />
<property name="maxIdle" value="5" />
<property name="maxWaitMillis" value="2000" />
<property name="testOnBorrow" value="false" />
</bean>
<bean id="jedis.shardInfo1" class="redis.clients.jedis.JedisShardInfo">
<constructor-arg index="0" value="${redisCacheHost1}" />
<constructor-arg index="1" value="${redisCachePort}" />
<constructor-arg index="2" value="${redisTimeout}" />
<constructor-arg index="3" value="${redisCacheName1}" />
<property name="password" value="${redisCachePwd}"></property>
</bean>
<bean id="jedis.shardInfo2" class="redis.clients.jedis.JedisShardInfo">
<constructor-arg index="0" value="${redisCacheHost2}" />
<constructor-arg index="1" value="${redisCachePort}" />
<constructor-arg index="2" value="${redisTimeout}" />
<constructor-arg index="3" value="${redisCacheName2}" />
<property name="password" value="${redisCachePwd}"></property>
</bean>
<bean id="jedis.shardInfo3" class="redis.clients.jedis.JedisShardInfo">
<constructor-arg index="0" value="${redisCacheHost3}" />
<constructor-arg index="1" value="${redisCachePort}" />
<constructor-arg index="2" value="${redisTimeout}" />
<constructor-arg index="3" value="${redisCacheName3}" />
<property name="password" value="${redisCachePwd}"></property>
</bean>
<bean id="jedis.shardInfo4" class="redis.clients.jedis.JedisShardInfo">
<constructor-arg index="0" value="${redisCacheHost4}" />
<constructor-arg index="1" value="${redisCachePort}" />
<constructor-arg index="2" value="${redisTimeout}" />
<constructor-arg index="3" value="${redisCacheName4}" />
<property name="password" value="${redisCachePwd}"></property>
</bean>
<bean id="jedis.shardInfo5" class="redis.clients.jedis.JedisShardInfo">
<constructor-arg index="0" value="${redisCacheHost5}" />
<constructor-arg index="1" value="${redisCachePort}" />
<constructor-arg index="2" value="${redisTimeout}" />
<constructor-arg index="3" value="${redisCacheName5}" />
<property name="password" value="${redisCachePwd}"></property>
</bean>
<bean id="jedis.shardInfo6" class="redis.clients.jedis.JedisShardInfo">
<constructor-arg index="0" value="${redisCacheHost6}" />
<constructor-arg index="1" value="${redisCachePort}" />
<constructor-arg index="2" value="${redisTimeout}" />
<constructor-arg index="3" value="${redisCacheName6}" />
<property name="password" value="${redisCachePwd}"></property>
</bean>
<bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool">
<constructor-arg index="0" ref="jedisPoolConfig" />
<constructor-arg index="1">
<list>
<ref bean="jedis.shardInfo1" />
<ref bean="jedis.shardInfo2" />
<ref bean="jedis.shardInfo3" />
<ref bean="jedis.shardInfo4" />
<ref bean="jedis.shardInfo5" />
<ref bean="jedis.shardInfo6" />
</list>
</constructor-arg>
</bean>
<!-- redis 配置 end ====================== -->
2.在服务器上,这里以linux服务器为例,为redis配置密码。
(1)首先进入redis,如果没有开启redis则需要先开启:
[root@iZ94jzcra1hZ bin]# redis-cli -p 6379
127.0.0.1:6379>
(2)查看当前redis有没有设置密码:
127.0.0.1:6379> config get requirepass
1) "requirepass"
2) ""
(3)为以上显示说明没有密码,那么现在来设置密码:
127.0.0.1:6379> config set requirepass abcdefg
OK
127.0.0.1:6379>
(4)再次查看当前redis就提示需要密码:
127.0.0.1:6379> config get requirepass
(error) NOAUTH Authentication required.
127.0.0.1:6379>
当前这种linux配置redis密码的方法是一种临时的,如果redis重启之后密码就会失效,
需要永久配置密码的话就去redis.conf的配置文件中找到requirepass这个参数,如下配置:
requirepass abcdefg
保存后重启redis就可以了
3.顺便说一下redis常用的命令。
如果需要关闭redis:
[root@iZ94jzcra1hZ bin]# pkill redis
如果需要开启redis:
[root@iZ94jzcra1hZ bin]# redis-server &
加&符号的作用是为了让此进程转换为后台进程,不占用shell的服务。