Search in sources :

Example 1 with RedisConnectionConfig

use of com.alibaba.csp.sentinel.datasource.redis.config.RedisConnectionConfig in project Sentinel by alibaba.

the class RedisDataSource method getRedisSentinelClient.

private RedisClient getRedisSentinelClient(RedisConnectionConfig connectionConfig) {
    char[] password = connectionConfig.getPassword();
    String clientName = connectionConfig.getClientName();
    RedisURI.Builder sentinelRedisUriBuilder = RedisURI.builder();
    for (RedisConnectionConfig config : connectionConfig.getRedisSentinels()) {
        sentinelRedisUriBuilder.withSentinel(config.getHost(), config.getPort());
    }
    if (password != null) {
        sentinelRedisUriBuilder.withPassword(connectionConfig.getPassword());
    }
    if (StringUtil.isNotEmpty(connectionConfig.getClientName())) {
        sentinelRedisUriBuilder.withClientName(clientName);
    }
    sentinelRedisUriBuilder.withSentinelMasterId(connectionConfig.getRedisSentinelMasterId()).withTimeout(connectionConfig.getTimeout(), TimeUnit.MILLISECONDS);
    return RedisClient.create(sentinelRedisUriBuilder.build());
}
Also used : RedisConnectionConfig(com.alibaba.csp.sentinel.datasource.redis.config.RedisConnectionConfig) RedisURI(io.lettuce.core.RedisURI)

Example 2 with RedisConnectionConfig

use of com.alibaba.csp.sentinel.datasource.redis.config.RedisConnectionConfig in project Sentinel by alibaba.

the class RedisConnectionConfigTest method testRedisClusterDefaultPortSuccess.

@Test
public void testRedisClusterDefaultPortSuccess() {
    String host = "localhost";
    RedisConnectionConfig redisConnectionConfig = RedisConnectionConfig.Builder.redisCluster(host).withPassword("211233").build();
    Assert.assertNull(redisConnectionConfig.getHost());
    Assert.assertEquals(1, redisConnectionConfig.getRedisClusters().size());
    Assert.assertEquals(RedisConnectionConfig.DEFAULT_CLUSTER_PORT, redisConnectionConfig.getRedisClusters().get(0).getPort());
}
Also used : RedisConnectionConfig(com.alibaba.csp.sentinel.datasource.redis.config.RedisConnectionConfig) Test(org.junit.Test)

Example 3 with RedisConnectionConfig

use of com.alibaba.csp.sentinel.datasource.redis.config.RedisConnectionConfig in project Sentinel by alibaba.

the class RedisConnectionConfigTest method testRedisClusterMoreThanOneServerSuccess.

@Test
public void testRedisClusterMoreThanOneServerSuccess() {
    String host = "localhost";
    String host2 = "server2";
    int port1 = 1879;
    int port2 = 1880;
    RedisConnectionConfig redisConnectionConfig = RedisConnectionConfig.Builder.redisCluster(host, port1).withRedisCluster(host2, port2).build();
    Assert.assertNull(redisConnectionConfig.getHost());
    Assert.assertEquals(2, redisConnectionConfig.getRedisClusters().size());
}
Also used : RedisConnectionConfig(com.alibaba.csp.sentinel.datasource.redis.config.RedisConnectionConfig) Test(org.junit.Test)

Example 4 with RedisConnectionConfig

use of com.alibaba.csp.sentinel.datasource.redis.config.RedisConnectionConfig in project Sentinel by alibaba.

the class SentinelModeRedisDataSourceTest method initData.

@Before
public void initData() {
    Converter<String, List<FlowRule>> flowConfigParser = buildFlowConfigParser();
    RedisConnectionConfig config = RedisConnectionConfig.builder().withRedisSentinel(host, redisSentinelPort).withRedisSentinel(host, redisSentinelPort).withSentinelMasterId(redisSentinelMasterId).build();
    initRedisRuleData();
    ReadableDataSource<String, List<FlowRule>> redisDataSource = new RedisDataSource<>(config, ruleKey, channel, flowConfigParser);
    FlowRuleManager.register2Property(redisDataSource.getProperty());
}
Also used : RedisConnectionConfig(com.alibaba.csp.sentinel.datasource.redis.config.RedisConnectionConfig) List(java.util.List)

Example 5 with RedisConnectionConfig

use of com.alibaba.csp.sentinel.datasource.redis.config.RedisConnectionConfig in project Sentinel by alibaba.

the class StandaloneRedisDataSourceTest method buildResource.

@Before
public void buildResource() {
    try {
        // Bind to a random port.
        server = RedisServer.newRedisServer();
        server.start();
    } catch (IOException e) {
        e.printStackTrace();
    }
    Converter<String, List<FlowRule>> flowConfigParser = buildFlowConfigParser();
    client = RedisClient.create(RedisURI.create(server.getHost(), server.getBindPort()));
    RedisConnectionConfig config = RedisConnectionConfig.builder().withHost(server.getHost()).withPort(server.getBindPort()).build();
    initRedisRuleData();
    ReadableDataSource<String, List<FlowRule>> redisDataSource = new RedisDataSource<List<FlowRule>>(config, ruleKey, channel, flowConfigParser);
    FlowRuleManager.register2Property(redisDataSource.getProperty());
}
Also used : RedisConnectionConfig(com.alibaba.csp.sentinel.datasource.redis.config.RedisConnectionConfig) List(java.util.List) IOException(java.io.IOException) FlowRule(com.alibaba.csp.sentinel.slots.block.flow.FlowRule) Before(org.junit.Before)

Aggregations

RedisConnectionConfig (com.alibaba.csp.sentinel.datasource.redis.config.RedisConnectionConfig)14 Test (org.junit.Test)9 List (java.util.List)3 RedisURI (io.lettuce.core.RedisURI)2 FlowRule (com.alibaba.csp.sentinel.slots.block.flow.FlowRule)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Before (org.junit.Before)1