Redis客户端的几种使用

POM文件:

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-redis</artifactId>
    <version>1.5.2.RELEASE</version>
</dependency>
<dependency>
	<groupId>redis.clients</groupId>
	<artifactId>jedis</artifactId>
</dependency>

代码片段演示基于junit进行 。

 

场景一:单机Redis部署,使用Jedis

配置片段:

<bean id="jedis" class="redis.clients.jedis.Jedis">
	<constructor-arg name="host" value="192.168.0.200" />
	<constructor-arg name="port" value="6379" />
</bean>

 

代码片段:

FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext("classpath:spring/scm-cache-redis-config1.xml");
Jedis jedis = (Jedis) context.getBean("jedis");
/**
 * 设置连接密码
 */
//jedis.auth("123456");
jedis.set("jedis", "jedis test");
assertEquals("jedis test", jedis.get("jedis"));

 

场景二:集群Redis部署(主从+读写分离),使用Jedis连接池(JedisPool)

配置片段:

<!--Jedis连接池的相关配置 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
	<!--新版是maxTotal,旧版是maxActive -->
	<property name="maxTotal">
		<value>1000</value>
	</property>
	<property name="maxIdle">
		<value>50</value>
	</property>
	<property name="testOnBorrow" value="true" />
	<property name="testOnReturn" value="true" />
</bean>
<!-- Jedis连接池 -->
<bean id="jedisPool" class="redis.clients.jedis.JedisPool">
	<constructor-arg name="poolConfig" ref="jedisPoolConfig" />
	<constructor-arg name="host" value="192.168.0.200" />
	<constructor-arg name="port" value="6379" type="int" />
	<constructor-arg name="timeout" value="5000" type="int" />
	<!--密码和数据库索引配置 
	<constructor-arg name="password" value="123456" />
	<constructor-arg name="database" value="0" type="int" />
	 -->
</bean>

 

扫描二维码关注公众号,回复: 255937 查看本文章

代码片段:

FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext("classpath:spring/scm-cache-redis-config1.xml");
JedisPool jedisPool=(JedisPool) context.getBean("jedisPool");
Jedis jedis=jedisPool.getResource();
jedis.set("jedis pool", "jedis pool test");
assertEquals("jedis pool test", jedis.get("jedis pool"));

 

场景三:集群Redis部署(主从+读写分离)+哨兵集群

说明:使用spring提供的客户端模板(RedisTemplate)进行数据操作,依赖spring的spring-data-redis-1.5.2.RELEASE.jar

 

配置片段:

<!-- Jedis连接池的相关配置 -->
<bean id="jedisPoolConfig2" class="redis.clients.jedis.JedisPoolConfig">
	<property name="maxTotal" value="1024" />
	<property name="maxIdle" value="200" />
	<property name="numTestsPerEvictionRun" value="1024" />
	<property name="timeBetweenEvictionRunsMillis" value="30000" />
	<property name="minEvictableIdleTimeMillis" value="30000" />
	<property name="softMinEvictableIdleTimeMillis" value="10000" />
	<property name="maxWaitMillis" value="1000" />
	<property name="testOnBorrow" value="true" />
</bean>
<!-- 哨兵配置 -->
<bean id="sentinelConfiguration"
	class="org.springframework.data.redis.connection.RedisSentinelConfiguration">
	<property name="master">
		<bean class="org.springframework.data.redis.connection.RedisNode">
			<!--这个值要和Sentinel中指定的master的值一致,不然启动时找不到Sentinel会报错的 -->
			<property name="name" value="m1"></property>
		</bean>
	</property>
	<!--记住了,这里是指定Sentinel的IP和端口,不是Master和Slave的 -->
	<property name="sentinels">
		<set>
			<bean class="org.springframework.data.redis.connection.RedisNode">
				<constructor-arg name="host" value="192.168.0.200"></constructor-arg>
				<constructor-arg name="port" value="26379"></constructor-arg>
			</bean>
			<bean class="org.springframework.data.redis.connection.RedisNode">
				<constructor-arg name="host" value="192.168.0.201"></constructor-arg>
				<constructor-arg name="port" value="26379"></constructor-arg>
			</bean>
			<bean class="org.springframework.data.redis.connection.RedisNode">
				<constructor-arg name="host" value="192.168.0.202"></constructor-arg>
				<constructor-arg name="port" value="26379"></constructor-arg>
			</bean>
		</set>
	</property>
</bean>
<!-- 连接工厂 -->
<bean id="redisConnectionFactory"
	class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
	<constructor-arg name="sentinelConfig" ref="sentinelConfiguration"></constructor-arg>
	<constructor-arg name="poolConfig" ref="jedisPoolConfig2"></constructor-arg>
</bean>
<!-- redis模板工具类 -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
	<property name="connectionFactory" ref="redisConnectionFactory"></property>
	<!-- 解决使用RedisTemplate乱码问题 -->
	<property name="keySerializer">
		<bean
			class="org.springframework.data.redis.serializer.StringRedisSerializer" />
	</property>
	<property name="valueSerializer">
		<bean
			class="org.springframework.data.redis.serializer.StringRedisSerializer" />
	</property>
	<property name="hashKeySerializer">
		<bean
			class="org.springframework.data.redis.serializer.StringRedisSerializer" />
	</property>
	<property name="hashValueSerializer">
		<bean
			class="org.springframework.data.redis.serializer.StringRedisSerializer" />
	</property>
</bean>

 

代码片段:

FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext("classpath:spring/scm-cache-redis-config1.xml");
RedisTemplate redisTemplate=(RedisTemplate) context.getBean("redisTemplate");
redisTemplate.opsForValue().set("xa", "西安");
assertEquals("西安",redisTemplate.opsForValue().get("xa"));

 

场景四:集群Redis部署(主从+读写分离)+哨兵集群

说明:使用Jedis自带的哨兵池(JedisSentinelPool)进行Jedis资源的管理(类似于JedisPool)

配置片段:

<!-- Jedis连接池的相关配置 -->
<bean id="jedisPoolConfig3" class="redis.clients.jedis.JedisPoolConfig">
	<property name="maxTotal" value="1024" />
	<property name="maxIdle" value="200" />
	<property name="numTestsPerEvictionRun" value="1024" />
	<property name="timeBetweenEvictionRunsMillis" value="30000" />
	<property name="minEvictableIdleTimeMillis" value="30000" />
	<property name="softMinEvictableIdleTimeMillis" value="10000" />
	<property name="maxWaitMillis" value="1000" />
	<property name="testOnBorrow" value="true" />
</bean>
<!-- JedisSentinelPool池配置 -->
<bean id="jedisSentinelPool" class="redis.clients.jedis.JedisSentinelPool">
	<!--这个值要和Sentinel中指定的master的值一致,不然启动时找不到Sentinel会报错的 -->
<constructor-arg index="0" value="m1" />
<constructor-arg index="1">
    <set>
	<value>192.168.0.200:26379</value>
	<value>192.168.0.201:26379</value>
	<value>192.168.0.202:26379</value>
    </set>
</constructor-arg>
<constructor-arg index="2" ref="jedisPoolConfig3" />
</bean>

代码片段:

FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext("classpath:spring/scm-cache-redis-config1.xml");
JedisSentinelPool jedisSentinelPool=(JedisSentinelPool) context.getBean("jedisSentinelPool");
Jedis jedis = null;
try {
    jedis = jedisSentinelPool.getResource();
    jedis.set("bj", "北京");
    assertEquals("北京", jedis.get("bj"));
} catch (Exception e) {
    e.printStackTrace();
} finally {
	jedisSentinelPool.returnBrokenResource(jedis);
}
jedisSentinelPool.close();

场景五:分片(多Master,独立)+一致性HASH

配置片段:

<!-- Jedis连接池的相关配置 -->
	<bean id="jedisPoolConfig4" class="redis.clients.jedis.JedisPoolConfig">
		<property name="maxTotal" value="1024" />
		<property name="maxIdle" value="200" />
		<property name="numTestsPerEvictionRun" value="1024" />
		<property name="timeBetweenEvictionRunsMillis" value="30000" />
		<property name="minEvictableIdleTimeMillis" value="30000" />
		<property name="softMinEvictableIdleTimeMillis" value="10000" />
		<property name="maxWaitMillis" value="1000" />
		<property name="testOnBorrow" value="true" />
	</bean>
    <bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool">
        <constructor-arg index="0" ref="jedisPoolConfig4" />
        <constructor-arg index="1">
            <list>
                <bean class="redis.clients.jedis.JedisShardInfo">
                    <constructor-arg name="host" value="192.168.0.200" />
                    <constructor-arg name="name" value="master:200" />
                    <constructor-arg name="port" value="6379" />
                    <constructor-arg name="timeout" value="5000" />
                    <constructor-arg name="weight" value="1" />
                    <!-- <property name="password" value="123456" /> -->
                </bean>
                <bean class="redis.clients.jedis.JedisShardInfo">
                    <constructor-arg name="host" value="192.168.0.201" />
                    <constructor-arg name="name" value="master:201" />
                    <constructor-arg name="port" value="6379" />
                    <constructor-arg name="timeout" value="5000" />
                    <constructor-arg name="weight" value="1" />
                    <!-- <property name="password" value="123456" /> -->
                </bean>
                <bean class="redis.clients.jedis.JedisShardInfo">
                    <constructor-arg name="host" value="192.168.0.202" />
                    <constructor-arg name="name" value="master:202" />
                    <constructor-arg name="port" value="6379" />
                    <constructor-arg name="timeout" value="5000" />
                    <constructor-arg name="weight" value="1" />
                    <!-- <property name="password" value="123456" /> -->
                </bean>
            </list>
        </constructor-arg>
    </bean>
 

代码片段:

FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext("classpath:spring/scm-cache-redis-config1.xml");
ShardedJedisPool shardedJedisPool=(ShardedJedisPool) context.getBean("shardedJedisPool");
ShardedJedis shardedJedis = shardedJedisPool.getResource();;
try {
	shardedJedis.set("cw", "宠物");
	assertEquals("宠物", shardedJedis.get("cw"));
} catch (Exception e) {
    e.printStackTrace();
} finally {
    if(shardedJedis!=null) {
	shardedJedis.close();
    }
}

场景六:redis+cluster

配置片段:

    <bean id="jedisCluster" class="redis.clients.jedis.JedisCluster">
        <constructor-arg index="0">
        	<!-- 集群中的任意节点,个数不限 -->
            <set>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg name="host" value="192.168.0.200"/>
                    <constructor-arg name="port" value="7000"/>
                </bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg name="host" value="192.168.0.200"/>
                    <constructor-arg name="port" value="7001"/>
                </bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg name="host" value="192.168.0.201"/>
                    <constructor-arg name="port" value="7000"/>
                </bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg name="host" value="192.168.0.201"/>
                    <constructor-arg name="port" value="7001"/>
                </bean>
            </set>
        </constructor-arg>
    </bean>

代码片段:

FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext("classpath:spring/scm-cache-redis-config1.xml");
JedisCluster jedisCluster=(JedisCluster) context.getBean("jedisCluster");
try {
	jedisCluster.set("mary", "麦瑞");
	assertEquals("麦瑞", jedisCluster.get("mary"));
} catch (Exception e) {
	e.printStackTrace();
} finally {
	if(jedisCluster!=null) {
		try {
			jedisCluster.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

猜你喜欢

转载自hengdu.iteye.com/blog/2405046