org.springframework.cloud.client.serviceregistry.Registration Java Examples

The following examples show how to use org.springframework.cloud.client.serviceregistry.Registration. 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: CamelSpringCloudServiceRegistry.java    From camel-spring-boot with Apache License 2.0 6 votes vote down vote up
@Override
public void deregister(ServiceDefinition definition) {
    synchronized (this) {
        if (definitions.stream().noneMatch(d -> matchById(d, definition))) {
            LOGGER.debug("Deregister service with definition: {} with registrations: {}", definition, registrationType);
            
            // compute registration from definition
            Registration result = convertServiceDefinition(definition);

            serviceRegistry.deregister(result);
        }

        // remove any instance with the same id
        definitions.removeIf(d -> matchById(d, definition));
    }
}
 
Example #2
Source File: CamelSpringCloudServiceRegistry.java    From camel-spring-boot with Apache License 2.0 6 votes vote down vote up
@Override
public void register(ServiceDefinition definition) {
    synchronized (this) {
        // keep track of registered definition to remove them upon registry
        // shutdown
        if (definitions.stream().noneMatch(d -> matchById(d, definition))) {
            LOGGER.debug("Register service with definition: {} with registrations: {}", definition, registrationType);

            // compute registration from definition
            Registration result = convertServiceDefinition(definition);

            serviceRegistry.register(result);

            definitions.add(definition);
        }
    }
}
 
Example #3
Source File: NacosServiceRegistry.java    From spring-cloud-alibaba with Apache License 2.0 6 votes vote down vote up
@Override
public void deregister(Registration registration) {

	log.info("De-registering from Nacos Server now...");

	if (StringUtils.isEmpty(registration.getServiceId())) {
		log.warn("No dom to de-register for nacos client...");
		return;
	}

	NamingService namingService = namingService();
	String serviceId = registration.getServiceId();
	String group = nacosDiscoveryProperties.getGroup();

	try {
		namingService.deregisterInstance(serviceId, group, registration.getHost(),
				registration.getPort(), nacosDiscoveryProperties.getClusterName());
	}
	catch (Exception e) {
		log.error("ERR_NACOS_DEREGISTER, de-register failed...{},",
				registration.toString(), e);
	}

	log.info("De-registration finished.");
}
 
Example #4
Source File: NacosServiceRegistry.java    From spring-cloud-alibaba with Apache License 2.0 6 votes vote down vote up
@Override
public Object getStatus(Registration registration) {

	String serviceName = registration.getServiceId();
	try {
		List<Instance> instances = nacosDiscoveryProperties.namingServiceInstance()
				.getAllInstances(serviceName);
		for (Instance instance : instances) {
			if (instance.getIp().equalsIgnoreCase(nacosDiscoveryProperties.getIp())
					&& instance.getPort() == nacosDiscoveryProperties.getPort()) {
				return instance.isEnabled() ? "UP" : "DOWN";
			}
		}
	}
	catch (Exception e) {
		log.error("get all instance of {} error,", serviceName, e);
	}
	return null;
}
 
Example #5
Source File: NacosServiceRegistry.java    From spring-cloud-alibaba with Apache License 2.0 5 votes vote down vote up
@Override
public void register(Registration registration) {

	if (StringUtils.isEmpty(registration.getServiceId())) {
		log.warn("No service to register for nacos client...");
		return;
	}

	NamingService namingService = namingService();
	String serviceId = registration.getServiceId();
	String group = nacosDiscoveryProperties.getGroup();

	Instance instance = getNacosInstanceFromRegistration(registration);

	try {
		namingService.registerInstance(serviceId, group, instance);
		log.info("nacos registry, {} {} {}:{} register finished", group, serviceId,
				instance.getIp(), instance.getPort());
	}
	catch (Exception e) {
		log.error("nacos registry, {} register failed...{},", serviceId,
				registration.toString(), e);
		// rethrow a RuntimeException if the registration is failed.
		// issue : https://github.com/alibaba/spring-cloud-alibaba/issues/1132
		rethrowRuntimeException(e);
	}
}
 
Example #6
Source File: NacosServiceRegistry.java    From spring-cloud-alibaba with Apache License 2.0 5 votes vote down vote up
@Override
public void setStatus(Registration registration, String status) {

	if (!status.equalsIgnoreCase("UP") && !status.equalsIgnoreCase("DOWN")) {
		log.warn("can't support status {},please choose UP or DOWN", status);
		return;
	}

	String serviceId = registration.getServiceId();

	Instance instance = getNacosInstanceFromRegistration(registration);

	if (status.equalsIgnoreCase("DOWN")) {
		instance.setEnabled(false);
	}
	else {
		instance.setEnabled(true);
	}

	try {
		nacosDiscoveryProperties.namingMaintainServiceInstance()
				.updateInstance(serviceId, instance);
	}
	catch (Exception e) {
		throw new RuntimeException("update nacos instance status fail", e);
	}

}
 
Example #7
Source File: NacosServiceRegistry.java    From spring-cloud-alibaba with Apache License 2.0 5 votes vote down vote up
private Instance getNacosInstanceFromRegistration(Registration registration) {
	Instance instance = new Instance();
	instance.setIp(registration.getHost());
	instance.setPort(registration.getPort());
	instance.setWeight(nacosDiscoveryProperties.getWeight());
	instance.setClusterName(nacosDiscoveryProperties.getClusterName());
	instance.setEnabled(nacosDiscoveryProperties.isInstanceEnabled());
	instance.setMetadata(registration.getMetadata());

	return instance;
}
 
Example #8
Source File: DubboServiceRegistrationAutoConfiguration.java    From spring-cloud-alibaba with Apache License 2.0 5 votes vote down vote up
/**
 * Handle the pre-registered event of {@link ServiceInstance} for Consul.
 * @param event {@link ServiceInstancePreRegisteredEvent}
 */
@EventListener(ServiceInstancePreRegisteredEvent.class)
public void onServiceInstancePreRegistered(
		ServiceInstancePreRegisteredEvent event) {
	Registration registration = event.getSource();
	Class<?> registrationClass = AopUtils.getTargetClass(registration);
	String registrationClassName = registrationClass.getName();
	if (CONSUL_AUTO_SERVICE_AUTO_REGISTRATION_CLASS_NAME
			.equalsIgnoreCase(registrationClassName)) {
		ConsulRegistration consulRegistration = (ConsulRegistration) registration;
		attachURLsIntoMetadata(consulRegistration);
	}
}
 
Example #9
Source File: DubboServiceRegistrationAutoConfiguration.java    From spring-cloud-alibaba with Apache License 2.0 5 votes vote down vote up
@EventListener(ServiceInstancePreRegisteredEvent.class)
public void onServiceInstancePreRegistered(
		ServiceInstancePreRegisteredEvent event) {
	Registration registration = event.getSource();
	EurekaRegistration eurekaRegistration = EurekaRegistration.class
			.cast(registration);
	InstanceInfo instanceInfo = eurekaRegistration.getApplicationInfoManager()
			.getInfo();
	attachDubboMetadataServiceMetadata(instanceInfo.getMetadata());
}
 
Example #10
Source File: DubboServiceRegistrationAutoConfiguration.java    From spring-cloud-alibaba with Apache License 2.0 5 votes vote down vote up
private void attachDubboMetadataServiceMetadata(Registration registration) {
	if (registration == null) {
		return;
	}
	synchronized (registration) {
		Map<String, String> metadata = registration.getMetadata();
		attachDubboMetadataServiceMetadata(metadata);
	}
}
 
Example #11
Source File: DubboServiceRegistrationNonWebApplicationAutoConfiguration.java    From spring-cloud-alibaba with Apache License 2.0 5 votes vote down vote up
/**
 * Handle the pre-registered event of {@link ServiceInstance} for Consul.
 * @param event {@link ServiceInstancePreRegisteredEvent}
 */
@EventListener(ServiceInstancePreRegisteredEvent.class)
public void onServiceInstancePreRegistered(
		ServiceInstancePreRegisteredEvent event) {
	Registration registration = event.getSource();
	ConsulAutoRegistration consulRegistration = (ConsulAutoRegistration) registration;
	setPort(consulRegistration);
}
 
Example #12
Source File: DefaultEndpointLocator.java    From spring-cloud-sleuth with Apache License 2.0 5 votes vote down vote up
DefaultEndpointLocator(Registration registration, ServerProperties serverProperties,
		Environment environment, ZipkinProperties zipkinProperties,
		InetUtils inetUtils) {
	this.registration = registration;
	this.serverProperties = serverProperties;
	this.environment = environment;
	this.zipkinProperties = zipkinProperties;
	this.firstNonLoopbackAddress = findFirstNonLoopbackAddress(inetUtils);
}
 
Example #13
Source File: DefaultEndpointLocatorConfigurationTest.java    From spring-cloud-sleuth with Apache License 2.0 5 votes vote down vote up
@Bean
public Registration getRegistration() {
	return new Registration() {
		@Override
		public String getServiceId() {
			return "from-registration";
		}

		@Override
		public String getHost() {
			return null;
		}

		@Override
		public int getPort() {
			return 0;
		}

		@Override
		public boolean isSecure() {
			return false;
		}

		@Override
		public URI getUri() {
			return null;
		}

		@Override
		public Map<String, String> getMetadata() {
			return null;
		}
	};
}
 
Example #14
Source File: LazyInstanceLocalInfoInitiralizer.java    From spring-cloud-gray with Apache License 2.0 5 votes vote down vote up
protected InstanceLocalInfo createInstanceLocalInfo() {
    Registration registration = applicationContext.getBean(Registration.class);
    return InstanceLocalInfo.builder()
            .instanceId(getLocalInstanceId())
            .serviceId(registration.getServiceId())
            .host(registration.getHost())
            .port(registration.getPort())
            .build();
}
 
Example #15
Source File: DynamicRouteConfiguration.java    From pig with MIT License 5 votes vote down vote up
public DynamicRouteConfiguration(Registration registration, DiscoveryClient discovery,
                                 ZuulProperties zuulProperties, ServerProperties server, RedisTemplate redisTemplate) {
    this.registration = registration;
    this.discovery = discovery;
    this.zuulProperties = zuulProperties;
    this.server = server;
    this.redisTemplate = redisTemplate;
}
 
Example #16
Source File: DynamicRouteConfiguration.java    From Taroco with Apache License 2.0 5 votes vote down vote up
public DynamicRouteConfiguration(Registration registration, DiscoveryClient discovery,
                                 ZuulProperties zuulProperties, ServerProperties server, TarocoRedisRepository redisRepository) {
    this.registration = registration;
    this.discovery = discovery;
    this.zuulProperties = zuulProperties;
    this.server = server;
    this.redisRepository = redisRepository;
}
 
Example #17
Source File: ServiceRegistryEndpointTests.java    From spring-cloud-commons with Apache License 2.0 5 votes vote down vote up
@Bean
Registration registration() {
	return new Registration() {
		@Override
		public String getInstanceId() {
			return "testRegistrationInstance1";
		}

		@Override
		public String getServiceId() {
			return "testRegistration1";
		}

		@Override
		public String getHost() {
			return null;
		}

		@Override
		public int getPort() {
			return 0;
		}

		@Override
		public boolean isSecure() {
			return false;
		}

		@Override
		public URI getUri() {
			return null;
		}

		@Override
		public Map<String, String> getMetadata() {
			return null;
		}
	};
}
 
Example #18
Source File: CamelSpringCloudServiceRegistry.java    From camel-spring-boot with Apache License 2.0 5 votes vote down vote up
private Registration convertServiceDefinition(ServiceDefinition definition) {
    for (int i = 0; i < conversionServices.size(); i++) {
        ConversionService cs = conversionServices.get(i);

        if (cs.canConvert(ServiceDefinition.class, registrationType)) {
            return cs.convert(definition, registrationType);
        }
    }

    throw new IllegalStateException("Unable to convert service definition to native registration of type:" + registrationType);
}
 
Example #19
Source File: CamelSpringCloudServiceRegistry.java    From camel-spring-boot with Apache License 2.0 5 votes vote down vote up
/**
 * Determine the native registration type. This is needed because the registry
 * specific implementation provided by spring-cloud-xyz does not handle generic
 * Registration object but needs a Registration specific to the underlying
 * technology used.
 *
 * @return the registration type
 */
private Class<? extends Registration> determineRegistrationType(String methodName) {
    Class<? extends Registration> type = null;
    Method[] methods = serviceRegistry.getClass().getDeclaredMethods();

    for (Method method: methods) {
        if (!methodName.equals(method.getName())) {
            continue;
        }

        if (method.getParameterCount() != 1) {
            continue;
        }

        Class<?> parameterType = method.getParameterTypes()[0];
        if (Registration.class.isAssignableFrom(parameterType)) {
            if (type == null) {
                type = (Class<? extends Registration>)parameterType;
            } else {
                if (type.isAssignableFrom(parameterType)) {
                    type = (Class<? extends Registration>)parameterType;
                }
            }
        }
    }

    return type != null ? type : Registration.class;
}
 
Example #20
Source File: HazelCastConfigration.java    From sctalk with Apache License 2.0 4 votes vote down vote up
@Autowired(required = false)
public void setRegistration(Registration registration) {
    this.registration = registration;
}
 
Example #21
Source File: TxlcnZoneAvoidanceRule.java    From tx-lcn with Apache License 2.0 4 votes vote down vote up
TxlcnZoneAvoidanceRule(Registration registration) {
    this.registration = registration;
}
 
Example #22
Source File: ServiceRegistryEndpoint.java    From spring-cloud-commons with Apache License 2.0 4 votes vote down vote up
public void setRegistration(Registration registration) {
	this.registration = registration;
}
 
Example #23
Source File: LoadbalancerConfiguration.java    From tx-lcn with Apache License 2.0 4 votes vote down vote up
@Bean
@ConditionalOnProperty(name = "tx-lcn.ribbon.loadbalancer.dtx.enabled", matchIfMissing = true)
@Scope("prototype")
public IRule ribbonRule(Registration registration) {
    return new TxlcnZoneAvoidanceRule(registration);
}
 
Example #24
Source File: ServiceRegistryEndpointTests.java    From spring-cloud-commons with Apache License 2.0 4 votes vote down vote up
@Override
public void setStatus(Registration registration, String status) {
	this.updatedStatus.compareAndSet(null, status);
}
 
Example #25
Source File: ServiceRegistryEndpointTests.java    From spring-cloud-commons with Apache License 2.0 4 votes vote down vote up
@Override
public Object getStatus(Registration registration) {
	return MYSTATUS;
}
 
Example #26
Source File: NacosAutoServiceRegistration.java    From spring-cloud-alibaba with Apache License 2.0 4 votes vote down vote up
public NacosAutoServiceRegistration(ServiceRegistry<Registration> serviceRegistry,
		AutoServiceRegistrationProperties autoServiceRegistrationProperties,
		NacosRegistration registration) {
	super(serviceRegistry, autoServiceRegistrationProperties);
	this.registration = registration;
}
 
Example #27
Source File: CacheConfiguration.java    From tutorials with MIT License 4 votes vote down vote up
@Autowired(required = false)
public void setRegistration(Registration registration) {
    this.registration = registration;
}
 
Example #28
Source File: CacheConfiguration.java    From tutorials with MIT License 4 votes vote down vote up
@Autowired(required = false)
public void setRegistration(Registration registration) {
    this.registration = registration;
}
 
Example #29
Source File: CacheConfiguration.java    From tutorials with MIT License 4 votes vote down vote up
@Autowired(required = false)
public void setRegistration(Registration registration) {
    this.registration = registration;
}
 
Example #30
Source File: CacheConfiguration.java    From okta-jhipster-microservices-oauth-example with Apache License 2.0 4 votes vote down vote up
@Autowired(required = false)
public void setRegistration(Registration registration) {
    this.registration = registration;
}