io.lettuce.core.cluster.RedisClusterClient Java Examples

The following examples show how to use io.lettuce.core.cluster.RedisClusterClient. 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: 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 #2
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 #3
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 #4
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 #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: 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 #7
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 #8
Source File: LettuceConnectionManager.java    From jetcache with Apache License 2.0 5 votes vote down vote up
public StatefulConnection connection(AbstractRedisClient redisClient) {
    LettuceObjects lo = getLettuceObjectsFromMap(redisClient);
    if (lo.connection == null) {
        if (redisClient instanceof RedisClient) {
            lo.connection = ((RedisClient) redisClient).connect(new JetCacheCodec());
        } else if (redisClient instanceof RedisClusterClient) {
            lo.connection = ((RedisClusterClient) redisClient).connect(new JetCacheCodec());
        } else {
            throw new CacheConfigException("type " + redisClient.getClass() + " is not supported");
        }
    }
    return lo.connection;
}
 
Example #9
Source File: RedisClusterClientConstructorInterceptor.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Override
public void onConstruct(EnhancedInstance objInst, Object[] allArguments) {
    @SuppressWarnings("unchecked") Iterable<RedisURI> redisURIs = (Iterable<RedisURI>) allArguments[1];
    RedisClusterClient redisClusterClient = (RedisClusterClient) objInst;
    StringBuilder peer = new StringBuilder();
    for (RedisURI redisURI : redisURIs) {
        peer.append(redisURI.getHost()).append(":").append(redisURI.getPort()).append(";");
    }
    EnhancedInstance optionsInst = (EnhancedInstance) redisClusterClient.getOptions();
    optionsInst.setSkyWalkingDynamicField(PeerFormat.shorten(peer.toString()));
}
 
Example #10
Source File: RedisLettuceStarterTest.java    From jetcache with Apache License 2.0 5 votes vote down vote up
@Test
public void tests() throws Exception {
    if (RedisLettuceCacheTest.checkOS()) {
        System.setProperty("spring.profiles.active", "redislettuce-cluster");
    } else {
        System.setProperty("spring.profiles.active", "redislettuce");
    }
    context = SpringApplication.run(RedisLettuceStarterTest.class);
    doTest();
    A bean = context.getBean(A.class);
    bean.test();

    RedisClient t1 = (RedisClient) context.getBean("defaultClient");
    RedisClient t2 = (RedisClient) context.getBean("a1Client");
    Assert.assertNotNull(t1);
    Assert.assertNotNull(t2);
    Assert.assertNotSame(t1, t2);

    AutoConfigureBeans acb = context.getBean(AutoConfigureBeans.class);

    String key = "remote.A1";
    Assert.assertTrue(new LettuceFactory(acb, key, StatefulRedisConnection.class).getObject() instanceof StatefulRedisConnection);
    Assert.assertTrue(new LettuceFactory(acb, key, RedisCommands.class).getObject() instanceof RedisCommands);
    Assert.assertTrue(new LettuceFactory(acb, key, RedisAsyncCommands.class).getObject() instanceof RedisAsyncCommands);
    Assert.assertTrue(new LettuceFactory(acb, key, RedisReactiveCommands.class).getObject() instanceof RedisReactiveCommands);

    if (RedisLettuceCacheTest.checkOS()) {
        key = "remote.A2";
        Assert.assertTrue(new LettuceFactory(acb, key, RedisClusterClient.class).getObject() instanceof RedisClusterClient);
        Assert.assertTrue(new LettuceFactory(acb, key, RedisClusterCommands.class).getObject() instanceof RedisClusterCommands);
        Assert.assertTrue(new LettuceFactory(acb, key, RedisClusterAsyncCommands.class).getObject() instanceof RedisClusterAsyncCommands);
        Assert.assertTrue(new LettuceFactory(acb, key, RedisClusterReactiveCommands.class).getObject() instanceof RedisClusterReactiveCommands);

        key = "remote.A2_slave";
        Assert.assertTrue(new LettuceFactory(acb, key, RedisClusterClient.class).getObject() instanceof RedisClusterClient);
    }
}
 
Example #11
Source File: RedisLettuceCacheTest.java    From jetcache with Apache License 2.0 5 votes vote down vote up
private void testUnwrap(AbstractRedisClient client) {
    Assert.assertTrue(cache.unwrap(AbstractRedisClient.class) instanceof AbstractRedisClient);
    if (client instanceof RedisClient) {
        Assert.assertTrue(cache.unwrap(RedisClient.class) instanceof RedisClient);
        Assert.assertTrue(cache.unwrap(RedisCommands.class) instanceof RedisCommands);
        Assert.assertTrue(cache.unwrap(RedisAsyncCommands.class) instanceof RedisAsyncCommands);
        Assert.assertTrue(cache.unwrap(RedisReactiveCommands.class) instanceof RedisReactiveCommands);
    } else {
        Assert.assertTrue(cache.unwrap(RedisClusterClient.class) instanceof RedisClusterClient);
        Assert.assertTrue(cache.unwrap(RedisClusterCommands.class) instanceof RedisClusterCommands);
        Assert.assertTrue(cache.unwrap(RedisClusterAsyncCommands.class) instanceof RedisClusterAsyncCommands);
        Assert.assertTrue(cache.unwrap(RedisClusterReactiveCommands.class) instanceof RedisClusterReactiveCommands);
    }
}
 
Example #12
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 #13
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 #14
Source File: ProtobufRedisLoadingCache.java    From curiostack with MIT License 5 votes vote down vote up
private <K extends Message, V extends Message> RemoteCache<K, V> createRedisRemoteCache(
    String name,
    RedisClusterClient redisClient,
    K keyPrototype,
    V valuePrototype,
    ReadFrom readFrom) {
  StatefulRedisClusterConnection<K, V> connection =
      redisClient.connect(
          new ProtobufRedisCodec<>(
              (name + ":").getBytes(StandardCharsets.UTF_8), keyPrototype, valuePrototype));
  connection.setReadFrom(readFrom);
  return new RedisRemoteCache<>(connection.async(), name, meterRegistry);
}
 
Example #15
Source File: ProtobufRedisLoadingCache.java    From curiostack with MIT License 5 votes vote down vote up
@Inject
public Factory(
    Lazy<RedisClusterClient> redisClusterClient,
    Lazy<RedisClient> redisClient,
    RedisConfig config,
    MeterRegistry meterRegistry) {
  this.redisClusterClient = redisClusterClient;
  this.redisClient = redisClient;
  this.config = config;
  this.meterRegistry = meterRegistry;
}
 
Example #16
Source File: RedisClusterOnlineRetriever.java    From feast with Apache License 2.0 5 votes vote down vote up
public static OnlineRetriever create(Map<String, String> config) {
  List<RedisURI> redisURIList =
      Arrays.stream(config.get("connection_string").split(","))
          .map(
              hostPort -> {
                String[] hostPortSplit = hostPort.trim().split(":");
                return RedisURI.create(hostPortSplit[0], Integer.parseInt(hostPortSplit[1]));
              })
          .collect(Collectors.toList());

  StatefulRedisClusterConnection<byte[], byte[]> connection =
      RedisClusterClient.create(redisURIList).connect(new ByteArrayCodec());

  return new RedisClusterOnlineRetriever(connection);
}
 
Example #17
Source File: LettuceCacheProvider.java    From J2Cache with Apache License 2.0 5 votes vote down vote up
/**
 * Get PubSub connection
 * @return connection instance
 */
private StatefulRedisPubSubConnection pubsub() {
    if(redisClient instanceof RedisClient)
        return ((RedisClient)redisClient).connectPubSub();
    else if(redisClient instanceof RedisClusterClient)
        return ((RedisClusterClient)redisClient).connectPubSub();
    return null;
}
 
Example #18
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 #19
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 #20
Source File: RedisClusterRateLimiterFactory.java    From ratelimitj with Apache License 2.0 4 votes vote down vote up
public RedisClusterRateLimiterFactory(RedisClusterClient client) {
    this.client = requireNonNull(client);
}
 
Example #21
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();
}
 
Example #22
Source File: RedisClusterConnectionSetupExtension.java    From ratelimitj with Apache License 2.0 4 votes vote down vote up
public RedisClusterClient getClient() {
    return client;
}
 
Example #23
Source File: Client.java    From sherlock with GNU General Public License v3.0 4 votes vote down vote up
/**
 * @return the RedisClusterClient instance
 */
public RedisClusterClient getRedisClusterClient() {
    return redisClusterClient;
}
 
Example #24
Source File: LettuceRedisClusterCacheManager.java    From AutoLoadCache with Apache License 2.0 4 votes vote down vote up
public LettuceRedisClusterCacheManager(RedisClusterClient redisClusterClient, ISerializer<Object> serializer) {
    super(serializer);
    this.redisClusterClient = redisClusterClient;
}
 
Example #25
Source File: LettuceCluster5Driver.java    From hazelcast-simulator with Apache License 2.0 4 votes vote down vote up
@Override
public RedisClusterClient getVendorInstance() {
    return client;
}