@Override public ServiceRegistryExecutionPlan registerServiceRegistry(final ServiceRegistry registry) { LOGGER.trace("Registering service registry [{}] into the execution plan", registry.getName()); serviceRegistries.add(registry); return this; }
/** * Return number of records held in this service registry. Provides default implementation so that implementations * needed this new functionality could override it and other implementations not caring for it could be left alone. * * @return number of registered services held by any particular implementation * @since 5.0.0 */ default long size() { return load().size(); }
@Override public boolean delete(final RegisteredService registeredService) { return serviceRegistries.stream() .map(registry -> registry.delete(registeredService)) .filter(Boolean::booleanValue) .findAny() .orElse(Boolean.FALSE); }
/** * Init service registry if necessary. */ @SuppressFBWarnings("PRMC_POSSIBLY_REDUNDANT_METHOD_CALLS") public void initServiceRegistryIfNecessary() { val size = this.serviceRegistry.size(); LOGGER.trace("Service registry contains [{}] service definition(s)", size); LOGGER.warn("Service registry [{}] will be auto-initialized from JSON service definitions. " + "This behavior is only useful for testing purposes and MAY NOT be appropriate for production. " + "Consider turning off this behavior via the setting [cas.serviceRegistry.initFromJson=false] " + "and explicitly register definitions in the services registry.", this.serviceRegistry.getName()); val servicesLoaded = this.jsonServiceRegistry.load(); LOGGER.debug("Loaded JSON services are [{}]", servicesLoaded.stream().map(RegisteredService::getName).collect(Collectors.joining(","))); servicesLoaded .forEach(r -> { if (!findExistingMatchForService(r)) { LOGGER.debug("Initializing service registry with the [{}] JSON service definition...", r.getName()); this.serviceRegistry.save(r); } }); this.servicesManager.load(); LOGGER.info("Service registry [{}] contains [{}] service definitions", this.serviceRegistry.getName(), this.servicesManager.count()); }
/** * Load services that are provided by the DAO. */ @Override public Collection<RegisteredService> load() { LOGGER.trace("Loading services from [{}]", serviceRegistry.getName()); this.services = this.serviceRegistry.load() .stream() .collect(Collectors.toConcurrentMap(r -> { LOGGER.debug("Adding registered service [{}]", r.getServiceId()); return r.getId(); }, Function.identity(), (r, s) -> s == null ? r : s)); loadInternal(); publishEvent(new CasRegisteredServicesLoadedEvent(this, getAllServices())); evaluateExpiredServiceDefinitions(); LOGGER.info("Loaded [{}] service(s) from [{}].", this.services.size(), this.serviceRegistry.getName()); return services.values(); }
private boolean findExistingMatchForService(final RegisteredService r) { if (StringUtils.isNotBlank(r.getServiceId())) { val match = this.serviceRegistry.findServiceById(r.getServiceId()); if (match != null) { LOGGER.warn("Skipping [{}] JSON service definition as a matching service [{}] is found in the registry", r.getName(), match.getName()); return true; } val match2 = this.serviceRegistry.findServiceByExactServiceId(r.getServiceId()); if (match2 != null) { LOGGER.warn("Skipping [{}] JSON service definition as a matching service [{}] is found in the registry", r.getName(), match2.getName()); return true; } } val match = this.serviceRegistry.findServiceById(r.getId()); if (match != null) { LOGGER.warn("Skipping [{}] JSON service definition as a matching id [{}] is found in the registry", r.getName(), match.getId()); return true; } return false; } }
LOGGER.debug("Service found in the cache [{}] is marked as a deleted service. CAS will update the service registry " + "of this CAS node to remove the local service, if found", value); serviceRegistry.delete(value); this.distributedCacheManager.remove(value, item); return service; + "and will update the service registry of this CAS node with the cache entry for future look-ups", value); if (properties.getReplicationMode() == StreamingServiceRegistryProperties.ReplicationModes.ACTIVE_ACTIVE) { serviceRegistry.save(value); serviceRegistry.save(value);
@Override public RegisteredService save(final RegisteredService registeredService) { serviceRegistries.forEach(registry -> registry.save(registeredService)); return registeredService; }
@Override public RegisteredService findServiceById(final String id) { return serviceRegistries.stream() .map(registry -> registry.findServiceById(id)) .filter(Objects::nonNull) .findFirst() .orElse(null); }
@Override public RegisteredService findServiceByExactServiceId(final String id) { return serviceRegistries.stream() .map(registry -> registry.findServiceByExactServiceId(id)) .filter(Objects::nonNull) .findFirst() .orElse(null); }
@Override public RegisteredService findServiceByExactServiceName(final String name) { return serviceRegistries.stream() .map(registry -> registry.findServiceByExactServiceName(name)) .filter(Objects::nonNull) .findFirst() .orElse(null); }
private void updateServiceRegistryWithRegisteredService(final List<RegisteredService> services, final RegisteredService cachedService, final ServiceRegistry serviceRegistry) { if (properties.getReplicationMode() == StreamingServiceRegistryProperties.ReplicationModes.ACTIVE_ACTIVE) { serviceRegistry.save(cachedService); } services.add(cachedService); }
@Override public RegisteredService findServiceById(final long id) { return serviceRegistries.stream() .map(registry -> registry.findServiceById(id)) .filter(Objects::nonNull) .findFirst() .orElse(null); }
/** * Find a service by an exact match of the service name. * * @param name the name * @return the registered service */ default RegisteredService findServiceByExactServiceName(final String name) { return load() .stream() .filter(r -> r.getName().equals(name)) .findFirst() .orElse(null); }
@Bean public ServiceRegistryInitializer serviceRegistryInitializer() { val serviceRegistryInstance = serviceRegistry.getIfAvailable(); val initializer = new ServiceRegistryInitializer(embeddedJsonServiceRegistry(), serviceRegistryInstance, servicesManager.getIfAvailable()); LOGGER.info("Attempting to initialize the service registry [{}] from service definition resources found at [{}]", serviceRegistryInstance.getName(), getServiceRegistryInitializerServicesDirectoryResource()); initializer.initServiceRegistryIfNecessary(); return initializer; }
@Override public List<RegisteredService> updateLoadedRegisteredServicesFromCache(final List<RegisteredService> services, final ServiceRegistry serviceRegistry) { val cachedServices = this.distributedCacheManager.getAll(); for (val entry : cachedServices) { val cachedService = entry.getValue(); LOGGER.debug("Found cached service definition [{}] in the replication cache [{}]", cachedService, distributedCacheManager.getName()); if (isRegisteredServiceMarkedAsDeletedInCache(entry)) { LOGGER.debug("Service found in the cache [{}] is marked as a deleted service. CAS will update the service registry " + "of this CAS node to remove the local service, if found.", cachedService); serviceRegistry.delete(cachedService); this.distributedCacheManager.remove(cachedService, entry); continue; } val matchingService = services.stream() .filter(s -> s.getId() == cachedService.getId()) .findFirst() .orElse(null); if (matchingService != null) { updateServiceRegistryWithMatchingService(services, cachedService, matchingService, serviceRegistry); } else { updateServiceRegistryWithNoMatchingService(services, cachedService, serviceRegistry); } } return services; }
@Audit(action = "SAVE_SERVICE", actionResolverName = "SAVE_SERVICE_ACTION_RESOLVER", resourceResolverName = "SAVE_SERVICE_RESOURCE_RESOLVER") @Override public synchronized RegisteredService save(final RegisteredService registeredService, final boolean publishEvent) { publishEvent(new CasRegisteredServicePreSaveEvent(this, registeredService)); val r = this.serviceRegistry.save(registeredService); this.services.put(r.getId(), r); saveInternal(registeredService); if (publishEvent) { publishEvent(new CasRegisteredServiceSavedEvent(this, r)); } return r; }
/** * Find a service by an exact match of the service id. * * @param id the id * @return the registered service */ default RegisteredService findServiceByExactServiceId(final String id) { return load() .stream() .filter(r -> StringUtils.isNotBlank(r.getServiceId()) && r.getServiceId().equals(id)) .findFirst() .orElse(null); }
@Audit(action = "DELETE_SERVICE", actionResolverName = "DELETE_SERVICE_ACTION_RESOLVER", resourceResolverName = "DELETE_SERVICE_RESOURCE_RESOLVER") @Override public synchronized RegisteredService delete(final RegisteredService service) { if (service != null) { publishEvent(new CasRegisteredServicePreDeleteEvent(this, service)); this.serviceRegistry.delete(service); this.services.remove(service.getId()); deleteInternal(service); publishEvent(new CasRegisteredServiceDeletedEvent(this, service)); } return service; }