spring配置redis集羣並使用
這裏用的是spirng不是spingBoot ,兩者的差別就是配置方式不通,實際應用中都是一樣的,有時間把boot的也補上。
1. applicationContext.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<description>Spring公共配置 </description>
<!-- 其他配置省略。。 -->
<!-- 加載資源文件 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<!-- 加載jedis資源文件 -->
<value>classpath:jedis.properties</value>
</list>
</property>
</bean>
<!-- ================================redis==================================== -->
<!-- 讀取redis pool的配置文件 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${redis_maxTotal}"></property>
<property name="minIdle" value="${redis_minIdle}"></property>
<property name="maxIdle" value="${redis_maxIdle}"></property>
</bean>
<bean id="jedisCluster" class="redis.clients.jedis.JedisCluster">
<!-- nodes的注入可以先創建多個HostAndPort的bean,下面使用<ref bean="node1"/>進行注入 -->
<constructor-arg name="nodes">
<set>
<!-- 配置Redis集羣中的Redis節點信息,端口分別是5001-5006共6個節點 -->
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="${node1_ip}"></constructor-arg>
<constructor-arg name="port" value="${node1_port}"></constructor-arg>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="${node2_ip}"></constructor-arg>
<constructor-arg name="port" value="${node2_port}"></constructor-arg>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="${node3_ip}"></constructor-arg>
<constructor-arg name="port" value="${node3_port}"></constructor-arg>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="${node4_ip}"></constructor-arg>
<constructor-arg name="port" value="${node4_port}"></constructor-arg>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="${node5_ip}"></constructor-arg>
<constructor-arg name="port" value="${node5_port}"></constructor-arg>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="${node6_ip}"></constructor-arg>
<constructor-arg name="port" value="${node6_port}"></constructor-arg>
</bean>
</set>
</constructor-arg>
<constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg>
</bean>
<!-- ================================redis==================================== -->
</beans>
2. 配置文件jedis.properties
# 連接池最大連接數
redis_maxTotal=30
# 最大使用數
redis_maxIdle=15
# 最小連接數
redis_minIdle=5
node1_ip=xxx.xxx.xxx.xxx
node1_port=5001
node2_ip=xxx.xxx.xxx.xxx
node2_port=5002
node3_ip=xxx.xxx.xxx.xxx
node3_port=5003
node4_ip=xxx.xxx.xxx.xxx
node4_port=5004
node5_ip=xxx.xxx.xxx.xxx
node5_port=5005
node6_ip=xxx.xxx.xxx.xxx
node6_port=5006
3. 使用
@Service
public class GetWqhtInfo {
@Autowired
private JedisCluster jedisClient;
//使用示例
public String getToken(Boolean errorFlag) {
String token= jedisClient.get("tokenName");
}
}
這裏就是個簡單的使用示例,獲取redis中,名爲tokenName 的值。
具體更多使用方法參考: jedis常用API