在centos下的安裝
參考
- 下載壓縮包 wget http://download.redis.io/releases/redis-3.2.6.tar.gz 安裝
cd redis-3.2.6make && make install
啟動
src/redis-serverredis-server /etc/redis.conf #啟動時指定設定檔
cp redis-benchmark redis-cli redis-server /usr/bin/
這樣就不用再執行時加上./了,而且可以在任何地方執行 測試redis
[root@localhost redis-3.2.6]# redis-cli 127.0.0.1:6379> set name hiOK127.0.0.1:6379> get name"hi"
關閉redis服務
redis-cli shutdown
在spring中使用
maven依賴
<dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.0.0.RELEASE</version> </dependency>
xml配置
在網上看了一圈,都是用註解配置的,平時這些配置代碼喜歡用xml風格,就配置一下
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig" /> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" > <property name="hostName" value="127.0.0.1"/> <property name="port" value="6379" /> <property name="password" value="xxx" /> <property name="poolConfig" ref="poolConfig" /> </bean> <bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="jedisConnectionFactory" /> <property name="keySerializer" ref="stringRedisSerializer" /> </bean></beans>
操作redis的方法
package com.xxx.ws.redis;import com.xxx.ws.entity.Hi;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.stereotype.Component;import java.util.HashMap;import java.util.Map;import java.util.concurrent.TimeUnit;/** * Created on 2017/1/25. */@Componentpublic class RedisHandler{ @Autowired private RedisTemplate<String,Object> redisTemplate; public Object getValue(final String key) { return redisTemplate.opsForValue().get(key); } public void setValue(final String key,final String value) { redisTemplate.opsForValue().set(key,value); redisTemplate.expire(key,1, TimeUnit.SECONDS); } public void setHi( final Hi user ) { final String key = String.format( "hi:%s", user.getId() ); final Map< String, Object > properties = new HashMap< String, Object >(); properties.put( "id", user.getId() ); properties.put( "name", user.getName() ); properties.put( "msg", user.getMsg() ); properties.put( "num", user.getNum() ); redisTemplate.opsForHash().putAll( key, properties); } public Hi getHi( final int id ) { final String key = String.format( "hi:%s", id ); final String name = ( String )redisTemplate.opsForHash().get( key, "name" ); final Integer num = ( Integer )redisTemplate.opsForHash().get( key, "num" ); final String msg = ( String )redisTemplate.opsForHash().get( key, "msg" ); return new Hi( id, name, num,msg ); }}
然後就可以使用了 關於redis.conf的protected mode
redis.clients.jedis.exceptions.JedisDataException: DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified,no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server. 3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.
如果出現上面的情況是redis預設有個protected mode yes.這種模式下其他機器是不能連,只能bind的ip可以連.把它設定成no,然後把bind的機器注釋掉,同時設定requiredpass,然後就能連了