参考
- 下载压缩包 wget http://download.redis.io/releases/redis-3.2.6.tar.gz
cd redis-3.2.6
make && make install
src/redis-server
redis-server /etc/redis.conf #启动时指定配置文件
cp redis-benchmark redis-cli redis-server /usr/bin/
这样就不用再执行时加上./了,而且可以在任何地方执行
[root@localhost redis-3.2.6]# redis-cli
127.0.0.1:6379> set name hi
OK
127.0.0.1:6379> get name
"hi"
redis-cli shutdown
<dependency>
<groupId>org.springframework.datagroupId>
<artifactId>spring-data-redisartifactId>
<version>1.0.0.RELEASEversion>
dependency>
在网上看了一圈,都是用注解配置的,平时这些配置代码喜欢用xml风格,就配置一下
version="1.0" encoding="UTF-8"?>
"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">
id="poolConfig" class="redis.clients.jedis.JedisPoolConfig" />
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" />
id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory" />
<property name="keySerializer" ref="stringRedisSerializer" />
操作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.
*/
@Component
public class RedisHandler
{
@Autowired
private RedisTemplate 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.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,然后就能连了