Liunx下Redis集群的安装与测试,以及项目中的应用。
首先准备ruby和redis接口:
redis-3.0.0.gem和
1、使用ruby脚本搭建集群。需要ruby的运行环境。
安装ruby
yum install ruby
yum install rubygems
1.1安装ruby脚本运行使用的包。
[[email protected] ~]# gem install redis-3.0.0.gem
Successfully installed redis-3.0.0
1 gem installed
Installing ri documentation for redis-3.0.0...
Installing RDoc documentation for redis-3.0.0...
[[email protected] ~]#
[[email protected] ~]# cd redis-3.0.0/src
[[email protected] src]# ll *.rb
-rwxrwxr-x. 1 root root 48141 Apr 1 2015 redis-trib.rb
2.搭建步骤
需要6台redis服务器。搭建伪分布式。
需要6个redis实例。
需要运行在不同的端口7001-7006
第一步:创建6个redis实例,每个实例运行在不同的端口。需要修改redis.conf配置文件。配置文件中还需要把cluster-enabled yes前的注释去掉。
第二步:启动每个redis实例。
第三步:使用ruby脚本搭建集群。
./redis-trib.rb create --replicas 1 192.168.25.153:7001 192.168.25.153:7002 192.168.25.153:7003 192.168.25.153:7004 192.168.25.153:7005 192.168.25.153:7006 |
创建关闭集群的脚本:
[[email protected] redis-cluster]# vim shutdow-all.sh
redis01/redis-cli -p 7001 shutdown
redis01/redis-cli -p 7002 shutdown
redis01/redis-cli -p 7003 shutdown
redis01/redis-cli -p 7004 shutdown
redis01/redis-cli -p 7005 shutdown
redis01/redis-cli -p 7006 shutdown
[[email protected] redis-cluster]# chmod u+x shutdow-all.sh
[[email protected] redis-cluster]# ./redis-trib.rb create --replicas 1 192.168.25.153:7001 192.168.25.153:7002 192.168.25.153:7003 192.168.25.153:7004 192.168.25.153:7005 192.168.25.153:7006 >>> Creating cluster Connecting to node 192.168.25.153:7001: OK Connecting to node 192.168.25.153:7002: OK Connecting to node 192.168.25.153:7003: OK Connecting to node 192.168.25.153:7004: OK Connecting to node 192.168.25.153:7005: OK Connecting to node 192.168.25.153:7006: OK >>> Performing hash slots allocation on 6 nodes... Using 3 masters: 192.168.25.153:7001 192.168.25.153:7002 192.168.25.153:7003 Adding replica 192.168.25.153:7004 to 192.168.25.153:7001 Adding replica 192.168.25.153:7005 to 192.168.25.153:7002 Adding replica 192.168.25.153:7006 to 192.168.25.153:7003 M: 2e48ae301e9c32b04a7d4d92e15e98e78de8c1f3 192.168.25.153:7001 slots:0-5460 (5461 slots) master M: 8cd93a9a943b4ef851af6a03edd699a6061ace01 192.168.25.153:7002 slots:5461-10922 (5462 slots) master M: 2935007902d83f20b1253d7f43dae32aab9744e6 192.168.25.153:7003 slots:10923-16383 (5461 slots) master S: 74f9d9706f848471583929fc8bbde3c8e99e211b 192.168.25.153:7004 replicates 2e48ae301e9c32b04a7d4d92e15e98e78de8c1f3 S: 42cc9e25ebb19dda92591364c1df4b3a518b795b 192.168.25.153:7005 replicates 8cd93a9a943b4ef851af6a03edd699a6061ace01 S: 8b1b11d509d29659c2831e7a9f6469c060dfcd39 192.168.25.153:7006 replicates 2935007902d83f20b1253d7f43dae32aab9744e6 Can I set the above configuration? (type ‘yes‘ to accept): yes >>> Nodes configuration updated >>> Assign a different config epoch to each node >>> Sending CLUSTER MEET messages to join the cluster Waiting for the cluster to join..... >>> Performing Cluster Check (using node 192.168.25.153:7001) M: 2e48ae301e9c32b04a7d4d92e15e98e78de8c1f3 192.168.25.153:7001 slots:0-5460 (5461 slots) master M: 8cd93a9a943b4ef851af6a03edd699a6061ace01 192.168.25.153:7002 slots:5461-10922 (5462 slots) master M: 2935007902d83f20b1253d7f43dae32aab9744e6 192.168.25.153:7003 slots:10923-16383 (5461 slots) master M: 74f9d9706f848471583929fc8bbde3c8e99e211b 192.168.25.153:7004 slots: (0 slots) master replicates 2e48ae301e9c32b04a7d4d92e15e98e78de8c1f3 M: 42cc9e25ebb19dda92591364c1df4b3a518b795b 192.168.25.153:7005 slots: (0 slots) master replicates 8cd93a9a943b4ef851af6a03edd699a6061ace01 M: 8b1b11d509d29659c2831e7a9f6469c060dfcd39 192.168.25.153:7006 slots: (0 slots) master replicates 2935007902d83f20b1253d7f43dae32aab9744e6 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. [[email protected] redis-cluster]# |
- 集群的使用方法
Redis-cli连接集群。
[[email protected] redis-cluster]# redis01/redis-cli -p 7002 -c
-c:代表连接的是redis集群
无论连接那个端口都可以。
4.在java程序中测试集群的使用方法
4.1首先导入以下几个jar包(版本可以不同)
4.2首先写个User对象,用于向redis储存对象,和list<User>集合。
package com.pingan.pojo;
import java.io.Serializable;
public class User implements Serializable{
private int Id;
private String Name;
public int getId() {
return Id;
}
public void setId(int id) {
Id = id;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
@Override
public String toString() {
return "User [Id=" + Id + ", Name=" + Name + "]";
}
}
4.3写个demo
package com.pingan.redis;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.alibaba.fastjson.JSON;
import com.pingan.pojo.User;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
public class Demo1 {
private JedisCluster jedisCluster;
@Before
public void before() {
//jedis = new Jedis("192.168.25.129",7006);
// 第一步:使用JedisCluster对象。需要一个Set<HostAndPort>参数。Redis节点的列表。
Set<HostAndPort> nodes = new HashSet<>();
nodes.add(new HostAndPort("192.168.25.129", 7001));
nodes.add(new HostAndPort("192.168.25.129", 7002));
nodes.add(new HostAndPort("192.168.25.129", 7003));
nodes.add(new HostAndPort("192.168.25.129", 7004));
nodes.add(new HostAndPort("192.168.25.129", 7005));
nodes.add(new HostAndPort("192.168.25.129", 7006));
//JedisCluster jedisCluster = new JedisCluster(nodes);
jedisCluster = new JedisCluster(nodes);
}
@After
public void after() {
jedisCluster.close();
}
/**
* 简单添加
*/
@Test
public void test1() {
String name = "name";
String value = "qq";
jedisCluster.set(name, value);
System.out.println("追加前:" + jedisCluster.get(name)); // 追加前:qq
// 在原有值得基础上添加,如若之前没有该key,则导入该key
jedisCluster.append(name, "ww");
System.out.println("追加后:" + jedisCluster.get(name)); // 追加后:qqww
jedisCluster.append("id", "ee");
System.out.println("没此key:" + jedisCluster.get(name));
System.out.println("get此key:" + jedisCluster.get("id"));
}
Test1运行效果如下:
/**
* map
*/
@Test
public void test3() {
Map<String, String> map = new HashMap<String, String>();
map.put("name", "fujianchao");
map.put("password", "123");
map.put("age", "12");
// 存入一个map
jedisCluster.hmset("user", map);
// map key的个数
System.out.println("map的key的个数" + jedisCluster.hlen("user"));
// map key
System.out.println("map的key" + jedisCluster.hkeys("user"));
// map value
System.out.println("map的value" + jedisCluster.hvals("user"));
// (String key, String... fields)返回值是一个list
List<String> list = jedisCluster.hmget("user", "age", "name");
System.out.println("redis中key的各个 fields值:"
+ jedisCluster.hmget("user", "age", "name") + list.size());
// 删除map中的某一个键 的值 password
// 当然 (key, fields) 也可以是多个fields
jedisCluster.hdel("user", "age");
System.out.println("删除后map的key" + jedisCluster.hkeys("user"));
}
/**
* list
*/
@Test
public void test4() {
jedisCluster.lpush("list", "aa");
jedisCluster.lpush("list", "bb");
jedisCluster.lpush("list", "cc");
//查询list全部数据
System.out.println(jedisCluster.lrange("list", 0, -1));
//查询从0个到1个
System.out.println(jedisCluster.lrange("list", 0, 1));
//查询栈定元素
System.out.println(jedisCluster.lpop("list")); // 栈顶
jedisCluster.del("list");
}
/**
* 自定义对象 User为例 id name
*
*
* 将对象转为json串,然后set进redis,取出来再转换成对象。
*/
@Test
public void test5() {
User user = new User();
user.setId(123);
user.setName("fighter");
String jsonString = JSON.toJSONString(user);
System.out.println("将对象转为字符串:"+jsonString);
// 存入一个 user对象
jedisCluster.set("user", jsonString);
// 获取
String userjson = jedisCluster.get("user");
//JSON.toJavaObject("userjson", User.class);
User user2 = JSON.parseObject(userjson, User.class);
System.out.println("获取redis的字符串:"+userjson);
System.out.println("将字符串转换成对象:"+user2);
}
Test5效果:
//将list<user>转为json串,然后set进redis,取出来再转换成对象。
@Test
public void test6() {
//创建对象
User user = new User();
user.setId(789);
user.setName("xixi");
User user2 = new User();
user2.setId(456);
user2.setName("haha");
//创建集合
//ArrayList<User> list = new ArrayList<>();
List<User> list = new ArrayList<>();
list.add(user);
list.add(user2);
System.out.println("list:"+list);
//将集合转换成json
String jsonString = JSON.toJSONString(list);
System.out.println("将list转为字符串:"+jsonString);
// 存入一个 user对象
jedisCluster.set("list", jsonString);
// 获取
String userjson = jedisCluster.get("list");
List<User> list2 = JSON.parseArray(userjson, User.class);
System.out.println("获取redis的字符串:"+userjson);
System.out.println("将字符串转换成list集合:"+list2);
}
public static String createJsonString(Object value){
String alibabaJson = JSON.toJSONString(value);
return alibabaJson;
}
}
Test6效果:
总结:在项目中的应用,去数据库查询出来的数据,这里指的是对象和list集合,当然其他的也行JedisCluster,只能操作字符串,所有我们必须先把对象,或者集合转为字符串,这里使用的是阿里巴巴的fastjson。然后再set进redis,取出来的时候再把字符串转换成对象或者集合。