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

发布了132 篇原创文章 · 获赞 108 · 访问量 27万+

猜你喜欢

转载自blog.csdn.net/fanbaodan/article/details/103136693