Java Code Examples for io.lettuce.core.cluster.RedisClusterClient#create()

The following examples show how to use io.lettuce.core.cluster.RedisClusterClient#create() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Client.java    From sherlock with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Initialize the {@code RedisClusterClient} if it has not already been.
 *
 * @param params Store params used to create a Redis URI
 */
public void initializeRedisClusterClient(StoreParams params) {
    if (redisClusterClient != null) {
        return;
    }
    redisClusterClient = RedisClusterClient.create(produceURI(params));
    // Adaptive cluster topology refresh for redis cluster client
    ClusterTopologyRefreshOptions topologyRefreshOptions = ClusterTopologyRefreshOptions.builder()
        .enablePeriodicRefresh(true)
        .enableAllAdaptiveRefreshTriggers()
        .dynamicRefreshSources(true)
        .closeStaleConnections(true)
        .build();

    redisClusterClient.setOptions(ClusterClientOptions.builder()
                                      .validateClusterNodeMembership(true)
                                      .maxRedirects(5)
                                      .topologyRefreshOptions(topologyRefreshOptions)
                                      .build());
}
 
Example 2
Source File: RedisClusterIngestionClient.java    From feast with Apache License 2.0 6 votes vote down vote up
public RedisClusterIngestionClient(StoreProto.Store.RedisClusterConfig redisClusterConfig) {
  this.uriList =
      Arrays.stream(redisClusterConfig.getConnectionString().split(","))
          .map(
              hostPort -> {
                String[] hostPortSplit = hostPort.trim().split(":");
                return RedisURI.create(hostPortSplit[0], Integer.parseInt(hostPortSplit[1]));
              })
          .collect(Collectors.toList());

  long backoffMs =
      redisClusterConfig.getInitialBackoffMs() > 0 ? redisClusterConfig.getInitialBackoffMs() : 1;
  this.backOffExecutor =
      new BackOffExecutor(redisClusterConfig.getMaxRetries(), Duration.millis(backoffMs));
  this.clusterClient = RedisClusterClient.create(uriList);
}
 
Example 3
Source File: RedisModule.java    From curiostack with MIT License 6 votes vote down vote up
@Provides
@Singleton
static RedisClusterClient redisClusterClient(
    RedisConfig config, MeterRegistry registry, Tracing tracing) {
  RedisClusterClient client =
      RedisClusterClient.create(
          DefaultClientResources.builder()
              .eventExecutorGroup(CommonPools.workerGroup())
              .eventLoopGroupProvider(ArmeriaEventLoopGroupProvider.INSTANCE)
              .commandLatencyCollector(
                  new MicrometerCommandLatencyCollector(DEFAULT_METER_ID_PREFIX, registry))
              .tracing(BraveTracing.create(tracing))
              .build(),
          config.getUrl());
  client.setOptions(ClusterClientOptions.builder().validateClusterNodeMembership(false).build());
  return client;
}
 
Example 4
Source File: LettuceAsyncService.java    From uavstack with Apache License 2.0 6 votes vote down vote up
public LettuceAsyncService(String redisServerAddress, int minConcurrent, int maxConcurrent, int queueSize,
        String password) {
    String[] redisCluster = redisServerAddress.split(",");
    List<RedisURI> nodes = new ArrayList<RedisURI>();
    for (int i = 0; i < redisCluster.length; i++) {
        String[] uriArray = redisCluster[i].split(":");
        Integer port = Integer.valueOf(uriArray[1]);
        if (!StringHelper.isEmpty(password)) {
            nodes.add(RedisURI.Builder.redis(uriArray[0], port).withPassword(password).build());
        }
        else {
            nodes.add(RedisURI.create(uriArray[0], port));
        }
    }

    client = RedisClusterClient.create(nodes);

    connect = client.connect();

}
 
Example 5
Source File: LettuceConnectionManagerTest.java    From jetcache with Apache License 2.0 6 votes vote down vote up
@Test
public void testCluster() {
    if (!RedisLettuceCacheTest.checkOS()) {
        return;
    }
    RedisURI node1 = RedisURI.create("127.0.0.1", 7000);
    RedisURI node2 = RedisURI.create("127.0.0.1", 7001);
    RedisURI node3 = RedisURI.create("127.0.0.1", 7002);
    RedisClusterClient client = RedisClusterClient.create(Arrays.asList(node1, node2, node3));
    LettuceConnectionManager m = LettuceConnectionManager.defaultManager();
    m.init(client, null);
    Assert.assertSame(m.commands(client), m.commands(client));
    Assert.assertSame(m.asyncCommands(client), m.asyncCommands(client));
    Assert.assertSame(m.reactiveCommands(client), m.reactiveCommands(client));
    m.removeAndClose(client);
}
 
Example 6
Source File: RedisLettuceCacheTest.java    From jetcache with Apache License 2.0 6 votes vote down vote up
@Test
public void testCluster2() throws Exception {
    if (!checkOS()) {
        return;
    }
    RedisURI node1 = RedisURI.create("127.0.0.1", 7000);
    RedisURI node2 = RedisURI.create("127.0.0.1", 7001);
    RedisURI node3 = RedisURI.create("127.0.0.1", 7002);
    RedisClusterClient client = RedisClusterClient.create(Arrays.asList(node1, node2, node3));
    StatefulRedisClusterConnection con = client.connect(new JetCacheCodec());
    con.setReadFrom(ReadFrom.SLAVE_PREFERRED);
    cache = RedisLettuceCacheBuilder.createRedisLettuceCacheBuilder()
            .redisClient(client)
            .connection(con)
            .keyPrefix(new Random().nextInt() + "")
            .buildCache();
    cache.put("K1", "V1");
    Thread.sleep(100);
    Assert.assertEquals("V1", cache.get("K1"));
}
 
Example 7
Source File: LettuceCluster5Driver.java    From hazelcast-simulator with Apache License 2.0 6 votes vote down vote up
@Override
    public void startVendorInstance() throws Exception {
        String workerType = get("WORKER_TYPE");
        if ("javaclient".equals(workerType)) {
            String[] uris = get("URI").split(",");

            DefaultClientResources clientResources = DefaultClientResources.builder() //
                    .dnsResolver(new DirContextDnsResolver()) // Does not cache DNS lookups
                    .build();

            List<RedisURI> nodes = new LinkedList<>();
            for (String uri : uris) {
                nodes.add(RedisURI.create(uri));
            }

//            client = RedisClient.create();
//
//            StatefulRedisMasterReplicaConnection<String, String> connection = MasterReplica
//                    .connect(client, new Utf8StringCodec(), nodes);
//            connection.setReadFrom(ReadFrom.MASTER_PREFERRED);

            client = RedisClusterClient.create(nodes);

            // client = RedisClient.create(clientResources, get("URI"));
        }
    }
 
Example 8
Source File: ClusterScaleProcessServiceImpl.java    From cymbal with Apache License 2.0 5 votes vote down vote up
private void rebalanceCluster(final Cluster cluster) {
    RedisClusterClient redisClusterClient = null;
    StatefulRedisClusterConnection redisConnection = null;

    try {
        final List<InstanceBO> instanceBOS = instanceProcessService
                .queryInstanceBOsByClusterId(cluster.getClusterId());

        List<RedisURI> redisURIs = new ArrayList(instanceBOS.size());
        for (InstanceBO each : instanceBOS) {
            redisURIs.add(RedisURI.builder().withHost(each.getNode().getIp()).withPort(each.getSelf().getPort())
                    .build());
        }

        redisClusterClient = RedisClusterClient.create(redisURIs);
        redisClusterClient.setDefaultTimeout(Duration.ofSeconds(DEFAULT_TIMEOUT));
        redisConnection = redisClusterClient.connect();

        // assign slot
        List<MigratePlan> plans = getMigratePlans(redisConnection);
        migrateSlots(redisConnection, plans);
    } finally {
        if (redisConnection != null) {
            redisConnection.close();
        }

        if (redisClusterClient != null) {
            redisClusterClient.shutdown();
        }
    }
}
 
Example 9
Source File: RedisLettuceCacheTest.java    From jetcache with Apache License 2.0 5 votes vote down vote up
@Test
public void testCluster() throws Exception {
    if (!checkOS()) {
        return;
    }
    RedisURI node1 = RedisURI.create("127.0.0.1", 7000);
    RedisURI node2 = RedisURI.create("127.0.0.1", 7001);
    RedisURI node3 = RedisURI.create("127.0.0.1", 7002);
    RedisClusterClient client = RedisClusterClient.create(Arrays.asList(node1, node2, node3));
    test(client,null);
}
 
Example 10
Source File: RedisClusterIngestionClient.java    From feast with Apache License 2.0 4 votes vote down vote up
@Override
public void setup() {
  this.clusterClient = RedisClusterClient.create(this.uriList);
}
 
Example 11
Source File: RedisClusterFeatureSinkTest.java    From feast with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() throws IOException {
  redisCluster = new RedisCluster(REDIS_CLUSTER_PORT1, REDIS_CLUSTER_PORT2, REDIS_CLUSTER_PORT3);
  redisCluster.start();
  redisClusterClient =
      RedisClusterClient.create(
          Arrays.asList(
              RedisURI.create(REDIS_CLUSTER_HOST, REDIS_CLUSTER_PORT1),
              RedisURI.create(REDIS_CLUSTER_HOST, REDIS_CLUSTER_PORT2),
              RedisURI.create(REDIS_CLUSTER_HOST, REDIS_CLUSTER_PORT3)));
  StatefulRedisClusterConnection<byte[], byte[]> connection =
      redisClusterClient.connect(new ByteArrayCodec());
  redisClusterCommands = connection.sync();
  redisClusterCommands.setTimeout(java.time.Duration.ofMillis(600000));

  FeatureSetSpec spec1 =
      FeatureSetSpec.newBuilder()
          .setName("fs")
          .setProject("myproject")
          .addEntities(EntitySpec.newBuilder().setName("entity").setValueType(Enum.INT64).build())
          .addFeatures(
              FeatureSpec.newBuilder().setName("feature").setValueType(Enum.STRING).build())
          .build();

  FeatureSetSpec spec2 =
      FeatureSetSpec.newBuilder()
          .setName("feature_set")
          .setProject("myproject")
          .addEntities(
              EntitySpec.newBuilder()
                  .setName("entity_id_primary")
                  .setValueType(Enum.INT32)
                  .build())
          .addEntities(
              EntitySpec.newBuilder()
                  .setName("entity_id_secondary")
                  .setValueType(Enum.STRING)
                  .build())
          .addFeatures(
              FeatureSpec.newBuilder().setName("feature_1").setValueType(Enum.STRING).build())
          .addFeatures(
              FeatureSpec.newBuilder().setName("feature_2").setValueType(Enum.INT64).build())
          .build();

  Map<FeatureSetReference, FeatureSetSpec> specMap =
      ImmutableMap.of(
          FeatureSetReference.of("myproject", "fs", 1), spec1,
          FeatureSetReference.of("myproject", "feature_set", 1), spec2);
  RedisClusterConfig redisClusterConfig =
      RedisClusterConfig.newBuilder()
          .setConnectionString(CONNECTION_STRING)
          .setInitialBackoffMs(2000)
          .setMaxRetries(4)
          .build();

  redisClusterFeatureSink =
      RedisFeatureSink.builder().setRedisClusterConfig(redisClusterConfig).build();
  redisClusterFeatureSink.prepareWrite(p.apply("Specs-1", Create.of(specMap)));
}
 
Example 12
Source File: RedisClusterConnectionSetupExtension.java    From ratelimitj with Apache License 2.0 4 votes vote down vote up
@Override
public void beforeAll(ExtensionContext context) {
    client = RedisClusterClient.create("redis://localhost:7000");
    connect = client.connect();
    reactiveCommands = connect.reactive();
}