Redis集群 之 Cluster模式Windows篇

1.环境:

  a.下载Redis:访问 https://github.com/MicrosoftArchive/redis/releases 下载Windows版Redis,并解压

  b.安装Ruby:访问 http://dl.bintray.com/oneclick/rubyinstaller/rubyinstaller-2.3.3-x64.exe 下载Windows版Ruby,并安装

 

  c.配置Ruby:在cmd命令行执行 gem install redis

 

 

2.集群配置

  a.复制6份Redis作为6个节点,分别命名为8081、8082、8083、8084、8085 和 8086

 

 

  b.修改 Redis 各节点中的 redis.windows.conf 配置文件

#端口号改为8081,其他5个端口号分别为 8082、8083、8084、8085、8086
port 8081
#开启集群模式
cluster-enabled yes
#保存节点配置,自动创建,自动更新(建议命名时加上端口号)
cluster-config-file nodes-8081.conf
#集群超时时间,节点超过这个时间没反应就断定是宕机
cluster-node-timeout 15000
#指定是否在每次更新操作后进行日志记录,Redis在默认情况下是异步的把数据写入磁盘,如果不开启,可能会在断电时导致一段时间内的数据丢失。 yes表示:存储方式,aof,将写操作记录保存到日志中
appendonly yes

 

  c.在每个节点目录下创建 start.bat 来启动 redis

title redis-8081
redis-server.exe redis.windows.conf

 

  d.下载redis-trib.rb

    1)访问 https://github.com/microsoftarchive/redis/releases/tag/win-3.2.100 下载 Source code(zip) 源码包

    2)解压源码包将 src 目录下的 redis-trib.rb 文件复制到 redis集群的目录下

 

  e.配置Redis集群

    1)双击各节点文件夹下的 start.bat 文件启动各Redis节点

    2)到redis集群目录下执行cmd命令:ruby redis-trib.rb create --replicas 1 127.0.0.1:8081 127.0.0.1:8082 127.0.0.1:8083 127.0.0.1:8084 127.0.0.1:8085 127.0.0.1:8086

    3)在出现 Can I set the above configuration? (type 'yes' to accept): 时输入 yes

 

  f.注意:

    1)Ruby集群命令只需要执行一次,以后只需要执行各节点的 start.bat 即可

 

 

3.测试

  a.连接任意一个节点:redis-cli.exe -c -p 8081

  b.设置一个键:set name vettel

  c.退出Redis控制台:exit

  d.再连接另一个节点:redis-cli.exe -c -p 8083

  e.获取这个键:get name

 

 

4.使用Jedis操作Cluster模式Redis集群

  a.导入maven依赖

    <dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
      <version>2.10.0</version>
    </dependency>

 

  b.使用

public class TestCluster {

    private static JedisCluster jedis;

    static {
        init();
    }

    /**
     * 初始化redis连接池
     */
    private static void init(){
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxIdle(5);   //最大空闲数
        config.setMaxWaitMillis(60000); //连接时最大的等待时间(毫秒)
        config.setMaxTotal(500);    //最大连接数
        config.setTestOnBorrow(true);   //在提取一个jedis实例时,是否提前进行验证操作;如果为true,则得到的jedis实例均是可用的

        Set<HostAndPort> nodes = new HashSet<>();
        nodes.add(new HostAndPort("127.0.0.1", 8081));
        nodes.add(new HostAndPort("127.0.0.1", 8082));
        nodes.add(new HostAndPort("127.0.0.1", 8083));
        nodes.add(new HostAndPort("127.0.0.1", 8084));
        nodes.add(new HostAndPort("127.0.0.1", 8085));
        nodes.add(new HostAndPort("127.0.0.1", 8086));
        jedis = new JedisCluster(nodes, config);
    }

    public static JedisCluster getJedis() {
        return jedis;
    }

    public static void returnJedis(JedisCluster jedis){
        if(jedis != null){
            try {
                jedis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 测试
     * @param args
     */
    public static void main(String[] args) {
        JedisCluster jedis = TestCluster.getJedis();
        jedis.set("name", "zwin");
        jedis.set("age", "29");

        jedis = TestCluster.getJedis();
        String name = jedis.get("name");
        String age = jedis.get("age");
        System.out.println("name[" + name + "], age[" + age + "]");
        //JedisCluster为单例模式,需最后再关闭
        returnJedis(jedis);
    }

}

 

 

6.Spring操作Cluster模式Redis集群

  a.导入Maven依赖

  <properties>
    
    .....

    <!-- spring -->
    <spring.version>5.1.1.RELEASE</spring.version>
    <!-- jackson-json -->
    <jackson.version>2.9.4</jackson.version>
    <!-- log4j -->
    <slf4j.version>1.7.18</slf4j.version>
    <log4j.version>1.2.17</log4j.version>
  </properties>

  <dependencies>
    <!-- spring -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-oxm</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <!-- Jackson -->
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>${jackson.version}</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>${jackson.version}</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-annotations</artifactId>
      <version>${jackson.version}</version>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>

    <!-- AOP -->
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjrt</artifactId>
      <version>1.8.6</version>
    </dependency>
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.8.6</version>
    </dependency>

    <!-- 日志相关 -->
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>${log4j.version}</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>${slf4j.version}</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>${slf4j.version}</version>
    </dependency>

    <!-- redis -->
    <dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-redis</artifactId>
      <version>2.1.1.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
      <version>2.10.0</version>
    </dependency>
  </dependencies>

 

  b.创建spring.xml

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

    <!-- 自动扫描的包名 -->
    <context:component-scan base-package="com.wode" />

    <!-- 开启AOP代理 -->
    <aop:aspectj-autoproxy proxy-target-class="true" />

    <!--开启注解处理器 -->
    <context:annotation-config>
    </context:annotation-config>

    <context:property-placeholder location="classpath:redis.properties"/>
    <!-- Spring中引入其他配置文件 -->
    <import resource="classpath*:/spring-redis.xml" />

</beans>

 

  c.创建spring-redis.xml

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">

    <!-- jedis配置 -->
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <!-- 最大空闲数:空闲连接数大于maxIdle时,将进行回收 -->
        <property name="maxIdle" value="${redis.maxIdle}" />
        <!-- 最大连接数:能够同时建立的最大连接个数 -->
        <property name="maxTotal" value="${redis.maxTotal}" />
        <!-- 最大等待时间:单位ms -->
        <property name="maxWaitMillis" value="${redis.maxWait}" />
        <!-- 使用连接时,检测连接是否成功 -->
        <property name="testOnBorrow" value="${redis.testOnBorrow}" />
    </bean>

    <!--cluster集群 -->
    <bean id="redisClusterConfiguration" class="org.springframework.data.redis.connection.RedisClusterConfiguration">
        <!-- 最大重定向数目 -->
        <property name="maxRedirects" value="${redis.maxRedirects}"></property>
        <property name="clusterNodes">
            <set>
                <bean class="org.springframework.data.redis.connection.RedisNode">
                    <constructor-arg name="host" value="${redis.node1.host}" />
                    <constructor-arg name="port" value="${redis.node1.port}" />
                </bean>
                <bean class="org.springframework.data.redis.connection.RedisNode">
                    <constructor-arg name="host" value="${redis.node2.host}" />
                    <constructor-arg name="port" value="${redis.node2.port}" />
                </bean>
                <bean class="org.springframework.data.redis.connection.RedisNode">
                    <constructor-arg name="host" value="${redis.node3.host}" />
                    <constructor-arg name="port" value="${redis.node3.port}" />
                </bean>
                <bean class="org.springframework.data.redis.connection.RedisNode">
                    <constructor-arg name="host" value="${redis.node4.host}" />
                    <constructor-arg name="port" value="${redis.node4.port}" />
                </bean>
                <bean class="org.springframework.data.redis.connection.RedisNode">
                    <constructor-arg name="host" value="${redis.node5.host}" />
                    <constructor-arg name="port" value="${redis.node5.port}" />
                </bean>
                <bean class="org.springframework.data.redis.connection.RedisNode">
                    <constructor-arg name="host" value="${redis.node6.host}" />
                    <constructor-arg name="port" value="${redis.node6.port}" />
                </bean>
            </set>
        </property>
    </bean>

    <bean id="redisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:password="${redis.password}">
        <constructor-arg name="clusterConfig" ref="redisClusterConfiguration"></constructor-arg>
        <constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg>
    </bean>

    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="redisConnectionFactory" />
        <!-- 配置Serializer -->
        <property name="keySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
        </property>
        <property name="hashKeySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
        </property>
        <property name="valueSerializer">
            <bean class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/>
        </property>
        <property name="hashValueSerializer">
            <bean class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/>
        </property>
    </bean>
</beans>

 

  d.创建redis.properties

redis.password=
redis.maxIdle=5
redis.maxWait=60000
redis.maxTotal=500
redis.testOnBorrow=true

redis.maxRedirects=5

redis.node1.host=127.0.0.1
redis.node1.port=8081

redis.node2.host=127.0.0.1
redis.node2.port=8082

redis.node3.host=127.0.0.1
redis.node3.port=8083

redis.node4.host=127.0.0.1
redis.node4.port=8084

redis.node5.host=127.0.0.1
redis.node5.port=8085

redis.node6.host=127.0.0.1
redis.node6.port=8086

 

  e.创建实体类

public class TestBean {

    private String name;
    private int age;

    public TestBean(){}

    public TestBean(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "[TestBean]: name[" + name + "], age[" + age + "]";
    }
}

 

  f.使用

@Component
public class TestSpringCluster {

    @Resource
    private RedisTemplate redisTemplate;

    public void set(String key, Object value){
//        redisTemplate.boundValueOps(key).set(value);
        redisTemplate.opsForValue().set(key, value);
    }

    public Object get(String key){
//        return redisTemplate.boundValueOps(key).get();
        return redisTemplate.opsForValue().get(key);
    }


    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
        TestSpringCluster testSpringCluster = (TestSpringCluster) context.getBean("testSpringCluster");

        testSpringCluster.set("name", "zwj");
        testSpringCluster.set("age", "28");
        testSpringCluster.set("bean", new TestBean("xf", 26));

        String name = (String) testSpringCluster.get("name");
        String age = (String) testSpringCluster.get("age");
        System.out.println("name[" + name + "], age[" + age + "]");

        TestBean bean = (TestBean) testSpringCluster.get("bean");
        System.out.println(bean);
    }
}

 

 

5.参考文档:

  https://www.cnblogs.com/thirteen-zxh/p/9187875.html

  https://www.jianshu.com/p/a3721ab14a9a

 

posted @ 2019-11-12 17:44  晨M风  阅读(1150)  评论(0编辑  收藏  举报