Master-slave replication - redis master-slave, the java client uses jedis to connect to the master, will the read request be routed to the slave?
仅有的幸福
仅有的幸福 2017-05-16 13:20:21
0
1
1159

Redis master-slave achieves a read-write separation effect similar to mysql. Is it necessary to execute slave host at the code level?
Currently, the sentinel cluster is connected through the jedis client JedisSentinelPool. When viewing the log output, the connection should be to the master's host
Will the read request be automatically routed to the slave?

仅有的幸福
仅有的幸福

reply all(1)
淡淡烟草味

Configure the master's IP and port, and configure the slave's master address and port number. Sentinel monitors the master's IP and port number, and the java code can directly use the master's name and password.
`

public static void main(String[] args) {

    Set<String> sentinels = new HashSet<String>();
     String hostAndPort1 = "127.0.0.1:26379";
     String hostAndPort2 = "127.0.0.1:26380";
    sentinels.add(hostAndPort1);
    sentinels.add(hostAndPort2);

    String clusterName = "mymaster";
     String password = "123456";

     JedisSentinelPool redisSentinelJedisPool = new JedisSentinelPool(clusterName,sentinels,password);

    Jedis jedis = null;
     try {
         jedis = redisSentinelJedisPool.getResource();

         System.out.println(jedis.get("key"));
     } catch (Exception e) {
         e.printStackTrace();
     } finally {
         redisSentinelJedisPool.returnBrokenResource(jedis);
    }

    redisSentinelJedisPool.close();

`

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template