Redis command line client
-
The command format
/redis-cli -h 127.0.0.1 -p 6379
-
Parameters that
-h: indicates the IP address of the Redis server. -p: indicates the port number of the Redis instanceCopy the code
-
The default mode
./redis-cli
- The default host address is 127.0.0.1
- The default port is 6379
-
legend
Java client Jedis
Jedis introduction
Redis is operated not only by command, but also by program client. Currently, most of the major languages have client support, such as Java, C, C#, C++, PHP, Node.js, Go, etc
In the official website listed some Java clients, there are Jedis, Redisson, Jredis, JDBC-Redis, etc., which the official recommendation to use Jedis and Redisson. The most commonly used in enterprises is Jedis. Let’s focus on Jedis
Jedis is also hosted on Github at github.com/xetorthio/j…
Add the dependent
<dependencies> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> < version > 5.0.7. RELEASE < / version > < / dependency > < the dependency > < groupId > org. Springframework < / groupId > < artifactId > spring - test < / artifactId > < version > 5.0.7. RELEASE < / version > < / dependency > <! Junit </artifactId> <version>4.12</version> </version> </dependency> </dependencies> <build> <plugins> <! <groupId> <groupId>org.apache.maven.plugins</groupId> < artifactId > maven -- the compiler plugin < / artifactId > < version > 3.2 < / version > < configuration > < source > 1.8 < / source > <target>1.8</target> <encoding> utF-8 </encoding> </configuration> </plugins> </build>Copy the code
Single instance connection
@test public void testJedis() {// create a Jedis connection. Jedis Jedis = new Jedis("127.0.0.1", 6379); Set ("mytest", "hello world, this is jedis client!") ); String result = jedis.get("mytest"); System.out.println(result); // Close the connection jedis.close(); }Copy the code
Connection pool connection
@test public void testJedisPool() {// create a connection pool object JedisPool JedisPool = new JedisPool("127.0.0.1", 6379); Jedis Jedis = jedispool.getResource (); String result = jedis.get("mytest") ; System.out.println(result); // Close the connection jedis.close(); // Close the connection pool jedispool.close (); }Copy the code
Connect to the Redis cluster
Create the JedisCluster class to connect to the Redis cluster
Public void testJedisCluster() throws Exception {// Create a connection, Set<HostAndPort> Nodes = new HashSet<>(); Set<HostAndPort> Nodes = new HashSet<>(); Nodes. The add (new HostAndPort (" 192.168.242.129 ", 7001)); Nodes. The add (new HostAndPort (" 192.168.242.129 ", 7002)); Nodes. The add (new HostAndPort (" 192.168.242.129 ", 7003)); Nodes. The add (new HostAndPort (" 192.168.242.129 ", 7004)); Nodes. The add (new HostAndPort (" 192.168.242.129 ", 7005)); Nodes. The add (new HostAndPort (" 192.168.242.129 ", 7006)); JedisCluster cluster = new JedisCluster(nodes); // Execute the methods in the JedisCluster object, which correspond to redis. cluster.set("cluster-test", "my jedis cluster test"); String result = cluster.get("cluster-test"); System.out.println(result); // JedisCluster object cluster.close(); }Copy the code
Jedis integration of spring
Configure the Spring configuration file applicationContext.xml
<? The XML version = "1.0" encoding = "utf-8"? > <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <! - the connection pool configuration - > < bean id = "jedisPoolConfig" class = "redis. Clients. Jedis. JedisPoolConfig" > <! <property name="maxTotal" value="30" /> <! -- maxnumber of idle connections --> <property name="maxIdle" value="10" /> <! <property name="numTestsPerEvictionRun" value="1024" /> <! - release connection scanning interval (ms) - > < property name = "timeBetweenEvictionRunsMillis" value = "30000" / > <! - connect the minimum free time - > < property name = "minEvictableIdleTimeMillis" value = "1800000" / > <! - how long connection idle after release, when idle time > the value and free connection > when the maximum number of idle connections directly release - > < property name = "softMinEvictableIdleTimeMillis" value = "10000" / > <! -- Maximum number of milliseconds to wait to get a connection, less than zero: indefinite blocking time, default -1 --> <property name="maxWaitMillis" value="1500" /> <! <property name="testOnBorrow" value="true" /> <! TestWhileIdle value="true" /> <! --> <property name="blockWhenExhausted" value="false" /> </bean> <! - redis LAN through the connection pool - - > < bean id = "jedisPool" class = "redis. Clients. Jedis. JedisPool" destroy - method = "close" > < constructor - arg Name ="poolConfig" ref="jedisPoolConfig" /> <constructor-arg name="host" value="192.168.10.135" /> <constructor-arg name="port" value="6379" /> </bean> <! - redis cluster - > < bean id = "jedisCluster" class = "redis. Clients. Jedis. JedisCluster" > < constructor - arg index = "0" > < set > < bean Class = "redis. Clients. Jedis. HostAndPort" > < constructor - arg index = "0" value = "192.168.10.135" > < / constructor - arg > <constructor-arg index="1" value="7001"></constructor-arg> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg index="0" value="192.168.10.135"></constructor-arg> <constructor-arg index="1" value="7002"></constructor-arg> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg index="0" Value ="192.168.10.135"></constructor-arg> <constructor-arg index="1" value="7003"></constructor-arg> </bean> <bean Class = "redis. Clients. Jedis. HostAndPort" > < constructor - arg index = "0" value = "192.168.10.135" > < / constructor - arg > <constructor-arg index="1" value="7004"></constructor-arg> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg index="0" value="192.168.10.135"></constructor-arg> <constructor-arg index="1" value="7005"></constructor-arg> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg index="0" Value ="192.168.10.135"></constructor-arg> <constructor-arg index="1" value="7006"></constructor-arg> </bean> </set> </constructor-arg> <constructor-arg index="1" ref="jedisPoolConfig"></constructor-arg> </bean> </beans>Copy the code
The test code
import javax.annotation.Resource; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisCluster; import redis.clients.jedis.JedisPool; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:application.xml") public class TestJedis2 { @Autowired private JedisPool jedisPool; @Resource private JedisCluster cluster; @test public void testJedisPool() {// get a connection from the connection pool Jedis Jedis = jedispool.getResource (); String result = jedis.get("mytest"); System.out.println(result); // Close the connection jedis.close(); } @test public void testJedisCluster() throws Exception {// Execute methods in the JedisCluster object. Methods correspond to redis one to one. cluster.set("cluster-test", "my jedis cluster test"); String result = cluster.get("cluster-test"); System.out.println(result); }}Copy the code