This article learns to use a Java client (Jedis / Lettuce) to operate Redis.
1 Open remote connection
Redis does not support remote connections by default and needs to be turned on and modified manuallyRedis.confThere are three major changes:
vi /usr/local/redis-5.0.8/redis.conf # bind 127.0.0.1 protected-mode no requirepass 123456
Then restart Redis.
2 Jedis
2.1 Basic Use
GitHub address for Jedis: https://github.com/xetorthio/jedis .
First create a regular Maven project redis-jedis, and after the project is created successfully, add a Jedis dependency:
<dependencies> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>3.2.0</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13</version> <scope>test</scope> </dependency> </dependencies>
Then create a test class and test method:
public class JedisTests { @Test public void testJedis() { // 1. Construct a Jedis object, because the default port 6379 used here is not configured Jedis jedis = new Jedis("192.168.71.62"); // 2. Password authentication jedis.auth("123456"); // 3. Test for successful connection String r = jedis.ping(); // 4. Returning pong indicates successful connection System.out.println(r); jedis.set("k1", "hellojedis"); String v = jedis.get("k1"); System.out.println(v); } }
In Jedis, the API of the method is highly consistent with Redis's commands, so the method in Jedis is well-known and can be used directly.
2.2 Connection Pool
Because Jedis objects are not thread-safe, in practice, we usually obtain them through connection pools and return them to them after use.
@Test public void testJedisPool() { Jedis jedis = null; // 1. Construct a Jedis connection pool JedisPool pool = new JedisPool("192.168.71.62", 6379); // 2. Get a Jedis connection from the connection pool jedis = pool.getResource(); jedis.auth("123456"); try { // 3.Jedis operation String r = jedis.ping(); System.out.println(r); } catch (Exception e) { e.printStackTrace(); } finally { // 4. Return Connections if (jedis != null) { jedis.close(); } } }
In the above code, we use try-catch-final to ensure that the jedis object is closed, or try-with-resource in JDK1.7 (the code looks simpler than before), with the following modifications:
@Test public void testJedisPool2() { JedisPool pool = new JedisPool("192.168.71.62"); try (Jedis jedis = pool.getResource()) { jedis.auth("123456"); String ping = jedis.ping(); System.out.println(ping); } }
JedisPool can also be initialized using GenericObjectPoolConfig to support richer configurations as follows:
@Test public void testJedisPool3() { GenericObjectPoolConfig config = new GenericObjectPoolConfig(); // Connection pool maximum idle config.setMaxIdle(300); // maximum connection config.setMaxTotal(1000); // Maximum wait time for connection, if -1 means no limit config.setMaxWaitMillis(30000); // Check validity when idle config.setTestOnBorrow(true); JedisPool pool = new JedisPool(config, "192.168.71.62", 6379, Protocol.DEFAULT_TIMEOUT, "123456"); try (Jedis jedis = pool.getResource()) { String r = jedis.ping(); System.out.println(r); } }
3 Lettuce
GitHub address for Lettuce: https://github.com/lettuce-io/lettuce-core .
Lettuce vs. Jedis:
- Jedis connects directly to Redis in the implementation process and shares a Jedis instance among multiple threads, which is thread insecure. If you want to use Jedis in a multithreaded scenario, you have to use a connection pool, so each thread has its own Jedis instance.
- Lettuce is built on the currently popular Netty NIO framework, so it overcomes thread insecurity in Jedis. Lettuce supports synchronous, asynchronous, and responsive calls, and multiple threads can share a connection instance.
Create a normal Maven project redis-lettuce first, and after the project is created successfully, add a Lettuce dependency:
<dependencies> <dependency> <groupId>io.lettuce</groupId> <artifactId>lettuce-core</artifactId> <version>5.2.2.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13</version> <scope>test</scope> </dependency> </dependencies>
Then create a test class and test method:
public class LettuceTests { @Test public void testLettuce() { RedisClient redisClient = RedisClient.create("redis://123456@192.168.71.62"); StatefulRedisConnection<String, String> connect = redisClient.connect(); RedisCommands<String, String> sync = connect.sync(); sync.set("k1", "hellolettuce"); String v = sync.get("k1"); System.out.println(v); } }
- Redis Tutorial Collection (The full text can be read directly from the bottom left of WeChat).
- Sample code for this article: https://github.com/cxy35/samples/tree/master/redis
Scavenging focuses on WeChat Public Number programmer 35, getting the latest technology dry goods, and chatting with #programmer 35, programmer 35#.Standalone site: https://cxy35.com