這篇文章給大家分享的是有關基于spring中redis配置的示例分析的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

成都創(chuàng)新互聯(lián)公司專注于遵化網站建設服務及定制,我們擁有豐富的企業(yè)做網站經驗。 熱誠為您提供遵化營銷型網站建設,遵化網站制作、遵化網頁設計、遵化網站官網定制、微信小程序開發(fā)服務,打造遵化網絡公司原創(chuàng)品牌,更為您提供遵化網站排名全網營銷落地服務。
需要的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}" />
<!--創(chuàng)建實例時最長等待時間-->
<property name="maxWaitMillis" value="${redis.maxWaitMillis}" />
<!--創(chuàng)建實例時是否驗證-->
<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提供的序列化功能。 優(yōu)點是反序列化時不需要提供類型信息(class),但缺點是序列化后的結果非常龐大,是JSON格式的5倍左右,這樣就會消耗redis服務器的大量內存。
4.Jackson2JsonRedisSerializer:使用Jackson庫將對象序列化為JSON字符串。優(yōu)點是速度快,序列化后的字符串短小精悍。但缺點也非常致命,那就是此類的構造函數中有一個類型參數,必須提供要序列化對象的類型信息(.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+主要區(qū)別(當然是我自己覺得啊)是把各模塊分成了不同的jar,而對于使用spring-data-redis模板化處理redis的話,單機情況下spring2.5.6和spring4不會沖突,而如果使用集群模式需要配置redis集群的時候就會出現jar包沖突,這個時候就看要如何取決了,可以直接使用jedisCluster來連接redis集群(不過很多方法都需要自己去寫),也可以把spring2.5.6替換成高版本的spring4,只是框架替換需要注意的事情更多了啊(我們公司的直接全部替換沒啥毛病好吧,O(∩_∩)O哈哈~),至于重寫JedisConnectionFactory和RedisClusterConfiguration我還未去嘗試,這個可以作為后續(xù)補充吧...
4.順便說句,spring4不在支持ibatis了,如果你需要用spring4,又需要連接ibatis的話,最粗暴的方式是把spring-orm包換成spring3版本,其他的jar還是4版本即可。
感謝各位的閱讀!關于“基于spring中redis配置的示例分析”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
分享名稱:基于spring中redis配置的示例分析
URL地址:http://chinadenli.net/article36/gphhsg.html
成都網站建設公司_創(chuàng)新互聯(lián),為您提供網站策劃、企業(yè)建站、網站內鏈、網站收錄、品牌網站建設、手機網站建設
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)