Java Code Examples for com.ecwid.consul.v1.agent.model.NewService#setAddress()

The following examples show how to use com.ecwid.consul.v1.agent.model.NewService#setAddress() . 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: DiscoveryBootstrapConfigurationTests.java    From spring-cloud-vault with Apache License 2.0 8 votes vote down vote up
@BeforeClass
public static void beforeClass() {

	assumeTrue(CanConnect.to(new InetSocketAddress(CONSUL_HOST, CONSUL_PORT)));

	ConsulClient client = new ConsulClient();

	Response<List<CatalogService>> response = client.getCatalogService("vault",
			QueryParams.DEFAULT);

	if (response.getValue().isEmpty()) {

		NewService service = new NewService();
		service.setAddress("localhost");
		service.setPort(8200);
		service.setId("vault");
		service.setName("vault");

		client.agentServiceRegister(service);
	}
}
 
Example 2
Source File: ServiceDefinitionToConsulRegistration.java    From camel-spring-boot with Apache License 2.0 7 votes vote down vote up
@Override
public ConsulRegistration convert(ServiceDefinition source) {
    NewService service = new NewService();
    service.setName(source.getName());
    service.setId(source.getId());
    service.setAddress(properties.getServiceRegistry().getServiceHost());
    service.setPort(source.getPort());

    service.setTags(
        source.getMetadata().entrySet().stream()
            .map(e -> e.getKey() + "=" + e.getValue())
            .collect(Collectors.toList())
    );

    return new ConsulRegistration(service, null) {
        @Override
        public boolean isSecure() {
            return service.getPort() == 443 || Objects.equals("https", source.getMetadata().get(ServiceDefinition.SERVICE_META_PROTOCOL));
        }
    };
}
 
Example 3
Source File: NacosSyncToConsulServiceImpl.java    From nacos-sync with Apache License 2.0 7 votes vote down vote up
public NewService buildSyncInstance(Instance instance, TaskDO taskDO) {
    NewService newService = new NewService();
    newService.setAddress(instance.getIp());
    newService.setPort(instance.getPort());
    newService.setName(taskDO.getServiceName());
    newService.setId(instance.getInstanceId());
    List<String> tags = Lists.newArrayList();
    tags.addAll(instance.getMetadata().entrySet().stream()
        .map(entry -> String.join("=", entry.getKey(), entry.getValue())).collect(Collectors.toList()));
    tags.add(String.join("=", SkyWalkerConstants.DEST_CLUSTERID_KEY, taskDO.getDestClusterId()));
    tags.add(String.join("=", SkyWalkerConstants.SYNC_SOURCE_KEY,
        skyWalkerCacheServices.getClusterType(taskDO.getSourceClusterId()).getCode()));
    tags.add(String.join("=", SkyWalkerConstants.SOURCE_CLUSTERID_KEY, taskDO.getSourceClusterId()));
    newService.setTags(tags);
    return newService;
}
 
Example 4
Source File: ConsulRegistry.java    From dubbo3 with Apache License 2.0 7 votes vote down vote up
@Override
protected void doRegister(URL url) {
    NewService dubboService = new NewService();
    dubboService.setTags(Arrays.asList("dubbo"));
    dubboService.setAddress(url.toFullString());
    dubboService.setPort(url.getPort());
    dubboService.setId(convertConsulSerivceId(url));
    dubboService.setName(url.getServiceInterface());
    //set health checker
    String dubboHTTPCheckURL = System.getProperty("DUBBO_HTTP_CHECK_URL");
    if (dubboHTTPCheckURL == null) {
        dubboHTTPCheckURL = System.getenv("DUBBO_HTTP_CHECK_URL");
    }
    if (dubboHTTPCheckURL != null) {
        NewService.Check check = new NewService.Check();
        check.setHttp(dubboHTTPCheckURL);
        check.setInterval("15s");
        check.setTimeout("3s");
        dubboService.setCheck(check);
    }
    consulClient.agentServiceRegister(dubboService);
}
 
Example 5
Source File: ConsulAutoRegistration.java    From spring-cloud-consul with Apache License 2.0 7 votes vote down vote up
public static ConsulAutoRegistration managementRegistration(
		AutoServiceRegistrationProperties autoServiceRegistrationProperties,
		ConsulDiscoveryProperties properties, ApplicationContext context,
		List<ConsulManagementRegistrationCustomizer> managementRegistrationCustomizers,
		HeartbeatProperties heartbeatProperties) {
	NewService management = new NewService();
	management.setId(getManagementServiceId(properties, context));
	management.setAddress(properties.getHostname());
	management
			.setName(getManagementServiceName(properties, context.getEnvironment()));
	management.setPort(getManagementPort(properties, context));
	management.setTags(properties.getManagementTags());
	management.setEnableTagOverride(properties.getManagementEnableTagOverride());
	management.setMeta(properties.getManagementMetadata());
	if (properties.isRegisterHealthCheck()) {
		management.setCheck(createCheck(getManagementPort(properties, context),
				heartbeatProperties, properties));
	}
	ConsulAutoRegistration registration = new ConsulAutoRegistration(management,
			autoServiceRegistrationProperties, properties, context,
			heartbeatProperties, managementRegistrationCustomizers);
	managementCustomize(managementRegistrationCustomizers, registration);
	return registration;
}
 
Example 6
Source File: ConsulNamingService.java    From brpc-java with Apache License 2.0 6 votes vote down vote up
private NewService getConsulNewService(RegisterInfo registerInfo) {
    NewService newService = new NewService();
    newService.setName(registerInfo.getServiceId());
    newService.setId(generateInstanceId(registerInfo));
    newService.setAddress(registerInfo.getHost());
    newService.setPort(registerInfo.getPort());
    newService.setTags(Arrays.asList(ConsulConstants.CONSUL_SERVICE_TAG));

    NewService.Check check = new NewService.Check();
    check.setTtl(this.consulInterval + "s");
    check.setDeregisterCriticalServiceAfter("3m");
    newService.setCheck(check);

    return newService;
}
 
Example 7
Source File: ConsulService.java    From saluki with Apache License 2.0 6 votes vote down vote up
public NewService getNewService() {
    NewService consulService = new NewService();
    consulService.setName(this.name);
    consulService.setId(this.id);
    consulService.setAddress(this.address);
    consulService.setPort(this.port);
    consulService.setTags(unmodifiableList(new ArrayList<>(this.tags)));
    NewService.Check check = new NewService.Check();
    check.setTtl(this.interval + "s");
    check.setDeregisterCriticalServiceAfter("3m");
    consulService.setCheck(check);
    return consulService;
}
 
Example 8
Source File: ConsulRegistry.java    From dubbox with Apache License 2.0 6 votes vote down vote up
private NewService createServiceDef(URL url, long ttl) {
	NewService newService = new NewService();
	newService.setId(toCategoryPath(url));
	newService.setName(url.getServiceInterface());
	newService.setAddress(URL.encode(url.toFullString()));
	newService.setPort(url.getPort());
	String version = makeVersionTag(url);
	newService.setTags(Collections.singletonList(version));
	int ttlInSeconds = (int) ttl / 1000;
	NewService.Check check = new NewService.Check();
	check.setTtl(ttlInSeconds + "s");
	newService.setCheck(check);
	return newService;
}
 
Example 9
Source File: GrpcConsulRegistrar.java    From grpc-spring-boot-starter with Apache License 2.0 6 votes vote down vote up
private ConsulRegistration getRegistration(GRpcServerInitializedEvent event) {
    ApplicationContext applicationContext = event.getApplicationContext();


    ConsulDiscoveryProperties consulProperties = applicationContext.getBean(ConsulDiscoveryProperties.class);
    GRpcServerProperties gRpcServerProperties = event.getApplicationContext().getBean(GRpcServerProperties.class);

    NewService grpcService = new NewService();
    grpcService.setPort(event.getServer().getPort());
    if (!consulProperties.isPreferAgentAddress()) {
        grpcService.setAddress(consulProperties.getHostname());
    }
    String appName = "grpc-" + ConsulAutoRegistration.getAppName(consulProperties, applicationContext.getEnvironment());
    grpcService.setName(ConsulAutoRegistration.normalizeForDns(appName));
    grpcService.setId("grpc-" + ConsulAutoRegistration.getInstanceId(consulProperties, applicationContext));
    grpcService.setTags(ConsulAutoRegistration.createTags(consulProperties)
            .stream()
            .filter(t->!t.startsWith("secure="))
            .collect(Collectors.toList())
    );


    if(consulProperties.isRegisterHealthCheck()) {
        GRpcConsulHealthCheck health = GRpcConsulHealthCheck.builder()
                .socketAddr(consulProperties.getHostname() + ":" + event.getServer().getPort())
                .grpcUseTlc(Objects.nonNull(gRpcServerProperties.getSecurity()))
                .interval(consulProperties.getHealthCheckInterval())
                .timeout(consulProperties.getHealthCheckTimeout())
                .build();

        health.setDeregisterCriticalServiceAfter(consulProperties.getHealthCheckCriticalTimeout());

        grpcService.setCheck(health);
    }



    return new ConsulRegistration(grpcService, consulProperties);
}
 
Example 10
Source File: ConsulDiscoveryClientDefaultQueryTagTests.java    From spring-cloud-consul with Apache License 2.0 5 votes vote down vote up
private NewService serviceForEnvironment(String env, int port) {
	NewService service = new NewService();
	service.setAddress("localhost");
	service.setId(NAME + env);
	service.setName(NAME);
	service.setPort(port);
	service.setTags(Arrays.asList(env));
	return service;
}
 
Example 11
Source File: ConsulServiceRegistryTests.java    From spring-cloud-consul with Apache License 2.0 4 votes vote down vote up
@Test
public void contextLoads() {

	assertThat(this.autoRegistration).as("autoRegistration created erroneously")
			.isNull();

	String serviceId = "myNonAutoRegisteredService";

	NewService service = new NewService();
	service.setAddress("localhost");
	service.setId("myNonAutoRegisteredService-A1");
	service.setName(serviceId);
	service.setPort(this.port);
	service.setTags(Collections.singletonList("mytag"));

	ConsulRegistration registration = new ConsulRegistration(service,
			this.properties);
	Throwable t = null;
	try {
		this.serviceRegistry.register(registration);
		assertHasInstance(serviceId);

		assertStatus(registration, "UP");

		// only works if query-passing = true
		this.serviceRegistry.setStatus(registration, "OUT_OF_SERVICE");
		assertEmptyInstances(serviceId);
		assertStatus(registration, "OUT_OF_SERVICE");

		this.serviceRegistry.setStatus(registration, "UP");
		assertHasInstance(serviceId);
	}
	catch (RuntimeException e) {
		throw e;
	}
	finally {
		this.serviceRegistry.deregister(registration);
		if (t == null) { // just deregister, test already failed
			assertEmptyInstances(serviceId);
		}
	}

}