Spring整合Redis
1.配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
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-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
<!--集群版的配置 -->
<!-- <bean id="jedisCluster" class="redis.clients.jedis.JedisCluster">
<constructor-arg>
<set>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.25.130"></constructor-arg>
<constructor-arg name="port" value="7001"></constructor-arg>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.25.130"></constructor-arg>
<constructor-arg name="port" value="7001"></constructor-arg>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.25.130"></constructor-arg>
<constructor-arg name="port" value="7001"></constructor-arg>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.25.130"></constructor-arg>
<constructor-arg name="port" value="7001"></constructor-arg>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.25.130"></constructor-arg>
<constructor-arg name="port" value="7001"></constructor-arg>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.25.130"></constructor-arg>
<constructor-arg name="port" value="7001"></constructor-arg>
</bean>
</set>
</constructor-arg>
</bean>
<bean id="jedisClientCluster" class="cn.e3mall.common.jedis.JedisClientCluster">
<property name="jedisCluster" ref="jedisCluster"></property>
</bean> -->
<!--单机版 -->
<bean id="jedisPool" class="redis.clients.jedis.JedisPool">
<constructor-arg name="host" value="192.168.25.130"></constructor-arg>
<constructor-arg name="port" value="6379"></constructor-arg>
</bean>
<bean id="jedisClientPool" class="cn.e3mall.common.jedis.JedisClientPool">
<property name="jedisPool" ref="jedisPool"></property>
</bean>
</beans>
测试代码
@Test
public void testJedis() throws Exception{
//第一步:创建一个Jedis对象,需要指定服务端的ip及端口
Jedis jedis = new Jedis("192.168.25.130",6379);
//第二步:使用Jedis对象操作数据库,每个redis命令对应一个方法
jedis.set("jedis", "helloworld");
String result = jedis.get("jedis");
System.out.println(result);
//关闭Jedis
jedis.close();
}
@Test
public void testJedisPool() {
//第一步: 创建一个JedisPool对象。需要指定服务端的ip及端口
JedisPool jedisPool = new JedisPool("192.168.25.130",6379);
//第二步:从JedisPool中获得Jedis对象
Jedis jedis = jedisPool.getResource();
//第三步:使用Jedis操作redis服务器
jedis.set("JedisPool", "test");
String result = jedis.get("JedisPool");
System.out.println(result);
//第四步:操作完毕后关闭jedis对象,连接池回收资源
jedis.close();
//第五步:关闭JedisPool对象
jedisPool.close();
}
@Test
public void testJedisCluster() throws Exception {
// 第一步:使用JedisCluster对象。需要一个Set<HostAndPort>参数。Redis节点的列表。
Set<HostAndPort> nodes = new HashSet<>();
nodes.add(new HostAndPort("192.168.25.130", 7001));
nodes.add(new HostAndPort("192.168.25.130", 7002));
nodes.add(new HostAndPort("192.168.25.130", 7003));
nodes.add(new HostAndPort("192.168.25.130", 7004));
nodes.add(new HostAndPort("192.168.25.130", 7005));
nodes.add(new HostAndPort("192.168.25.130", 7006));
JedisCluster jedisCluster = new JedisCluster(nodes);
// 第二步:直接使用JedisCluster对象操作redis。在系统中单例存在。
jedisCluster.set("hello", "100");
String result = jedisCluster.get("hello");
// 第三步:打印结果
System.out.println(result);
// 第四步:系统关闭前,关闭JedisCluster对象。
// jedisCluster.close();
}
@Test
public void testJedisClientPool() {
//加载配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-redis.xml");
//获取对象(单机版)
JedisClientPool jedisClientPool = (JedisClientPool) context.getBean("jedisClientPool");
//使用jedisClientPool
String result = jedisClientPool.get("JedisPool");
//输出结果
System.out.println(result);
}
@Test
public void testJedisClientCluster() {
//加载配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-redis.xml");
//获取对象
JedisClientCluster jedisClientCluster = (JedisClientCluster) context.getBean("jedisClientCluster");
//使用
String result = jedisClientCluster.get("hello");
System.out.println(result);
}
SpringBoot整合
SpringBoot操作数据:spring-data jpa jdbc mongodb redis
SpringData 也是和SpringBoot齐名的项目
说明:在SpringBoot2.x之后,原来使用的jedis被替换为lettuce
jedis:采用的直连,多个线程操作的话,是不安全的,如果想要避免不安全,使用jedis pool连接池 BIO
lettuce:采用netty,实例可以在多个线程中进行共享,不存在线程不安全的情况
/编写我们自己的RedisTempalte
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
//我们为了自己开发方便,一般直接使用<String,Object>
RedisTemplate<String,Object> template = new RedisTemplate<String,Object>();
template.setConnectionFactory(factory);
//Json序列化配置
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
//String的序列化
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
//key采用String的序列化方法
template.setKeySerializer(stringRedisSerializer);
//hash的key也采用String的序列化方式
template.setHashKeySerializer(stringRedisSerializer);
//value序列化方式采用jackson
template.setHashValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
序列化配置实现类