欢迎投稿

今日深度:

redis档次提高(主从、哨兵)与spring结合,redisspring

redis档次提高(主从、哨兵)与spring结合,redisspring


redis主从

1:创建从redis目录,在原有redis服务中复制如下文件:
  a:redis-benchmark
  b:redis-cli
  c:redis.conf
  d:redis-server

2:修改redis.conf配置文件
    a:修改服务端口号,保证端口号唯一
        port 6380
    b:添加从配置
        slaveof 127.0.0.1 6379

3:启动服务,查看从redis是否存在主redis的数据(./redis-server redis.conf)
4:备注
    a:需要自己创建从redis相关目录
    b:从redis只有读取权限

redis哨兵

1:创建从哨兵目录,在redis安装目录中复制如下文件:
  a:redis-sentinel

2:添加哨兵配置文件-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 144

3:启动服务,查看从哨兵进程是否存在(./redis-server redis.conf)
  a:启动服务  ./redis-sentinel sentinel.conf
  b:查看服务  ps aux|grep redis-sentinel
4:备注
    a:如果主redis挂了,哨兵回去修改从redis的配置文件,和哨兵本身配置文件,将从的redis修改为主的redis
    b:最好自己手动测试一下

redis与spring整合

1:spring+maven,添加相关jar包:
  a:spring-data-redis
  b:jedis

2:添加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:还有其他方式的配置,请再补充

“`

www.htsjk.Com true http://www.htsjk.com/redis/26286.html NewsArticle redis档次提高(主从、哨兵)与spring结合,redisspring redis主从 1 :创建从redis目录,在原有redis服务中复制如下文件: a:redis-benchmark b:redis- cli c:redis .conf d:redis-server 2 :修改redis .conf 配置文件...
相关文章
    暂无相关文章
评论暂时关闭