1. 程式人生 > >redis集群及java的實現

redis集群及java的實現

truct hset keyword fig 版本 -m dex and 註意

根據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

7.由於 Redis 集群需要使用 ruby 命令,所以我們需要安裝 ruby 和相關接口

yum install ruby
yum install rubygems
gem install redis 

8. 192.168.31.245 是虛擬機地址 7000是指端口號

redis-trib.rb  create  --replicas  1  192.168.31.245:7000 192.168.31.245:7001  192.168.31.245:7002 192.168.31.210:7003  192.168.31.210:7004  192.168.31.210:7005

然後再啟動,集群就算完成,註意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>

  

redis集群及java的實現