com.orbitz.consul.model.agent.ImmutableRegistration Java Examples
The following examples show how to use
com.orbitz.consul.model.agent.ImmutableRegistration.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ServiceAdvertizer.java From pragmatic-microservices-lab with MIT License | 6 votes |
public void advertize(ServiceAdvertisement advertisement, ScheduledExecutorService scheduler) { Config config = ConfigProvider.getConfig(); String serviceAddress = getAddressFromAdvert(advertisement, config); Integer servicePort = getPortFromAdvert(advertisement, config); Registration service = ImmutableRegistration.builder().id(idFromAdvert(advertisement)) .name(advertisement.getName()).address(serviceAddress).port(servicePort) .check(Registration.RegCheck.ttl(3L)) // registers with a TTL of 3 seconds .meta(Collections.singletonMap(ConsulClient.KEY_CTX_ROOT, advertisement.getContextRoot())).build(); try { ConsulClient.build().agentClient().register(service); } catch (RuntimeException e) { Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Is consul running?", e); } // keep refreshing the status within the TTL scheduler.scheduleAtFixedRate(() -> refreshAdvertisement(advertisement), 2, 2, TimeUnit.SECONDS); }
Example #2
Source File: ConsulAdvertiserTest.java From dropwizard-consul with Apache License 2.0 | 6 votes |
@Test public void testRegister() { when(agent.isRegistered(serviceId)).thenReturn(false); advertiser.register("http", 8080, 8081); final ImmutableRegistration registration = ImmutableRegistration.builder() .port(8080) .check( ImmutableRegCheck.builder() .http("http://127.0.0.1:8081/admin/healthcheck") .interval("1s") .deregisterCriticalServiceAfter("1m") .build()) .name("test") .meta(ImmutableMap.of("scheme", "http")) .id(serviceId) .build(); verify(agent).register(registration); }
Example #3
Source File: ConsulAdvertiserTest.java From dropwizard-consul with Apache License 2.0 | 6 votes |
@Test public void testRegisterWithHttps() { when(agent.isRegistered(serviceId)).thenReturn(false); advertiser.register("https", 8080, 8081); final ImmutableRegistration registration = ImmutableRegistration.builder() .port(8080) .check( ImmutableRegCheck.builder() .http("https://127.0.0.1:8081/admin/healthcheck") .interval("1s") .deregisterCriticalServiceAfter("1m") .build()) .name("test") .meta(ImmutableMap.of("scheme", "https")) .id(serviceId) .build(); verify(agent).register(registration); }
Example #4
Source File: Advertiser.java From thorntail with Apache License 2.0 | 6 votes |
public void advertise(Registration registration) { if (this.advertisements.contains(registration)) { return; } AgentClient client = this.agentClientInjector.getValue(); com.orbitz.consul.model.agent.Registration consulReg = ImmutableRegistration.builder() .address(registration.getAddress()) .port(registration.getPort()) .id(serviceId(registration)) .name(registration.getName()) .addTags(registration.getTags().toArray(new String[]{})) .check(com.orbitz.consul.model.agent.Registration.RegCheck.ttl(checkTTL)) .build(); client.register(consulReg); this.advertisements.add(registration); log.info("Registered service " + consulReg.getId() + ", checkTTL: " + checkTTL); }
Example #5
Source File: Advertiser.java From ARCHIVE-wildfly-swarm with Apache License 2.0 | 6 votes |
public void advertise(Registration registration) { if (this.advertisements.contains(registration)) { return; } AgentClient client = this.agentClientInjector.getValue(); com.orbitz.consul.model.agent.Registration consulReg = ImmutableRegistration.builder() .address(registration.getAddress()) .port(registration.getPort()) .id(serviceId(registration)) .name(registration.getName()) .addTags(registration.getTags().toArray(new String[]{})) .check(com.orbitz.consul.model.agent.Registration.RegCheck.ttl(3L)) .build(); client.register(consulReg); this.advertisements.add(registration); }
Example #6
Source File: ConsulCoordinator.java From skywalking with Apache License 2.0 | 6 votes |
@Override public void registerRemote(RemoteInstance remoteInstance) throws ServiceRegisterException { if (needUsingInternalAddr()) { remoteInstance = new RemoteInstance(new Address(config.getInternalComHost(), config.getInternalComPort(), true)); } AgentClient agentClient = client.agentClient(); this.selfAddress = remoteInstance.getAddress(); TelemetryRelatedContext.INSTANCE.setId(selfAddress.toString()); Registration registration = ImmutableRegistration.builder() .id(remoteInstance.getAddress().toString()) .name(serviceName) .address(remoteInstance.getAddress().getHost()) .port(remoteInstance.getAddress().getPort()) .check(Registration.RegCheck.grpc(remoteInstance.getAddress() .getHost() + ":" + remoteInstance .getAddress() .getPort(), 5)) // registers with a TTL of 5 seconds .build(); agentClient.register(registration); }
Example #7
Source File: ConsulAdvertiserTest.java From dropwizard-consul with Apache License 2.0 | 5 votes |
@Test public void testHostFromConfig() { factory.setServicePort(8888); factory.setServiceAddress("127.0.0.1"); when(agent.isRegistered(anyString())).thenReturn(false); final ConsulAdvertiser advertiser = new ConsulAdvertiser(environment, factory, consul, serviceId); advertiser.register("http", 8080, 8081); final ImmutableRegistration registration = ImmutableRegistration.builder() .id(serviceId) .port(8888) .address("127.0.0.1") .check( ImmutableRegCheck.builder() .http("http://127.0.0.1:8081/admin/healthcheck") .interval("1s") .deregisterCriticalServiceAfter("1m") .build()) .name("test") .meta(ImmutableMap.of("scheme", "http")) .build(); verify(agent).register(registration); }
Example #8
Source File: ConsulAdvertiserTest.java From dropwizard-consul with Apache License 2.0 | 5 votes |
@Test public void testTagsFromConfig() { final List<String> tags = Arrays.asList("test", "second-test"); factory.setTags(tags); when(agent.isRegistered(serviceId)).thenReturn(false); final ConsulAdvertiser advertiser = new ConsulAdvertiser(environment, factory, consul, serviceId); advertiser.register("http", 8080, 8081); final ImmutableRegistration registration = ImmutableRegistration.builder() .tags(tags) .check( ImmutableRegCheck.builder() .http("http://127.0.0.1:8081/admin/healthcheck") .interval("1s") .deregisterCriticalServiceAfter("1m") .build()) .name("test") .meta(ImmutableMap.of("scheme", "http")) .port(8080) .id(serviceId) .build(); verify(agent).register(registration); }
Example #9
Source File: ConsulAdvertiserTest.java From dropwizard-consul with Apache License 2.0 | 5 votes |
@Test public void testAclTokenFromConfig() { String aclToken = "acl-token"; factory.setAclToken(aclToken); when(agent.isRegistered(serviceId)).thenReturn(false); final ConsulAdvertiser advertiser = new ConsulAdvertiser(environment, factory, consul, serviceId); advertiser.register("http", 8080, 8081); final ImmutableRegistration registration = ImmutableRegistration.builder() .id(serviceId) .check( ImmutableRegCheck.builder() .http("http://127.0.0.1:8081/admin/healthcheck") .interval("1s") .deregisterCriticalServiceAfter("1m") .build()) .name("test") .port(8080) .meta(ImmutableMap.of("scheme", "http")) .id(serviceId) .build(); verify(agent).register(registration); }
Example #10
Source File: ConsulAdvertiserTest.java From dropwizard-consul with Apache License 2.0 | 5 votes |
@Test public void testServiceMetaFromConfig() { final Map<String, String> serviceMeta = new HashMap<>(); serviceMeta.put("meta1-key", "meta1-value"); serviceMeta.put("meta2-key", "meta2-value"); factory.setServiceMeta(serviceMeta); when(agent.isRegistered(serviceId)).thenReturn(false); final ConsulAdvertiser advertiser = new ConsulAdvertiser(environment, factory, consul, serviceId); advertiser.register("http", 8080, 8081); final ImmutableRegistration registration = ImmutableRegistration.builder() .meta(serviceMeta) .putMeta("scheme", "http") .check( ImmutableRegCheck.builder() .http("http://127.0.0.1:8081/admin/healthcheck") .interval("1s") .deregisterCriticalServiceAfter("1m") .build()) .name("test") .port(8080) .id(serviceId) .build(); verify(agent).register(registration); }
Example #11
Source File: BaseRegistrator.java From hazelcast-consul-discovery-spi with Apache License 2.0 | 5 votes |
@Override public void register() throws Exception { try { this.myServiceId = this.consulServiceName + "-" + this.myLocalAddress.getInetAddress().getHostAddress() +"-" + this.myLocalAddress.getHost() + "-" + this.myLocalAddress.getPort(); ImmutableRegistration.Builder builder = ImmutableRegistration.builder() .name(this.consulServiceName) .id(myServiceId) .address(this.myLocalAddress.getInetAddress().getHostAddress()) .port(this.myLocalAddress.getPort()) .tags(Arrays.asList(tags)); String healthCheckProvider = getHealthCheckProvider( (String)registratorConfig.get(CONFIG_PROP_HEALTH_CHECK_PROVIDER) ); HealthCheckBuilder healthBuilder = (HealthCheckBuilder)Class.forName(healthCheckProvider).newInstance(); RegCheck regCheck =healthBuilder.buildRegistrationCheck(registratorConfig, this.myLocalAddress); if (regCheck != null) { builder.check(regCheck); } // register... this.consulAgentClient.register(builder.build(), ConsulUtility.getAclToken(this.consulAclToken)); this.logger.info("Registered with Consul["+this.consulHost+":"+this.consulPort+"] serviceId:"+myServiceId); } catch(Exception e) { String msg = "Unexpected error in register(serviceId:"+myServiceId+"): " + e.getMessage(); logger.severe(msg,e); throw new Exception(msg,e); } }
Example #12
Source File: DiscoveryAgentActor.java From docker-nginx-consul with MIT License | 5 votes |
public DiscoveryAgentActor(AppConfiguration configuration){ this.configuration = configuration; this.SCHEDULED_WORK_DELAY = new FiniteDuration(configuration.serviceDiscoveryConfiguration.healthCheckTimeout, TimeUnit.SECONDS); // todo: terminate system if error occur while connecting to consul // get consul connection this.consul = Consul .builder() .withHostAndPort( HostAndPort.fromParts( configuration.serviceDiscoveryConfiguration.host, configuration.serviceDiscoveryConfiguration.port) ) .build(); // get agent agentClient = consul.agentClient(); // set registration config Registration service = ImmutableRegistration.builder() .id(configuration.appId) .name(configuration.serviceName) .port(configuration.port) .address(configuration.host) .check(Registration.RegCheck.ttl(configuration.serviceDiscoveryConfiguration.healthCheckTimeout)) .tags(Collections.singletonList("tag1")) .meta(Collections.singletonMap("version", "1.0")) .build(); // register service agentClient.register(service); // check in with Consul, serviceId required only. client will prepend "service:" for service level checks. // Note that you need to continually check in before the TTL expires, otherwise your service's state will be marked as "critical". }
Example #13
Source File: ITClusterModuleConsulProviderFunctionalTest.java From skywalking with Apache License 2.0 | 4 votes |
private ClusterModuleConsulProvider createProvider(String serviceName, String internalComHost, int internalComPort) throws Exception { ClusterModuleConsulProvider provider = new ClusterModuleConsulProvider(); ClusterModuleConsulConfig config = (ClusterModuleConsulConfig) provider.createConfigBeanIfAbsent(); config.setHostPort(consulAddress); config.setServiceName(serviceName); if (!StringUtil.isEmpty(internalComHost)) { config.setInternalComHost(internalComHost); } if (internalComPort > 0) { config.setInternalComPort(internalComPort); } provider.prepare(); provider.start(); provider.notifyAfterCompleted(); ConsulCoordinator consulCoordinator = (ConsulCoordinator) provider.getService(ClusterRegister.class); // ignore health check ClusterRegister register = remoteInstance -> { if (needUsingInternalAddr(config)) { remoteInstance = new RemoteInstance(new Address(config.getInternalComHost(), config.getInternalComPort(), true)); } Consul client = Whitebox.getInternalState(consulCoordinator, "client"); AgentClient agentClient = client.agentClient(); Whitebox.setInternalState(consulCoordinator, "selfAddress", remoteInstance.getAddress()); TelemetryRelatedContext.INSTANCE.setId(remoteInstance.getAddress().toString()); Registration registration = ImmutableRegistration.builder() .id(remoteInstance.getAddress().toString()) .name(serviceName) .address(remoteInstance.getAddress().getHost()) .port(remoteInstance.getAddress().getPort()) .build(); agentClient.register(registration); }; provider.registerServiceImplementation(ClusterRegister.class, register); return provider; }
Example #14
Source File: ConsulIntegrationTest.java From wildfly-camel with Apache License 2.0 | 4 votes |
@Test public void testServiceDiscovery() throws Exception { final AgentClient client = getConsul().agentClient(); try { registrations = new ArrayList<>(3); for (int i = 0; i < 3; i++) { Registration r = ImmutableRegistration.builder() .id("service-" + i) .name("my-service") .address("127.0.0.1") .addTags("a-tag") .addTags("key1=value1") .addTags("key2=value2") .port(9000 + i) .build(); client.register(r); registrations.add(r); } ConsulConfiguration configuration = new ConsulConfiguration(); configuration.setUrl(consulUrl); ServiceDiscovery discovery = new ConsulServiceDiscovery(configuration); List<ServiceDefinition> services = discovery.getServices("my-service"); assertNotNull(services); assertEquals(3, services.size()); for (ServiceDefinition service : services) { assertFalse(service.getMetadata().isEmpty()); assertTrue(service.getMetadata().containsKey("service.name")); assertTrue(service.getMetadata().containsKey("service.id")); assertTrue(service.getMetadata().containsKey("service.node")); assertTrue(service.getMetadata().containsKey("a-tag")); assertTrue(service.getMetadata().containsKey("key1")); assertTrue(service.getMetadata().containsKey("key2")); } } finally { if (registrations != null && client != null) { registrations.forEach(r -> client.deregister(r.getId())); } } }