In Spring Boot, Jedis is the default client, but after Spring, the default is Lettuce
Redis remote connection is not supported by default and needs to be manually enabled.
Modify the config file:
Comment out bind: 127.0.0.1
Turn on password verification and remove the comments of requireprass
Using the jedis client:
jedis' GitHub address:/xetorthio/jedis
How to connect remotely:
//Construct a jedis objectJedis jedis=new Jedis("127.0.0.1"); //If you have a password, you need to authenticate("root"); //Test whether the connection is successfulString ping =(); //Return to pong means success(ping);
The method API in jedis is the same as the commands for operating data in redis, so it is very convenient to use.
In actual applications, Jedis instances are generally obtained using connection pools, because Java is originally multi-threaded, and the Jedis object is not thread-safe, so you need to use a connection pool to obtain Jedis from the connection pool, and then return it to the connection pool after use. Make sure that his threads are safe.
How to create a Jedis connection pool
//1. Construct a jedis connection poolJedisPool pool=new JedisPool("127.0.0.1",6379); //2. Get a jedis connection from the connection poolJedis jedis=(); //3. Test whether the connection is successfulString ping =(); //Return to pong means success(ping); //4. Return the connection();
If there is a problem with our business code in the third step, that is, we cannot proceed to the fourth step. We can do an optimization, add a finally to close in the business code block, and return it to it if it is finally judged that Jedis is not empty. In this way, you can ensure that you return the jedis object every time.
However, this obviously feels very bloated and not very binding, so it can be optimized again, that is, it is handled in the form of interfaces and interface implementations, and then implement this interface when it needs to be called:
Create an interface:
public interface CallJedis { void call(Jedis jedis); }
Implementation of creating interfaces
public class Redis { private JedisPool pool; public Redis() { GenericObjectPoolConfig config = new GenericObjectPoolConfig(); //The maximum number of idle numbers for connection pools (300); //Maximum number of connections (1000); //The maximum connection waiting time, if it is -1 means there is no limit (30000); //Check validity when idle (true); /** * 1. Redis address * 2. Redis port * 3. Connection timeout * 4. Password */ pool = new JedisPool(config, "192.168.91.128", 6379, 30000, "root"); } public void execute(CallJedis callJedis) { try (Jedis jedis = ()) { (jedis); } } }
Calling the interface:
Redis redis = new Redis(); (jedis -> { (()); });
Using the Lettuce client:
GitHub address:/lettuce-io/lettuce-core
Lettuce and Jedis comparison
During the implementation process, Jedis is directly connected to redis. Sharing a Jedis instance between multiple threads is thread-insecure. If you want to use Jedis in a multi-threaded scenario, you have to use a connection pool. In this way, each thread has its own Jedis instance, but one disadvantage is that it will consume too much physical resources.
Lettuce is thread-safe because it is built using the Netty NIO framework. Supports synchronization, asynchronous, and response calls. Multiple threads can share a Lettuce instance without worrying about concurrency of multiple threads.
How to use in java:
1. Add dependencies.
2. Test
//Create the connection root is the password, 127.0.0.1 is the server address RedisClient redisClient =("redis://[email protected]"); //Create a connection channel StatefulRedisConnection<String, String> connect = (); //Get synchronous call object RedisCommands<String, String> sync = (); //Assignment ("name", "zl"); //Get the value String name = ("name"); //test (name); }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.