Reprinted from: http://aofengblog.blog.163.com/blog/static/631702120147298317919/
In the previous article, "Redis Command Guide," explains the storage operation of key=> by command line, in the actual project development, various languages are using Redis's client library to interact with Redis. For the Java language, Redis officially recommends Jedis.
Jedis provides a variety of operating methods: single-connection mode, stand-alone connection pool mode, multi-machine distributed + connection pool mode.
Prepare
jedis-2.5.2
Commons-pool2-2.2.jar
Using a single connection
This method is recommended only for the development environment to be used as a tuning trial.
// 创建连接
String host = "192.168.56.102";int port = 6379;Jedis client = new Jedis(host, port);
// 执行set指令String result = client.set("key-string", "Hello, Redis!");System.out.println( String.format("set指令执行结果:%s", result) );
// 执行get指令String value = client.get("key-string");System.out.println( String.format("get指令执行结果:%s", value) );
Run the above code, console output:
Set command execution result:OK
Get Command execution result:Hello, Redis!
Using connection pooling
This works for scenarios that use only a single Redis instance.
// 生成连接池配置信息JedisPoolConfig config = new JedisPoolConfig();config.setMaxIdle(10);config.setMaxTotal(30);config.setMaxWaitMillis(3*1000);// 在应用初始化的时候生成连接池JedisPool pool = new JedisPool(config, "192.168.56.102", 6379);// 在业务操作时,从连接池获取连接Jedis client = pool.getResource();try { // 执行指令 String result = client.set("key-string", "Hello, Redis!"); System.out.println( String.format("set指令执行结果:%s", result) ); String value = client.get("key-string"); System.out.println( String.format("get指令执行结果:%s", value) );} catch (Exception e) { // TODO: handle exception} finally { // 业务操作完成,将连接返回给连接池 if (null != client) { pool.returnResource(client); }} // end of try block// 应用关闭时,释放连接池资源pool.destroy();
Run the above code, console output:
Set command execution result:OK
Get Command execution result:Hello, Redis!
Using connection pooling + distributed
In larger systems, there are often multiple Redis instances for load balancing. It also implements a master-slave backup that, when the primary instance fails, switches to service from the instance.
Similar to the memcached client, Jedis also provides a way for client-side distributed operations, using a consistent hashing algorithm.
// 生成多机连接信息列表List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();shards.add( new JedisShardInfo("127.0.0.1", 6379) );shards.add( new JedisShardInfo("192.168.56.102", 6379) );// 生成连接池配置信息JedisPoolConfig config = new JedisPoolConfig();config.setMaxIdle(10);config.setMaxTotal(30);config.setMaxWaitMillis(3*1000);// 在应用初始化的时候生成连接池ShardedJedisPool pool = new ShardedJedisPool(config, shards);// 在业务操作时,从连接池获取连接ShardedJedis client = pool.getResource();try { // 执行指令 String result = client.set("key-string", "Hello, Redis!"); System.out.println( String.format("set指令执行结果:%s", result) ); String value = client.get("key-string"); System.out.println( String.format("get指令执行结果:%s", value) );} catch (Exception e) { // TODO: handle exception} finally { // 业务操作完成,将连接返回给连接池 if (null != client) { pool.returnResource(client); }} // end of try block// 应用关闭时,释放连接池资源pool.destroy();
Run the above code, console output:
Set command execution result:OK
Get Command execution result:Hello, Redis!
Java Client Jedis using Redis