zoukankan      html  css  js  c++  java
  • redis集群搭建

    缓存问题一向是提高效率的有效方法,redis的用法一搜一大把,想深入些就搭建cluster集群环境。

    主要操作参考http://baijiahao.baidu.com/s?id=1570378814116719

    在这里记录几个要点

    首先需要bind 的地址是本机的地址,也就是192.168.*.*,如果是127.0.0.1的方式就无法建立集群。

    这里给出junit测试代码:

    import java.util.HashSet;
    import java.util.Set;
    
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import redis.clients.jedis.HostAndPort;
    import redis.clients.jedis.Jedis;
    import redis.clients.jedis.JedisCluster;
    import redis.clients.jedis.JedisPool;
    
    public class JedisTest {
    
        @Test
        public void testJedisSingle(){
            // 创建一个jedis对象
            Jedis jedis = new Jedis("192.168.31.136", 6379);
            // 直接调用jedis对象的方法,方法名称和redis命令一致
            jedis.set("key1", "jedis test");
            String value = jedis.get("key1");
            System.out.println(value);
            jedis.del("key1");
            // 关闭jedis
            jedis.close();
        }
        
        @Test
        public void testJedisPool(){
            // 创建连接池
            JedisPool pool = new JedisPool("192.168.31.136", 6379);
            // 从连接池中获得jedis对象
            Jedis jedis = pool.getResource();
            jedis.set("key1", "jedis test");
            String value = jedis.get("key1");
            System.out.println(value);
            jedis.del("key1");
            // 关闭jedis
            jedis.close();
            pool.close();
        }
        
        @Test
        public void testJedisCluster(){
            Set<HostAndPort> nodes = new HashSet<HostAndPort>();
            nodes.add(new HostAndPort("192.168.31.136", 7001));
            nodes.add(new HostAndPort("192.168.31.136", 7002));
            nodes.add(new HostAndPort("192.168.31.136", 7003));
            nodes.add(new HostAndPort("192.168.31.136", 7004));
            nodes.add(new HostAndPort("192.168.31.136", 7005));
            nodes.add(new HostAndPort("192.168.31.136", 7006));
            JedisCluster cluster = new JedisCluster(nodes);        
            cluster.set("key1", "1000");
            String str = cluster.get("key1");
            System.out.println(str);
            cluster.del("key1");
            cluster.close();
        }
        
        @Test
        public void testSpringJedisSingle(){
            ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:config/applicationContext-*.xml");
            JedisPool pool = (JedisPool) ac.getBean("jedisPool");
            Jedis jedis = pool.getResource();
            jedis.set("key1", "jedis spring test");
            String value = jedis.get("key1");
            System.out.println(value);
            jedis.del("key1");
            // 关闭jedis
            jedis.close();
            pool.close();
        }
        
        @Test
        public void testSpringJedisCluster(){
            ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:config/applicationContext-*.xml");
            JedisCluster cluster = (JedisCluster) ac.getBean("redisClient");
            cluster.set("key1", "jedis spring test");
            String value = cluster.get("key1");
            System.out.println(value);
            cluster.del("key1");
            // 关闭jedis
            cluster.close();
        }
    }

    applicationContext-jedis.xml代码如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
        xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
        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
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
    
        <!-- jedis客户端单机版 -->
        <bean id="jedisPool" class="redis.clients.jedis.JedisPool">
            <constructor-arg name="host" value="192.168.31.136"></constructor-arg>
            <constructor-arg name="port" value="6379"></constructor-arg>
        </bean>
         
        <!-- jedis集群版配置 -->
         
        <bean id="redisClient" class="redis.clients.jedis.JedisCluster">
            <constructor-arg name="nodes">
                <set>
                    <bean class="redis.clients.jedis.HostAndPort">
                        <constructor-arg name="host" value="192.168.31.136"></constructor-arg>
                        <constructor-arg name="port" value="7001"></constructor-arg>
                    </bean>
                    <bean class="redis.clients.jedis.HostAndPort">
                        <constructor-arg name="host" value="192.168.31.136"></constructor-arg>
                        <constructor-arg name="port" value="7002"></constructor-arg>
                    </bean>
                    <bean class="redis.clients.jedis.HostAndPort">
                        <constructor-arg name="host" value="192.168.31.136"></constructor-arg>
                        <constructor-arg name="port" value="7003"></constructor-arg>
                    </bean>
                    <bean class="redis.clients.jedis.HostAndPort">
                        <constructor-arg name="host" value="192.168.31.136"></constructor-arg>
                        <constructor-arg name="port" value="7004"></constructor-arg>
                    </bean>
                    <bean class="redis.clients.jedis.HostAndPort">
                        <constructor-arg name="host" value="192.168.31.136"></constructor-arg>
                        <constructor-arg name="port" value="7005"></constructor-arg>
                    </bean>
                    <bean class="redis.clients.jedis.HostAndPort">
                        <constructor-arg name="host" value="192.168.31.136"></constructor-arg>
                        <constructor-arg name="port" value="7006"></constructor-arg>
                    </bean>
                </set>
            </constructor-arg>
        </bean>
        
    </beans>

    测试可以通过,在这里做个记录。

    顺便说一下,在测试中无法自动编译,所以采用下面的方式修改后就可以了。

    项目上右键-->properties-->Java compiler-->building-->enable project specific setting-->build path problems-->选中abort   Incomplete build path/Circular dependencies  这两个选项修改为Warning

  • 相关阅读:
    git常用命令
    国内优秀npm镜像,nvm
    canvas --> getImageData()
    canvas sprite动画 简单封装
    springboot项目中ttf和woff字体图标页面无法显示
    树莓派配置Oracle JDK8
    记一次SqlServer大表查询语句优化和执行计划分析
    linux 查看某个进程和服务内存占用情况命令
    安装MySQL后,需要调整的10个性能配置项
    ARM架构上的Debian10编译timescaledb
  • 原文地址:https://www.cnblogs.com/PPBoy/p/7209397.html
Copyright © 2011-2022 走看看