public void register(Registration registration) { register(registration, QueryOptions.BLANK); }
env.onStarted(() -> agentClient.register(registration)); env.onStop(() -> agentClient.deregister(registration.getId()));
@Test public void registerServiceForConsul() throws Exception { Consul consul = Consul.builder().withHostAndPort(HostAndPort.fromParts("127.0.0.1", 8500)).build(); AgentClient agentClient = consul.agentClient(); String serviceId = UUID.randomUUID().toString(); String serviceName = "java-consul-service"; String serviceTag = "test"; long ttl = 3l; agentClient.register(9090, ttl, serviceName, serviceId, serviceTag); agentClient.pass(serviceId); System.out.println("succ."); TimeUnit.MINUTES.sleep(2); } }
/** * De-register a particular service from the Consul Agent. */ public void deregister(String serviceId) { deregister(serviceId, QueryOptions.BLANK); }
return () -> { try { if (!getConsulClient().agentClient().isRegistered(serviceId)){ getConsulClient().agentClient().register( webconsolePort, regCheck,
/** Deregister a service from Consul */ public void deregister() { final AgentClient agent = consul.agentClient(); if (!agent.isRegistered(serviceId)) { LOGGER.info("No service registered with ID \"{}\"", serviceId); return; } LOGGER.info("Deregistering service ID \"{}\"", serviceId); try { consul.agentClient().deregister(serviceId); } catch (ConsulException e) { LOGGER.error("Failed to deregister service from Consul", e); } }
final Map<String, String> meta = singletonMap("uuid", serviceUuid); consul.agentClient().register(0, 30L, this.serviceName, this.serviceId, tags, meta); consul.agentClient().fail(this.serviceId, "(" + this.serviceId + ") Connector process terminated."); } catch (Exception e) { System.err.println("Failed to report termination to Consul agent.");
@Override protected void doStop() throws Exception { if (scheduler != null) { getCamelContext().getExecutorServiceManager().shutdownNow(scheduler); scheduler = null; } if (configuration.isDeregisterServicesOnStop()) { for (Service service: client.agentClient().getServices().values()) { try { if (serviceList.contains(service.getId())) { client.agentClient().deregister(service.getId()); serviceList.remove(service.getId()); } } catch (Exception e) { LOGGER.warn("Error de-registering service: " + service, e); } } client = null; } }
public ConsulAgentProducer(ConsulEndpoint endpoint, ConsulConfiguration configuration) { super(endpoint, configuration, Consul::agentClient); bind(ConsulAgentActions.CHECKS, wrap(c -> c.getChecks())); bind(ConsulAgentActions.SERVICES, wrap(c -> c.getServices())); bind(ConsulAgentActions.MEMBERS, wrap(c -> c.getMembers())); bind(ConsulAgentActions.AGENT, wrap(c -> c.getAgent())); } }
@Override public void sendHeartbeat() throws SiteWhereException { if (getConsulClient() != null) { AgentClient agentClient = getConsulClient().agentClient(); try { String serviceId = getMicroservice().getId().toString(); if (agentClient.isRegistered(serviceId)) { agentClient.pass(serviceId); } } catch (NotRegisteredException e) { throw new SiteWhereException("Unable to send heartbeat.", e); } } else { getLogger().info("Skipping heartbeat. Consul client not connected."); } }
public synchronized void passHealthCheck() { if (!shouldPassHealthCheck) { // throwing an exception stops the scheduled task from running again. throw new RuntimeException("Stopping recurring health check task."); } try { consul.agentClient().pass(serviceId, "(" + serviceId + ") OK"); LOGGER.debug("Passed health check."); } catch (Throwable t) { fatalErrorConsumer.accept(new RuntimeException("Failed to tell Consul agent we passed health check.", t)); Throwables.throwIfUnchecked(t); throw new RuntimeException(t); } }
public static Map<String, String> getPEServices() { LOG.info("Load PE service status"); Consul consul = consulInstance(); AgentClient agent = consul.agentClient(); Map<String, Service> services = consul.agentClient().getServices(); Map<String, HealthCheck> checks = agent.getChecks(); Map<String, String> peServices = new HashMap<>(); for(Map.Entry<String, Service> entry : services.entrySet()) { if(entry.getValue().getTags().contains(PE_SERVICE_NAME)) { String serviceId = entry.getValue().getId(); String serviceStatus = "critical"; if(checks.containsKey("service:" + entry.getKey())) { serviceStatus = checks.get("service:" + entry.getKey()).getStatus(); } LOG.info("Service id: " + serviceId + " service status: " + serviceStatus); peServices.put(serviceId, serviceStatus); } } return peServices; }
/** * Indicates whether or not a particular service is registered with * the local Consul agent. * * @return <code>true</code> if a particular service is registered with * the local Consul agent, otherwise <code>false</code>. */ public boolean isRegistered(String serviceId) { Map<String, Service> serviceIdToService = getServices(); return serviceIdToService.containsKey(serviceId); }
@Override public void run() { for (final Map.Entry<String, AgentClient> serviceReg : services.entrySet()) { serviceReg.getValue().deregister(serviceReg.getKey()); } } }
if (agent.isRegistered(serviceId)) { LOGGER.info( "Service ({}) [{}] already registered", configuration.getServiceName(), serviceId); consul.agentClient().register(builder.build()); return true;
@Override public void run() { AgentClient client = this.agentClientInjector.getValue(); while (true) { this.advertisements .stream() .forEach(e -> { try { client.pass(serviceId(e)); } catch (NotRegisteredException e1) { // ignore? e1.printStackTrace(); } }); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); break; } } }
/** * Registers the client as a service with Consul with a ttl check. * * @param port The public facing port of the service to register with Consul. * @param ttl Time to live in seconds for the Consul dead man's switch. * @param name Service name to register. * @param id Service id to register. * @param tags Tags to register with. * @param meta Meta to register with. */ public void register(int port, long ttl, String name, String id, List<String> tags, Map<String, String> meta) { Registration.RegCheck check = Registration.RegCheck.ttl(ttl); register(port, check, name, id, tags, meta); }
env.onStarted(() -> agentClient.register(registration)); env.onStop(() -> agentClient.deregister(registration.getId()));