基于spring的redis配置(单机和集群模式)
本篇文章给大家带来的内容是关于基于spring的redis配置(单机和集群模式) ,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
需要的jar包:spring版本:4.3.6.RELEASE,jedis版本:2.9.0,spring-data-redis:1.8.0.RELEASE;如果使用jackson序列化的话还额外需要:jackson-annotations和jackson-databind包
spring集成redis单机版: 1.配置RedisTemplate <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="connectionFactory"/> <property name="defaultSerializer" ref="stringRedisSerializer"/> <property name="keySerializer" ref="stringRedisSerializer"/> <property name="valueSerializer" ref="valueSerializer"/> </bean> 2.配置connectionFactory <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <!-- 配置ip --> <property name="hostName" value="${redis.host}"/> <!-- 配置port --> <property name="port" value="${redis.port}"/> <!-- 是否使用连接池--> <property name="usePool" value="${redis.usePool}"/> <!-- 配置redis连接池--> <property name="poolConfig" ref="poolConfig"/> </bean> 3.配置连接池 <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> <!--最大空闲实例数--> <property name="maxIdle" value="${redis.maxIdle}" /> <!--最大活跃实例数--> <property name="maxTotal" value="${redis.maxTotal}" /> <!--创建实例时最长等待时间--> <property name="maxWaitMillis" value="${redis.maxWaitMillis}" /> <!--创建实例时是否验证--> <property name="testOnBorrow" value="${redis.testOnBorrow}" /> </bean> spring集成redis集群 1.配置RedisTemplate步骤与单机版一致 2.配置connectionFactory <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <!-- 配置redis连接池--> <constructor-arg ref="poolConfig"></constructor-arg> <!-- 配置redis集群--> <constructor-arg ref="clusterConfig"></constructor-arg> <!-- 是否使用连接池--> <property name="usePool" value="${redis.usePool}"/> </bean> 3.配置连接池步骤与单机版一致 4.配置redis集群 <bean id="clusterConfig" class="org.springframework.data.redis.connection.RedisClusterConfiguration"> <property name="maxRedirects" value="3"></property> <property name="clusterNodes"> <set> <bean class="org.springframework.data.redis.connection.RedisClusterNode"> <constructor-arg value="${redis.host1}"></constructor-arg> <constructor-arg value="${redis.port1}"></constructor-arg> </bean> <bean class="org.springframework.data.redis.connection.RedisClusterNode"> <constructor-arg value="${redis.host2}"></constructor-arg> <constructor-arg value="${redis.port2}"></constructor-arg> </bean> ...... </set> </property> </bean> 或者 <bean name="propertySource" class="org.springframework.core.io.support.ResourcePropertySource"> <constructor-arg name="location" value="classpath:properties/spring-redis-cluster.properties" /> </bean> <bean id="clusterConfig" class="org.springframework.data.redis.connection.RedisClusterConfiguration"> <constructor-arg name="propertySource" ref="propertySource"/> </bean>
序列化配置简述:
1.stringRedisSerializer:由于redis的key是String类型所以一般使用StringRedisSerializer 2.valueSerializer:对于redis的value序列化,spring-data-redis提供了许多序列化类,这里建议使用Jackson2JsonRedisSerializer,默认为JdkSerializationRedisSerializer 3.JdkSerializationRedisSerializer: 使用JDK提供的序列化功能。 优点是反序列化时不需要提供类型信息(class),但缺点是序列化后的结果非常庞大,是JSON格式的5倍左右,这样就会消耗redis服务器的大量内存。 4.Jackson2JsonRedisSerializer:使用Jackson库将对象序列化为JSON字符串。优点是速度快,序列化后的字符串短小精悍。但缺点也非常致命,那就是此类的构造函数中有一个类型参数,必须提供要序列化对象的类型信息(.class对象)。
使用spring注解式来配置redis,这里只配置集群样例
@Configuration @EnableCaching public class RedisConfig extends CachingConfigurerSupport { //spring3支持注解方式获取value 在application里配置配置文件路径即可获取 @Value("${spring.redis.cluster.nodes}") private String clusterNodes; @Value("${spring.redis.cluster.timeout}") private Long timeout; @Value("${spring.redis.cluster.max-redirects}") private int redirects; @Value("${redis.maxIdle}") private int maxIdle; @Value("${redis.maxTotal}") private int maxTotal; @Value("${redis.maxWaitMillis}") private long maxWaitMillis; @Value("${redis.testOnBorrow}") private boolean testOnBorrow; /** * 选择redis作为默认缓存工具 * @param redisTemplate * @return */ @Bean public CacheManager cacheManager(RedisTemplate redisTemplate) { RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate); //cacheManager.setDefaultExpiration(60); //Map<String,Long> expiresMap=new HashMap<>(); //expiresMap.put("redisCache",5L); //cacheManager.setExpires(expiresMap); return cacheManager; } @Bean public RedisClusterConfiguration redisClusterConfiguration(){ Map<String, Object> source = new HashMap<>(); source.put("spring.redis.cluster.nodes", clusterNodes); source.put("spring.redis.cluster.timeout", timeout); source.put("spring.redis.cluster.max-redirects", redirects); return new RedisClusterConfiguration(new MapPropertySource("RedisClusterConfiguration", source)); } @Bean public JedisConnectionFactory redisConnectionFactory(RedisClusterConfiguration configuration){ JedisPoolConfig poolConfig = new JedisPoolConfig(); poolConfig.setMaxIdle(maxIdle); poolConfig.setMaxTotal(maxTotal); poolConfig.setMaxWaitMillis(maxWaitMillis); poolConfig.setTestOnBorrow(testOnBorrow); return new JedisConnectionFactory(configuration,poolConfig); } /** * retemplate相关配置 * @param factory * @return */ @Bean public RedisTemplate<String, Object> redisTemplate(JedisConnectionFactory factory) { RedisTemplate<String, Object> template = new RedisTemplate<>(); // 配置连接工厂 template.setConnectionFactory(factory); //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式) Jackson2JsonRedisSerializer jacksonSeial = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper om = new ObjectMapper(); // 指定要序列化的域,field,get和set,以及修饰符范围,ANY是都有包括private和public om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); // 指定序列化输入的类型,类必须是非final修饰的,final修饰的类,比如String,Integer等会跑出异常 //om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jacksonSeial.setObjectMapper(om); // 值采用json序列化 template.setValueSerializer(jacksonSeial); //使用StringRedisSerializer来序列化和反序列化redis的key值 template.setKeySerializer(new StringRedisSerializer()); // 设置hash key 和value序列化模式 template.setHashKeySerializer(new StringRedisSerializer()); template.setHashValueSerializer(jacksonSeial); template.afterPropertiesSet(); return template; } }
注意事项:
1.采用注解式配置redis或者使用@Configuration需要在配置文件中指定扫描什么哪些包下的配置文件,当然如果是springboot那当我没说过这句话...
<context:component-scan base-package="com.*"/>
2.spring3支持注解方式获取Value,但是需要在加载的配置文件配置文件路径即可,具体值自己指定吧...
<value>classpath:properties/spring-redis-cluster.properties</value>
3.由于我们公司原有的框架采用的是spring2.5.6, 而对于spring2 和spring2+主要区别(当然是我自己觉得啊)是把各模块分成了不同的jar,而对于使用spring-data-redis模板化处理redis的话,单机情况下spring2.5.6和spring4不会冲突,而如果使用集群模式需要配置redis集群的时候就会出现jar包冲突,这个时候就看要如何取决了,可以直接使用jedisCluster来连接redis集群(不过很多方法都需要自己去写),也可以把spring2.5.6替换成高版本的spring4,只是框架替换需要注意的事情更多了啊(我们公司的直接全部替换没啥毛病好吧,O(∩_∩)O哈哈~),至于重写JedisConnectionFactory和RedisClusterConfiguration我还未去尝试,这个可以作为后续补充吧...
4.顺便说句,spring4不在支持ibatis了,如果你需要用spring4,又需要连接ibatis的话,最粗暴的方式是把spring-orm包换成spring3版本,其他的jar还是4版本即可。(当然我这边直接替换是没啥问题,但同3一样可能存在潜在问题啊,所以说嘛,公司有时候还是需要更新换代下吧...)
以上是基于spring的redis配置(单机和集群模式)的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

Redis集群模式通过分片将Redis实例部署到多个服务器,提高可扩展性和可用性。搭建步骤如下:创建奇数个Redis实例,端口不同;创建3个sentinel实例,监控Redis实例并进行故障转移;配置sentinel配置文件,添加监控Redis实例信息和故障转移设置;配置Redis实例配置文件,启用集群模式并指定集群信息文件路径;创建nodes.conf文件,包含各Redis实例的信息;启动集群,执行create命令创建集群并指定副本数量;登录集群执行CLUSTER INFO命令验证集群状态;使

PHP是一种广泛应用于服务器端的脚本语言,特别适合web开发。1.PHP可以嵌入HTML,处理HTTP请求和响应,支持多种数据库。2.PHP用于生成动态网页内容,处理表单数据,访问数据库等,具有强大的社区支持和开源资源。3.PHP是解释型语言,执行过程包括词法分析、语法分析、编译和执行。4.PHP可以与MySQL结合用于用户注册系统等高级应用。5.调试PHP时,可使用error_reporting()和var_dump()等函数。6.优化PHP代码可通过缓存机制、优化数据库查询和使用内置函数。7

PHP和Python各有优势,选择应基于项目需求。1.PHP适合web开发,语法简单,执行效率高。2.Python适用于数据科学和机器学习,语法简洁,库丰富。

如何清空 Redis 数据:使用 FLUSHALL 命令清除所有键值。使用 FLUSHDB 命令清除当前选定数据库的键值。使用 SELECT 切换数据库,再使用 FLUSHDB 清除多个数据库。使用 DEL 命令删除特定键。使用 redis-cli 工具清空数据。

PHP适合web开发,特别是在快速开发和处理动态内容方面表现出色,但不擅长数据科学和企业级应用。与Python相比,PHP在web开发中更具优势,但在数据科学领域不如Python;与Java相比,PHP在企业级应用中表现较差,但在web开发中更灵活;与JavaScript相比,PHP在后端开发中更简洁,但在前端开发中不如JavaScript。

PHP和Python各有优势,适合不同场景。1.PHP适用于web开发,提供内置web服务器和丰富函数库。2.Python适合数据科学和机器学习,语法简洁且有强大标准库。选择时应根据项目需求决定。

要从 Redis 读取队列,需要获取队列名称、使用 LPOP 命令读取元素,并处理空队列。具体步骤如下:获取队列名称:以 "queue:" 前缀命名,如 "queue:my-queue"。使用 LPOP 命令:从队列头部弹出元素并返回其值,如 LPOP queue:my-queue。处理空队列:如果队列为空,LPOP 返回 nil,可先检查队列是否存在再读取元素。

PHP成为许多网站首选技术栈的原因包括其易用性、强大社区支持和广泛应用。1)易于学习和使用,适合初学者。2)拥有庞大的开发者社区,资源丰富。3)广泛应用于WordPress、Drupal等平台。4)与Web服务器紧密集成,简化开发部署。
