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

分布式缓存技术redis学习系列(七)—— spring 整合redis如何操作redis集群

2016-08-27 13:28 1111 查看
Redis 3.X版本引入了集群的新特性,为了保证所开发系统的高可用性项目组决定引用Redis的集群特性。目前spring-data-redis已发布的主干版本都不能很好的支持Redis Cluster的新特性,所以在这里我们以直接调用jedis来实现。

首先保证你的redis集群是正常启动的,请参考:Redis的集群搭建 。

项目结构如下:



applicationContext-dao.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:context="http://www.springframework.org/schema/context"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<context:property-placeholder location="classpath:database.properties" />

<bean id="genericObjectPool" class="org.apache.commons.pool2.impl.GenericObjectPool">
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="maxTotal" value="${redis.maxActive}" />
<property name="maxWaitMillis" value="${redis.maxWait}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
</bean>

<bean id="jedisCluster" class="redis.clients.jedis.JedisCluster">
<constructor-arg index="0">
<set>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg type="String" value="${redis1.host}"/>
<constructor-arg type="int" value="${redis1.port}"/>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg type="String" value="${redis2.host}"/>
<constructor-arg type="int" value="${redis2.port}"/>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg type="String" value="${redis3.host}"/>
<constructor-arg type="int" value="${redis3.port}"/>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg type="String" value="${redis4.host}"/>
<constructor-arg type="int" value="${redis4.port}"/>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg type="String" value="${redis5.host}"/>
<constructor-arg type="int" value="${redis5.port}"/>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg type="String" value="${redis6.host}"/>
<constructor-arg type="int" value="${redis6.port}"/>
</bean>
</set>
</constructor-arg>
<constructor-arg index="1" ref="genericObjectPool"/>
</bean>
</beans>

database.properties配置文件如下:
redis.maxIdle=10
redis.maxActive=20
redis.maxWait=10000
redis.testOnBorrow=true
redis1.host=192.168.1.76
redis1.port=7001

redis2.host=192.168.1.77
redis2.port=7002

redis3.host=192.168.1.78
redis3.port=7003

redis4.host=192.168.1.79
redis4.port=7004

redis5.host=192.168.1.80
redis5.port=7005

redis6.host=192.168.1.81
redis6.port=7006


核心代码:
package com.npf.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import redis.clients.jedis.JedisCluster;

import com.npf.dao.StudentDao;
import com.npf.model.Student;

@Repository
public class StudentDaoImpl implements StudentDao{

@Autowired
private JedisCluster jedisCluster;

public static final String STUDENT = "student";

@Override
public void save(Student student) {
jedisCluster.hset(STUDENT, student.getId(), student.getId() + ","
+ student.getName() + "," + student.getPassword());
}

@Override
public Student find(String id) {
String[] stu = jedisCluster.hget(STUDENT, id).split(",");
Student student = new Student();
student.setId(stu[0]);
student.setName(stu[1]);
student.setPassword(stu[2]);
return student;
}

@Override
public void delete(String id) {
jedisCluster.hdel(STUDENT,id);
}

@Override
public void update(Student student) {
jedisCluster.hset(STUDENT, student.getId(), student.getId() + ","
+ student.getName() + "," + student.getPassword());
}

@Override
public List<Student> findAll() {
Map<String,String> stuMap = jedisCluster.hgetAll(STUDENT);
List<Student> stuList = new ArrayList<Student>();
for(Entry<String, String> entry : stuMap.entrySet()){
String[] stuArray = entry.getValue().split(",");
Student stu = new Student();
stu.setId(stuArray[0]);
stu.setName(stuArray[1]);
stu.setPassword(stuArray[2]);
stuList.add(stu);
}
return stuList;
}
}


控制层代码:
package com.npf.controller;

import java.util.List;
import java.util.UUID;

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 com.npf.model.Student;
import com.npf.service.StudentService;

@Controller
public class StudentController {

@Autowired
private StudentService studentService;

@RequestMapping("/student/save")
public String saveStudent(Student student){
String id = UUID.randomUUID().toString();
System.out.println(id);
student.setId(id);
studentService.save(student);
return "redirect:/student/find/all";
}

@RequestMapping("/student/update")
public String updateStudent(Student student){
studentService.update(student);
return "redirect:/student/find/all";
}

@RequestMapping("/student/to/save/form")
public String toSaveStudentForm(){
return "save";
}

@RequestMapping("/student/delete")
public String deleteStudent(@RequestParam("id") String id){
studentService.delete(id);
return "redirect:/student/find/all";
}

@RequestMapping("/student/to/update/form")
public String toUpdateStudentForm(@RequestParam("id") String id,Model model){
Student stu = studentService.find(id);
model.addAttribute("stu", stu);
return "update";
}

@RequestMapping("/student/find/all")
public String findStudents(Model model){
List<Student> stuList = studentService.findAll();
model.addAttribute("stuList", stuList);
return "list";
}
}


测试:
(1).访问:http://localhost:8080/springdatarediscluster/student/find/all



(2).点击create,去创建一个student.



(3).点击submit,提交表单,保存刚创建的student,然后系统会跳转到list页面。



(4).点击update,去更新student.



(5).点击submit,保存刚才的更新,然后跳转到list页面。



接下来我们通过命令行去redis的集群中去查看数据,我们登陆任意的一个节点客户端都可以,这里我以7002这个端口号的节点为例:

[root@npf1 src]# ./redis-cli -c -h 192.168.1.76 -p 7002



回车后,就可以看见如下所示:



当我们运行keys * 命令后,并不能看见我们存进去的student的hash数据,为什么?因为集群是有分区的概念的,keys * 只能看到该节点的数据。



我们直接运行hget student a6f0ebd9-91e4-41e4-b4ca-be0ad2be51a2,其中a6f0ebd9-91e4-41e4-b4ca-be0ad2be51a2是我们刚存进去的数据的key.



可以看到,我们定位到其他的节点了。

本博客的代码已经托管到Github上面:springdatarediscluster
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐