1.redis配置文件 : redis.properties
# Redis settings
#sentinel_node_1
redis.sentinel1.host=192.168.0.1
redis.sentinel1.port=8001
#sentinel_node_2
redis.sentinel2.host=192.168.0.1
redis.sentinel2.port=8002
#sentinel_node_3
redis.sentinel3.host=192.168.0.1
redis.sentinel3.port=8003
#sentinel_auth
redis.sentinel.masterName=TestMaster
redis.sentinel.password=testmaster123
redis.maxIdle=500
redis.maxTotal=5000
redis.maxWaitTime=1000
redis.minIdle=300
redis.testOnBorrow=true
2.redis数据源的配置文件:redis-datasource.xml
这里给大家介绍两种配置模式 spring-redis配置模式 非spring-redis配置模式
2.1spring-redis配置模式:
maven依赖:
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.8.0</version> </dependency> <!-- spring-redis --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.6.4.RELEASE</version> </dependency>
<!-- redis服务配置 开始--> <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxTotal" value="${redis.maxTotal}" /> <property name="minIdle" value="${redis.minIdle}" /> <property name="maxWaitMillis" value="${redis.maxWaitTime}" /> <property name="maxIdle" value="${redis.maxIdle}" /> <property name="testOnBorrow" value="${redis.testOnBorrow}" /> <property name="testOnReturn" value="true" /> <property name="testWhileIdle" value="true" /> </bean> <bean id="sentinelConfiguration" class="org.springframework.data.redis.connection.RedisSentinelConfiguration"> <property name="master"> <bean class="org.springframework.data.redis.connection.RedisNode"> <property name="name" value="${redis.sentinel.masterName}"></property> </bean> </property> <property name="sentinels"> <set> <bean class="org.springframework.data.redis.connection.RedisNode"> <constructor-arg name="host" value="${redis.sentinel1.host}"></constructor-arg> <constructor-arg name="port" value="${redis.sentinel1.port}"></constructor-arg> </bean> <bean class="org.springframework.data.redis.connection.RedisNode"> <constructor-arg name="host" value="${redis.sentinel2.host}"></constructor-arg> <constructor-arg name="port" value="${redis.sentinel2.port}"></constructor-arg> </bean> <bean class="org.springframework.data.redis.connection.RedisNode"> <constructor-arg name="host" value="${redis.sentinel3.host}"></constructor-arg> <constructor-arg name="port" value="${redis.sentinel3.port}"></constructor-arg> </bean> </set> </property> </bean> <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:password="${redis.sentinel.password}"> <constructor-arg name="sentinelConfig" ref="sentinelConfiguration"></constructor-arg> <constructor-arg name="poolConfig" ref="poolConfig"></constructor-arg> </bean> <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate"> <property name="connectionFactory" ref="connectionFactory" /> </bean> <bean id="redisService" class="com.usi.hlqb.service.redis.RedisService"> <property name="redisTemplate" ref="redisTemplate"></property> </bean> <!-- redis服务配置 结束 -->
2.2 非spring-redis的配置模式:
maven依赖:
<dependency> <groupId>com.imwinston</groupId> <artifactId>JedisBase</artifactId> <version>2.1.17-sentinel-4</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.8.0</version> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>18.0</version> </dependency> <dependency> <groupId>com.esotericsoftware</groupId> <artifactId>kryo</artifactId> <version>3.0.1</version> </dependency>
<!-- jedis 连接池配置--> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxIdle" value="${redis.maxIdle}" /> <property name="maxTotal" value="${redis.maxTotal}" /> <property name="testOnBorrow" value="${redis.testOnBorrow}" /> </bean>
<!--哨兵配置--> <bean id="jedisPool" class="redis.clients.jedis.JedisSentinelPool"> <constructor-arg index="0" value="myMaster" /> <constructor-arg index="1"> <set> <value>192.168.0.1:8001</value> <value>192.168.0.1:8002</value> <value>192.168.0.1:8003</value> </set> </constructor-arg> <constructor-arg index="2" ref="jedisPoolConfig" /> <constructor-arg index="3" value="${redis.sentinel.password}" /> </bean> <bean id="jedisBase" class="com.imwinston.redis.jedisbase.JedisBase"> <property name="pool" ref="jedisPool" /> </bean> <!-- 本地sqlite数据源定义 --> <bean id="sqlDs" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="org.sqlite.JDBC"/> <property name="url" value="jdbc:sqlite:"/> <property name="username" value=""/> <property name="password" value=""/> </bean>