Java Code Examples for org.springframework.cloud.consul.discovery.ConsulDiscoveryProperties

The following examples show how to use org.springframework.cloud.consul.discovery.ConsulDiscoveryProperties. These examples are extracted from open source projects. 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
public static void setCheck(NewService service,
		AutoServiceRegistrationProperties autoServiceRegistrationProperties,
		ConsulDiscoveryProperties properties, ApplicationContext context,
		HeartbeatProperties heartbeatProperties) {
	if (properties.isRegisterHealthCheck() && service.getCheck() == null) {
		Integer checkPort;
		if (shouldRegisterManagement(autoServiceRegistrationProperties, properties,
				context)) {
			checkPort = getManagementPort(properties, context);
		}
		else {
			checkPort = service.getPort();
		}
		Assert.notNull(checkPort, "checkPort may not be null");
		service.setCheck(createCheck(checkPort, heartbeatProperties, properties));
	}
}
 
Example 2
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 3
public SidecarConsulAutoRegistration(NewService service,
		AutoServiceRegistrationProperties autoServiceRegistrationProperties,
		ConsulDiscoveryProperties properties, ApplicationContext context,
		HeartbeatProperties heartbeatProperties,
		List<ConsulManagementRegistrationCustomizer> managementRegistrationCustomizers) {
	super(service, autoServiceRegistrationProperties, properties, context,
			heartbeatProperties, managementRegistrationCustomizers);
}
 
Example 4
/**
 * Update grpc port of consul tags after Conusl Registered
 * */
@EventListener
public void listenInstanceRegisteredEvent(InstanceRegisteredEvent instanceRegisteredEvent){
  if(alphaServerPort == 0){
    if(instanceRegisteredEvent.getConfig() instanceof ConsulDiscoveryProperties){
      ConsulDiscoveryProperties properties = (ConsulDiscoveryProperties)instanceRegisteredEvent.getConfig();
      this.consuleInstanceId = formatConsulInstanceId(properties.getInstanceId());
      Response<List<CatalogService>> services = consulClient.getCatalogService(serviceName,null);
      if(services.getValue() != null){
        services.getValue().stream().filter(service ->
            service.getServiceId().equalsIgnoreCase(this.consuleInstanceId)).forEach(service -> {

          NewService newservice =  new NewService();
          newservice.setName(service.getServiceName());
          newservice.setId(service.getServiceId());
          newservice.setAddress(service.getAddress());
          newservice.setPort(service.getServicePort());
          List<String> tags = service.getServiceTags();
          tags.remove("alpha-server-port=0");
          tags.add("alpha-server-port="+actualAlphaServerPort);
          newservice.setTags(tags);
          consulClient.agentServiceRegister(newservice);
        });
      }
    }
  }
}
 
Example 5
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 6
@Bean
@ConditionalOnMissingBean(ConsulAutoRegistration.class)
public ConsulAutoRegistration consulAutoRegistration(
        final MetaDataExtender metaDataExtender,
        final AutoServiceRegistrationProperties autoServiceRegistrationProperties,
        final ConsulDiscoveryProperties consulDiscoveryProperties,
        final ApplicationContext applicationContext,
        final List<ConsulRegistrationCustomizer> consulRegistrationCustomizers,
        final List<ConsulManagementRegistrationCustomizer> managementRegistrationCustomizers,
        final HeartbeatProperties heartbeatProperties) {

    metaDataExtender.extendMetaData();
    return ConsulAutoRegistration.registration(
            autoServiceRegistrationProperties,
            consulDiscoveryProperties,
            applicationContext,
            consulRegistrationCustomizers,
            managementRegistrationCustomizers,
            heartbeatProperties);
}
 
Example 7
@Deprecated
public ConsulAutoRegistration(NewService service,
		AutoServiceRegistrationProperties autoServiceRegistrationProperties,
		ConsulDiscoveryProperties properties, ApplicationContext context,
		HeartbeatProperties heartbeatProperties) {
	this(service, autoServiceRegistrationProperties, properties, context,
			heartbeatProperties, Collections.emptyList());
}
 
Example 8
public ConsulAutoRegistration(NewService service,
		AutoServiceRegistrationProperties autoServiceRegistrationProperties,
		ConsulDiscoveryProperties properties, ApplicationContext context,
		HeartbeatProperties heartbeatProperties,
		List<ConsulManagementRegistrationCustomizer> managementRegistrationCustomizers) {
	super(service, properties);
	this.autoServiceRegistrationProperties = autoServiceRegistrationProperties;
	this.context = context;
	this.heartbeatProperties = heartbeatProperties;
	this.managementRegistrationCustomizers = managementRegistrationCustomizers;
}
 
Example 9
private static Map<String, String> getMetadata(ConsulDiscoveryProperties properties) {
	LinkedHashMap<String, String> metadata = new LinkedHashMap<>();
	if (!CollectionUtils.isEmpty(properties.getMetadata())) {
		metadata.putAll(properties.getMetadata());
	}

	// add metadata from other properties. See createTags above.
	if (!StringUtils.isEmpty(properties.getInstanceZone())) {
		metadata.put(properties.getDefaultZoneMetadataName(),
				properties.getInstanceZone());
	}
	if (!StringUtils.isEmpty(properties.getInstanceGroup())) {
		metadata.put("group", properties.getInstanceGroup());
	}

	// store the secure flag in the tags so that clients will be able to figure
	// out whether to use http or https automatically
	metadata.put("secure",
			Boolean.toString(properties.getScheme().equalsIgnoreCase("https")));

	return metadata;
}
 
Example 10
public static NewService.Check createCheck(Integer port,
		HeartbeatProperties ttlConfig, ConsulDiscoveryProperties properties) {
	NewService.Check check = new NewService.Check();
	if (StringUtils.hasText(properties.getHealthCheckCriticalTimeout())) {
		check.setDeregisterCriticalServiceAfter(
				properties.getHealthCheckCriticalTimeout());
	}
	if (ttlConfig.isEnabled()) {
		// FIXME 3.0.0
		// https://github.com/spring-cloud/spring-cloud-consul/issues/614
		check.setTtl(ttlConfig.getTtl().getSeconds() + "s");
		return check;
	}

	Assert.notNull(port, "createCheck port must not be null");
	Assert.isTrue(port > 0, "createCheck port must be greater than 0");

	if (properties.getHealthCheckUrl() != null) {
		check.setHttp(properties.getHealthCheckUrl());
	}
	else {
		check.setHttp(String.format("%s://%s:%s%s", properties.getScheme(),
				properties.getHostname(), port, properties.getHealthCheckPath()));
	}
	check.setHeader(properties.getHealthCheckHeaders());
	check.setInterval(properties.getHealthCheckInterval());
	check.setTimeout(properties.getHealthCheckTimeout());
	check.setTlsSkipVerify(properties.getHealthCheckTlsSkipVerify());
	return check;
}
 
Example 11
@Bean
public ConsulAutoRegistration consulRegistration(
		AutoServiceRegistrationProperties autoServiceRegistrationProperties,
		ConsulDiscoveryProperties properties, ApplicationContext applicationContext,
		ObjectProvider<List<ConsulRegistrationCustomizer>> registrationCustomizers,
		ObjectProvider<List<ConsulManagementRegistrationCustomizer>> managementRegistrationCustomizers,
		HeartbeatProperties heartbeatProperties,
		SidecarProperties sidecarProperties) {
	return SidecarConsulAutoRegistration.registration(
			autoServiceRegistrationProperties, properties, applicationContext,
			registrationCustomizers.getIfAvailable(),
			managementRegistrationCustomizers.getIfAvailable(), heartbeatProperties,
			sidecarProperties);
}
 
Example 12
@Bean
public SidecarDiscoveryClient sidecarDiscoveryClient(
		ConsulDiscoveryProperties properties, ConsulServiceRegistry serviceRegistry,
		ConsulAutoRegistration registration) {
	return new SidecarConsulDiscoveryClient(properties, serviceRegistry,
			registration);
}
 
Example 13
public ConsulStubsRegistrar(StubRunning stubRunning, ConsulClient consulClient,
		StubMapperProperties stubMapperProperties,
		ConsulDiscoveryProperties consulDiscoveryProperties, InetUtils inetUtils) {
	this.stubRunning = stubRunning;
	this.consulClient = consulClient;
	this.stubMapperProperties = stubMapperProperties;
	this.consulDiscoveryProperties = consulDiscoveryProperties;
	this.inetUtils = inetUtils;
}
 
Example 14
@Bean(initMethod = "registerStubs")
public StubsRegistrar stubsRegistrar(StubRunning stubRunning,
		ConsulClient consulClient, StubMapperProperties stubMapperProperties,
		ConsulDiscoveryProperties consulDiscoveryProperties, InetUtils inetUtils) {
	return new ConsulStubsRegistrar(stubRunning, consulClient, stubMapperProperties,
			consulDiscoveryProperties, inetUtils);
}
 
Example 15
public static String getInstanceId(ConsulDiscoveryProperties properties,
		ApplicationContext context) {
	if (!StringUtils.hasText(properties.getInstanceId())) {
		return normalizeForDns(IdUtils.getDefaultInstanceId(context.getEnvironment(),
				properties.isIncludeHostnameInInstanceId()));
	}
	return normalizeForDns(properties.getInstanceId());
}
 
Example 16
/**
 * @param properties consul discovery properties
 * @param env Spring environment
 * @return the app name, currently the spring.application.name property
 */
public static String getAppName(ConsulDiscoveryProperties properties,
		Environment env) {
	final String appName = properties.getServiceName();
	if (StringUtils.hasText(appName)) {
		return appName;
	}
	return env.getProperty("spring.application.name", "application");
}
 
Example 17
/**
 * @param autoServiceRegistrationProperties registration properties
 * @param properties discovery properties
 * @param context Spring application context
 * @return if the management service should be registered with the
 * {@link ServiceRegistry}
 */
public static boolean shouldRegisterManagement(
		AutoServiceRegistrationProperties autoServiceRegistrationProperties,
		ConsulDiscoveryProperties properties, ApplicationContext context) {
	return autoServiceRegistrationProperties.isRegisterManagement()
			&& getManagementPort(properties, context) != null
			&& ManagementServerPortUtils.isDifferent(context);
}
 
Example 18
/**
 * @param properties discovery properties
 * @param context Spring application context
 * @return the serviceId of the Management Service
 */
public static String getManagementServiceId(ConsulDiscoveryProperties properties,
		ApplicationContext context) {
	final String instanceId = properties.getInstanceId();
	if (StringUtils.hasText(instanceId)) {
		return normalizeForDns(
				instanceId + SEPARATOR + properties.getManagementSuffix());
	}
	return normalizeForDns(
			IdUtils.getDefaultInstanceId(context.getEnvironment(), false)) + SEPARATOR
			+ properties.getManagementSuffix();
}
 
Example 19
/**
 * @param properties discovery properties
 * @param env Spring environment
 * @return the service name of the Management Service
 */
public static String getManagementServiceName(ConsulDiscoveryProperties properties,
		Environment env) {
	final String appName = properties.getServiceName();
	if (StringUtils.hasText(appName)) {
		return normalizeForDns(
				appName + SEPARATOR + properties.getManagementSuffix());
	}
	return normalizeForDns(getAppName(properties, env)) + SEPARATOR
			+ properties.getManagementSuffix();
}
 
Example 20
/**
 * @param properties discovery properties
 * @param context Spring application context
 * @return the port of the Management Service
 */
public static Integer getManagementPort(ConsulDiscoveryProperties properties,
		ApplicationContext context) {
	// If an alternate external port is specified, use it instead
	if (properties.getManagementPort() != null) {
		return properties.getManagementPort();
	}
	return ManagementServerPortUtils.getPort(context);
}
 
Example 21
public ConsulAutoServiceRegistration(ConsulServiceRegistry serviceRegistry,
		AutoServiceRegistrationProperties autoServiceRegistrationProperties,
		ConsulDiscoveryProperties properties, ConsulAutoRegistration registration) {
	super(serviceRegistry, autoServiceRegistrationProperties);
	this.properties = properties;
	this.registration = registration;
}
 
Example 22
Source Project: spring-cloud-consul   Source File: ConsulServiceRegistry.java    License: Apache License 2.0 5 votes vote down vote up
public ConsulServiceRegistry(ConsulClient client,
		ConsulDiscoveryProperties properties, TtlScheduler ttlScheduler,
		HeartbeatProperties heartbeatProperties) {
	this.client = client;
	this.properties = properties;
	this.ttlScheduler = ttlScheduler;
	this.heartbeatProperties = heartbeatProperties;
}
 
Example 23
@Bean
@ConditionalOnMissingBean
public ConsulServiceRegistry consulServiceRegistry(ConsulClient consulClient,
		ConsulDiscoveryProperties properties, HeartbeatProperties heartbeatProperties,
		@Autowired(required = false) TtlScheduler ttlScheduler) {
	return new ConsulServiceRegistry(consulClient, properties, ttlScheduler,
			heartbeatProperties);
}
 
Example 24
@Bean
@ConditionalOnMissingBean
public ConsulAutoServiceRegistration consulAutoServiceRegistration(
		ConsulServiceRegistry registry,
		AutoServiceRegistrationProperties autoServiceRegistrationProperties,
		ConsulDiscoveryProperties properties,
		ConsulAutoRegistration consulRegistration) {
	return new ConsulAutoServiceRegistration(registry,
			autoServiceRegistrationProperties, properties, consulRegistration);
}
 
Example 25
@Bean
@ConditionalOnMissingBean
public ConsulAutoRegistration consulRegistration(
		AutoServiceRegistrationProperties autoServiceRegistrationProperties,
		ConsulDiscoveryProperties properties, ApplicationContext applicationContext,
		ObjectProvider<List<ConsulRegistrationCustomizer>> registrationCustomizers,
		ObjectProvider<List<ConsulManagementRegistrationCustomizer>> managementRegistrationCustomizers,
		HeartbeatProperties heartbeatProperties) {
	return ConsulAutoRegistration.registration(autoServiceRegistrationProperties,
			properties, applicationContext, registrationCustomizers.getIfAvailable(),
			managementRegistrationCustomizers.getIfAvailable(), heartbeatProperties);
}
 
Example 26
@Bean
public CustomAutoRegistration consulAutoServiceRegistration(
		ConsulServiceRegistry serviceRegistry,
		AutoServiceRegistrationProperties autoServiceRegistrationProperties,
		ConsulDiscoveryProperties properties,
		ConsulAutoRegistration registration) {
	return new CustomAutoRegistration(serviceRegistry,
			autoServiceRegistrationProperties, properties, registration);
}
 
Example 27
@Autowired
public CustomAutoRegistration(ConsulServiceRegistry serviceRegistry,
		AutoServiceRegistrationProperties autoServiceRegistrationProperties,
		ConsulDiscoveryProperties properties,
		ConsulAutoRegistration registration) {
	super(serviceRegistry, autoServiceRegistrationProperties, properties,
			registration);
}
 
Example 28
@Bean
public ConsulDiscoveryClient consulDiscoveryClient(
		ConsulDiscoveryProperties properties) {
	ConsulDiscoveryClient client = mock(ConsulDiscoveryClient.class);
	ServiceInstance instance = new DefaultServiceInstance("configserver1",
			"configserver", properties.getHostname(), properties.getPort(), false);
	given(client.getInstances("configserver")).willReturn(Arrays.asList(instance));
	return client;
}
 
Example 29
@Test
public void offByDefault() {
	this.context = new AnnotationConfigApplicationContext(
			ConsulConfigServerAutoConfiguration.class);
	assertThat(
			this.context.getBeanNamesForType(ConsulDiscoveryProperties.class).length)
					.isEqualTo(0);
}
 
Example 30
@Test
public void onWhenRequested() {
	setup("spring.cloud.config.server.prefix=/config",
			"spring.cloud.consul.discovery.tags-as-metadata=false");
	assertThat(
			this.context.getBeanNamesForType(ConsulDiscoveryProperties.class).length)
					.isEqualTo(1);
	ConsulDiscoveryProperties properties = this.context
			.getBean(ConsulDiscoveryProperties.class);
	assertThat(properties.getMetadata()).containsEntry("configPath", "/config");
}