您的位置:首页 > 数据库 > Redis

Redis(七)-- SpringMVC整合Redis集群

2017-04-22 18:23 309 查看

1.pom.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation=" http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.2.xsd">

<!-- 解决springMVC响应数据乱码   text/plain就是响应的时候原样返回数据 -->
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes" value = "text/plain;charset=UTF-8" />
</bean>
</mvc:message-converters>
</mvc:annotation-driven>

<!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->
<context:component-scan base-package="com.*.controller" />

<!-- 视图解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".jsp"></property>
</bean>

<!-- 控制器异常处理 -->
<bean id="exceptionResolver"
class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="java.lang.Exception">
error
</prop>
</props>
</property>
</bean>
</beans>


View Code

7.redis集群工厂配置,JedisClusterFactory.java

package com.xbq.redis;

import java.util.HashSet;
import java.util.Properties;
import java.util.Set;
import java.util.regex.Pattern;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.io.Resource;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;

/**
* Jedis集群工厂
* @author xbq
* @Date 2017-05-14
*/
public class JedisClusterFactory implements InitializingBean,FactoryBean<JedisCluster>{

private Resource addressConfig;

// 下面变量 对应spring redis配置文件中的 property的name
private JedisCluster jedisCluster;
private String addressKeyPrefix;
private Integer timeout;
private Integer maxRedirections;
private GenericObjectPoolConfig genericObjectPoolConfig;

// 正则表达式 匹配 ip和port
private Pattern p = Pattern.compile("^.+[:]\\d{1,5}\\s*$");

/**
* 实现 InitializingBean 的接口,初始化的 得到 jedisCluster
*/
public void afterPropertiesSet() throws Exception {
Set<HostAndPort> jedisClusterNode= this.parseHostAndPort();
jedisCluster = new JedisCluster(jedisClusterNode, timeout, maxRedirections, genericObjectPoolConfig);
}

/**
* 实现 FactoryBean 的接口
* 获取 jedisCluster对象
*/
public JedisCluster getObject() throws Exception {
return jedisCluster;
}

/**
* 实现 FactoryBean 的接口
* 获取 jedisCluster的类型
*/
public Class<? extends JedisCluster> getObjectType() {
return (jedisCluster != null ? this.jedisCluster.getClass() : JedisCluster.class);
}

/**
* 实现 FactoryBean 的接口
*/
public boolean isSingleton() {
return true;
}

/**
* 解析Jedis配置文件,看是否满足 IP和端口
* @return
*/
private Set<HostAndPort> parseHostAndPort() throws Exception{
Set<HostAndPort> hostAndPorts = new HashSet<HostAndPort>();
try {
Properties properties = new Properties();
properties.load(this.addressConfig.getInputStream());

for(Object key : properties.keySet()){
// 如果key不是以 addressKeyPrefix的值 开头,则continue
if(!((String)key).startsWith(addressKeyPrefix)){
continue;
}
// 根据 key从properties中取出值
String valus = (String) properties.get(key);
// 判断取出的value是否是ip和port
boolean isIPProt = p.matcher(valus).matches();
if(!isIPProt){
throw new IllegalArgumentException("ip和port不合法!");
}
String[] ipAndPort = valus.split(":");
HostAndPort hostAndPort = new HostAndPort(ipAndPort[0], Integer.parseInt(ipAndPort[1]));
hostAndPorts.add(hostAndPort);
}
} catch (Exception e) {
throw new Exception("解析 jedis 配置文件失败!");
}
return hostAndPorts;
}

// set方法
public void setJedisCluster(JedisCluster jedisCluster) {
this.jedisCluster = jedisCluster;
}
public void setAddressKeyPrefix(String addressKeyPrefix) {
this.addressKeyPrefix = addressKeyPrefix;
}
public void setTimeout(Integer timeout) {
this.timeout = timeout;
}
public void setMaxRedirections(Integer maxRedirections) {
this.maxRedirections = maxRedirections;
}
public void setGenericObjectPoolConfig(GenericObjectPoolConfig genericObjectPoolConfig) {
this.genericObjectPoolConfig = genericObjectPoolConfig;
}
public void setAddressConfig(Resource addressConfig) {
this.addressConfig = addressConfig;
}
}


8.控制层,RedisController.java

package com.xbq.controller;

import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import redis.clients.jedis.JedisCluster;

@Controller
@RequestMapping("/redis")
public class RedisController {

@Resource
private JedisCluster jedisCluster;

@RequestMapping("/hello")
public @ResponseBody String sayHello(){

for(int i = 0; i < 10; i++){
jedisCluster.set("name" + i, "hello" + i);
}
return "hello world!";
}
}


访问地址:http://localhost:8080/SpringRedisCluster/redis/hello.do ,进行测试。

9.源码下载

  点我下载
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: