com.lambdaworks.redis.RedisURI Java Examples

The following examples show how to use com.lambdaworks.redis.RedisURI. 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: RedisGetSetClient.java    From moleculer-java with MIT License 6 votes vote down vote up
@Override
public final void connect() {
	super.connect();
	List<RedisURI> redisURIs = parseURLs(urls, password, secure);
	ByteArrayCodec codec = new ByteArrayCodec();
	if (urls.length > 1) {

		// Clustered client
		clusteredClient = RedisClusterClient.create(resources, redisURIs).connect(codec).async();

	} else {

		// Single server connection
		client = RedisClient.create(resources, redisURIs.get(0)).connect(codec).async();
	}
}
 
Example #2
Source File: RedisPubSubClient.java    From moleculer-java with MIT License 6 votes vote down vote up
public final void connect() {
	super.connect();
	List<RedisURI> redisURIs = parseURLs(urls, password, secure);
	StatefulRedisPubSubConnection<byte[], byte[]> connection;
	ByteArrayCodec codec = new ByteArrayCodec();
	if (urls.length > 1) {

		// Clustered client
		connection = RedisClusterClient.create(resources, redisURIs).connectPubSub(codec);

	} else {

		// Single connection
		connection = RedisClient.create(resources, redisURIs.get(0)).connectPubSub(codec);
	}

	// Add listener
	if (listener != null) {
		connection.addListener(listener);
	}
	client = connection.async();
}
 
Example #3
Source File: RedisLettuceService.java    From samantha with MIT License 6 votes vote down vote up
private void startUp() {
    logger.info("Starting RedisLettuceService");
    {
        logger.debug("Redis settings:");
        logger.debug("* host={}", cfgHost);
        logger.debug("* port={}", cfgPort);
        logger.debug("* db={}", cfgDb);
    }

    RedisURI redisURI = new RedisURI();
    redisURI.setHost(cfgHost);
    redisURI.setPort(cfgPort);
    redisURI.setDatabase(cfgDb);
    client = RedisClient.create(redisURI);
    connection = client.connect();
    asyncConnection = client.connect();
    syncCommands = connection.sync();
    asyncCommands = asyncConnection.async();
    asyncConnection.setAutoFlushCommands(false);

    logger.info("Connected to a redis client");
}
 
Example #4
Source File: RedisSyncSingleStorageImpl.java    From mithqtt with Apache License 2.0 6 votes vote down vote up
@Override
public void init(AbstractConfiguration config) {
    if (!config.getString("redis.type").equals("single")) {
        throw new IllegalStateException("RedisSyncSingleStorageImpl class can only be used with single redis setup, but redis.type value is " + config.getString("redis.type"));
    }

    List<String> address = parseRedisAddress(config.getString("redis.address"), 6379);
    int databaseNumber = config.getInt("redis.database", 0);
    String password = StringUtils.isNotEmpty(config.getString("redis.password")) ? config.getString("redis.password") + "@" : "";

    // lettuce
    RedisURI lettuceURI = RedisURI.create("redis://" + password + address.get(0) + "/" + databaseNumber);
    this.lettuce = RedisClient.create(lettuceURI);
    this.lettuceConn = this.lettuce.connect();

    // params
    initParams(config);
}
 
Example #5
Source File: RedisSyncSentinelStorageImpl.java    From mithqtt with Apache License 2.0 6 votes vote down vote up
@Override
public void init(AbstractConfiguration config) {
    if (!config.getString("redis.type").equals("sentinel")) {
        throw new IllegalStateException("RedisSyncSingleStorageImpl class can only be used with sentinel redis setup, but redis.type value is " + config.getString("redis.type"));
    }

    List<String> address = parseRedisAddress(config.getString("redis.address"), 26379);
    int databaseNumber = config.getInt("redis.database", 0);
    String password = StringUtils.isNotEmpty(config.getString("redis.password")) ? config.getString("redis.password") + "@" : "";
    String masterId = config.getString("redis.master");

    // lettuce
    RedisURI lettuceURI = RedisURI.create("redis-sentinel://" + password + String.join(",", address) + "/" + databaseNumber + "#" + masterId);
    this.lettuceSentinel = RedisClient.create(lettuceURI);
    this.lettuceSentinelConn = MasterSlave.connect(this.lettuceSentinel, new Utf8StringCodec(), lettuceURI);
    this.lettuceSentinelConn.setReadFrom(ReadFrom.valueOf(config.getString("redis.read")));

    // params
    initParams(config);
}
 
Example #6
Source File: RedisSyncMasterSlaveStorageImpl.java    From mithqtt with Apache License 2.0 6 votes vote down vote up
@Override
public void init(AbstractConfiguration config) {
    if (!config.getString("redis.type").equals("master_slave")) {
        throw new IllegalStateException("RedisSyncSingleStorageImpl class can only be used with master slave redis setup, but redis.type value is " + config.getString("redis.type"));
    }

    List<String> address = parseRedisAddress(config.getString("redis.address"), 6379);
    int databaseNumber = config.getInt("redis.database", 0);
    String password = StringUtils.isNotEmpty(config.getString("redis.password")) ? config.getString("redis.password") + "@" : "";

    // lettuce
    RedisURI lettuceURI = RedisURI.create("redis://" + password + address.get(0) + "/" + databaseNumber);
    this.lettuceMasterSlave = RedisClient.create(lettuceURI);
    this.lettuceMasterSlaveConn = MasterSlave.connect(this.lettuceMasterSlave, new Utf8StringCodec(), lettuceURI);
    this.lettuceMasterSlaveConn.setReadFrom(ReadFrom.valueOf(config.getString("redis.read")));

    // params
    initParams(config);
}
 
Example #7
Source File: AbstractRedisClient.java    From moleculer-java with MIT License 5 votes vote down vote up
protected final List<RedisURI> parseURLs(String[] urls, String password, boolean secure) {
	ArrayList<RedisURI> list = new ArrayList<>(urls.length);
	for (String url : urls) {
		url = url.trim();
		if ((password == null || password.isEmpty()) && !secure) {

			// URL contains all of the connection parameters
			try {
				list.add(RedisURI.create(url));
				continue;
			} catch (Exception ignored) {

				// Ignore, use old parsing below.
			}
		}

		// Simple "host" or "host:port" or "redis://host:port" syntax
		if (url.startsWith("redis://")) {
			url = url.substring(8);
		}
		if (url.endsWith("/")) {
			url = url.substring(0, url.length() - 1);
		}
		int i = url.indexOf(':');
		String host = "localhost";
		int port = 6379;
		if (i > -1) {
			host = url.substring(0, i);
			port = Integer.parseInt(url.substring(i + 1));
		} else {
			host = url;
		}
		RedisURI.Builder builder = RedisURI.builder().withHost(host).withPort(port).withSsl(secure);
		if (password != null && !password.isEmpty()) {
			builder.withPassword(password);
		}
		list.add(builder.build());
	}
	return list;
}
 
Example #8
Source File: RedisSyncClusterStorageImpl.java    From mithqtt with Apache License 2.0 5 votes vote down vote up
@Override
public void init(AbstractConfiguration config) {
    if (!config.getString("redis.type").equals("cluster")) {
        throw new IllegalStateException("RedisSyncSingleStorageImpl class can only be used with cluster redis setup, but redis.type value is " + config.getString("redis.type"));
    }

    List<String> address = parseRedisAddress(config.getString("redis.address"), 6379);
    int databaseNumber = config.getInt("redis.database", 0);
    String password = StringUtils.isNotEmpty(config.getString("redis.password")) ? config.getString("redis.password") + "@" : "";

    // lettuce
    RedisURI lettuceURI = RedisURI.create("redis://" + password + address.get(0) + "/" + databaseNumber);
    this.lettuceCluster = RedisClusterClient.create(lettuceURI);
    this.lettuceCluster.setOptions(ClusterClientOptions.builder()
            .topologyRefreshOptions(ClusterTopologyRefreshOptions.builder()
                    .enablePeriodicRefresh(config.getBoolean("redis.cluster.periodicRefreshEnabled", ClusterTopologyRefreshOptions.DEFAULT_PERIODIC_REFRESH_ENABLED))
                    .refreshPeriod(config.getLong("redis.cluster.refreshPeriod", ClusterTopologyRefreshOptions.DEFAULT_REFRESH_PERIOD), TimeUnit.SECONDS)
                    .closeStaleConnections(config.getBoolean("redis.cluster.closeStaleConnections", ClusterTopologyRefreshOptions.DEFAULT_CLOSE_STALE_CONNECTIONS))
                    .build())
            .validateClusterNodeMembership(config.getBoolean("redis.cluster.validateClusterNodeMembership", ClusterClientOptions.DEFAULT_VALIDATE_CLUSTER_MEMBERSHIP))
            .maxRedirects(config.getInt("redis.cluster.refreshPeriod", ClusterClientOptions.DEFAULT_MAX_REDIRECTS))
            .build());
    this.lettuceClusterConn = this.lettuceCluster.connect();
    this.lettuceClusterConn.setReadFrom(ReadFrom.valueOf(config.getString("redis.read")));

    // params
    initParams(config);
}