SpringMvc集成使用redisCluster
来源:互联网 发布:火车头采集本地数据 编辑:程序博客网 时间:2024/06/05 11:36
最近在项目中引入了RedisCluster集群部署,下面就和大家分享一下引入的方式:
首先,在pom.xml中引入jedis包
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version></dependency>
然后使用配置文件配置:
<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <bean id="jedisPoolConfig" class="org.apache.commons.pool2.impl.GenericObjectPoolConfig"> <property name="maxIdle" value="${redis.maxIdle}" /> <property name="minIdle" value="${redis.minIdle}" /> <property name="maxTotal" value="${redis.maxTotal}" /> <property name="testOnBorrow" value="true" /> </bean> <bean id="jedisCluster" class="com.**.redis.config.JedisClusterFactory"> <property name="useable" value="${redis.useable}"></property> <property name="hostNameAndPort" value="${redis.service.location}" /> <property name="timeout" value="${redis.service.timeout}" /> <property name="socketTimeout" value="${redis.service.socketTimeout}" /> <property name="maxRedirections" value="${redis.service.maxRedirections}" /> <property name="authPassword" value="${redis.service.authPassword}"></property> <property name="GenericObjectPoolConfig" ref="jedisPoolConfig" /> </bean></beans>
其中参数配置,在文件redis.properties中:
#加了个开关,是否使用redis,考虑到有的项目单机的话,就可以把redis关掉redis.useable = true#redis连接池参数redis.maxTotal = 10redis.minIdle = 2redis.maxIdle = 10redis.maxWait = 600000#cluster地址,ip和端口之间使用冒号,然后使用逗号分隔redis.service.location = ip1:port1,ip2:port2,ip3:posrt3#如果redis开启了密码验证,需要配置密码redis.service.authPassword = ****redis.service.timeout = 300000redis.service.socketTimeout = 60000redis.service.maxRedirections = 2
以下是JedisClusterFactory的实现:
import java.util.HashSet;import java.util.Set;import org.apache.commons.pool2.impl.GenericObjectPoolConfig;import org.springframework.beans.factory.FactoryBean;import org.springframework.beans.factory.InitializingBean;import redis.clients.jedis.HostAndPort;import redis.clients.jedis.JedisCluster;public class JedisClusterFactory implements FactoryBean<JedisCluster>,InitializingBean{ private JedisCluster jedisCluster; private GenericObjectPoolConfig poolConfig; private int timeout; private String hostNameAndPort; private int maxRedirections; private int socketTimeout; private String authPassword; private boolean useable; public JedisCluster getObject() throws Exception { return jedisCluster; } public Class<?> getObjectType() { return (this.jedisCluster != null ? this.jedisCluster.getClass() : JedisCluster.class); } public boolean isSingleton() { return true; } public void setGenericObjectPoolConfig(GenericObjectPoolConfig config) { this.poolConfig = config; } public void setHostNameAndPort(String hostNameAndPort) { this.hostNameAndPort = hostNameAndPort; } public void setTimeout(int timeout) { this.timeout = timeout; } public void setMaxRedirections(int maxRedirections) { this.maxRedirections = maxRedirections; } public GenericObjectPoolConfig getPoolConfig() { return poolConfig; } public void setPoolConfig(GenericObjectPoolConfig poolConfig) { this.poolConfig = poolConfig; } public int getTimeout() { return timeout; } public String getHostNameAndPort() { return hostNameAndPort; } public int getMaxRedirections() { return maxRedirections; } public int getSocketTimeout() { return socketTimeout; } public void setSocketTimeout(int socketTimeout) { this.socketTimeout = socketTimeout; } public String getAuthPassword() { return authPassword; } public void setAuthPassword(String authPassword) { this.authPassword = authPassword; } public void setUseable(String useable) { this.useable = Boolean.valueOf(useable); } //cluster的地址初始化 private Set<HostAndPort> parseHostAndPort() throws Exception { Set<HostAndPort> haps = new HashSet<HostAndPort>(); String[] hostAndPorts = hostNameAndPort.split(","); for (String hostAndPort : hostAndPorts) { String host = hostAndPort.split(":")[0]; int port = Integer.valueOf(hostAndPort.split(":")[1]); HostAndPort hap = new HostAndPort(host, port); haps.add(hap); } return haps; } //项目启动自动执行,配置redis public void afterPropertiesSet() throws Exception { if(useable) { Set<HostAndPort> haps = parseHostAndPort(); jedisCluster = new JedisCluster(haps, timeout,socketTimeout, maxRedirections,authPassword,poolConfig); } }}
使用方式:
…………………………//注入@Autowiredprivate JedisCluster jedisCluster;……//直接使用jedisCluster中的方法jedisCluster.get(key);jedisCluster.set(key,value);……
阅读全文
1 0
- SpringMvc集成使用redisCluster
- spring集成redisCluster
- rediscluster lua使用
- springmvc集成使用redis
- springMVC 与beanvalidate 集成使用
- SpringMVC和rabbitmq集成使用
- windows下使用RedisCluster集群简单实例
- windows下使用RedisCluster集群简单实例
- windows下使用RedisCluster集群简单实例
- windows下使用RedisCluster集群简单实例
- [Redis]RedisCluster使用pipeline批量读取数据
- springmvc集成xhEditor编辑器的使用
- springmvc集成xhEditor编辑器的使用
- eclipse 使用maven 集成spring+springMVC+mybatis
- IntelliJ IDEA 使用maven 集成SpringMVC+Hibernate
- IntelliJ IDEA 使用maven 集成SpringMVC+Hibernate
- Spring Mobile 集成springmvc的使用
- RedisCluster 安装
- PAT乙级 1014. 福尔摩斯的约会 (20)
- scala 学习笔记之条件控制
- Android studio release版SHA1与debug版SHA1获取
- connect函数和accept函数哪个先返回? 客户端和服务端哪个先完成建立tcp连接?
- POJ 1753(Flip Game)
- SpringMvc集成使用redisCluster
- 关于STL的一些总结1:vector,queue,stack
- ListView
- 关于spoutConfig中maxOffsetBehind与startOffsetTime
- Codeforces Round #421(Div2) B
- 【网络】TCP报文中的PSH和URG字段的介绍
- druid数据库账号密码加密解密
- 云付董事长 张凉凉:光环背后的“执拗者”
- python 图像的保存和显示,遍历保存于显示, 去除白色像素点