Jedis Getting Started - 翻译

362 阅读5分钟

配置依赖 Configure a Maven dependency

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.9.0</version>
    <type>jar</type>
    <scope>compile</scope>
</dependency>

基本用法 Basic usage example

在多线程环境使用Jedis using Jedis in a multithreaded environment

You shouldn't use the same instance from different threads because you'll have strange errors. And sometimes creating lots of Jedis instances is not good enough because it means lots of sockets and connections, which leads to strange errors as well. A single Jedis instance is not threadsafe! To avoid these problems, you should use JedisPool, which is a threadsafe pool of network connections. You can use the pool to reliably create several Jedis instances, given you return the Jedis instance to the pool when done. This way you can overcome those strange errors and achieve great performance.
你不应该在多线程中使用相同实例,因为你会遇到奇怪错误. 有时候创建大量 Jedis 实例并不好,因为这意味着会创建大量的 sockets 和连接,也会导致奇怪的错误.一个单例 Jedis 不是线程安全的!
为了避免这些问题,你应该使用 JedisPool, 是一个线程安全的网络连接池. 你可以使用该连接池可靠地创建多个Jedis实例, 使用完之后把 Jedis 示例还给连接池。
用这种方法,您可以克服这些奇怪的错误并获得出色的性能。

To use it, init a pool:
为了使用它,初始化一个连接池:

JedisPool pool = new JedisPool(new JedisPoolConfig(), "localhost");

You can store the pool somewhere statically, it is thread-safe. 您可以将池静态存储在某个地方,它是线程安全的。

JedisPoolConfig includes a number of helpful Redis-specific connection pooling defaults. JedisPool is based on Commons Pool 2, so you may want to have a look at Commons Pool's configuration. Please see commons.apache.org/proper/comm… for more details.
JedisPoolConfig 包含许多有用的 Redis-规范 的连接池默认值。
JedisPool基于 Commons Pool 2,因此您可能需要查看 Commons Pool 的配置。
有关更多详细信息,请参见 commons.apache.org/proper/comm…

You use it by:
你可以这么使用它(连接池):

/// 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();

If you can't use try-with-resource, you can still enjoy with Jedis.close().
如果你不能使用 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();

If Jedis was borrowed from pool, it will be returned to pool with proper method since it already determines there was JedisConnectionException occurred. If Jedis wasn't borrowed from pool, it will be disconnected and closed.
如果从池中借用了 Jedis ,使用适当的方法将它还给连接池,因为(如果)发生了 JedisConnectionException 则 Jedis 已经结束。
如果 Jedis 不是 从池中借用的,它将断开并关闭。

设置主从 Setting up master/slave distribution

启用副本 enable replication

Redis is primarily built for master/slave distribution. This means that write requests have to be explicitly addressed to the master (a redis server), which replicates changes to slaves (which are also redis servers). Read requests then can be (but must not necessarily) addressed to the slaves, which alleviates the master.
Redis 主要用于主/从分发。 这意味着必须将写请求显式寻址到主服务器(redis服务器),该主服务器将数据变化复制到从服务器(也是redis服务器)。
然后可以(但不必一定)将读取请求发送给从属设备,从而减轻了主设备的负担。

You use the master as shown above. In order to enable replication, there are two ways to tell a slave it will be "slaveOf" a given master:
您使用了如上所述的主服务器。 为了启用副本,有两种方法可以设置从服务器:

  • Specify it in the respective section in the Redis Config file of the redis server
    在 redis 服务器的 Redis Config 文件的相应部分中指定它
  • on a given jedis instance (see above), call the slaveOf method and pass IP (or "localhost") and port as argument: 在给定的jedis实例上(参见上文),调用 slaveOf 方法并传递IP(或“ localhost”)和端口作为参数:
jedis.slaveof("localhost", 6379);  //  if the master is on the same PC which runs your code
jedis.slaveof("192.168.1.35", 6379); 

Note: since Redis 2.6 slaves are read only by default, so write requests to them will result in an error. 注意:从 Redis 2.6 开始从服务器默认情况下是只读的,因此对它们的写入请求将导致错误。

If you change that setting they will behave like normal redis servers and accept write requests without errors, but the changes won't be replicated, and hence those changes are at risk to be silently overwritten, if you mix up your jedis instances. 如果更改该设置,则它们将像普通的 Redis 服务器一样工作,并接受无错误的写入请求,但是这些更改将不会被复制。
因此,如果您混合使用jedis实例,则这些更改有被静默覆盖的风险。

禁用复制/在主服务器出现故障时,升级从服务器 disable replication / upon failing master, promote a slave

In case your master goes down, you may want to promote a slave to be the new master. You should first (try to) disable replication of the offline master first, then, in case you have several slaves, enable replication of the remaining slaves to the new master:

万一您的主服务器出现故障,您可能希望将一个从服务器升级为新的主服务器。
首先,您应该(尝试)禁用主服务器的复制,然后,如果您有多个从服务器,则将其余从服务器作为新的主服务器的副本:

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