安装redis
在CentOS虚拟机中安装redis,安装好后将 redis.conf 复制3次到/etc/redis目录下(/etc/下没有redis文件夹的话就建一个),分别命名为6379.conf,6380.conf,6381.conf(由于只有一台电脑,所以分成复制3次conf文件,运行3个redis进程),并且在conf文件中将端口分别改成6379,6380,6381.
安装twemproxy
1.安装twemproxy:
[root@COS6 src]# tar xvf nutcracker-0.4.0.tar.gz
[root@COS6 nutcracker-0.4.0]# cd nutcracker-0.4.0
[root@COS6 src]#./configure
[root@COS6 nutcracker-0.4.0]# make && make install
2.编辑配置文件:
默认listen0.0.0.0:22121,为了让在其他电脑上的java程序能访问到,必须改成192.168.6.128
[root@COS6 conf]# cd /usr/src/nutcracker-0.4.0/conf
[root@COS6 conf]# cp nutcracker.yml /etc/
[root@COS6 conf]# vim /etc/nutcracker.yml
alpha:
listen: 192.168.6.128:22121 --twemproxy所在虚拟服务器的ip和监听端口
hash: fnv1a_64
distribution: ketama
auto_eject_hosts: true
redis: true
server_retry_timeout: 2000
server_failure_limit: 1
servers: --3台redis服务器的地址和端口
- 127.0.0.1:6379:1
- 127.0.0.1:6380:1
- 127.0.0.1:6381:1
3.启动twemproxy:
[root@COS6 nutcracker-0.4.0]# nutcracker --help
This is nutcracker-0.4.0
Usage: nutcracker [-?hVdDt] [-v verbosity level] [-o output file]
[-c conf file] [-s stats port] [-a stats addr]
[-i stats interval] [-p pid file] [-m mbuf size]
Options:
-h, --help : this help
-V, --version : show version and exit
-t, --test-conf : test configuration for syntax errors and exit
-d, --daemonize : run as a daemon
-D, --describe-stats : print stats description and exit
-v, --verbosity=N : set logging level (default: 5, min: 0, max: 11)
-o, --output=S : set logging file (default: stderr)
-c, --conf-file=S : set configuration file (default: conf/nutcracker.yml) #配置
-s, --stats-port=N : set stats monitoring port (default: 22222)
-a, --stats-addr=S : set stats monitoring ip (default: 0.0.0.0)
-i, --stats-interval=N : set stats aggregation interval in msec (default: 30000 msec)
-p, --pid-file=S : set pid file (default: off)
-m, --mbuf-size=N : set size of mbuf chunk in bytes (default: 16384 bytes)
[root@COS6 nutcracker-0.4.0]# nutcracker -d -c /etc/nutcracker.yml
[root@COS6 nutcracker-0.4.0]# ps -ef|grep nutcracker
root 15358 1 0 02:40 ? 00:00:00 nutcracker -d -c /etc/nutcracker.yml
4.测试twemproxy:
启动必须加上 -h 192.168.6.128,否则启动不起来
[root@COS6 ~]# redis-cli -h 192.168.6.128 -p 22121
192.168.6.128:22121> set hello world
OK
192.168.6.128:22121> set wang yi
OK
192.168.6.128:22121> set peng fei
OK
192.168.6.128:22121> set zhang yang
OK
192.168.6.128:22121> set ali baba
OK
192.168.6.128:22121> set gao de
OK
192.168.6.128:22121>
这时候分别运行3个redis的客户端程序就可以看到发送给twemproxy的key/value分别保存在3个redis的缓存里,具体内容就不贴了。
[root@CentOS6 src]# redis-cli -h 127.0.0.1 -p 6379
[root@CentOS6 src]# redis-cli -h 127.0.0.1 -p 6380
[root@CentOS6 src]# redis-cli -h 127.0.0.1 -p 6381
5.关闭CentOS的防火墙(必须关闭)
[root@CentOS6 ~]# /etc/init.d/iptables stop
iptables:将链设置为政策 ACCEPT:filter [确定]
iptables:清除防火墙规则: [确定]
iptables:正在卸载模块: [确定]
[root@CentOS6 ~]# /etc/init.d/iptables status
iptables:未运行防火墙。
Java程序连接Twemproxy代理
1.在Eclipse创建java项目,并引用 jedis-2.1.0.jar 和 commons-pool-1.5.5.jar ,fastjson-1.2.2.jar
java项目目录结构如下
User.java
package com.redis;
public class User {
public int id;
public String userName;
public String password;
public User(int id,String userName,String password)
{
this.id = id;
this.userName = userName;
this.password = password;
}
}
RedisClient.java
注意生成JedisPool和JedisShardInfo时的IP地址和端口,要设置成CentOS虚拟机的IP地址和twemproxy监听的端口
package com.redis;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import com.alibaba.fastjson.JSON;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
import redis.clients.jedis.SortingParams;
public class RedisClient {
private Jedis jedis;//非切片额客户端连接
private JedisPool jedisPool;//非切片连接池
private ShardedJedis shardedJedis;//切片额客户端连接
private ShardedJedisPool shardedJedisPool;//切片连接池
public RedisClient()
{
initialPool();
initialShardedPool();
shardedJedis = shardedJedisPool.getResource();
jedis = jedisPool.getResource();
}
/**
* 初始化非切片池
*/
private void initialPool()
{
// 池基本配置
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxActive(20);
config.setMaxIdle(5);
config.setMaxWait(1000l);
config.setTestOnBorrow(false);
jedisPool = new JedisPool(config,"192.168.6.128",22121);
}
/**
* 初始化切片池
*/
private void initialShardedPool()
{
// 池基本配置
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxActive(20);
config.setMaxIdle(5);
config.setMaxWait(1000l);
config.setTestOnBorrow(false);
// slave链接
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
shards.add(new JedisShardInfo("192.168.6.128", 22121, "master"));
// 构造池
shardedJedisPool = new ShardedJedisPool(config, shards);
}
public void show() {
ObjectOperate() ;
jedisPool.returnResource(jedis);
shardedJedisPool.returnResource(shardedJedis);
}
private void ObjectOperate()
{
User user = new User(2,"test","test");
String userStr = JSON.toJSONString(user);
System.out.println(JSON.toJSONString(user));
jedis.set("test",JSON.toJSONString(user));
String value = jedis.get("test");
System.out.println(value);
}
}
Redis_Test.java
package com.redis;
public class Redis_Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
new RedisClient().show();
}
}
2.运行程序,结果如下
eclipse中运行结果如下,第一行输出是jedis.set的内容,第二行是jedis.get的内容
查看CentOS中的运行结果,发现被保存在端口为6379的redis缓存中。
127.0.0.1:6379> keys *
1) "hello"
2) "gao"
3) "ali"
4) "chen"
5) "wang"
6) "cyb"
7) "test"
8) "lai"
127.0.0.1:6379> get test
"{\"id\":2,\"password\":\"test\",\"userName\":\"test\"}"
127.0.0.1:6379>