Jedis记录

您不应该使用来自不同线程的同一实例,
因为您会遇到奇怪的错误。有时创建大量的jedis实例还不够好,
因为它意味着大量的套接字和连接,这也会导致奇怪的错误。一个jedis实例不是threadsafe!为了避免这些问题,您应该使用jedispool,它是一个网络连接的线程安全池。您可以使用池来可靠地创建几个jedis实例,前提是完成后将jedis实例返回到池中。通过这种方式,您可以克服这些奇怪的错误并获得出色的性能。
要使用它,请初始化池:
JedisPool pool = new JedisPool(new JedisPoolConfig(), "localhost");

您可以静态地将池存储在某个地方,它是线程安全的。
jedispoolconfig包含许多有用的redis特定连接池默认值。jedispool基于commons pool 2,因此您可能需要查看commons pool的配置。有关详细信息,请参阅
 http://commons.apache.org/proper/commons-pool/apidocs/org/apache/commons/pool2/impl/GenericObjectPoolConfig.html。
 
 /// Jedis implements Closeable. Hence, the jedis instance will be auto-closed after the last statement.
try (Jedis jedis = pool.getResource()) {
  /// ... do stuff here ... for example
  jedis.set("foo", "bar");
  String foobar = jedis.get("foo");
  jedis.zadd("sose", 0, "car"); jedis.zadd("sose", 0, "bike"); 
  Set<String> sose = jedis.zrange("sose", 0, -1);
}
/// ... when closing your application:
pool.close();


 CONFIG set requirepass "密码"


如果你不能使用Try with Resource,你仍然可以使用jedis.close()。

Jedis jedis = null;
try {
  jedis = pool.getResource();
  /// ... do stuff here ... for example
  jedis.set("foo", "bar");
  String foobar = jedis.get("foo");
  jedis.zadd("sose", 0, "car"); jedis.zadd("sose", 0, "bike"); 
  Set<String> sose = jedis.zrange("sose", 0, -1);
} finally {
  // You have to close jedis object. If you don't close then
  // it doesn't release back to pool and you can't get a new
  // resource from pool.
  if (jedis != null) {
    jedis.close();
  }
}
/// ... when closing your application:
pool.close();

如果jedis是从pool中借用的,那么它将以适当的方法返回pool,因为它已经确定发生了jedisconnectionexception。
如果jedis不是从pool借来的,它将被断开并关闭。


设置主/从分配


启用复制


Redis主要用于主/从分发。这意味着写请求必须显式地发送到主服务器(redis服务器),主服务器将更改复制到从服务器(也是redis服务器)。
然后,读请求可以(但不一定)发送到从系统,这可以减轻主系统的负担。您使用上面所示的master。为了启用复制,有两种方法可以告诉从属服务器它将“从”给定的主服务器:
1、在redis服务器的redis配置文件的相应部分中指定它。
2、在给定的jedis实例上(参见上文),调用slaveof方法并将ip(或“localhost”)和port作为参数传递:

jedis.slaveof("localhost", 6379);  //  if the master is on the same PC which runs your code
jedis.slaveof("192.168.1.35", 6379); 

注意:由于Redis2.6以来,从默认为只读,因此对它们的写请求将导致错误。

如果更改该设置,它们的行为将类似于普通的redis服务器,并接受写入请求而不会出错,但是更改不会被复制,因此,如果混合了jedis实例,这些更改将有被静默覆盖的风险。

禁用复制/主服务器出现故障时,升级从属服务器

如果你的master go down,你可能想提升一个从成为新的主。您应该首先(尝试)禁用脱机主机的复制,然后如果您有多个从属服务器,请启用将其余从属服务器复制到新主服务器的功能:

slave1jedis.slaveofNoOne();
slave2jedis.slaveof("192.168.1.36", 6379); 

普通的redis实例不能是redis集群的一部分;只能是
#以群集节点可以启动。为了启动Redis实例
#群集节点启用群集支持,取消对以下内容的注释:
cluster-enabled yes
port  7000                                        //端口7000,7002,7003        
bind 本机ip                                       //默认ip为127.0.0.1 需要改为其他节点机器可访问的ip 否则创建集群时无法访问对应的端口,无法创建集群
daemonize    yes                               //redis后台运行
pidfile  /var/run/redis_7000.pid          //pidfile文件对应7000,7001,7002
cluster-enabled  yes                           //开启集群  把注释#去掉
cluster-config-file  nodes_7000.conf   //集群的配置  配置文件首次启动自动生成 7000,7001,7002
cluster-node-timeout  15000                //请求超时  默认15秒,可自行设置
appendonly  yes                           //aof日志开启  有需要就开启,它会每次写操作都记录一条日志 


requirepass 密码


至少3个master 3个salve

./redis-server  ../../redis_cluster/7000/redis.conf
./redis-server  ../../redis_cluster/7001/redis.conf
./redis-server  ../../redis_cluster/7002/redis.conf
./redis-server  ../../redis_cluster/7003/redis.conf
./redis-server  ../../redis_cluster/7004/redis.conf
./redis-server  ../../redis_cluster/7005/redis.conf


./redis-cli --cluster create 192.168.119.10:7000 192.168.119.10:7001 192.168.119.10:7002 192.168.119.10:7003 192.168.119.10:7004 192.168.119.10:7005 --cluster-replicas 1 -a 密码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值