use of net.azisaba.kuvel.discovery.impl.SimpleLoadBalancerDiscovery in project Kuvel by AzisabaNetwork.
the class Kuvel method onProxyInitialization.
@Subscribe
public void onProxyInitialization(ProxyInitializeEvent event) {
kuvelConfig = new KuvelConfig(this);
try {
kuvelConfig.load();
} catch (Exception e) {
logger.severe("Failed to load config file. Plugin feature will be disabled.");
e.printStackTrace();
return;
}
kuvelServiceHandler = new KuvelServiceHandler(this, client);
if (kuvelConfig.isRedisEnabled()) {
Objects.requireNonNull(kuvelConfig.getRedisConnectionData());
Objects.requireNonNull(kuvelConfig.getProxyGroupName());
proxyIdProvider = new ProxyIdProvider(kuvelConfig.getRedisConnectionData().createJedisPool(), kuvelConfig.getProxyGroupName());
proxyIdProvider.runTask(proxy, this);
logger.info("This proxy's id is: " + proxyIdProvider.getId());
redisConnectionLeader = new RedisConnectionLeader(this, kuvelConfig.getRedisConnectionData().createJedisPool(), kuvelConfig.getProxyGroupName(), proxyIdProvider.getId());
redisConnectionLeader.trySwitch();
if (redisConnectionLeader.isLeader()) {
logger.info("This proxy is selected as leader.");
}
kuvelServiceHandler.setAndRunLoadBalancerDiscovery(new RedisLoadBalancerDiscovery(client, this, kuvelConfig.getRedisConnectionData().createJedisPool(), kuvelConfig.getProxyGroupName(), redisConnectionLeader, kuvelServiceHandler));
kuvelServiceHandler.setAndRunServerDiscovery(new RedisServerDiscovery(client, this, kuvelConfig.getRedisConnectionData().createJedisPool(), kuvelConfig.getProxyGroupName(), redisConnectionLeader, kuvelServiceHandler));
proxy.getScheduler().buildTask(this, () -> {
if (redisConnectionLeader.isLeader()) {
redisConnectionLeader.extendLeader();
} else {
redisConnectionLeader.trySwitch();
}
}).repeat(5, TimeUnit.MINUTES).schedule();
redisSubscriber = new RedisSubscriber(kuvelConfig.getRedisConnectionData().createJedisPool(), this, kuvelConfig.getProxyGroupName(), kuvelServiceHandler, redisConnectionLeader);
redisSubscriber.subscribe();
} else {
kuvelServiceHandler.setAndRunLoadBalancerDiscovery(new SimpleLoadBalancerDiscovery(client, this, kuvelServiceHandler));
kuvelServiceHandler.setAndRunServerDiscovery(new SimpleServerDiscovery(client, this, kuvelServiceHandler));
}
proxy.getEventManager().register(this, new LoadBalancerListener(this, kuvelServiceHandler));
}
Aggregations