客户端连接Codis集群
原创
©著作权归作者所有:来自51CTO博客作者风云正的原创作品,请联系作者获取转载授权,否则将追究法律责任
新建maven webapp项目
添加相关依赖:
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>3.3.2</version>
</dependency>
编写测试类:
package com.czhappy.redis_test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
public class JedisTest {
public static void main(String[] args) {
// 开启Jedis连接池
JedisPoolConfig jpc = new JedisPoolConfig();
jpc.setMaxTotal(50);
jpc.setMinIdle(10);
JedisPool jp = new JedisPool(jpc,"192.168.84.128",19000);
// 开启Jedis客户端
Jedis jedis = jp.getResource();
jedis.set("k1","v1");
System.out.println(jedis.get("k1"));
jedis.close();
}
}
package com.czhappy.redis_test;
import org.redisson.Redisson;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
public class RedissonTest {
public static void main(String[] args) {
Config config = new Config();
config.setCodec(new org.redisson.client.codec.StringCodec());
config. useSingleServer().setAddress("192.168.84.128:19000");
RedissonClient redisson = Redisson.create(config);
RBucket<String> keyObject = redisson.getBucket("k2");
keyObject.set("v2");
System.out.println(keyObject.get());
redisson.shutdown();
}
}
运行上面的两个Main方法,观察数据是否保存成功
6831为主节点,6832为从节点
进入redis的bin目录下,启动redis命令行窗口: