spring jedis 配置
2018-06-18 02:45:56来源:未知 阅读 ()
1、自定义一个工厂类,实现FactoryBean 交由spring管理
public class JedisClusterFactory implements FactoryBean<JedisCluster> {
private String hostAndPort;
private JedisCluster jedisCluster;
private Integer timeout;
private Integer maxRedirections;
private GenericObjectPoolConfig genericObjectPoolConfig;
private Pattern p = Pattern.compile("^.+[:]\\d{1,5}\\s*$");
@Override
public JedisCluster getObject() throws Exception {
return jedisCluster;
}
@Override
public Class<?> getObjectType() {
return (this.jedisCluster != null ? this.jedisCluster.getClass() : JedisCluster.class);
}
@Override
public boolean isSingleton() {
return true;
}
private Set<HostAndPort> parseHostAndPort() throws Exception {
try {
Set<HostAndPort> haps = new HashSet<HostAndPort>();
String[] strArray = hostAndPort.split(",");
for (String str : strArray) {
boolean isIpPort = p.matcher(str).matches();
if (!isIpPort) {
throw new IllegalArgumentException("ip 或 port 不合法");
}
String[] ipAndPort = str.split(":");
HostAndPort hap = new HostAndPort(ipAndPort[0],
Integer.parseInt(ipAndPort[1]));
haps.add(hap);
}
return haps;
}
catch (IllegalArgumentException ex) {
throw ex;
}
catch (Exception ex) {
throw new Exception("解析 jedis 配置文件失败", ex);
}
}
public void afterPropertiesSet() throws Exception {
Set<HostAndPort> haps = this.parseHostAndPort();
jedisCluster = new JedisCluster(haps, timeout, maxRedirections, genericObjectPoolConfig);
}
public void setHostAndPort(String hostAndPort) {
this.hostAndPort = hostAndPort;
}
public void setTimeout(int timeout) {
this.timeout = timeout;
}
public void setMaxRedirections(int maxRedirections) {
this.maxRedirections = maxRedirections;
}
public void setGenericObjectPoolConfig(
GenericObjectPoolConfig genericObjectPoolConfig) {
this.genericObjectPoolConfig = genericObjectPoolConfig;
}
}
2、配置文件:
<bean name="genericObjectPoolConfig" class="org.apache.commons.pool2.impl.GenericObjectPoolConfig">
<property name="maxWaitMillis" value="${redis.pool.maxWaitMillis}" />
<property name="maxTotal" value="${redis.pool.maxActive}" />
<property name="minIdle" value="${redis.pool.minIdle}" />
<property name="maxIdle" value="${redis.pool.maxIdle}" />
</bean>
<bean id="jedisCluster" class="com.bbw.redis.JedisClusterFactory" init-method="afterPropertiesSet" >
<property name="hostAndPort" value="${redis.pool.host}"/>
<property name="timeout" value="${redis.pool.timeout}" />
<property name="maxRedirections" value="${redis.pool.maxRedirections}" />
<property name="genericObjectPoolConfig" ref="genericObjectPoolConfig" />
</bean>
3、在spring 里面注解使用:
@Autowired
JedisCluster jedisCluster;
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:新环境第一次使用设计模式有感
- Spring系列.ApplicationContext接口 2020-06-11
- springboot2配置JavaMelody与springMVC配置JavaMelody 2020-06-11
- 给你一份超详细 Spring Boot 知识清单 2020-06-11
- SpringBoot 2.3 整合最新版 ShardingJdbc + Druid + MyBatis 2020-06-11
- 掌握SpringBoot-2.3的容器探针:实战篇 2020-06-11
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash