Redis java client ==> Jedis
https://github.com/xetorthio/jedis
Jedis is a blazingly small and sane Redis java client.
Jedis was conceived to be EASY to use.
Jedis is fully compatible with redis 2.8.x and 3.0.x.
小、功能健全、简单易用、全面兼容
1. 单客户端
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.7.2</version> </dependency>
Jedis jedis = new Jedis("localhost"); jedis.set("foo", "bar"); String value = jedis.get("foo");
2. 集群
Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>(); //Jedis Cluster will attempt to discover cluster nodes automatically jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7379)); JedisCluster jc = new JedisCluster(jedisClusterNodes); jc.set("foo", "bar"); String value = jc.get("foo");
Spring 集成
简单的
pom.xml
<dependencies> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.7.2</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.1.6.RELEASE</version> </dependency> </dependencies>
beans.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"> <context:annotation-config /> <beans> <bean id="jedis" class="redis.clients.jedis.Jedis"> <constructor-arg value="172.16.162.248"></constructor-arg> </bean> </beans> </beans>
@Autowired private Jedis jedis;
Jedis Cluster
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.zno</groupId> <artifactId>redisCluster</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.1.6.RELEASE</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.7.2</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>3.2.3.RELEASE</version> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> </dependencies> </project>
beans-redis.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"> <context:annotation-config /> <bean class="redis.clients.jedis.JedisCluster"> <constructor-arg> <set> <ref bean="node1" /> <ref bean="node2" /> <ref bean="node3" /> <ref bean="node4" /> <ref bean="node5" /> <ref bean="node6" /> <ref bean="node7" /> <ref bean="node8" /> <ref bean="node9" /> </set> </constructor-arg> </bean> <bean id="node1" class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="172.16.20.46"></constructor-arg> <constructor-arg name="port" value="7001"></constructor-arg> </bean> <bean id="node2" class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="172.16.20.46"></constructor-arg> <constructor-arg name="port" value="7002"></constructor-arg> </bean> <bean id="node3" class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="172.16.20.46"></constructor-arg> <constructor-arg name="port" value="7003"></constructor-arg> </bean> <bean id="node4" class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="172.16.20.46"></constructor-arg> <constructor-arg name="port" value="7004"></constructor-arg> </bean> <bean id="node5" class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="172.16.20.46"></constructor-arg> <constructor-arg name="port" value="7005"></constructor-arg> </bean> <bean id="node6" class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="172.16.20.46"></constructor-arg> <constructor-arg name="port" value="7006"></constructor-arg> </bean> <bean id="node7" class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="172.16.20.46"></constructor-arg> <constructor-arg name="port" value="7007"></constructor-arg> </bean> <bean id="node8" class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="172.16.20.46"></constructor-arg> <constructor-arg name="port" value="7008"></constructor-arg> </bean> <bean id="node9" class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="172.16.20.46"></constructor-arg> <constructor-arg name="port" value="7009"></constructor-arg> </bean> </beans>
TestRedisCluster.java
package redisCluster; 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.JedisCluster; @ContextConfiguration(locations = {"classpath:beans-redis.xml"}) @RunWith(SpringJUnit4ClassRunner.class) public class TestRedisCluster { @Autowired private JedisCluster jedisCluster; @Test public void tt(){ String key = "testkey"; jedisCluster.set(key, "1"); System.out.println(jedisCluster.get(key)); jedisCluster.del(key); System.out.println(jedisCluster.get(key)); } }
打印结果:
九月 16, 2015 4:57:10 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from class path resource [beans-redis.xml] 九月 16, 2015 4:57:11 下午 org.springframework.context.support.GenericApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.GenericApplicationContext@1a705d7: startup date [Wed Sep 16 16:57:11 CST 2015]; root of context hierarchy 1 null