private void NotifyServiceChange(ServiceChangeListener listener, ServiceChangeEvent serviceChangeEvent) { if (listener == null || serviceChangeEvent == null) { return; } try { _serviceChangeNotifies.Enqueue(() => { try { listener.OnChange(serviceChangeEvent); } catch (Exception e) { _log.Error("execute service change listener failed", e); } }); } catch (Exception ex) { _log.Error("enqueue service change notification failed.", ex); } }
public virtual void Update(InstanceChange instanceChange) { try { string changeType = instanceChange.ChangeType; Instance instance = instanceChange.Instance; if (string.IsNullOrWhiteSpace(changeType) || instance == null) { return; } ServiceContext currentContext = _services[instance.ServiceId.ToLower()]; if (currentContext == null) { return; } bool updated = false; if (InstanceChange.CHANGE_TYPE.DELETE.Equals(changeType)) { updated = currentContext.DeleteInstance(instance); } else if (InstanceChange.CHANGE_TYPE.NEW.Equals(changeType)) { updated = currentContext.AddInstance(instance); } else if (InstanceChange.CHANGE_TYPE.CHANGE.Equals(changeType)) { updated = currentContext.UpdateInstance(instance); } else { _log.Info("unexpected changeType" + changeType); } if (updated) { ServiceChangeEvent serviceChangeEvent = currentContext.newServiceChangeEvent(changeType); foreach (ServiceChangeListener listener in currentContext.GetListeners()) { NotifyServiceChange(listener, serviceChangeEvent); } _log.Info("Operation:" + changeType + "\nInstance: " + instance); } Metric(changeType, updated, instance); } catch (Exception e) { _log.Warn("update service instance failed", e); } }
public void OnChange(ServiceChangeEvent serviceChangeEvent) { serviceChangeEvents.Add(serviceChangeEvent); if (InstanceChange.CHANGE_TYPE.DELETE.Equals(serviceChangeEvent.ChangeType)) { deleteCount.IncrementAndGet(); } if (InstanceChange.CHANGE_TYPE.NEW.Equals(serviceChangeEvent.ChangeType)) { addCount.IncrementAndGet(); } }
public virtual void Update(Service service) { try { if (service == null) { return; } string serviceId = service.ServiceId; if (serviceId == null) { return; } ServiceContext currentContext = _services[serviceId.ToLower()]; if (currentContext == null) { return; } currentContext.SetService(service); ServiceChangeEvent serviceChangeEvent = currentContext.newServiceChangeEvent(InstanceChange.CHANGE_TYPE.RELOAD); foreach (ServiceChangeListener listener in currentContext.GetListeners()) { NotifyServiceChange(listener, serviceChangeEvent); } _log.Info("Operation:" + serviceChangeEvent.ChangeType + "\n" + "service: " + service.ToJson()); Metric(serviceChangeEvent.ChangeType, true, service); } catch (Exception e) { _log.Warn("update service instance failed", e); } }
public void OnChange(ServiceChangeEvent serviceChangeEvent) { _serviceChangeEvents.Add(serviceChangeEvent); }
public void TestUpdate_Instance() { for (int i = 0; i < 5; i++) { DiscoveryConfig discoveryConfig = Constants.NewDiscoveryConfig(); string serviceId = discoveryConfig.ServiceId; List <DefaultServiceChangeListener> listeners = new List <DefaultServiceChangeListener>() { new DefaultServiceChangeListener(), new DefaultServiceChangeListener(), new DefaultServiceChangeListener(), new DefaultServiceChangeListener() }; foreach (DefaultServiceChangeListener listener in listeners) { _serviceRepository.RegisterServiceChangeListener(discoveryConfig, listener); } Assert.IsTrue(_serviceRepository.ContainsService(serviceId)); string[] changeTypes = new string[] { InstanceChange.CHANGE_TYPE.DELETE, InstanceChange.CHANGE_TYPE.NEW, InstanceChange.CHANGE_TYPE.NEW, InstanceChange.CHANGE_TYPE.NEW, InstanceChange.CHANGE_TYPE.DELETE, InstanceChange.CHANGE_TYPE.DELETE, InstanceChange.CHANGE_TYPE.CHANGE, InstanceChange.CHANGE_TYPE.CHANGE }; Instance context = Constants.NewInstance(serviceId); Instance changeContext = Constants.NewInstance(serviceId); foreach (string changeType in changeTypes) { if (InstanceChange.CHANGE_TYPE.CHANGE.Equals(changeType)) { _serviceRepository.Update(Constants.newInstanceChange(changeType, changeContext)); } else { _serviceRepository.Update(Constants.newInstanceChange(changeType, context)); } } Threads.Sleep(100); foreach (DefaultServiceChangeListener listener in listeners) { Assert.AreEqual(6, listener.ServiceChangeEvents.Count); ServiceChangeEvent add1 = listener.ServiceChangeEvents[0]; ServiceChangeEvent add2 = listener.ServiceChangeEvents[1]; ServiceChangeEvent add3 = listener.ServiceChangeEvents[2]; ServiceChangeEvent delete = listener.ServiceChangeEvents[3]; ServiceChangeEvent change1 = listener.ServiceChangeEvents[4]; ServiceChangeEvent change2 = listener.ServiceChangeEvents[5]; Assert.AreEqual(add1.ChangeType, InstanceChange.CHANGE_TYPE.NEW); Assert.AreEqual(add2.ChangeType, InstanceChange.CHANGE_TYPE.NEW); Assert.AreEqual(add3.ChangeType, InstanceChange.CHANGE_TYPE.NEW); Assert.AreEqual(delete.ChangeType, InstanceChange.CHANGE_TYPE.DELETE); Assert.AreEqual(change1.ChangeType, InstanceChange.CHANGE_TYPE.CHANGE); Assert.AreEqual(change1.ChangeType, InstanceChange.CHANGE_TYPE.CHANGE); Assert.IsTrue(new HashSet <Instance>(add1.ChangedService.Instances).Contains(context)); Assert.IsTrue(new HashSet <Instance>(add2.ChangedService.Instances).Contains(context)); Assert.IsTrue(new HashSet <Instance>(add2.ChangedService.Instances).Contains(context)); Assert.IsTrue(delete.ChangedService.Instances.Count == 0); Assert.IsTrue(new HashSet <Instance>(change1.ChangedService.Instances).Contains(changeContext)); Assert.IsTrue(new HashSet <Instance>(change1.ChangedService.Instances).Contains(changeContext)); } } }