欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

redis配置master/slave主从模式集群

程序员文章站 2022-03-22 21:22:53
...
redis配置master/slave主从模式集群

http://download.redis.io/releases/redis-5.0.3.tar.gz

server1(172.25.2.1) redis主服务器        /home/redis    解压reids
server2(172.25.2.1) redis从服务器1  /home/redis2  解压reids

make MALLOC=libc
make install

server1的配置:redis.conf
bind 172.25.2.1
protected-mode no
port 6379

server2的配置:redis2.conf
bind 172.25.2.1
protected-mode no
port 6389
replicaof 172.25.2.1 6379    

分别启动server1、server2:

redis-cli server1:
set "a1" 999

redis-cli server2:
get "a2"

info Replication


//redis 主从模式集群
public class RedisMS {

public static void main(String[] args) {
isServerRunning();
Jedis client = getConnection(6379);
System.out.println(client.isConnected());

client.set("count", "123456");  //master 可读可写
String c1=client.get("count");
client.close();

Jedis client2 = getConnection(6389); //slave  只读
String c2=client2.get("count");
client2.close();
System.out.println("c1="+c1+",c2="+c2);
}

static Jedis getConnection(int port){
String host = "172.25.2.1";
  //连接本地的 Redis 服务
      Jedis client = new Jedis(host, port);
    return client;
}

private static void isServerRunning(){
String host = "172.25.2.1";
      Jedis jedis = new Jedis(host, 6379);
      //查看服务是否运行
      System.out.println("Server is running: "+jedis.ping());
}

}
相关标签: redis