mybatis二级缓存,Redis集群与SSM整合利用格局

作者: 数据库信息  发布:2019-12-23

SSM redis整合

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

ssm框架以前曾经搭建过了,这里不再做代码复制专业。

图片 1

此间首如若行使redis去做mybatis的二级缓存,mybaits映射文件中兼有的select都会刷新本来就有缓存,假如海市蜃楼就能新建缓存,全部的insert,update操作都会更新缓存。

因为redis起码须要6个节点(三主三从),为了越来越好的领悟,小编这里开创了两台设想机(192.168.0.109 192.168.0.110),分别在两台设想机的/opt/redis-4.0.1/redis-cluster下创立四个节点文件夹

redis的益处也分明,能够使系统的数目访问品质更加高。本节只是显示了组合方式和遵从,后边会补齐redis集群、负载均衡和session分享的稿子。

192.168.0.109:

上边就从头整合职业:

图片 2

图片 3

192.168.0.110:

后台首先运营redis-server(后台运转与远程连接linux服务的办法都亟待改redis.conf文件),运行命令“./src/redis-server ./redis.conf”

图片 4

自家这里是windows系统下支付的,推荐四个可视化学工业具“Redis Desktop manager”,供给中间距连接linux下的redis,须要linux下打起头口门户开放(具体方法是修改/etc/sysconfig/iptables文件,扩充对外端口开拓命令)。

以上6个节点全体创立达成,分别再在这里多个公文夹下创制redis.conf配置文件,此中安顿如图:

上述操作都成功后,就能够远程连接成功了,如图:

port 7000
bind 192.168.0.109
daemonize yes
pidfile /var/run/redis_7000.pid
cluster-enabled yes
cluster-config-file nodes_7000.conf
cluster-node-timeout 10000
appendonly yes

图片 5

里面要求将port pidfile cluster-config-file改正成节点端口号黄金时代致,bind改开销机ip,以便远程访谈,全部制改善动完后,就可以运转redis服务:

图片 6

开头命令:

当今还没缓存记录,下边步向代码阶段,首先在pom.xml中追加内需的redis jar包

192.168.0.109下的通令:“for((i=0;i<=2;i 卡塔尔(英语:State of Qatar)卡塔尔(قطر‎; do /opt/redis-4.0.1/src/redis-server /opt/redis-4.0.1/redis-cluster/700$i/redis.conf; done”

<dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
      <version>2.9.0</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-redis</artifactId>
      <version>1.6.2.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-ehcache</artifactId>
      <version>1.0.0</version>
    </dependency>
     <!-- 添加druid连接池包 -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.0.24</version>
    </dependency>

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”

pom.xml写好后,还索要新扩展七个结构文件:redis.properties

图片 7

redis.host=192.168.0.109
redis.port=6379
redis.pass=123456
redis.maxIdle=200
redis.maxActive=1024
redis.maxWait=10000
redis.testOnBorrow=true

能够看出后台形式运维成功的日记打字与印刷,两日机器都须求各种运营全数节点。节点启动成功后,就能够成立集群服务:

中间字段也都很好了然,再参预配置文件:spring-redis.xml

在内部生机勃勃台设想机上执行如下命令“/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”

<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:mvc="http://www.springframework.org/schema/mvc"
 xmlns:util="http://www.springframework.org/schema/util"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:task="http://www.springframework.org/schema/task" 
 xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
   http://www.springframework.org/schema/util
   http://www.springframework.org/schema/util/spring-util-4.3.xsd
   http://www.springframework.org/schema/mvc
   http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
   http://www.springframework.org/schema/aop
   http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-4.3.xsd">
  <!-- 连接池基本参数配置,类似数据库连接池 -->
   <context:property-placeholder location="classpath*:redis.properties" />
  <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
    <property name="maxTotal" value="${redis.maxActive}"/>
    <property name="maxIdle" value="${redis.maxIdle}" />
    <property name="testOnBorrow" value="${redis.testOnBorrow}"/>
  </bean>
  <!-- 连接池配置,类似数据库连接池 -->
  <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" >
    <property name="hostName" value="${redis.host}"></property>
    <property name="port" value="${redis.port}"></property>
    <property name="password" value="${redis.pass}"></property>
    <property name="poolConfig" ref="poolConfig"></property> 
  </bean>
  <!-- 调用连接池工厂配置 -->
  <!-- <bean id="redisTemplate" class=" org.springframework.data.redis.core.RedisTemplate">
    <property name="jedisConnectionFactory" ref="jedisConnectionFactory"></property>
    如果不配置Serializer,那么存储的时候智能使用String,如果用User类型存储,那么会提示错误User can't cast to String!!! 
     <property name="keySerializer"> 
      <bean 
      class="org.springframework.data.redis.serializer.StringRedisSerializer" /> 
    </property> 
    <property name="valueSerializer"> 
      <bean 
        class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" /> 
    </property> 
  </bean> -->
  <bean id="redisCacheTransfer" class="com.cjl.util.RedisCacheTransfer">
    <property name="jedisConnectionFactory" ref="jedisConnectionFactory" />
  </bean>
</beans>

图片 8

结构文件写好后,就从头java代码的编辑撰写:

 千万记住只要求在风流罗曼蒂克台上举行就能够,若是卡在join处无法往下试行,平日情状是出在防火墙端口被禁以致,有二种艺术可以缓慢解决:

JedisClusterFactory.java

1、不但须求展开7000对外端口,还索要敞开17000(因为redis总线端口须要加1000)。

package com.cjl.util;
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;
public class JedisClusterFactory implements FactoryBean<JedisCluster>, InitializingBean {
  private Resource addressConfig;
  private String addressKeyPrefix;
  private JedisCluster jedisCluster;
  private Integer timeout;
  private Integer maxRedirections;
  private GenericObjectPoolConfig genericObjectPoolConfig;
  private Pattern p = Pattern.compile("^. [:]\d{1,5}\s*$");
  public JedisCluster getObject() throws Exception {
    return jedisCluster;
  }
  public Class<? extends JedisCluster> getObjectType() {
    return (this.jedisCluster != null ? this.jedisCluster.getClass() : JedisCluster.class);
  }
  public boolean isSingleton() {
    return true;
  }
  private Set<HostAndPort> parseHostAndPort() throws Exception {
    try {
      Properties prop = new Properties();
      prop.load(this.addressConfig.getInputStream());
      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);
    }
  }
  public void afterPropertiesSet() throws Exception {
    Set<HostAndPort> haps = this.parseHostAndPort();
    jedisCluster = new JedisCluster(haps, timeout, maxRedirections, genericObjectPoolConfig);
  }
  public void setAddressConfig(Resource addressConfig) {
    this.addressConfig = addressConfig;
  }
  public void setTimeout(int timeout) {
    this.timeout = timeout;
  }
  public void setMaxRedirections(int maxRedirections) {
    this.maxRedirections = maxRedirections;
  }
  public void setAddressKeyPrefix(String addressKeyPrefix) {
    this.addressKeyPrefix = addressKeyPrefix;
  }
  public void setGenericObjectPoolConfig(GenericObjectPoolConfig genericObjectPoolConfig) {
    this.genericObjectPoolConfig = genericObjectPoolConfig;
  }
}

2、直接关门全部防火墙(因自身这里是自个儿的遇到,所以一向关门了防火墙服务)。

本文由金沙澳门官网发布于数据库信息,转载请注明出处:mybatis二级缓存,Redis集群与SSM整合利用格局

关键词: 金沙澳门官网

上一篇:简单实现,Redis集群与SSM整合使用方法
下一篇:没有了