【方法一】使用redis安装包自带的客户端
先将安装包传到服务器上,执行make编译好后,执行/src/redis-cli随便连一个redis结点,然后执行get,如果不在当前结点会提示所在结点,退出来重新连就行了。
此法有个缺陷,不能调试byte[]为key的存储,只能调试string类型
【方法二】对于Java语言可以使用Jedis客户端,如果以byte[]方式为key
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.0.0</version>
</dependency>
public class RedisClusterUtil {
private static JedisCluster jedis = null;
//可用连接实例的最大数目,默认为8;
//如果赋值为-1,则表示不限制,如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)
private static Integer MAX_TOTAL = 1024;
//控制一个pool最多有多少个状态为idle(空闲)的jedis实例,默认值是8
private static Integer MAX_IDLE = 200;
//等待可用连接的最大时间,单位是毫秒,默认值为-1,表示永不超时。
//如果超过等待时间,则直接抛出JedisConnectionException
private static Integer MAX_WAIT_MILLIS = 10000;
//在borrow(用)一个jedis实例时,是否提前进行validate(验证)操作;
//如果为true,则得到的jedis实例均是可用的
private static Boolean TEST_ON_BORROW = true;
//在空闲时检查有效性, 默认false
private static Boolean TEST_WHILE_IDLE = true;
//是否进行有效性检查
private static Boolean TEST_ON_RETURN = true;
//访问密码
//private static String AUTH = "1234@abcd";
/**
* 静态块,初始化Redis连接池
*/
static {
try {
JedisPoolConfig config = new JedisPoolConfig();
/*注意:
在高版本的jedis jar包,比如本版本2.9.0,JedisPoolConfig没有setMaxActive和setMaxWait属性了
这是因为高版本中官方废弃了此方法,用以下两个属性替换。
maxActive ==> maxTotal
maxWait==> maxWaitMillis
*/
config.setMaxTotal(MAX_TOTAL);
config.setMaxIdle(MAX_IDLE);
config.setMaxWaitMillis(MAX_WAIT_MILLIS);
config.setTestOnBorrow(TEST_ON_BORROW);
config.setTestWhileIdle(TEST_WHILE_IDLE);
config.setTestOnReturn(TEST_ON_RETURN);
Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>();
jedisClusterNode.add(new HostAndPort("10.12.26.139", 6410));
jedisClusterNode.add(new HostAndPort("10.12.26.138", 6410));
jedisClusterNode.add(new HostAndPort("10.12.26.128", 6410));
jedis = new JedisCluster(jedisClusterNode,1000,1000,5, config);
} catch (Exception e) {
e.printStackTrace();
}
}
public static JedisCluster getJedis(){
return jedis;
}
/**
* 反序列化
*/
public static Object unserialize(byte[] bytes) {
HessianInput ois = null;
try {
ois = new HessianInput(new ByteArrayInputStream(bytes));
return ois.readObject();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (ois != null) {
ois.close();
}
}
return null;
}
}
public static void main(String[] args) {
JedisCluster conn = RedisClusterUtil.getJedis();
String key = "keyyyyyy";
byte[] ret = conn.get(key.getBytes(Charset.forName("utf-8")));
if (ret == null) {
return ;
}
System.out.println("get redis success");
Object object = RedisClusterUtil.unserialize(ret);
Map<String, Rule> map = (Map<String, Rule>) object;
Set<String> keySet = map.keySet();
for (String k : keySet) {
String value = map.get(k).toString();
System.out.println(k + "==" + value);
}
}
将依赖打包成lib传到服务器上
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
-del.sh
-lib
-.jar
APPLICATION_HOME=`pwd`
export JAVA_OPTS="-server -Xmx256M -Xms256M util.Main &"
#set CLASSPATH
APPLICATION_CLASSPATH="./redis-cluster-conn-1.0-SNAPSHOT.jar"
for i in "$APPLICATION_HOME"/lib/*.jar
do
APPLICATION_CLASSPATH="$APPLICATION_CLASSPATH:$i"
done
#==============================================================================
#startup Server
RUN_CMD="nohup \"/usr/local/jdk8/bin/java\""
RUN_CMD="$RUN_CMD -classpath \"$APPLICATION_CLASSPATH\""
RUN_CMD="$RUN_CMD $JAVA_OPTS"
echo $RUN_CMD
eval $RUN_CMD