public RedisRegistry(AppContext appContext) {
    super(appContext);
    Config config = appContext.getConfig();
    this.clusterName = config.getClusterName();
    this.lock = new RedisLock("LTS_CLEAN_LOCK_KEY", config.getIdentity(), 2 * 60); // 锁两分钟过期

    JedisPoolConfig redisConfig = new JedisPoolConfig();
    // TODO 可以设置n多参数
    String address = NodeRegistryUtils.getRealRegistryAddress(config.getRegistryAddress());

    String cluster = config.getParameter("cluster", "failover");
    if (!"failover".equals(cluster) && !"replicate".equals(cluster)) {
      throw new IllegalArgumentException(
          "Unsupported redis cluster: "
              + cluster
              + ". The redis cluster only supported failover or replicate.");
    }
    replicate = "replicate".equals(cluster);

    this.reconnectPeriod =
        config.getParameter(
            ExtConfig.REGISTRY_RECONNECT_PERIOD_KEY, Constants.DEFAULT_REGISTRY_RECONNECT_PERIOD);

    String[] addrs = address.split(",");
    for (String addr : addrs) {
      int i = addr.indexOf(':');
      String host = addr.substring(0, i);
      int port = Integer.parseInt(addr.substring(i + 1));
      this.jedisPools.put(addr, new JedisPool(redisConfig, host, port, Constants.DEFAULT_TIMEOUT));
    }

    this.expirePeriod =
        config.getParameter(ExtConfig.REDIS_SESSION_TIMEOUT, Constants.DEFAULT_SESSION_TIMEOUT);

    this.expireFuture =
        expireExecutor.scheduleWithFixedDelay(
            new Runnable() {
              public void run() {
                try {
                  deferExpired(); // 延长过期时间
                } catch (Throwable t) { // 防御性容错
                  LOGGER.error(
                      "Unexpected exception occur at defer expire time, cause: " + t.getMessage(),
                      t);
                }
              }
            },
            expirePeriod / 2,
            expirePeriod / 2,
            TimeUnit.MILLISECONDS);
  }
예제 #2
0
 public ScheduledFuture<?> scheduleWithFixedDelay(
     Runnable command, long initialDelay, long delay, TimeUnit unit) {
   return e.scheduleWithFixedDelay(command, initialDelay, delay, unit);
 }