redis檔次提高(主從、哨兵)與spring結合

來源:互聯網
上載者:User

redis主從

1:建立從redis目錄,在原有redis服務中複製如下檔案:  a:redis-benchmark  b:redis-cli  c:redis.conf  d:redis-server2:修改redis.conf設定檔    a:修改服務連接埠號碼,保證連接埠號碼唯一        port 6380    b:添加從配置        slaveof 127.0.0.1 63793:啟動服務,查看從redis是否存在主redis的資料(./redis-server redis.conf)4:備忘    a:需要自己建立從redis相關目錄    b:從redis只有讀取許可權

redis哨兵

1:建立從哨兵目錄,在redis安裝目錄中複製如下檔案:  a:redis-sentinel2:添加哨兵設定檔-sentinel.conf    port 16379    #**連接埠唯一**    dir "/usr/redis-sen/6379/temp"   #**臨時目錄**       daemonize yes    #protected-mode no    logfile "/usr/redis-sen/6379/sentinel.log"  #**記錄檔**    sentinel monitor redisMaster 192.168.203.142 6379 1  #**主redis及別名**    sentinel down-after-milliseconds redisMaster 5000    #**5秒鐘檢測一次**    sentinel failover-timeout redisMaster 15000    #sentinel auth-pass redisMaster r123                 #**認證密碼**    # Generated by CONFIG REWRITE    sentinel config-epoch redisMaster 144    sentinel leader-epoch redisMaster 144    #sentinel known-slave redisMaster 192.168.203.141 6380    sentinel known-slave redisMaster 192.168.203.142 6380  #**從redis**    sentinel current-epoch 1443:啟動服務,查看從哨兵進程是否存在(./redis-server redis.conf)  a:啟動服務  ./redis-sentinel sentinel.conf  b:查看服務  ps aux|grep redis-sentinel4:備忘    a:如果主redis掛了,哨兵回去修改從redis的設定檔,和哨兵本身設定檔,將從的redis修改為主的redis    b:最好自己手動測試一下

redis與spring整合

1:spring+maven,添加相關jar包:  a:spring-data-redis  b:jedis2:添加spring-jedis.xml設定檔    <bean id="redisSentinelConfiguration"        class="org.springframework.data.redis.connection.RedisSentinelConfiguration">        <property name="master">            <bean class="org.springframework.data.redis.connection.RedisNode">                <property name="name" value="redisMaster"></property>            </bean>        </property>        <property name="sentinels">            <set>                <bean class="org.springframework.data.redis.connection.RedisNode">                    <constructor-arg name="host" value="192.168.203.142"></constructor-arg>                    <constructor-arg name="port" value="16379"></constructor-arg>            </set>        </property>    </bean>    <bean id="jeidsConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" >        <constructor-arg ref="redisSentinelConfiguration"></constructor-arg>    </bean>    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" p:connection-factory-ref="jeidsConnectionFactory" />

3:編寫測試類別

package com.raiyi.redis.spring.sen;import javax.annotation.Resource;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.dao.DataAccessException;import org.springframework.data.redis.connection.RedisConnection;import org.springframework.data.redis.core.RedisCallback;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.core.ValueOperations;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = { "classpath*:spring-mvc.xml", "classpath*:spring-jedis.xml", })public class RedisMSTest {    @Resource(name = "redisTemplate")    RedisTemplate<String, String> api;    @Resource(name = "redisTemplate")    ValueOperations<String, Object> voOper;    @Test    public void ms() {        new Thread() {            public void run() {                for (int i = 0; i < 111111111; i++) {                    api.execute(new RedisCallback<Boolean>() {                        @Override                        public Boolean doInRedis(RedisConnection connection) throws DataAccessException {                            byte[] key = ("tmpKey").getBytes();                            byte[] value = ("tmpValue" + System.currentTimeMillis() + "..." + Math.random()).getBytes();                            connection.set(key, value);                            return true;                        }                    });                    try {                        Thread.sleep(2000L);                    } catch (InterruptedException e) {                        // TODO Auto-generated catch block                        e.printStackTrace();                    }                }            }        }.start();        new Thread() {            public void run() {                while (true) {                    api.execute(new RedisCallback<Boolean>() {                        @Override                        public Boolean doInRedis(RedisConnection connection) throws DataAccessException {                            byte[] key = ("tmpKey").getBytes();                            String result = new String(connection.get(key));                            System.out.println(result);                            return true;                        }                    });                    try {                        Thread.sleep(2000L);                    } catch (InterruptedException e) {                        // TODO Auto-generated catch block                        e.printStackTrace();                    }                }            }        }.start();        while (true) {        }    }}

4:備忘
a:此哨兵中沒有配置redis的密碼
b:請使用redis短串連,操作一次斷開一次,否則如果主redis掛了切換不到哨兵配置修改之後的主redis
c:還有其他方式的配置,請再補充

“`

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.