单机配置

redis.properties配置

#redis的服务器地址
redis.host=127.0.0.1 #redis的服务端口
redis.port=6379 #客户端超时时间单位是毫秒
redis.timeout=100000 #最大建立连接等待时间
redis.maxWaitMillis=1000 #最小空闲数
redis.minIdle=5 #最大空闲数
redis.maxIdle=20 #最大连接数
redis.maxTotal=100

xml配置

<!-- 读取properties文件 -->
<bean id="propertyConfigurerForProject1"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="order" value="1" />
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="ignoreResourceNotFound" value="true" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="locations">
<list>
<value>classpath*:redis.properties</value>
</list>
</property>
</bean> <!-- Jedis连接池的配置对象 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!--最大空闲数 -->
<property name="maxIdle" value="${redis.maxIdle}" />
<!--最小空闲数 -->
<property name="minIdle" value="${redis.minIdle}" />
<!--最大连接数 -->
<property name="maxTotal" value="${redis.maxTotal}" />
<property name="testOnBorrow" value="true" />
<!--最大建立连接等待时间 -->
<property name="maxWaitMillis" value="${redis.maxWaitMillis}" />
</bean> <!--jedis服务器信息 -->
<bean id="jedisShardInfo" class="redis.clients.jedis.JedisShardInfo">
<constructor-arg index="0" value="${redis.host}" />
<constructor-arg index="1" value="${redis.port}" type="int" />
<constructor-arg index="2" value="${redis.timeout}"
type="int" />
</bean> <!--jedis连接池 -->
<bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool">
<constructor-arg index="0" ref="jedisPoolConfig" />
<constructor-arg index="1">
<list>
<ref bean="jedisShardInfo" />
</list>
</constructor-arg>
</bean> <!-- Redis连接工厂 -->
<bean id="jedisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="shardInfo" ref="jedisShardInfo" />
<property name="poolConfig" ref="jedisPoolConfig" />
</bean> <!-- 缓存序列化方式 -->
<!--对key的默认序列化器。默认值是StringSerializer -->
<bean id="keySerializer"
class="org.springframework.data.redis.serializer.StringRedisSerializer" /> <!--是对value的默认序列化器,默认值是取自DefaultSerializer的JdkSerializationRedisSerializer。 -->
<bean id="valueSerializer"
class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer" /> <!-- redis操作模板,对Jedis进行的通用API操作 -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory" />
<property name="keySerializer" ref="keySerializer" />
<property name="valueSerializer" ref="valueSerializer" />
<property name="hashKeySerializer" ref="keySerializer" />
<property name="hashValueSerializer" ref="valueSerializer" />
</bean>

自2.0开始,针对单机配置引入RedisStandaloneConfiguration 并且废弃了JedisConnectionFactoryHostNamePortPassword等属性的配置。

    /**
* 连接工厂类
* @return
*/
@Bean
public JedisConnectionFactory jedisConnectionFactory(){
// 单机配置
RedisStandaloneConfiguration standaloneConfig = new RedisStandaloneConfiguration();
standaloneConfig.setHostName("127.0.01");
standaloneConfig.setPort(6379); JedisConnectionFactory connectionFactory = new JedisConnectionFactory(standaloneConfig); return connectionFactory;
}

哨兵配置

为了处理高可用性的Redis,可以使用RedisSentinelConfiguration支持Redis Sentinel

/**
* jedis
*/
@Bean
public RedisConnectionFactory jedisConnectionFactory() {
RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration()
.master("mymaster")
.sentinel("127.0.0.1", 26379)
.sentinel("127.0.0.1", 26380);
return new JedisConnectionFactory(sentinelConfig);
}

可以配置的属性:

spring.redis.sentinel.master:master节点的名字

spring.redis.sentinel.nodes:主机:端口的逗号分隔列表

集群配置

 @Bean
public JedisConnectionFactory redisConnectionFactory() {
JedisConnectionFactory connectionFactory = new JedisConnectionFactory();
// 集群配置
RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration();
RedisNode redisNode7000 = new RedisNode("10.1.21.4", 7000);
RedisNode redisNode7001 = new RedisNode("10.1.21.4", 7001);
RedisNode redisNode7002 = new RedisNode("10.1.21.4", 7002);
RedisNode redisNode7003 = new RedisNode("10.1.21.4", 7003);
RedisNode redisNode7004 = new RedisNode("10.1.21.4", 7004);
RedisNode redisNode7005 = new RedisNode("10.1.21.4", 7005); redisClusterConfiguration.addClusterNode(redisNode7000);
redisClusterConfiguration.addClusterNode(redisNode7001);
redisClusterConfiguration.addClusterNode(redisNode7002);
redisClusterConfiguration.addClusterNode(redisNode7003);
redisClusterConfiguration.addClusterNode(redisNode7004);
redisClusterConfiguration.addClusterNode(redisNode7005);
connectionFactory = new JedisConnectionFactory(redisClusterConfiguration); // 设置连接池配置
connectionFactory.setPoolConfig(jedisConfig()); // 设置数据库,默认0
connectionFactory.setDatabase(2); // 设置使用连接池
connectionFactory.setUsePool(true); return connectionFactory;
}

配置说明

1. 以上配置是基于Jedis客户端的
2. 使用javaCode或者xml的配置方式依据个人喜好

Spring Data Redis入门示例:程序配置(五)的更多相关文章

  1. Spring Data Redis入门示例:数据序列化 (四)

    概述 RedisTemplate默认使用的是基于JDK的序列化器,所以存储在Redis的数据如果不经过相应的反序列化,看到的结果是这个样子的: 可以看到,出现了乱码,在程序层面上,不会影响程序的运行, ...

  2. Spring Data Redis入门示例:基于Jedis及底层API (二)

    使用底层API:RedisConnectionFactory和RedisConnection可以直接操作Redis,下面是一个简单的例子: ### Maven依赖 <properties> ...

  3. Spring Data Redis入门示例:字符串操作(六)

    Spring Data Redis对字符串的操作,封装在了ValueOperations和BoundValueOperations中,在集成好了SPD之后,在需要的地方引入: // 注入模板操作实例 ...

  4. Spring Data Redis入门示例:Hash操作(七)

    将对象存为Redis中的hash类型,可以有两种方式,将每个对象实例作为一个hash进行存储,则实例的每个属性作为hash的field:同种类型的对象实例存储为一个hash,每个实例分配一个field ...

  5. Spring Data Redis入门示例:基于RedisTemplate (三)

    使用底层API:RedisConnection操作Redis,需要对数据进行手动转换(String <---->byte),需要进行多数重复性工作,效率低下:org.springframe ...

  6. spring data redis使用示例

    1. 配置依赖文件 <dependencies> <dependency> <groupId>org.springframework.data</groupI ...

  7. Spring Data Redis —— 快速入门

    环境要求:Redis 2.6及以上,javase 8.0及以上: 一.Spring Data Redis 介绍 Spring-data-redis是spring的一部分,提供了在srping应用中通过 ...

  8. Spring Data Redis示例

    说明 关于Redis:一个基于键值对存储的NoSQL内存数据库,可存储复杂的数据结构,如List, Set, Hashes. 关于Spring Data Redis:简称SDR, 能让Spring应用 ...

  9. spring data redis jackson 配置,工具类

    spring data redis 序列化有jdk .jackson.string 等几种类型,自带的jackson不熟悉怎么使用,于是用string类型序列化,把对象先用工具类转成string,代码 ...

随机推荐

  1. 任务13:在Core Mvc中使用Options

    13 新建Controllers文件夹,在里面添加HomeController控制器 新建Views文件夹,再新建Home文件夹.再新建Index.cshtml的视图页面 注释上节课的代码,否则我们的 ...

  2. 斯坦福CS231n—深度学习与计算机视觉----学习笔记 课时2

    课时2 计算机视觉历史回顾与介绍中 1966年是计算机视觉的诞生年. 视觉处理流程的第一步,是对简单的形状结构处理,边缘排列. 边缘决定了结构. David Marr写了一本非常有影响力的书,视觉是分 ...

  3. Android App组件之ListFragment -- 说明和示例(转载)

    转自:http://www.cnblogs.com/skywang12345/p/3160260.html 1 ListFragement介绍 ListFragment继承于Fragment.因此它具 ...

  4. springcloud(二) 负载均衡器 ribbon

    代码地址:https://github.com/showkawa/springBoot_2017/tree/master/spb-demo ribbon是一个负载均衡客户端 类似nginx反向代理,可 ...

  5. 黑客攻防技术宝典web实战篇:工具web服务器习题

    猫宁!!! 参考链接:http://www.ituring.com.cn/book/885 随书答案. 1. 在什么情况下 Web 服务器会显示目录列表? 如果请求某目录的 URL 且满足以下条件,W ...

  6. redis的多路复用io

    redis基于reactor开发了自己的网路事件处理器,被称为文件事件处理器.使用io多路复用来同时监听多个套接字,来响应客户端的连接应答.命令请求.命令恢复.多路复用技术使得redis可以使用单进程 ...

  7. Codeforces Round #321 (Div. 2) A, B, C, D, E

    580A. Kefa and First Steps 题目链接: A. Kefa and First Steps 题意描述: 给出一个序列,求最长不降连续子序列多长? 解题思路: 水题,签到 代码: ...

  8. windows下配置java环境jdk

    Windows系统下搭建java的开发环境和配置环境变量 具体步骤打开链接地址:https://www.cnblogs.com/lijuntao/p/6694483.html

  9. [未读]backbonejs应用程序开发

    买来就没有动过,那阵子刚好离职找工作,之后学backbone的劲头就过去了= =

  10. azkaban-web-start.sh启动时出现Table 'execution_flows' is marked as crashed and should be repaired Query错误的解决办法(图文详解)

    问题详情 [hadoop@master bin]$ ./azkaban-web-start.sh Using Hadoop Using Hive from /home/hadoop/app/hive ...