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