org.redisson.config.ReplicatedServersConfig Java Examples

The following examples show how to use org.redisson.config.ReplicatedServersConfig. 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: RedisConfig.java    From earth-frost with Apache License 2.0 6 votes vote down vote up
/**
 * 云托管模式
 *
 * @param redissonProperties redisson配置
 * @return client
 */
static RedissonClient redissonReplicatedServers(RedissonProperties redissonProperties) {
  Config config = new Config();
  ReplicatedServersConfig serverConfig = config.useReplicatedServers()
      .addNodeAddress(redissonProperties.getNodeAddresses())
      .setTimeout(redissonProperties.getTimeout())
      .setScanInterval(redissonProperties.getScanInterval())
      .setDnsMonitoringInterval(redissonProperties.getDnsMonitoringInterval())
      .setSlaveConnectionPoolSize(redissonProperties.getSlaveConnectionPoolSize())
      .setMasterConnectionPoolSize(redissonProperties.getMasterConnectionPoolSize());

  if (redissonProperties.getPassword() != null && redissonProperties.getPassword().length() > 0) {
    serverConfig.setPassword(redissonProperties.getPassword());
  }

  return Redisson.create(config);
}
 
Example #2
Source File: ElasticacheSessionManager.java    From redis-session-manager with Apache License 2.0 5 votes vote down vote up
@Override
protected Config configure(Config config) {
    if (nodes == null || nodes.trim().length() == 0) {
        throw new IllegalStateException("Manager must specify node string. e.g., nodes=\"node1.com:6379 node2.com:6379\"");
    }
    LoadBalancer lb = null;
    if (loadBalancerClass != null && loadBalancerClass.trim().length() != 0) {
        try {
            lb = LoadBalancer.class.cast(Class.forName(loadBalancerClass).newInstance());
        } catch (Exception e) {
            log.error("Failed to instantiate LoadBalancer", e);
        }
    }

    ReplicatedServersConfig ecCfg = config.useReplicatedServers();
    ecCfg
        .addNodeAddress(nodes.trim().split("\\s+"))
        .setDatabase(database)
        .setMasterConnectionPoolSize(masterConnectionPoolSize)
        .setSlaveConnectionPoolSize(slaveConnectionPoolSize)
        .setPassword(password)
        .setTimeout(timeout)
        .setReadMode(ReadMode.MASTER_SLAVE)
        .setPingTimeout(pingTimeout)
        .setRetryAttempts(retryAttempts)
        .setRetryInterval(retryInterval)
        .setScanInterval(nodePollInterval);
    if (lb != null) {
        ecCfg.setLoadBalancer(lb);
    }
    return config;
}
 
Example #3
Source File: ReplicatedConnectionManager.java    From redisson with Apache License 2.0 4 votes vote down vote up
public ReplicatedConnectionManager(ReplicatedServersConfig cfg, Config config, UUID id) {
    super(config, id);

    this.config = create(cfg);
    initTimer(this.config);

    for (String address : cfg.getNodeAddresses()) {
        RedisURI addr = new RedisURI(address);
        RFuture<RedisConnection> connectionFuture = connectToNode(cfg, addr, null, addr.getHost());
        connectionFuture.awaitUninterruptibly();
        RedisConnection connection = connectionFuture.getNow();
        if (connection == null) {
            continue;
        }

        Role role = Role.valueOf(connection.sync(RedisCommands.INFO_REPLICATION).get(ROLE_KEY));
        if (Role.master.equals(role)) {
            if (currentMaster.get() != null) {
                stopThreads();
                throw new RedisException("Multiple masters detected");
            }
            currentMaster.set(addr);
            log.info("{} is the master", addr);
            this.config.setMasterAddress(addr.toString());
        } else {
            log.info("{} is a slave", addr);
            this.config.addSlaveAddress(addr.toString());
        }
    }

    if (currentMaster.get() == null) {
        stopThreads();
        throw new RedisConnectionException("Can't connect to servers!");
    }
    if (this.config.getReadMode() != ReadMode.MASTER && this.config.getSlaveAddresses().isEmpty()) {
        log.warn("ReadMode = " + this.config.getReadMode() + ", but slave nodes are not found! Please specify all nodes in replicated mode.");
    }

    initSingleEntry();

    scheduleMasterChangeCheck(cfg);
}
 
Example #4
Source File: ReplicatedConnectionManager.java    From redisson with Apache License 2.0 4 votes vote down vote up
@Override
protected MasterSlaveServersConfig create(BaseMasterSlaveServersConfig<?> cfg) {
    MasterSlaveServersConfig res = super.create(cfg);
    res.setDatabase(((ReplicatedServersConfig) cfg).getDatabase());
    return res;
}
 
Example #5
Source File: ReplicatedConnectionManager.java    From redisson with Apache License 2.0 4 votes vote down vote up
private void scheduleMasterChangeCheck(ReplicatedServersConfig cfg) {
    if (isShuttingDown()) {
        return;
    }
    
    monitorFuture = group.schedule(new Runnable() {
        @Override
        public void run() {
            if (isShuttingDown()) {
                return;
            }

            RedisURI master = currentMaster.get();
            log.debug("Current master: {}", master);
            
            AtomicInteger count = new AtomicInteger(cfg.getNodeAddresses().size());
            for (String address : cfg.getNodeAddresses()) {
                RedisURI addr = new RedisURI(address);
                RFuture<RedisConnection> connectionFuture = connectToNode(cfg, addr, null, addr.getHost());
                connectionFuture.onComplete((connection, exc) -> {
                    if (exc != null) {
                        log.error(exc.getMessage(), exc);
                        if (count.decrementAndGet() == 0) {
                            scheduleMasterChangeCheck(cfg);
                        }
                        return;
                    }
                    
                    if (isShuttingDown()) {
                        return;
                    }
                    
                    RFuture<Map<String, String>> result = connection.async(RedisCommands.INFO_REPLICATION);
                    result.onComplete((r, ex) -> {
                        if (ex != null) {
                            log.error(ex.getMessage(), ex);
                            closeNodeConnection(connection);
                            if (count.decrementAndGet() == 0) {
                                scheduleMasterChangeCheck(cfg);
                            }
                            return;
                        }
                        
                        Role role = Role.valueOf(r.get(ROLE_KEY));
                        if (Role.master.equals(role)) {
                            if (master.equals(addr)) {
                                log.debug("Current master {} unchanged", master);
                            } else if (currentMaster.compareAndSet(master, addr)) {
                                RFuture<RedisClient> changeFuture = changeMaster(singleSlotRange.getStartSlot(), addr);
                                changeFuture.onComplete((res, e) -> {
                                    if (e != null) {
                                        currentMaster.compareAndSet(addr, master);
                                    }
                                });
                            }
                        } else if (!config.checkSkipSlavesInit()) {
                            slaveUp(addr, connection.getRedisClient().getAddr());
                        }
                        
                        if (count.decrementAndGet() == 0) {
                            scheduleMasterChangeCheck(cfg);
                        }
                    });
                });
            }
        }

    }, cfg.getScanInterval(), TimeUnit.MILLISECONDS);
}
 
Example #6
Source File: SeimiConfig.java    From SeimiCrawler with Apache License 2.0 2 votes vote down vote up
/**
 * Init Replicated servers configuration.
 * Most used with Azure Redis Cache or AWS Elasticache
 *
 * config.useReplicatedServers()
 *     .setScanInterval(2000) // 主节点变化扫描间隔时间
 *     //可以用"rediss://"来启用SSL连接
 *     .addNodeAddress("redis://127.0.0.1:7000", "redis://127.0.0.1:7001")
 *     .addNodeAddress("redis://127.0.0.1:7002");
 *
 * @return ReplicatedServersConfig
 */
public ReplicatedServersConfig redisReplicatedServers() {
    enableDistributedQueue();
    return redissonConfig.useReplicatedServers();
}