redis集群及java的实现


根据redis安装的第一个redis开始的

1.在redis.conf的目录下创建redis-cluster目录 mkdir redis-cluster

2.进入redis-cluster ,创建几个集群6001-6006

mkdir 6001   mkdir 6001  mkdir 6003   mkdir 6004   mkdir 6005  mkdir 6006

3.然后再创建data包

mkdir -p 6001/data 6002/data 6003/data 6004/data 6005/data 6006/data

4.复制redis.conf,到6001-6006里面去 

cp redis.conf ./redis-cluster/6001 ....cp redis.conf ./redis-cluster/6006

5.修改每个redis.conf,把6001改成相应端口

port 6001(每个节点的端口号)
daemonize yes
bind 192.168.119.131(绑定当前机器 IP)
dir /usr/local/redis-cluster/6001/data/(数据文件存放位置)
pidfile /var/run/redis_6001.pid(pid 6001和port要对应)
cluster-enabled yes(启动集群模式)
cluster-config-file nodes6001.conf(6001和port要对应)
cluster-node-timeout 15000
appendonly yes

全部修改:%s/6001/6002/g

6.查看启动是否成功  ps -el | grep redis    

netstat -tlnp | grep redis

7.由于 Redis 集群需要使用 ruby 命令,所以我们需要安装 ruby 和相关接口

yum install ruby
yum install rubygems
gem install redis 

7.1,有时安装gem install redis 会报  redis requires Ruby version >= 2.2.2

查了查资料,CentOS7 yum库中ruby的版本支持到 2.0.0,可gem 安装redis需要最低是2.2.2,自己编译的ruby源码,再执行还是报错

解决办法

7.1.1 

1.安装RVM:

 gpg2 --keyserver hkp://keys.gnupg.net --recv-keys D39DC0E3 curl -L get.rvm.io | bash -s stable find / -name rvm -print

/usr/local/rvm 
/usr/local/rvm/src/rvm 
/usr/local/rvm/src/rvm/bin/rvm 
/usr/local/rvm/src/rvm/lib/rvm 
/usr/local/rvm/src/rvm/scripts/rvm 
/usr/local/rvm/bin/rvm 
/usr/local/rvm/lib/rvm 
/usr/local/rvm/scripts/rvm

 source /usr/local/rvm/scripts/rvm

2.查看rvm库中已知的ruby版本

 rvm list known

3.安装一个ruby版本

rvm install 2.3.3

Using /usr/local/rvm/gems/ruby-2.3.3 

4.使用一个ruby版本

rvm use 2.3.3

Using /usr/local/rvm/gems/ruby-2.3.3 
[5].设置默认版本

rvm use 2.3.3 --default

查看ruby版本:

ruby --version

ruby 2.3.3p222 (2016-11-21 revision 56859) [x86_64-linux]

安装redis: gem install redis

 

 

8. 192.168.31.245 是虚拟机地址   7000是指端口号

./redis-trib.rb  create  --replicas  1  192.168.88.134:6001 192.168.88.134:6002  192.168.88.134:6003 192.168.88.134:6004  192.168.88.134:6005  192.168.88.134:6006

8.1  redis集群报错Node is not empty。有可能报这个错

如果改了端口号,要注意,我只是改了上面添加了,下面的没有去掉,然后就一直这样了

 

 

想了一会发现这三个文件appendonly.aof dump.rdb nodes.conf是之前执行ip127.0.0.1时生成的,在我改为真机ip后在执行并没有生效。

 
 

这里解释一下dump.rdb文件:

dump.rdb是由Redis服务器自动生成的 默认情况下 每隔一段时间redis服务器程序会自动对数据库做一次遍历,把内存快照写在一个叫做“dump.rdb”的文件里,这个持久化机制叫做SNAPSHOT。有了SNAPSHOT后,如果服务器宕机,重新启动redis服务器程序时redis会自动加载dump.rdb,将数据库状态恢复到上一次做SNAPSHOT时的状态。

知道原因后就好办了,解决办法:

1)将每个节点下aof、rdb、nodes.conf本地备份文件删除;
2)172.168.63.201:7001> flushdb #清空当前数据库(可省略)
3)之后再执行脚本,成功执行;

 

 

输入 yes 即可,然后出现如下内容,说明安装成功

 

然后再启动,集群就算完成,注意redis要在3.0.0版本以后才能用,

java连接redis集群

public static void main(String[] args) {
    JedisPoolConfig poolConfig = new JedisPoolConfig();
    // 最大连接数
    poolConfig.setMaxTotal(1);
    // 最大空闲数
    poolConfig.setMaxIdle(1);
    // 最大允许等待时间,如果超过这个时间还未获取到连接,则会报JedisException异常:
    // Could not get a resource from the pool
    poolConfig.setMaxWaitMillis(1000);
    Set<HostAndPort> nodes = new LinkedHashSet<HostAndPort>();
    nodes.add(new HostAndPort("192.168.83.128", 6379));
    nodes.add(new HostAndPort("192.168.83.128", 6380));
    nodes.add(new HostAndPort("192.168.83.128", 6381));
    nodes.add(new HostAndPort("192.168.83.128", 6382));
    nodes.add(new HostAndPort("192.168.83.128", 6383));
    nodes.add(new HostAndPort("192.168.83.128", 6384));
    JedisCluster cluster = new JedisCluster(nodes, poolConfig);
    String name = cluster.get("name");
    System.out.println(name);
    cluster.set("age", "18");
    System.out.println(cluster.get("age"));
    try {
        cluster.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

spring 配置redis集群

<context:property-placeholder ignore-unresolvable="true" location="classpath:yonyou.properties" />
    
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxTotal" value="1000"/> 
        <property name="maxIdle" value="10"/> 
        <property name="minIdle" value="1"/>
        <property name="maxWaitMillis" value="30000"/> 
        <property name="testOnBorrow" value="true"/> 
        <property name="testOnReturn" value="true"/> 
        <property name="testWhileIdle" value="true"/>
        <!-- <property name="testWhileIdle" value="true"/> -->
    </bean>
    
       <bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool" destroy-method="destroy">
        <constructor-arg ref="jedisPoolConfig"/> 
        <constructor-arg>
      <!--如果以后需要扩展集群,只需要复制一份redis,修改端口,然后在这里配置即可-->
            <list> 
            <bean class="redis.clients.jedis.JedisShardInfo"> 
                <constructor-arg index="0" value="127.0.0.1"/>
                <constructor-arg index="1" value="6379"/>
                <constructor-arg index="2" value="instance:01"/>
            </bean>
            <bean class="redis.clients.jedis.JedisShardInfo">
                <constructor-arg index="0" value="127.0.0.1"/>
                <constructor-arg index="1" value="6380"/>
                <constructor-arg index="2" value="instance:02"/>
            </bean> 
            <bean class="redis.clients.jedis.JedisShardInfo">
                <constructor-arg index="0" value="127.0.0.1"/>
                <constructor-arg index="1" value="6381"/>
                <constructor-arg index="2" value="instance:03"/>
            </bean>
            </list>
        </constructor-arg>
        </bean>
     <!--java帮我们同步sentinel的信息,将主从信息同步到客户端来-->
   <bean class="redis.clients.jedis.JedisSentinelPool">
        <constructor-arg index="0" value="mymaster"/>
        <constructor-arg index="1">
            <set>
                <value>127.0.0.1:26379</value>
            </set>
        </constructor-arg>
        <constructor-arg index="2" ref="jedisPoolConfig"/>
    </bean>

  

集群验证过的

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

	<!--注入 不然会报 No bean named 'springSessionRepositoryFilter' is defined -->
	<context:component-scan
		base-package="org.springframework.web.filter.DelegatingFilterProxy" />

	<!-- 将session放入redis -->
	<bean id="redisHttpSessionConfiguration"
		class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
		<property name="maxInactiveIntervalInSeconds" value="600" />
	</bean>


	<!-- 连接池配置 -->
	<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
		<!-- 最大连接数 -->
		<property name="maxTotal" value="30" />
		<!-- 最大空闲连接数 -->
		<property name="maxIdle" value="10" />
		<!-- 每次释放连接的最大数目 -->
		<property name="numTestsPerEvictionRun" value="1024" />
		<!-- 释放连接的扫描间隔(毫秒) -->
		<property name="timeBetweenEvictionRunsMillis" value="30000" />
		<!-- 连接最小空闲时间 -->
		<property name="minEvictableIdleTimeMillis" value="1800000" />
		<!-- 连接空闲多久后释放, 当空闲时间>该值 且 空闲连接>最大空闲连接数 时直接释放 -->
		<property name="softMinEvictableIdleTimeMillis" value="10000" />
		<!-- 获取连接时的最大等待毫秒数,小于零:阻塞不确定的时间,默认-1 -->
		<property name="maxWaitMillis" value="1500" />
		<!-- 在获取连接的时候检查有效性, 默认false -->
		<property name="testOnBorrow" value="true" />
		<!-- 在空闲时检查有效性, 默认false -->
		<property name="testWhileIdle" value="true" />
		<!-- 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true -->
		<property name="blockWhenExhausted" value="false" />
	</bean>
	<bean id="clusterRedisNodes1" class="org.springframework.data.redis.connection.RedisNode">
		<constructor-arg value="192.168.88.134" />
		<constructor-arg value="6001" type="int" />
	</bean>
	<bean id="clusterRedisNodes2" class="org.springframework.data.redis.connection.RedisNode">
		<constructor-arg value="192.168.88.134" />
		<constructor-arg value="6002" type="int" />
	</bean>
	<bean id="clusterRedisNodes3" class="org.springframework.data.redis.connection.RedisNode">
		<constructor-arg value="192.168.88.134" />
		<constructor-arg value="6003" type="int" />
	</bean>
	<bean id="clusterRedisNodes4" class="org.springframework.data.redis.connection.RedisNode">
		<constructor-arg value="192.168.88.134" />
		<constructor-arg value="6004" type="int" />
	</bean>
	<bean id="clusterRedisNodes5" class="org.springframework.data.redis.connection.RedisNode">
		<constructor-arg value="192.168.88.134" />
		<constructor-arg value="6005" type="int" />
	</bean>
	<bean id="clusterRedisNodes6" class="org.springframework.data.redis.connection.RedisNode">
		<constructor-arg value="192.168.88.134" />
		<constructor-arg value="6006" type="int" />
	</bean>

	<bean id="redisClusterConfiguration"
		class="org.springframework.data.redis.connection.RedisClusterConfiguration">
		<property name="clusterNodes">
			<set>
				<ref bean="clusterRedisNodes1" />
				<ref bean="clusterRedisNodes2" />
				<ref bean="clusterRedisNodes3" />
				<ref bean="clusterRedisNodes4" />
				<ref bean="clusterRedisNodes5" />
				<ref bean="clusterRedisNodes6" />
			</set>
		</property>
	</bean>

	<!-- Redis连接 -->
	<bean id="jedisConnectionFactory"
		class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
		<constructor-arg ref="jedisPoolConfig" />
		<!--  
		<property name="password" value="${redis.password}"></property>
		-->
		<constructor-arg ref="redisClusterConfiguration" />
	</bean>
	<!-- 缓存序列化方式 
	<bean id="keySerializer"
		class="org.springframework.data.redis.serializer.StringRedisSerializer" />
	<bean id="valueSerializer"
		class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer" />
		-->
	<!-- 缓存 
	<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
		<property name="connectionFactory" ref="jedisConnectionFactory" />
		<property name="keySerializer" ref="keySerializer" />
		<property name="valueSerializer" ref="valueSerializer" />
		<property name="hashKeySerializer" ref="keySerializer" />
		<property name="hashValueSerializer" ref="valueSerializer" />
	</bean>
	
	<bean id="redisCacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">
		<constructor-arg index="0" ref="redisTemplate" />
		<property name="defaultExpiration" value="1000" />
	</bean>
-->
</beans>

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM