当前位置:   article > 正文

Ubuntu 环境下 Redis主从架构搭建详细步骤_ubuntu下部署redis主从二进制方式

ubuntu下部署redis主从二进制方式

Redis主从复制的功能非常强大,它有以下好处:

1.避免Redis单点故障

2.构建读写分离架构,满足读多写少的应用场景

主从架构

1.1、Redis主从架构拓扑结构图


1.2、搭建步骤

Redis集群不用安装多个Redis,只需复制多个配置文件,修改即可。所以如果要进行主从结构搭建,需先安装单机版Redis。

1.2.1、下载redis 3.2.8

root@shuyizhi-VirtualBox:/6381# wget http://download.redis.io/releases/redis-3.2.8.tar.gz

1.2.2、解压

root@shuyizhi-VirtualBox:/6381# tar -zxzf redis-3.2.8.tar.gz 

1.2.3、进入Redis所在的目录

root@shuyizhi-VirtualBox:/6381# cd redis-3.2.8/

1.2.4、新建三个目录

root@shuyizhi-VirtualBox:/6381# mkdir 6379

root@shuyizhi-VirtualBox:/6381# mkdir 6380

root@shuyizhi-VirtualBox:/6381# mkdir 6381

1.2.5、复制三份配置文件

root@shuyizhi-VirtualBox:/6381# cp redis-3.2.8/redis.conf 6379/6379.conf

root@shuyizhi-VirtualBox:/6381# cp redis-3.2.8/redis.conf 6380/6380.conf

root@shuyizhi-VirtualBox:/6381# cp redis-3.2.8/redis.conf 6381/6381.conf

1.2.6、分别修改6379~6380的conf文件

root@shuyizhi-VirtualBox:/6381# vi 6379.conf 

# Redis使用后台模式

daemonize yes

# 关闭保护模式

protected-mode no

# 注释以下内容开启远程访问# bind 127.0.0.1# 修改启动端口为6379

port 6379

# 修改pidfile指向路径pidfile

pidfile /6381/redis_6381.pid

以此类推,修改端口63806381配置。

1.2.7、分别启动三个redis实例

root@shuyizhi-VirtualBox:/6381# /redis-3.2.8/bin/redis-server /6379/6379.conf 

root@shuyizhi-VirtualBox:/6381# /redis-3.2.8/bin/redis-server /6380/6380.conf 

root@shuyizhi-VirtualBox:/6381# /redis-3.2.8/bin/redis-server /6381/6381.conf 

查看是否启动成功

root@shuyizhi-VirtualBox:/6381# netstat -tnlp


1.2.8、设置主从

在Redis中设置主从有2种方式:

1.在redis.conf中设置slaveof a)

slaveof <masterip> <masterport>

2、 使用redis-cli客户端连接到redis服务,执行slaveof命令

a) slaveof <masterip> <masterport>第二种方式在重启后将失去主从复制关系。

我们这里使用第二种方式设置主从:

使用Redis客户端连接上6380端口# redis-cli -h 192.168.56.101 -p 6380

设置6380端口Redis为6379的从

192.168.56.101:6380> slaveof 192.168.56.101 6379

OK

使用Redis客户端连接上6381端口#

redis-cli -h 192.168.56.101 -p 6381

设置6381端口Redis为6379的从

192.168.56.101:6381> slaveof 192.168.56.101 6379

OK

1.2.9、查看Redis主从关系

使用redis客户端连接上6379端口

root@shuyizhi-VirtualBox:/6381# redis-cli -h 192.168.56.101 -p 6379

查看redis主从关系

192.168.56.101:6379> info replication


role:角色信息 
slaveX:从库信息 

connected_slaves:从库数量

2、测试

主Redis写入:

192.168.56.101:6379> set test test1

OK

从Redis读取:

192.168.56.101:6381> get test

"test1"

192.168.56.101:6388> get test

"test1"

3、Java代码实现

RedisUtils:

  1. package com.example.Utils;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import redis.clients.jedis.JedisPool;
  5. /**
  6. * @Author: shuyizhi
  7. * @Date: 2018/4/17 14:49
  8. * @Description:
  9. */
  10. public class RedisUtils {
  11. private final static Logger logger = LoggerFactory.getLogger(RedisUtils.class);
  12. private JedisPool masterJedisPool;
  13. private JedisPool slaveJedisPool;
  14. public void setMasterJedisPool(JedisPool masterJedisPool) {
  15. this.masterJedisPool = masterJedisPool;
  16. }
  17. public void setSlaveJedisPool(JedisPool slaveJedisPool) {
  18. this.slaveJedisPool = slaveJedisPool;
  19. }
  20. public String setString(final String key, final String value) {
  21. String ret = masterJedisPool.getResource().set(key, value);
  22. logger.info("key: " + key + ",value: " + value + ",ret: " + ret);
  23. return ret;
  24. }
  25. public String getString(final String key) {
  26. String ret = slaveJedisPool.getResource().get(key);
  27. logger.info("key: " + key + ",value: " + ret);
  28. return ret;
  29. }
  30. }

spring-datasource.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5. <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
  6. <!--最大连接数-->
  7. <property name="maxTotal" value="100"></property>
  8. <!--最大空闲数-->
  9. <property name="maxIdle" value="50"></property>
  10. <!--最小空闲连接数-->
  11. <property name="minIdle" value="20"></property>
  12. <!--当池中连接耗尽,调用者最大阻塞时间,超出此时间将抛出异常。(单位:ms,默认:-1,永不超时)-->
  13. <property name="maxWaitMillis" value="1000"></property>
  14. <!-- 参考:http://biasedbit.com/redis-jedispool-configuration/ -->
  15. <!--调用者获取连接时,是否检测当前连接有效性,无效则从连接池中移除,并尝试继续获取(默认为false)-->
  16. <property name="testOnBorrow" value="true"></property>
  17. <!--向连接池中归还连接时,是否检测连接有效性(默认为false)-->
  18. <property name="testOnReturn" value="true"></property>
  19. <!--调用者获取连接时,是否检测空闲有效,如果超时,则会被移除(默认为false)-->
  20. <property name="testWhileIdle" value="true"></property>
  21. <!--空闲连接检测线程一次运行多少条连接-->
  22. <property name="numTestsPerEvictionRun" value="10"></property>
  23. <!--空闲连接检测线程调用周期,如果为负值,表示不运行检测线程(单位:ms,默认为-1)-->
  24. <property name="timeBetweenEvictionRunsMillis" value="60000"></property>
  25. <!--连接获取方式,队列:false;栈:true-->
  26. <!--<property name="lifo" value="false"></property>-->
  27. </bean>
  28. <bean id="masterJedisPool" class="redis.clients.jedis.JedisPool">
  29. <constructor-arg index="0" ref="poolConfig"></constructor-arg>
  30. <constructor-arg index="1" value="192.168.56.101" type="java.lang.String"></constructor-arg>
  31. <constructor-arg index="2" value="6379" type="int"></constructor-arg>
  32. </bean>
  33. <bean id="slaveJedisPool" class="redis.clients.jedis.JedisPool">
  34. <constructor-arg index="0" ref="poolConfig"></constructor-arg>
  35. <constructor-arg index="1" value="192.168.56.101" type="java.lang.String"></constructor-arg>
  36. <constructor-arg index="2" value="6380" type="int"></constructor-arg>
  37. </bean>
  38. <bean id="redisUtils" class="com.example.Utils.RedisUtils">
  39. <property name="masterJedisPool" ref="masterJedisPool"></property>
  40. <property name="slaveJedisPool" ref="slaveJedisPool"></property>
  41. </bean>
  42. </beans>

测试代码:

package com.example;

import com.example.Utils.RedisUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @Author: shuyizhi
 * @Date: 2018/4/17 15:22
 * @Description:
 */
public class RedisUtilTest {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-datasource.xml");
        System.out.println(ctx);
        RedisUtils utils = (RedisUtils) ctx.getBean("redisUtils");
        utils.setString("springboot", "springbootredis");

        System.out.println(utils.getString("springboot"));
    }
}
 

结果查看

6381端口:


6380端口:




声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/858370
推荐阅读
相关标签
  

闽ICP备14008679号