Java 액세스 Redis 5 클 러 스 터

8574 단어 redisjedislettuce
더 읽 기
pox 파일


  4.0.0

  com.kovansys.test
  1.1.0

  redis_test

  jar

  

    11
    11

    1.2.3
    1.7.26
    5.4.1
    UTF-8
    UTF-8
    3.0.0
    2.4.3
    5.0.0.RELEASE

  

  
    target


    

      
        org.apache.maven.plugins
        maven-compiler-plugin
        3.8.0
      

    

  


  
    
      redis.clients
      jedis
      ${jedis.version}
    

    
      io.lettuce
      lettuce-core
      ${lettuce.version}
    


    
      org.apache.commons
      commons-pool2
      ${commons-pool2.version}
    

    
      org.junit.jupiter
      junit-jupiter-api
      ${junit.version}
      test
    

    
      org.junit.jupiter
      junit-jupiter-engine
      ${junit.version}
      test
    

    
      org.slf4j
      slf4j-api
      ${slf4j.version}
    

    
      ch.qos.logback
      logback-core
      ${logback.version}
    

    
      ch.qos.logback
      logback-classic
      ${logback.version}
    

    
      org.slf4j
      jcl-over-slf4j
      ${slf4j.version}
    

    
      org.slf4j
      log4j-over-slf4j
      ${slf4j.version}
    

  


 
    Jedis 로 자바 구현 코드 연결:
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;

public class JRedisClusterJava {
  public static void main(String[] args) {
    try {
      // Connecting to Redis server on localhost
      Set set = new HashSet();
      set.add(new HostAndPort("192.168.0.180", 7000));
      set.add(new HostAndPort("192.168.0.180", 7001));
      set.add(new HostAndPort("192.168.0.180", 7002));
      set.add(new HostAndPort("192.168.0.180", 7003));
      set.add(new HostAndPort("192.168.0.180", 7004));
      set.add(new HostAndPort("192.168.0.180", 7005));

      JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
      jedisPoolConfig.setMaxIdle(300);
      jedisPoolConfig.setMaxTotal(5000);
      jedisPoolConfig.setMinIdle(6);
      jedisPoolConfig.setMaxWaitMillis(2000);
      jedisPoolConfig.setTestOnBorrow(true);
      JedisCluster jedis = new JedisCluster(set, 2000, 2000, 2, "test123", jedisPoolConfig);

      System.out.println("Connection to server sucessfully");

      long startTime = System.currentTimeMillis();

      for (int i = 0; i < 100000; i++) {
         jedis.set(String.valueOf(i), String.valueOf(i));
         String value = jedis.get(String.valueOf(i));
         //System.out.println(value);
      }

      System.out.println("Jedis Cost time:" + (System.currentTimeMillis() - startTime));
      jedis.close();
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }

}



}

 
    Lettuce 로 코드 구현:
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;

import io.lettuce.core.RedisURI;
import io.lettuce.core.cluster.RedisClusterClient;
import io.lettuce.core.cluster.api.StatefulRedisClusterConnection;
import io.lettuce.core.support.ConnectionPoolSupport;

public class LettuceClusterJava {
  public static void main(String[] args) {
    try {
      // Connecting to Redis server on localhost
      Set nodeSet = new HashSet();
      nodeSet.add(RedisURI.create("redis://[email protected]:7000"));
      nodeSet.add(RedisURI.create("redis://[email protected]:7001"));
      nodeSet.add(RedisURI.create("redis://[email protected]:7002"));
      nodeSet.add(RedisURI.create("redis://[email protected]:7003"));
      nodeSet.add(RedisURI.create("redis://[email protected]:7004"));
      nodeSet.add(RedisURI.create("redis://[email protected]:7005"));

      RedisClusterClient clusterClient = RedisClusterClient.create(nodeSet);

      GenericObjectPoolConfig jedisPoolConfig = new GenericObjectPoolConfig();
      jedisPoolConfig.setMaxIdle(300);
      jedisPoolConfig.setMaxTotal(5000);
      jedisPoolConfig.setMinIdle(6);
      jedisPoolConfig.setMaxWaitMillis(2000);
      jedisPoolConfig.setTestOnBorrow(true);

      GenericObjectPool> pool = ConnectionPoolSupport.createGenericObjectPool(() -> clusterClient.connect(), jedisPoolConfig);

      long startTime = System.currentTimeMillis();

      execute work
      try (StatefulRedisClusterConnection connection = pool.borrowObject()) {
       System.out.println("Connection to server sucessfully");
      
       for (int i = 0; i < 100000; i++) {
           connection.sync().set(String.valueOf(i), String.valueOf(i));
           String value = connection.sync().get(String.valueOf(i));
           // System.out.println(value);
       }
      
       System.out.println("Cost time:" + (System.currentTimeMillis() - startTime));
      
       }
      // terminating
      pool.close();

      System.out.println("Cost time:" + (System.currentTimeMillis() - startTime));

      clusterClient.shutdown();
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }

}

좋은 웹페이지 즐겨찾기