mirror of
https://github.com/MarginaliaSearch/MarginaliaSearch.git
synced 2025-02-24 21:29:00 +00:00

Look, this will make the git history look funny, but trimming unnecessary depth from the source tree is a very necessary sanity-preserving measure when dealing with a super-modularized codebase like this one. While it makes the project configuration a bit less conventional, it will save you several clicks every time you jump between modules. Which you'll do a lot, because it's *modul*ar. The src/main/java convention makes a lot of sense for a non-modular project though. This ain't that.
37 lines
1.4 KiB
Java
37 lines
1.4 KiB
Java
package nu.marginalia.service;
|
|
|
|
import com.google.inject.AbstractModule;
|
|
import nu.marginalia.service.discovery.ServiceRegistryIf;
|
|
import nu.marginalia.service.discovery.ZkServiceRegistry;
|
|
import org.apache.curator.framework.CuratorFramework;
|
|
import org.apache.curator.framework.CuratorFrameworkFactory;
|
|
import org.apache.curator.retry.ExponentialBackoffRetry;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import java.util.Optional;
|
|
|
|
/** Provides a Guice module for service discovery. */
|
|
public class ServiceDiscoveryModule extends AbstractModule {
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(ServiceDiscoveryModule.class);
|
|
|
|
public void configure() {
|
|
var hosts = getZookeeperHosts().orElseThrow(() -> new IllegalStateException("Zookeeper hosts not set"));
|
|
logger.info("Using Zookeeper service registry at {}", hosts);
|
|
CuratorFramework client = CuratorFrameworkFactory
|
|
.newClient(hosts, new ExponentialBackoffRetry(100, 10, 1000));
|
|
|
|
bind(CuratorFramework.class).toInstance(client);
|
|
bind(ServiceRegistryIf.class).to(ZkServiceRegistry.class);
|
|
}
|
|
|
|
private Optional<String> getZookeeperHosts() {
|
|
if (System.getProperty("zookeeper-hosts") != null) {
|
|
return Optional.of(System.getProperty("zookeeper-hosts"));
|
|
}
|
|
return Optional.ofNullable(System.getenv("ZOOKEEPER_HOSTS"));
|
|
}
|
|
|
|
}
|