系统:Ubuntu
开发工具:eclipse
JDK:1.8
服务器:Tomcat8.0
Redis:3.0.7
搭建流程:
1.搭建spring框架,见文章:http://blog.youkuaiyun.com/u012810317/article/details/51452604
2.在pom.xml中添加Redis依赖:
<!-- redis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.8.1</version>
</dependency>
3.在springMVC-servlet.xml中添加以下内容:
<context:component-scan base-package="com.springmvc.repository" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Repository" />
</context:component-scan>
4.在src/main/resources下创建connect-redis.properties文件;文件内容如下:
address1=192.168.56.102:7000
address2=192.168.56.102:7001
address3=192.168.56.103:7000
address4=192.168.56.103:7001
address5=192.168.56.104:7000
address6=192.168.56.105:7001
5.在src/main/java中创建com.springmvc.repository包,并在包中创建JedisClusterFactory.java;内容如下:
package com.springmvc.repository;
import java.io.InputStream;
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.stereotype.Repository;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
@Repository
public class JedisClusterFactory implements FactoryBean<JedisCluster>, InitializingBean {
private InputStream is;
private String addressKeyPrefix ;
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<? extends JedisCluster> getObjectType() {
return (this.jedisCluster != null ? this.jedisCluster.getClass() : JedisCluster.class);
}
@Override
public boolean isSingleton() {
return true;
}
private Set<HostAndPort> parseHostAndPort() throws Exception {
try {
Properties prop = new Properties();
prop.load(is);
Set<HostAndPort> haps = new HashSet<HostAndPort>();
for (Object key : prop.keySet()) {
if (!((String) key).startsWith(addressKeyPrefix)) {
continue;
}
String val = (String) prop.get(key);
boolean isIpPort = p.matcher(val).matches();
if (!isIpPort) {
throw new IllegalArgumentException("ip 或 port 不合法");
}
String[] ipAndPort = val.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);
}
}
@Override
public void afterPropertiesSet() throws Exception {
is = getClass().getResourceAsStream("/connect-redis.properties");
addressKeyPrefix = "address";
timeout = 300000;
maxRedirections = 6;
genericObjectPoolConfig = new GenericObjectPoolConfig();
genericObjectPoolConfig.setMaxWaitMillis(-1);
genericObjectPoolConfig.setMaxTotal(1000);
genericObjectPoolConfig.setMinIdle(8);
genericObjectPoolConfig.setMaxIdle(100);
Set<HostAndPort> haps = this.parseHostAndPort();
jedisCluster = new JedisCluster(haps, timeout, maxRedirections,genericObjectPoolConfig);
}
}
6.将src/main/java/com/springmvc/test的HelloController.java文件修改成:
package com.springmvc.test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import redis.clients.jedis.JedisCluster;
@Controller
public class HelloController {
@Autowired
private JedisCluster jediscluster;
@RequestMapping("/hello")
public String hello(@RequestParam(value="name",required=false,defaultValue="world")String name,Model model){
System.out.println(jediscluster.get("CM::百度::5608646537"));
model.addAttribute("name",name);
return "hello";
}
}
7.将src/main/webapp下的index.jsp文件修改成:
<html>
<body>
<a href="/springmvc/hello">hello</a>
</body>
</html>
8.右键项目--> Run As --> Run on Server --> Finish -->在浏览器地址栏输入“localhost:8080/springmvc” --> 点击页面中的“hello”;
如果控制台出现Value的值,则表示成功,否则失败。