redis集群+SSM整合使用

首先是创建redis-cluster文件夹:

因为redis最少需要6个节点(三主三从),为了更好的理解,我这里创建了两台虚拟机(192.168.0.109 192.168.0.110),分别在两台虚拟机的/opt/redis-4.0.1/redis-cluster下创建三个节点文件夹

192.168.0.109:

192.168.0.110:

以上6个节点全部创建完成,分别再在这六个文件夹下创建redis.conf配置文件,其中配置如图:

  1. port 7000
  2. bind 192.168.0.109
  3. daemonize yes
  4. pidfile /var/run/redis_7000.pid
  5. cluster-enabled yes
  6. cluster-config-file nodes_7000.conf
  7. cluster-node-timeout 10000
  8. appendonly yes

其中需要将port pidfile cluster-config-file修改成节点端口号一致,bind改成本机ip,以便远程访问,全部修改完后,即可启动redis服务:

启动命令:

192.168.0.109下的命令:“for((i=0;i<=2;i++)); do /opt/redis-4.0.1/src/redis-server /opt/redis-4.0.1/redis-cluster/700$i/redis.conf; done

192.168.0.110下的命令:“for((i=3;i<=5;i++)); do /opt/redis-4.0.1/src/redis-server /opt/redis-4.0.1/redis-cluster/700$i/redis.conf; done

可以看到后台模式启动成功的日志打印,两台机器都需要依次启动所有节点。节点启动完成后,即可创建集群服务:

在其中一台虚拟机上执行如下命令“/opt/redis-4.0.1/src/redis-trib.rb create --replicas 1 192.168.0.109:7000 192.168.0.109:7001 192.168.0.109:7002 192.168.0.110:7003 192.168.0.110:7004 192.168.0.110:7005”

千万记住只需要在一台上执行即可,如果卡在join处不能往下执行,一般情况是出在防火墙端口被禁导致,有两种方式可以解决:

1、不但需要开启7000对外端口,还需要开启17000(因为redis总线端口需要加10000)。

2、直接关闭所有防火墙(因我这里是自己的环境,所以直接关闭了防火墙服务)。

出现上图运行日志,基本就成功搭建好了集群服务,可以清晰的看到各个节点的主从关系,环境搭建好后,这里我们就和我上篇写到的SSM架构进行联合使用。

上次整合的mybaits二级缓存是个单机版本,由于这种方式不支持集群,所以这里从新使用jedis-cluster进行另外一种redis集群与java整合使用的方式。

首先在redis.properties文件中新增集群机器的配置,将6个节点依次加入配置:

  1. #cluster
  2. cluster1.host.port=192.168.0.109:7000
  3. cluster2.host.port=192.168.0.109:7001
  4. cluster3.host.port=192.168.0.109:7002
  5. cluster4.host.port=192.168.0.110:7003
  6. cluster5.host.port=192.168.0.110:7004
  7. cluster6.host.port=192.168.0.110:7005

redis配置文件中也与之前改动比较多,我直接列出来,可以直接拷去用了。

spring-redis.xml

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:mvc="http://www.springframework.org/schema/mvc"
  5. xmlns:util="http://www.springframework.org/schema/util"
  6. xmlns:aop="http://www.springframework.org/schema/aop"
  7. xmlns:context="http://www.springframework.org/schema/context"
  8. xmlns:task="http://www.springframework.org/schema/task"
  9. xsi:schemaLocation="http://www.springframework.org/schema/beans
  10. http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
  11. http://www.springframework.org/schema/util
  12. http://www.springframework.org/schema/util/spring-util-4.3.xsd
  13. http://www.springframework.org/schema/mvc
  14. http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
  15. http://www.springframework.org/schema/aop
  16. http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
  17. http://www.springframework.org/schema/context
  18. http://www.springframework.org/schema/context/spring-context-4.3.xsd">
  19.  
  20. <!-- 连接池基本参数配置,类似数据库连接池 -->
  21. <context:property-placeholder location="classpath*:redis.properties" />
  22.  
  23. <bean name="genericObjectPoolConfig" class="org.apache.commons.pool2.impl.GenericObjectPoolConfig" >
  24. <property name="maxWaitMillis" value="-1" />
  25. <property name="maxTotal" value="1000" />
  26. <property name="minIdle" value="8" />
  27. <property name="maxIdle" value="100" />
  28. </bean>
  29.  
  30. <!-- 连接池配置,类似数据库连接池 -->
  31. <!-- <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" >
  32. <property name="hostName" value="${redis.host}"></property>
  33. <property name="port" value="${redis.port}"></property>
  34. <property name="password" value="${redis.pass}"></property>
  35. <property name="poolConfig" ref="poolConfig"></property>
  36. </bean> -->
  37.  
  38. <!-- 调用连接池工厂配置 -->
  39. <!-- <bean id="redisTemplate" class=" org.springframework.data.redis.core.RedisTemplate">
  40. <property name="jedisConnectionFactory" ref="jedisConnectionFactory"></property>
  41.  
  42. 如果不配置Serializer,那么存储的时候智能使用String,如果用User类型存储,那么会提示错误User can't cast to String!!!
  43. <property name="keySerializer">
  44. <bean
  45. class="org.springframework.data.redis.serializer.StringRedisSerializer" />
  46. </property>
  47. <property name="valueSerializer">
  48. <bean
  49. class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
  50. </property>
  51. </bean> -->
  52. <bean id="jedisCluster" class="com.cjl.util.JedisClusterFactory">
  53. <property name="addressConfig">
  54. <value>classpath:redis.properties</value>
  55. </property>
  56. <property name="addressKeyPrefix" value="cluster" />
  57.  
  58. <property name="timeout" value="300000" />
  59. <property name="maxRedirections" value="6" />
  60. <property name="genericObjectPoolConfig" ref="genericObjectPoolConfig" />
  61. </bean>
  62. </beans>

将上篇SSM+redis整合中mybatis的开启缓存配置全部禁用,即可启动服务测试了

首先直接注入jedisCluster获取一个集群对象。

这里为了方便,我数据同步直接用了java写了个简单思想,其他方法也可实现,例如Spring AOP方式实现,使用第三方插件,或者数据库层面实现都可行。

启动成功后,反复调用方法。可以看到控制台并未打印sql语句,而是直接在redis集群中直接获取得到数据。以上简单的redis集群实例已经完成,因为时间关系,其中linux中有些坑我没有细细写出,如有疑问可以留言。

如有不对的地方或者更好的建议,欢迎评论中指出。我会尽快学习修改。

04.redis集群+SSM整合使用的更多相关文章

  1. 05.haproxy+mysql负载均衡 整合 redis集群+ssm

    本篇重点讲解haproxy+mysql负载均衡,搭建完成后与之前搭建的redis+ssm进行整合 (注:这里用到了两台mysql数据库,分别安装两台虚拟机上,已经成功实现主主复制,如果有需要,请查看我 ...

  2. 04: redis集群

    1.1 主从同步 1.CPA原理 1. CPA原理是分布式存储理论的基石: C(一致性):   A(可用性):  P(分区容忍性); 2. 当主从网络无法连通时,修改操作无法同步到节点,所以“一致性” ...

  3. 基于redis集群实现的分布式锁,可用于秒杀,定时器。

    在分布式系统中,经常会出现需要竞争同一资源的情况,使用redis可以实现分布式锁. 前提:redis集群已经整合项目,并且可以直接注入JedisCluster使用: @Autowired privat ...

  4. Redis集群与spring的整合

    上一篇详细的赘述了Redis的curd操作及集群的搭建.下面我们开始将他整合到我们实际的项目中去.我的项目采用的是标准的ssm框架,ssm框架这里不说,直接开始整合. 首先在maven管理中将我们的j ...

  5. Ubuntu16.04.1上搭建分布式的Redis集群

    为什么要集群: 通常为了,提高网站的响应速度,总是把一些经常用到的数据放到内存中,而不是放到数据库中,Redis是一个很好的Cache工具,当然了还有Memcached,这里只讲Redis.在我们的电 ...

  6. redis集群配置,spring整合jedis,缓存同步

    前台的商品数据(图片等加载缓慢)查询,先从redis缓存查询数据. redis是一个nosql数据库,内存版数据库,读取速度11w/s.本身具有内存淘汰机制,是单线程服务器(分时操作系统),线程安全. ...

  7. springboot+shiro+redis(集群redis版)整合教程

    相关教程: 1. springboot+shiro整合教程 2. springboot+shiro+redis(单机redis版)整合教程 3.springboot+shiro+redis(单机red ...

  8. SpringBoot整合Redis集群

    一.环境搭建 Redis集群环境搭建:https://www.cnblogs.com/zwcry/p/9174233.html 二.Spring整合Redis集群 1.pom.xml <proj ...

  9. SpringBoot2.0 整合 Redis集群 ,实现消息队列场景

    本文源码:GitHub·点这里 || GitEE·点这里 一.Redis集群简介 1.RedisCluster概念 Redis的分布式解决方案,在3.0版本后推出的方案,有效地解决了Redis分布式的 ...

随机推荐

  1. .xlsx文件总是默认用2007 Microsoft Office component 打开,且无法更改用EXCEL打开的解决方法

    之前装了OFFICE2003,后来改装了 OFFICE2007,之后XLSX文件双击总是用2007 Microsoft Office component 打开,导致无法打开. 解决方法: 打开注册表R ...

  2. BZOJ-2330-[SCOI2011]糖果(差分约束)

    Description 幼儿园里有N个小朋友,lxhgww老师现在想要给这些小朋友们分配糖果,要求每个小朋友都要分到糖果.但是小朋友们也有嫉妒心,总是会提出一些要求,比如小明不希望小红分到的糖果比他的 ...

  3. phpstorm2016.3+xdebug调试

    1.首先打开PHP配置文件,php.in修改相关xedebug配置 ; XDEBUG Extension [xdebug] zend_extension ="d:/wamp64/bin/ph ...

  4. Android-Async-Http 特性简单分析

    如下是官方文档描述此库的特点: All requests are made outside of your app’s main UI thread, but any callback logic w ...

  5. dubbo专题」dubbo其实很简单,就是一个远程服务调用的框架(1)

    一.dubbo是什么? 1)本质:一个Jar包,一个分布式框架,,一个远程服务调用的分布式框架. 既然是新手教学,肯定很多同学不明白什么是分布式和远程服务调用,为什么要分布式,为什么要远程调用.我简单 ...

  6. Debian6单用户模式

    开始的时候按"e"进入Grub的编辑界面,这个时候要找:linux /boot/vmlinuz-2.6.32-5-amd64 root=UUID=.......... ro qui ...

  7. 在foreach的判断条件里执行方法会有效率问题吗?

    楼猪平时一有空就有看别人代码的习惯,从许多优秀规范的代码中学习到了很多简约高效的写法和画龙点睛的思想精华.但是有的时候也会觉得某些写法很值得玩味.比如刚看到一段代码,在foreach的条件判断里加了一 ...

  8. C#生成缩略图源码

    先看调用的方法: ).ToUpper())                {                    case "JPG":                      ...

  9. 分享:苹果APP更新上架被拒的另一种理由(Safety - Objectionable Content)

    这两个星期,本来想和大伙分享:写IT连创业系列运营篇. 但时间飞过,仍只是写了开头,一直很忙,没能完往下写. 今天就动手写点其它内容,哈哈,免的和小伙伴太陌生〜〜〜 前几天更新了:IT恋和IT连的版本 ...

  10. Python之文件与目录

    file 通常建议使用open()打开文件,file用于类型判断 如果要把数据写到磁盘上,除调用flush()外,还得用sync(),以确保数据从系统缓冲区同步到磁盘.close()总是会调用这两个方 ...