本文整理汇总了Java中com.orbitz.consul.Consul类的典型用法代码示例。如果您正苦于以下问题:Java Consul类的具体用法?Java Consul怎么用?Java Consul使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Consul类属于com.orbitz.consul包,在下文中一共展示了Consul类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: get
import com.orbitz.consul.Consul; //导入依赖的package包/类
@Override
public Consul get() {
String consulUrl = configuration.getConsulUrl();
try {
Consul.Builder builder = Consul.builder().withUrl(consulUrl);
if (System.getenv(Constants.CONSUL_TOKEN_ENVIRONMENT) != null) {
builder.withAclToken(Constants.CONSUL_TOKEN_ENVIRONMENT);
log.info("connecting to consul with url '{}' using http token", consulUrl);
} else {
log.info("connecting to consul with url '{}'", consulUrl);
}
return builder.build();
} catch (ConsulException e) {
log.error("unable to connect to consul", e);
throw new UnrecoverableException();
}
}
示例2: registerSelf
import com.orbitz.consul.Consul; //导入依赖的package包/类
private void registerSelf() throws MalformedURLException {
if (clientOnly) {
LOG.debug("Client only. Not registering self with consul");
return;
}
LOG.info("Registering self with Consul");
Registration.RegCheck ttlCheck = ImmutableRegCheck.builder().ttl("60s").deregisterCriticalServiceAfter("1m").build();
Registration registration =
ImmutableRegistration.builder().address(consulServiceAddress).port(Integer.parseInt(consulServicePort))
.name(consulServiceName).id(consulServiceId).check(ttlCheck).build();
if (CONSUL_HOLDER != null && CONSUL_HOLDER.hasInstance()) {
CONSUL_HOLDER.registerSelf(registration);
} else {
CONSUL_HOLDER = ConsulHolder.initialize(Consul.builder().withUrl(basePath.toURL()).build(), this);
CONSUL_HOLDER.registerSelf(registration);
}
Runtime.getRuntime().removeShutdownHook(shutdownHook);
Runtime.getRuntime().addShutdownHook(shutdownHook);
}
示例3: initialize
import com.orbitz.consul.Consul; //导入依赖的package包/类
@Override
public Config initialize(final Config config) {
try {
visibleHost = getConfigPropertyString(config, "tsd.discovery.visble_host", "localhost");
visiblePort = getConfigPropertyInt(config, "tsd.discovery.visble_port", 4242);
serviceName = getConfigPropertyString(config, "tsd.discovery.service_name", "OpenTSDB");
serviceId = getConfigPropertyString(config, "tsd.discovery.service_id", "opentsdb");
tsdMode = getConfigPropertyString(config, "tsd.mode", "ro");
String consulUrl = getConfigPropertyString(config, "tsd.discovery.consul_url", "http://localhost:8500");
LOGGER.debug("Finished with config");
consul = Consul.builder().withUrl(consulUrl).build();
LOGGER.info("Consul ServiceDiscovery Plugin Initialized");
updateZookeeperQuorum(config);
} catch (Exception e) {
LOGGER.error("Could not register this instance with Consul", e);
}
return config;
}
示例4: start
import com.orbitz.consul.Consul; //导入依赖的package包/类
@Override
public void start(StartContext startContext) throws StartException {
Consul.Builder builder = Consul.builder();
// pool because of multiple threads.
ResteasyClientBuilder clientBuilder = new ResteasyClientBuilder();
clientBuilder = clientBuilder.connectionPoolSize(20);
builder.withClientBuilder(clientBuilder);
builder.withUrl(this.url);
try {
this.consul = builder.build();
} catch (Exception e) {
throw new StartException("Failed to connect consul at " + url, e);
}
}
示例5: initialize
import com.orbitz.consul.Consul; //导入依赖的package包/类
@Override
public void initialize(Bootstrap<?> bootstrap) {
// Replace variables with values from Consul KV. Please override
// getConsulAgentHost() and getConsulAgentPort() if Consul is not
// listening on the default localhost:8500.
try {
final Consul consul = Consul.builder().withHostAndPort(HostAndPort
.fromParts(getConsulAgentHost(), getConsulAgentPort()))
.build();
bootstrap.setConfigurationSourceProvider(
new SubstitutingSourceProvider(
bootstrap.getConfigurationSourceProvider(),
new ConsulSubstitutor(consul, strict,
substitutionInVariables)));
} catch (ConsulException e) {
LOGGER.warn(
"Unable to query Consul running on {}:{},"
+ " disabling configuration subsitution",
getConsulAgentHost(), getConsulAgentPort(), e);
}
}
示例6: setupEnvironment
import com.orbitz.consul.Consul; //导入依赖的package包/类
protected void setupEnvironment(ConsulFactory consulConfig, Environment environment) {
final Consul consul = consulConfig.build();
final String serviceId = getConsulServiceId();
final ConsulAdvertiser advertiser = new ConsulAdvertiser(environment,
consulConfig, consul, serviceId);
// Register a Jetty listener to get the listening host and port
environment.lifecycle().addServerLifecycleListener(
new ConsulServiceListener(advertiser));
// Register a ping healthcheck to the Consul agent
environment.healthChecks().register("consul",
new ConsulHealthCheck(consul));
// Register a shutdown manager to deregister the service
environment.lifecycle().manage(new ConsulAdvertiserManager(advertiser));
// Add an administrative task to toggle maintenance mode
environment.admin().addTask(new MaintenanceTask(consul, serviceId));
}
示例7: ConsulRoutePolicy
import com.orbitz.consul.Consul; //导入依赖的package包/类
public ConsulRoutePolicy(Consul consul) {
this.consul = consul;
this.sessionClient = consul.sessionClient();
this.keyValueClient = consul.keyValueClient();
this.suspendedRoutes = new HashSet<>();
this.leader = new AtomicBoolean(false);
this.lock = new Object();
this.index = new AtomicReference<>(BigInteger.valueOf(0));
this.serviceName = null;
this.servicePath = null;
this.ttl = 60;
this.lockDelay = 10;
this.executorService = null;
this.shouldStopConsumer = true;
this.sessionId = null;
}
示例8: ConsulMetadataStorage
import com.orbitz.consul.Consul; //导入依赖的package包/类
/**
* @param consul Consul-client
* @param prefix prefix for the key-value storage (Default /container)
* @param useRemoteServices use services from remote consul nodes
*/
public ConsulMetadataStorage(Consul consul, String prefix, boolean useRemoteServices)
{
this.consul = consul;
if (null == prefix || 0 == prefix.length()) {
prefix = "container";
}
this.prefix = prefix;
this.useRemoteServices = useRemoteServices;
}
示例9: start
import com.orbitz.consul.Consul; //导入依赖的package包/类
@Override
public void start(StartContext startContext) throws StartException {
Consul.Builder builder = Consul.builder();
// pool because of multiple threads.
ResteasyClientBuilder clientBuilder = new ResteasyClientBuilder();
clientBuilder = clientBuilder.connectionPoolSize(20);
builder.withClientBuilder(clientBuilder);
builder.withUrl(this.url);
try {
this.consul = builder.build();
} catch (Exception e) {
throw new StartException("Failed to connect consul at "+url, e);
}
}
示例10: getConsul
import com.orbitz.consul.Consul; //导入依赖的package包/类
private Consul getConsul() {
// test that the client is active - the purpose of this test is to make sure that the instance of consul in the
// cluster is alive, is it's not we'll create a new one before giving up. The assumption is that the hostname of
// consul is really a consul service that hides multiple instances of consul servers.
RetryTemplate retryTemplate = new RetryTemplate();
retryTemplate.setBackOffPolicy(new FixedBackOffPolicy());
TimeoutRetryPolicy retryPolicy = new TimeoutRetryPolicy();
retryPolicy.setTimeout(10000);
retryTemplate.setRetryPolicy(retryPolicy);
return retryTemplate.execute(context -> {
if (consul == null || consul.statusClient() == null || isEmpty(consul.statusClient().getLeader())) {
// if we can't find a leader this means that the consul client is not usable - we'll release it
log.warn("couldn't verify connection to Consul");
log.info("creating a new Consul client");
consul = newClient(consulProperties.getHostname(), consulProperties.getHttpPort());
// throwing this exception to retry according to the policies. The last retry will throw this exception
throw new IllegalStateException("Consul connection could not be verified");
} else {
log.info("Consul connection verified");
return consul;
}
});
}
示例11: configure
import com.orbitz.consul.Consul; //导入依赖的package包/类
@Override
public void configure(final Binder binder) {
loading();
cfgs.forEach((id, cfg) -> {
final Consul consul = build(cfg);
binder.bind(Consul.class).annotatedWith(Names.named(CONSUL_PREFIX + id)).toInstance(consul);
binder.bind(AgentClient.class).annotatedWith(Names.named(CONSUL_AGENT_CLIENT_PREFIX + id)).toInstance(consul.agentClient());
binder.bind(HealthClient.class).annotatedWith(Names.named(CONSUL_HEALTH_CLIENT_PREFIX + id)).toInstance(consul.healthClient());
binder.bind(KeyValueClient.class).annotatedWith(Names.named(CONSUL_KEY_VALUE_CLIENT_PREFIX + id)).toInstance(consul.keyValueClient());
binder.bind(CatalogClient.class).annotatedWith(Names.named(CONSUL_CATALOG_CLIENT_PREFIX + id)).toInstance(consul.catalogClient());
binder.bind(StatusClient.class).annotatedWith(Names.named(CONSUL_STATUS_CLIENT_PREFIX + id)).toInstance(consul.statusClient());
binder.bind(SessionClient.class).annotatedWith(Names.named(CONSUL_SESSION_CLIENT_PREFIX + id)).toInstance(consul.sessionClient());
binder.bind(EventClient.class).annotatedWith(Names.named(CONSUL_EVENT_CLIENT_PREFIX + id)).toInstance(consul.eventClient());
binder.bind(PreparedQueryClient.class).annotatedWith(Names.named(CONSUL_PREPARED_QUERY_CLIENT_PREFIX + id))
.toInstance(consul.preparedQueryClient());
binder.bind(CoordinateClient.class).annotatedWith(Names.named(CONSUL_COORDINATE_CLIENT_PREFIX + id))
.toInstance(consul.coordinateClient());
binder.bind(OperatorClient.class).annotatedWith(Names.named(CONSUL_OPERATOR_CLIENT + id)).toInstance(consul.operatorClient());
});
}
示例12: ConsulDiscoveryCallback
import com.orbitz.consul.Consul; //导入依赖的package包/类
@Inject
public ConsulDiscoveryCallback(final Consul consul,
final Configuration configuration,
final PluginExecutorService pluginExecutorService) {
this.consul = consul;
this.configuration = configuration;
this.pluginExecutorService = pluginExecutorService;
}
示例13: start
import com.orbitz.consul.Consul; //导入依赖的package包/类
@Override
public void start() throws Exception {
LOG.info("Starting Consul Discovery Agent for " + basePath);
taskRunner = new TaskRunnerFactory("Consul Discovery Agent");
taskRunner.init();
running.set(true);
if (CONSUL_HOLDER == null || !CONSUL_HOLDER.hasInstance()) {
CONSUL_HOLDER = ConsulHolder.initialize(Consul.builder().withUrl(basePath.toURL()).build(), this);
} else {
CONSUL_HOLDER = ConsulHolder.instance();
}
registerSelf();
executorService = Executors.newSingleThreadScheduledExecutor();
executorService.scheduleWithFixedDelay(new Runnable() {
@Override
public void run() {
if (running.get()) {
try {
if (CONSUL_HOLDER == null || !CONSUL_HOLDER.hasInstance()) {
CONSUL_HOLDER =
ConsulHolder.initialize(Consul.builder().withUrl(basePath.toURL()).build(), ConsulDiscoveryAgent.this);
}
checkTtl();
List<ServiceHealth> services = CONSUL_HOLDER.getServices(consulServiceName);
handleServices(services);
} catch (Throwable t) {
//We don't really want this thread to ever die as long as the agent is running, so we'll just log any
// errors.
LOG.warn("Failure in Consul service discovery", t);
}
} else {
Thread.currentThread().interrupt();
}
}
}, 0, pollingInterval, TimeUnit.MILLISECONDS);
}
示例14: initialize
import com.orbitz.consul.Consul; //导入依赖的package包/类
synchronized static ConsulHolder initialize(Consul consul, ConsulDiscoveryAgent consulDiscoveryAgent) {
synchronized (consulLock) {
if (ConsulHolder.instance != null) {
throw new ConsulDiscoveryException("Already intitialized");
}
instance = new ConsulHolder(consul, consulDiscoveryAgent);
return instance;
}
}
示例15: start
import com.orbitz.consul.Consul; //导入依赖的package包/类
public void start() {
consulContainer.start();
consulContainer.followOutput(new Slf4jLogConsumer(logger).withPrefix("consul"));
await("Consul is ready")
.atMost(10, TimeUnit.SECONDS)
.until(() -> {
try {
consul.set(Consul.builder().withUrl(getConsulUrl()).build());
return true;
} catch (Exception e) {
logger.info("Failed to create consul client, probably consul is not ready yet", e);
return false;
}
});
}