spring RedisTemplate的使用(一)--xml配置或JavaConfig配置
1、xml配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd "> <bean id="propertyConfigurer" class="utils.XmlPropertyPlaceholderSupport">
<property name="location" value="classpath:redis.properties" />
</bean> <!-- redis pool -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${redis.maxTotal}" />
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
</bean> <!-- redis pool config -->
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis.host}"></property>
<property name="port" value="${redis.port}"></property>
<property name="password" value="${redis.password}"></property>
<property name="poolConfig" ref="poolConfig"></property>
</bean> <!--redis redisTemplate -->
<bean id="redisTemplate" class="utils.RedisHelper">
<property name="connectionFactory" ref="jedisConnectionFactory" /> <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> <property name="enableTransactionSupport" value="true"></property>
</bean> <bean id="redisMessageListener" class="service.RedisSub">
<property name="redisTemplate" ref="redisTemplate" />
</bean> <bean id="redisMessageListenerContainer"
class="org.springframework.data.redis.listener.RedisMessageListenerContainer"
destroy-method="destroy">
<property name="connectionFactory" ref="jedisConnectionFactory" />
<property name="taskExecutor">
<bean class="org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler">
<property name="poolSize" value="10" />
</bean>
</property> <property name="messageListeners">
<map>
<entry key-ref="redisMessageListener">
<list>
<bean
class="org.springframework.data.redis.listener.ChannelTopic">
<constructor-arg value="cfd_md" />
</bean>
<!-- <bean class="org.springframework.data.redis.listener.PatternTopic">
<constructor-arg value="chat*" /> </bean> -->
</list>
</entry>
</map>
</property>
</bean>
</beans>
redis.properties
redis.hostpro=r-3ns15cdbd110c9c4.redis.rds.aliyuncs.com
redis.portpro=6379
redis.passwordpro=abc redis.hostbeta=127.0.0.1
redis.portbeta=6379
redis.passwordbeta=abc redis.hostlocal=47.244.48.230
redis.portlocal=6379
redis.passwordlocal=abc redis.timeout=10000
redis.maxIdle=1
redis.maxTotal=5
redis.maxWaitMillis=10000
redis.minEvictableIdleTimeMillis=300000
redis.numTestsPerEvictionRun=1024
redis.timeBetweenEvictionRunsMillis=30000
redis.testOnBorrow=true
redis.testWhileIdle=true #redis.sentinel.host1=172.20.1.230
#redis.sentinel.port1=26379 #redis.sentinel.host2=172.20.1.231
#redis.sentinel.port2=26379 #redis.sentinel.host3=172.20.1.232
#redis.sentinel.port3=26379
2、JavcConfig配置,使用springboot 2.1.3
package com.oy; import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.Topic;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; @Configuration
public class RedisConfig extends CachingConfigurerSupport {
public static final Logger log = LoggerFactory.getLogger(RedisConfig.class); @Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisSerializer<String> redisSerializer = new StringRedisSerializer(); // RedisTemplate
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(factory);
template.setKeySerializer(redisSerializer);
template.setValueSerializer(redisSerializer);
template.setHashKeySerializer(redisSerializer);
template.setHashValueSerializer(redisSerializer); template.afterPropertiesSet();
return template;
} @Bean(destroyMethod = "destroy")
public RedisMessageListenerContainer redisMessageListenerContainer(RedisConnectionFactory factory,
MessageListener redisMessageListener) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(factory); ThreadPoolTaskScheduler taskExecutor = new ThreadPoolTaskScheduler();
taskExecutor.setPoolSize(10);
taskExecutor.initialize();
container.setTaskExecutor(taskExecutor); Map<MessageListener, Collection<? extends Topic>> listeners = new HashMap<>();
List<Topic> list = new ArrayList<>();
list.add(new ChannelTopic("cfd_md"));
listeners.put(redisMessageListener, list);
container.setMessageListeners(listeners); return container;
} }
application.properties
#spring.profiles.active=beta
spring.profiles.active=local logging.file=/home/wwwlogs/xxx/log.log #redis
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=xxx spring.redis.jedis.pool.maxActive=8
spring.redis.jedis.pool.max-idle=8
spring.redis.jedis.pool.min-idle=0
spring.redis.timeout=0
spring RedisTemplate的使用(一)--xml配置或JavaConfig配置的更多相关文章
- spring bean的介绍以及xml和注解的配置方法
5.Bean 下边我们来了解一下Bean的: Bean的作用域Bean的生命周期Bean的自动装配Resources和ResourceLoader 5.1Bean容器的初始化 Bean容器的初始化 两 ...
- Spring RedisTemplate操作-xml配置(1)
网上没能找到全的spring redistemplate操作例子,故特意化了点时间做了接口调用练习,基本包含了所有redistemplate方法. 该操作例子是个系列,该片为spring xml配置, ...
- Spring学习记录(十三)---基于xml文件配置AOP
上一篇讲了用注解配置AOP,现在讲用xml怎么配置AOP 其实逻辑是一样的,只是用xml的方法,要把这种逻辑写出来,告诉spring框架去执行. 例子:这里的例子和上一篇的例子一样.换成xml方式 / ...
- (spring-第2回【IoC基础篇】)Spring的Schema,基于XML的配置
要深入了解Spring机制,首先需要知道Spring是怎样在IoC容器中装配Bean的.而了解这一点的前提是,要搞清楚Spring基于Schema的Xml配置方案. 在深入了解之前,必须要先明白几个标 ...
- Spring声明式事务(xml配置事务方式)
Spring声明式事务(xml配置事务方式) >>>>>>>>>>>>>>>>>>>& ...
- Spring第二篇和第三篇的补充【JavaConfig配置、c名称空间、装载集合、JavaConfig与XML组合】
前言 在写完Spring第二和第三篇后,去读了Spring In Action这本书-发现有知识点要补充,知识点跨越了第二和第三篇,因此专门再开一篇博文来写- 通过java代码配置bean 由于Spr ...
- 7 -- Spring的基本用法 -- 11... 基于XML Schema的简化配置方式
7.11 基于XML Schema的简化配置方式 Spring允许使用基于XML Schema的配置方式来简化Spring配置文件. 7.11.1 使用p:命名空间简化配置 p:命名空间不需要特定的S ...
- Spring介绍及配置(XML文件配置和注解配置)
本节内容: Spring介绍 Spring搭建 Spring概念 Spring配置讲解 使用注解配置Spring 一.Spring介绍 1. 什么是Spring Spring是一个开源框架,Sprin ...
- spring 和springmvc 在 web.xml中的配置
(1)问题:如何在Web项目中配置Spring的IoC容器? 答:如果需要在Web项目中使用Spring的IoC容器,可以在Web项目配置文件web.xml中做出如下配置: <!-- Sprin ...
随机推荐
- Kali-Dos洪水攻击之Hping3
在计算机行业,拒绝服务(DoS)或分布式拒绝服务(DDoS)攻击是指不法分子企图让某机器或网络资源无法被预期的用户所使用.虽然执行DoS攻击的方式.动机和目标不一样,但通常包括设法临时性或无限期中断或 ...
- 理解linux网络管理命令
linux 管理命令,iproute 查看帮助文件: man ip 以下为常用帮助文件. SEE ALSO ip-address(), ip-addrlabel(), ip-l2tp(), ip-li ...
- python 开发练习之 监控
本节内容 为什么要做监控? 常用监控系统设计讨论 监控系统架构设计 监控表结构设计 为什么要做监控? –熟悉IT监控系统的设计原理 –开发一个简版的类Zabbix监控系统 –掌握自动化开发项目的程序设 ...
- 用python写栈
class StackFullError(Exception): pass class StackEmptyError(Exception): pass class Stack: def __init ...
- xampp——apache服务启动问题(端口占用)
Apache启动提示 20:39:02 [Apache] Error: Apache shutdown unexpectedly.20:39:02 [Apache] This may be due t ...
- Linux内核源码分析 day01——内存寻址
前言 Linux内核源码分析 Antz系统编写已经开始了内核部分了,在编写时同时也参考学习一点Linux内核知识. 自制Antz操作系统 一个自制的操作系统,Antz .半图形化半命令式系统,同时嵌入 ...
- vue scrolle在tab 中使用
1. 使用npm 安装 npm i vue-scroller -S 地址: https://github.com/wangdahoo/vue-scroller2. 引入 main.js: import ...
- iOS __block 关键字的底层实现原理 -- 堆栈地址的变更
默认情况下,在block中访问的外部变量是复制过去的.但是可以加上 __block 来让其写操作生效. 原理: Block 不允许修改外部变量的值,这里所说的外部变量的值,指的是栈中指针的内存地址. ...
- HTML有哪些标签?html常用标签大全
html中标签有很多,每一种标签都有着不同的用处,下面这篇文章php中文网给大家总结html常用的标签,每一种标签都会跟随一个例子,话不多说,让我们来看看具体内容.<font>字体标签,用 ...
- zabbix报警逻辑初探
zabbix报警逻辑初探 首先贴出一张网上找的一张关于zabbix报警相关表结构及表关联逻辑图: actions表 actions表对应前端配置是动作(actions) action由conditio ...