diff --git a/joylive-core/joylive-core-api/src/main/java/com/jd/live/agent/core/bootstrap/AgentLifecycle.java b/joylive-core/joylive-core-api/src/main/java/com/jd/live/agent/core/bootstrap/AgentLifecycle.java index ed1b8c26a..58a51db05 100644 --- a/joylive-core/joylive-core-api/src/main/java/com/jd/live/agent/core/bootstrap/AgentLifecycle.java +++ b/joylive-core/joylive-core-api/src/main/java/com/jd/live/agent/core/bootstrap/AgentLifecycle.java @@ -16,7 +16,6 @@ package com.jd.live.agent.core.bootstrap; import java.util.Map; -import java.util.concurrent.Callable; /** * Defines the lifecycle operations for an agent component within a system. @@ -54,13 +53,4 @@ public interface AgentLifecycle { */ void execute(String command, Map args); - /** - * Adds a hook that will be executed when the system is ready. - * This can be used to perform initialization tasks or other operations - * that should occur once the system has completed its startup process. - * - * @param callable the hook to be executed when the system is ready - * @param classLoader execute in this classloader - */ - void addReadyHook(Callable callable, ClassLoader classLoader); } diff --git a/joylive-core/joylive-core-api/src/main/java/com/jd/live/agent/core/bootstrap/AppListenerSupplier.java b/joylive-core/joylive-core-api/src/main/java/com/jd/live/agent/core/bootstrap/AppListenerSupplier.java new file mode 100644 index 000000000..7e8993925 --- /dev/null +++ b/joylive-core/joylive-core-api/src/main/java/com/jd/live/agent/core/bootstrap/AppListenerSupplier.java @@ -0,0 +1,29 @@ +/* + * Copyright © ${year} ${owner} (${email}) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.jd.live.agent.core.bootstrap; + +/** + * An interface that supplies an AppListener instance. + */ +public interface AppListenerSupplier { + + /** + * Returns an AppListener instance. + * + * @return an AppListener instance + */ + AppListener getAppListener(); +} diff --git a/joylive-core/joylive-core-api/src/main/java/com/jd/live/agent/core/event/Publisher.java b/joylive-core/joylive-core-api/src/main/java/com/jd/live/agent/core/event/Publisher.java index cbe39fa46..b7068313e 100644 --- a/joylive-core/joylive-core-api/src/main/java/com/jd/live/agent/core/event/Publisher.java +++ b/joylive-core/joylive-core-api/src/main/java/com/jd/live/agent/core/event/Publisher.java @@ -49,6 +49,11 @@ public interface Publisher { */ String POLICY_SUBSCRIBER = "policy-subscriber"; + /** + * Topic identifier for endpoint events. + */ + String ENDPOINT = "endpoint"; + /** * Retrieves the topic associated with this publisher. * diff --git a/joylive-core/joylive-core-api/src/main/java/com/jd/live/agent/core/util/CollectionUtils.java b/joylive-core/joylive-core-api/src/main/java/com/jd/live/agent/core/util/CollectionUtils.java index 2cfc17d40..c94408f39 100644 --- a/joylive-core/joylive-core-api/src/main/java/com/jd/live/agent/core/util/CollectionUtils.java +++ b/joylive-core/joylive-core-api/src/main/java/com/jd/live/agent/core/util/CollectionUtils.java @@ -162,7 +162,10 @@ public static List toList(Iterable iterable, Function functio } List result = iterable instanceof Collection ? new ArrayList<>(((Collection) iterable).size()) : new ArrayList<>(); for (T t : iterable) { - result.add(function.apply(t)); + V apply = function.apply(t); + if (apply != null) { + result.add(apply); + } } return result; } diff --git a/joylive-core/joylive-core-framework/src/main/java/com/jd/live/agent/core/bootstrap/Bootstrap.java b/joylive-core/joylive-core-framework/src/main/java/com/jd/live/agent/core/bootstrap/Bootstrap.java index cdbf7a88d..f3c4216a8 100644 --- a/joylive-core/joylive-core-framework/src/main/java/com/jd/live/agent/core/bootstrap/Bootstrap.java +++ b/joylive-core/joylive-core-framework/src/main/java/com/jd/live/agent/core/bootstrap/Bootstrap.java @@ -79,11 +79,8 @@ import java.io.FileReader; import java.io.Reader; import java.lang.instrument.Instrumentation; -import java.lang.reflect.InvocationTargetException; import java.security.CodeSource; import java.util.*; -import java.util.concurrent.Callable; -import java.util.concurrent.CopyOnWriteArrayList; import static com.jd.live.agent.core.extension.condition.ConditionMatcher.DEPEND_ON_LOADER; @@ -223,8 +220,6 @@ public class Bootstrap implements AgentLifecycle { */ private List sourceSuppliers; - private final List> readies = new CopyOnWriteArrayList<>(); - private Shutdown shutdown; /** @@ -365,21 +360,6 @@ public void execute(String command, Map args) { } } - @Override - public void addReadyHook(Callable callable, ClassLoader classLoader) { - if (callable != null) { - readies.add(() -> { - ClassLoader old = Thread.currentThread().getContextClassLoader(); - Thread.currentThread().setContextClassLoader(classLoader); - try { - return callable.call(); - } finally { - Thread.currentThread().setContextClassLoader(old); - } - }); - } - } - private void printExtensions() { StringBuilder builder = new StringBuilder(); extensionManager.forEach(extensible -> { @@ -576,7 +556,14 @@ private void setupServiceManager() { } private List createApplicationListeners() { - return extensionManager.getOrLoadExtensible(AppListener.class, classLoaderManager.getCoreImplLoader()).getExtensions(); + List extensions = extensionManager.getOrLoadExtensible(AppListener.class, classLoaderManager.getCoreImplLoader()).getExtensions(); + List result = new ArrayList<>(extensions); + serviceManager.service(service -> { + if (service instanceof AppListenerSupplier) { + result.add(((AppListenerSupplier) service).getAppListener()); + } + }); + return result; } private PluginSupervisor createPluginManager() { @@ -715,7 +702,6 @@ private void onAgentEvent(AgentEvent event) { application.setStatus(AppStatus.STARTED); break; case APPLICATION_READY: - onReady(); application.setStatus(AppStatus.READY); break; case APPLICATION_STOP: @@ -724,24 +710,6 @@ private void onAgentEvent(AgentEvent event) { } } - /** - * Executes all registered ready hooks. - * Runs each runnable and logs exceptions if any occur during execution. - */ - private void onReady() { - // Some framework does not support multi thread to registration - for (Callable runnable : readies) { - try { - runnable.call(); - } catch (Throwable e) { - Throwable cause = e instanceof InvocationTargetException ? e.getCause() : null; - cause = cause != null ? cause : e; - onException(cause.getMessage(), cause); - } - readies.clear(); - } - } - /** * Logs an exception and optionally shuts down the application if not in dynamic mode. * diff --git a/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/config/ServiceConfig.java b/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/config/ServiceConfig.java index 19dd647ff..489587dae 100644 --- a/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/config/ServiceConfig.java +++ b/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/config/ServiceConfig.java @@ -167,5 +167,15 @@ public boolean isSystem(String path) { return path != null && systemPaths != null && !systemPaths.isEmpty() && systemPathTrie.match(path, PathMatchType.PREFIX) != null; } + + /** + * Returns the group associated with the specified service. + * + * @param service the name of the service + * @return the group associated with the service, or null if the service is not found or if the service groups map is null + */ + public String getGroup(String service) { + return serviceGroups == null || service == null ? null : serviceGroups.get(service); + } } diff --git a/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/interceptor/AbstractRegistryInterceptor.java b/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/interceptor/AbstractRegistryInterceptor.java index 2642c9841..c39f2dae9 100644 --- a/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/interceptor/AbstractRegistryInterceptor.java +++ b/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/interceptor/AbstractRegistryInterceptor.java @@ -17,9 +17,6 @@ import com.jd.live.agent.bootstrap.bytekit.context.ExecutableContext; import com.jd.live.agent.bootstrap.bytekit.context.MethodContext; -import com.jd.live.agent.bootstrap.logger.Logger; -import com.jd.live.agent.bootstrap.logger.LoggerFactory; -import com.jd.live.agent.core.bootstrap.AgentLifecycle; import com.jd.live.agent.core.instance.AppStatus; import com.jd.live.agent.core.instance.Application; import com.jd.live.agent.core.plugin.definition.InterceptorAdaptor; @@ -31,17 +28,12 @@ */ public abstract class AbstractRegistryInterceptor extends InterceptorAdaptor { - private final Logger logger = LoggerFactory.getLogger(AbstractRegistryInterceptor.class); - protected final Application application; - protected final AgentLifecycle lifecycle; - protected final Registry registry; - public AbstractRegistryInterceptor(Application application, AgentLifecycle lifecycle, Registry registry) { + public AbstractRegistryInterceptor(Application application, Registry registry) { this.application = application; - this.lifecycle = lifecycle; this.registry = registry; } @@ -51,12 +43,10 @@ public void onEnter(ExecutableContext ctx) { if (application.getStatus() == AppStatus.STARTING) { ServiceInstance instance = getInstance(mc); if (instance != null) { - logger.info("Delay registration until application is ready, service=" + instance.getService()); - lifecycle.addReadyHook(() -> { - logger.info("Register when application is ready, service=" + instance.getService()); - registry.register(instance); - return mc.invokeOrigin(); - }, ctx.getType().getClassLoader()); + registry.register(instance, () -> { + mc.invokeOrigin(); + return null; + }); mc.setSkip(true); } } diff --git a/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/policy/PolicyManager.java b/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/policy/PolicyManager.java index d6c99c4a0..26801963b 100644 --- a/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/policy/PolicyManager.java +++ b/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/policy/PolicyManager.java @@ -79,11 +79,11 @@ public class PolicyManager implements PolicySupervisor, InjectSourceSupplier, Ex private final AtomicReference policy = new AtomicReference<>(); - private final Map subscribers = new ConcurrentHashMap<>(); + private final Map subscriptions = new ConcurrentHashMap<>(); @Getter @Inject(Publisher.POLICY_SUBSCRIBER) - private Publisher policyPublisher; + private Publisher policyPublisher; @Getter @Inject(Publisher.SYSTEM) @@ -252,27 +252,21 @@ public CompletableFuture subscribe(String namespace, String service) { return CompletableFuture.completedFuture(null); } namespace = namespace == null || namespace.isEmpty() ? application.getService().getNamespace() : namespace; - PolicySubscriber subscriber = new PolicySubscriber(service, namespace, TYPE_SERVICE_SPACE, serviceSyncers); + PolicySubscription subscriber = new PolicySubscription(service, namespace, TYPE_SERVICE_SPACE, serviceSyncers); subscribe(subscriber); return subscriber.getFuture(); } @Override - public boolean isDone(String name) { - PolicySubscriber subscriber = name == null ? null : subscribers.get(name); - return subscriber != null && subscriber.isDone(); - } - - @Override - public List getSubscribers() { - return Collections.unmodifiableList(new ArrayList<>(subscribers.values())); + public List getSubscriptions() { + return Collections.unmodifiableList(new ArrayList<>(subscriptions.values())); } @Override public void waitReady() { - if (!subscribers.isEmpty()) { - List> futures = new ArrayList<>(subscribers.size()); - subscribers.forEach((k, v) -> futures.add(v.getFuture())); + if (!subscriptions.isEmpty()) { + List> futures = new ArrayList<>(subscriptions.size()); + subscriptions.forEach((k, v) -> futures.add(v.getFuture())); try { Futures.allOf(futures).get(governanceConfig.getInitializeTimeout(), TimeUnit.MILLISECONDS); systemPublisher.offer(AgentEvent.onServicePolicyReady("Application service policies are ready.")); @@ -398,18 +392,18 @@ private void warmup() { warmups.add(name); } if (!warmups.isEmpty()) { - warmups.forEach(o -> subscribe(new PolicySubscriber(o, namespace, TYPE_SERVICE_SPACE, serviceSyncers))); + warmups.forEach(o -> subscribe(new PolicySubscription(o, namespace, TYPE_SERVICE_SPACE, serviceSyncers))); } } } /** - * Subscribes a {@link PolicySubscriber} to the policy publisher. + * Subscribes a {@link PolicySubscription} to the policy publisher. * - * @param subscriber The {@link PolicySubscriber} to be subscribed. + * @param subscriber The {@link PolicySubscription} to be subscribed. */ - protected void subscribe(PolicySubscriber subscriber) { - PolicySubscriber exist = subscribers.putIfAbsent(subscriber.getName(), subscriber); + protected void subscribe(PolicySubscription subscriber) { + PolicySubscription exist = subscriptions.putIfAbsent(subscriber.getName(), subscriber); if (exist == null) { policyPublisher.offer(subscriber); } else { diff --git a/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/policy/PolicySubscriber.java b/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/policy/PolicySubscription.java similarity index 92% rename from joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/policy/PolicySubscriber.java rename to joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/policy/PolicySubscription.java index 90a5af8c9..18f0b7fe9 100644 --- a/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/policy/PolicySubscriber.java +++ b/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/policy/PolicySubscription.java @@ -32,7 +32,7 @@ * of the policy subscription process. */ @Getter -public class PolicySubscriber implements ServiceName { +public class PolicySubscription implements ServiceName { private final String name; @@ -54,7 +54,7 @@ public class PolicySubscriber implements ServiceName { * @param type The type of the subscriber. * @param owners The owner of the subscriber. */ - public PolicySubscriber(String name, String namespace, String type, List owners) { + public PolicySubscription(String name, String namespace, String type, List owners) { this.name = name; this.namespace = namespace; this.type = type; @@ -103,16 +103,6 @@ public boolean complete() { return false; } - /** - * Checks if the associated future has completed successfully. - * - * @return {@code true} if the future is done and has not completed exceptionally, - * otherwise {@code false} - */ - public boolean isDone() { - return future.isDone() && !future.isCompletedExceptionally(); - } - /** * Marks the subscription process as complete with an exception. This method completes the associated future * exceptionally, indicating that the subscription process has finished due to an error. diff --git a/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/policy/PolicySupervisor.java b/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/policy/PolicySupervisor.java index 2ba0c6ded..543539b84 100644 --- a/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/policy/PolicySupervisor.java +++ b/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/policy/PolicySupervisor.java @@ -63,11 +63,11 @@ default boolean update(Function updater) { } /** - * Retrieves a list of policy subscribers. + * Retrieves a list of policy subscription. * - * @return A list of {@link PolicySubscriber} who are subscribed to policy updates. + * @return A list of {@link PolicySubscription} who are subscribed to policy updates. */ - List getSubscribers(); + List getSubscriptions(); /** * Waits until the application is ready. diff --git a/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/policy/PolicySupplier.java b/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/policy/PolicySupplier.java index c9d26388b..1d1ce53c2 100644 --- a/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/policy/PolicySupplier.java +++ b/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/policy/PolicySupplier.java @@ -51,7 +51,6 @@ default CompletableFuture subscribe(String service) { return subscribe(null, service); } - /** * Subscribes a specific service policy based on its name. * @@ -60,14 +59,5 @@ default CompletableFuture subscribe(String service) { * @return A {@link CompletableFuture} that completes when the subscription is successful. */ CompletableFuture subscribe(String namespace, String service); - - /** - * Checks if the task associated with the given name has completed successfully. - * - * @param name the name of the task to check - * @return {@code true} if the task with the specified name is done and has not completed exceptionally, - * otherwise {@code false} - */ - boolean isDone(String name); } diff --git a/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/registry/EndpointEvent.java b/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/registry/EndpointEvent.java new file mode 100644 index 000000000..199831b00 --- /dev/null +++ b/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/registry/EndpointEvent.java @@ -0,0 +1,35 @@ +/* + * Copyright © ${year} ${owner} (${email}) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.jd.live.agent.governance.registry; + +import com.jd.live.agent.governance.instance.Endpoint; +import lombok.AllArgsConstructor; +import lombok.Getter; + +import java.util.List; + +@Getter +@AllArgsConstructor +public class EndpointEvent { + + private String service; + + private List endpoints; + + private List adds; + + private List removes; +} diff --git a/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/registry/LiveRegistry.java b/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/registry/LiveRegistry.java index 6c5b6c836..b2f477684 100644 --- a/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/registry/LiveRegistry.java +++ b/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/registry/LiveRegistry.java @@ -15,10 +15,11 @@ */ package com.jd.live.agent.governance.registry; -import com.jd.live.agent.core.event.AgentEvent; -import com.jd.live.agent.core.event.AgentEvent.EventType; -import com.jd.live.agent.core.event.EventHandler; -import com.jd.live.agent.core.event.EventHandler.EventProcessor; +import com.jd.live.agent.bootstrap.logger.Logger; +import com.jd.live.agent.bootstrap.logger.LoggerFactory; +import com.jd.live.agent.core.bootstrap.AppContext; +import com.jd.live.agent.core.bootstrap.AppListener; +import com.jd.live.agent.core.bootstrap.AppListenerSupplier; import com.jd.live.agent.core.event.Publisher; import com.jd.live.agent.core.extension.annotation.Extension; import com.jd.live.agent.core.inject.InjectSource; @@ -28,11 +29,21 @@ import com.jd.live.agent.core.service.AbstractService; import com.jd.live.agent.core.util.time.Timer; import com.jd.live.agent.governance.config.RegistryConfig; +import com.jd.live.agent.governance.instance.Endpoint; +import com.jd.live.agent.governance.policy.PolicySupplier; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; import java.util.Map; +import java.util.concurrent.Callable; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.function.Consumer; + +import static com.jd.live.agent.governance.registry.RegistryEvent.*; /** * {@code LiveRegistry} is an implementation of {@link Registry} that manages the registration and unregistration @@ -45,13 +56,12 @@ */ @Extension("LiveRegistry") @Injectable -public class LiveRegistry extends AbstractService implements Registry, InjectSourceSupplier { +public class LiveRegistry extends AbstractService implements RegistrySupervisor, InjectSourceSupplier, AppListenerSupplier { - @Inject(Publisher.REGISTRY) - private Publisher registryPublisher; + private static final Logger logger = LoggerFactory.getLogger(LiveRegistry.class); - @Inject(Publisher.SYSTEM) - private Publisher systemPublisher; + @Inject(Publisher.REGISTRY) + private Publisher publisher; @Inject(RegistryConfig.COMPONENT_REGISTRY_CONFIG) private RegistryConfig registryConfig; @@ -59,34 +69,53 @@ public class LiveRegistry extends AbstractService implements Registry, InjectSou @Inject(Timer.COMPONENT_TIMER) private Timer timer; - private final EventHandler readyHandler = (EventProcessor) this::onAgentEvent; + @Inject(PolicySupplier.COMPONENT_POLICY_SUPPLIER) + private PolicySupplier policySupplier; private final Map registrations = new ConcurrentHashMap<>(); + private final Map subscriptions = new ConcurrentHashMap<>(); + private final AtomicBoolean ready = new AtomicBoolean(false); @Override protected CompletableFuture doStart() { - systemPublisher.addHandler(readyHandler); return CompletableFuture.completedFuture(null); } @Override protected CompletableFuture doStop() { ready.set(false); - systemPublisher.removeHandler(readyHandler); + onApplicationStop(); return CompletableFuture.completedFuture(null); } @Override - public void register(ServiceInstance instance) { + public AppListener getAppListener() { + return new AppListener.AppListenerAdapter() { + + @Override + public void onReady(AppContext context) { + onApplicationReady(); + } + + public void onStop(AppContext context) { + onApplicationStop(); + } + }; + } + + @Override + public void register(ServiceInstance instance, Callable callback) { if (instance != null) { - Registration registration = new Registration(instance); - Registration old = registrations.putIfAbsent(instance.getService(), registration); - if (old == null) { - if (ready.get()) { - doRegister(registration); - } + String service = instance.getService(); + Registration registration = registrations.computeIfAbsent(service, + name -> new Registration(instance, callback, publisher, registryConfig, timer)); + if (ready.get()) { + // delay register + registration.register(); + } else { + logger.info("Delay registration until application is ready, service=" + service); } } } @@ -95,108 +124,282 @@ public void register(ServiceInstance instance) { public void unregister(ServiceInstance instance) { if (instance != null) { Registration registration = registrations.remove(instance.getService()); - doUnregister(registration); + if (registration != null) { + registration.unregister(); + } + } + } + + @Override + public CompletableFuture subscribe(String service) { + return policySupplier.subscribe(service); + } + + @Override + public void subscribe(String service, Consumer consumer) { + if (service != null && !service.isEmpty() && consumer != null) { + Subscription subscription = subscriptions.computeIfAbsent(service, Subscription::new); + subscription.addConsumer(consumer); } } + @Override + public void update(String service, List endpoints) { + if (service != null && !service.isEmpty()) { + Subscription subscription = subscriptions.get(service); + if (subscription != null) { + subscription.update(endpoints); + } + } + } + + @Override + public boolean isSubscribed(String service) { + return service != null && !service.isEmpty() && subscriptions.containsKey(service); + } + /** - * Handles {@link AgentEvent}s to update the readiness state of the registry. - * - * @param event the agent event to handle. + * Called when the application is ready to start. This method iterates through all registered services and calls their register method. */ - private void onAgentEvent(AgentEvent event) { - if (event.getType() == EventType.AGENT_READY) { - ready.set(true); - onReady(); - } else { - ready.set(false); + private void onApplicationReady() { + ready.set(true); + for (Registration registration : registrations.values()) { + registration.register(); } } /** - * Called when the registry becomes ready to register all pending registrations. + * Called when the application is stopping. This method iterates through all registered services and calls their stop method. */ - private void onReady() { + private void onApplicationStop() { + ready.set(false); for (Registration registration : registrations.values()) { - doRegister(registration); + registration.stop(); } } + @Override + public void apply(InjectSource source) { + source.add(Registry.COMPONENT_REGISTRY, this); + } + /** - * Performs the actual registration of a {@link Registration}. - * - * @param registration the registration to perform. + * A private static class that represents a registration of a service instance with the registry. */ - private void doRegister(Registration registration) { - if (registration != null) { - synchronized (registration) { - if (ready.get() && !registration.registered) { - registryPublisher.offer(RegistryEvent.register(registration.instance)); - registration.registered = true; - addHeartbeat(registration); + private static class Registration { + + /** + * The service to which the instance belongs. + */ + private final String service; + + /** + * The service instance being registered. + */ + private final ServiceInstance instance; + + /** + * A callback function that will be called when the registration is successful. + */ + private final Callable callback; + + /** + * The publisher to which registry events will be sent. + */ + private final Publisher publisher; + + /** + * The configuration for the registry. + */ + private final RegistryConfig registryConfig; + + /** + * A timer used to schedule heartbeat and registration delays. + */ + private final Timer timer; + + /** + * An atomic boolean indicating whether the registration has been started. + */ + private final AtomicBoolean started = new AtomicBoolean(true); + + /** + * An atomic boolean indicating whether the registration has been completed. + */ + private final AtomicBoolean registered = new AtomicBoolean(false); + + /** + * Creates a new registration object. + * + * @param instance the service instance being registered + * @param callback a callback function that will be called when the registration is successful + * @param publisher the publisher to which registry events will be sent + * @param registryConfig the configuration for the registry + * @param timer a timer used to schedule heartbeat and registration delays + */ + Registration(ServiceInstance instance, + Callable callback, + Publisher publisher, + RegistryConfig registryConfig, + Timer timer) { + this.service = instance.getService(); + this.instance = instance; + this.callback = callback; + this.publisher = publisher; + this.registryConfig = registryConfig; + this.timer = timer; + } + + /** + * Registers the service instance with the registry. + */ + public void register() { + if (registered.compareAndSet(false, true)) { + if (callback != null) { + doRegister(); } } } - } - private void doUnregister(Registration registration) { - if (registration != null) { - synchronized (registration) { - if (registration.registered) { - registryPublisher.offer(RegistryEvent.unregister(registration.instance)); - } + /** + * Unregisters the service instance from the registry. + */ + public void unregister() { + if (registered.compareAndSet(true, false)) { + doUnregister(); } } - } - /** - * Performs the actual unregistration of a {@link Registration}. - * - * @param registration the registration to perform. - */ - private void addHeartbeat(Registration registration) { - if (registration != null) { + /** + * Stops the registration process. + */ + public void stop() { + started.set(false); + unregister(); + } + + /** + * Delays the next heartbeat by a random amount of time. + */ + private void delayHeartbeat() { long delay = registryConfig.getHeartbeatInterval() + (long) (Math.random() * 2000.0); - timer.delay("registration-" + registration.getService(), delay, () -> doHeartbeat(registration)); + timer.delay("heartbeat-" + service, delay, this::doHeartbeat); } - } - /** - * Adds a heartbeat task for the given {@link Registration}. - * - * @param registration the registration to add a heartbeat for. - */ - private void doHeartbeat(Registration registration) { - if (registration != null) { - synchronized (registration) { - if (ready.get() && registrations.containsKey(registration.getService())) { - registryPublisher.offer(RegistryEvent.heartbeat(registration.instance)); - addHeartbeat(registration); + /** + * Delays the register process by a random amount of time. + */ + private void delayRegister() { + long delay = 1000 + (long) (Math.random() * 2000.0); + timer.delay("register-" + service, delay, this::doRegister); + } + + /** + * Performs the actual register of the service instance. + */ + private void doRegister() { + if (started.get()) { + logger.info("Register when application is ready, service=" + service); + try { + callback.call(); + publisher.offer(ofRegister(instance)); + delayHeartbeat(); + } catch (Exception e) { + logger.error("Register error, service=" + service + ", caused by " + e.getMessage(), e); + delayRegister(); } } } - } - @Override - public void apply(InjectSource source) { - source.add(Registry.COMPONENT_REGISTRY, this); + /** + * Performs the actual heartbeat for the service instance. + */ + private void doHeartbeat() { + if (started.get()) { + publisher.offer(ofHeartbeat(instance)); + delayHeartbeat(); + } + } + + /** + * Performs the actual unregister of the service instance. + */ + private void doUnregister() { + publisher.offer(ofUnregister(instance)); + } } /** - * Represents a registration of a {@link ServiceInstance}. + * A private static class that represents a subscription to endpoint events for a specific service group. */ - private static class Registration { + private static class Subscription { - protected boolean registered; + /** + * The service group that this subscription is for. + */ + private final String service; - protected ServiceInstance instance; + /** + * The consumer that will receive endpoint events. + */ + private final List> consumers = new CopyOnWriteArrayList<>(); - Registration(ServiceInstance instance) { - this.instance = instance; + /** + * A map of endpoints for the service group, keyed by their addresses. + */ + private Map endpoints; + + /** + * Creates a new subscription for the specified service. + * + * @param service the service to subscribe to + */ + Subscription(String service) { + this.service = service; + } + + /** + * Adds a new consumer to the list of consumers that will receive endpoint events. + * + * @param consumer the consumer to add + */ + public synchronized void addConsumer(Consumer consumer) { + if (consumer != null && !consumers.contains(consumer)) { + consumers.add(consumer); + } } - public String getService() { - return instance.getService(); + /** + * Updates the endpoints for the service group and notifies the consumer of any changes. + * + * @param endpoints the new list of endpoints for the service group + */ + public synchronized void update(List endpoints) { + Map newEndpoints = new HashMap<>(); + Map oldEndpoints = this.endpoints; + List adds = new ArrayList<>(); + List removes = new ArrayList<>(); + if (endpoints != null) { + for (Endpoint endpoint : endpoints) { + String address = endpoint.getAddress(); + if (oldEndpoints == null || !oldEndpoints.containsKey(address)) { + adds.add(endpoint); + } + newEndpoints.put(address, endpoint); + } + } + if (oldEndpoints != null) { + oldEndpoints.forEach((k, v) -> { + if (!newEndpoints.containsKey(k)) { + removes.add(v); + } + }); + } + this.endpoints = newEndpoints; + logger.info("Service instance is changed, service=" + service + ", adds=" + adds.size() + ", removes=" + removes.size()); + for (Consumer consumer : consumers) { + consumer.accept(new EndpointEvent(service, endpoints, adds, removes)); + } } } } diff --git a/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/registry/Registry.java b/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/registry/Registry.java index d01ec02bb..ad9402d95 100644 --- a/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/registry/Registry.java +++ b/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/registry/Registry.java @@ -15,8 +15,12 @@ */ package com.jd.live.agent.governance.registry; +import java.util.concurrent.Callable; +import java.util.concurrent.CompletableFuture; +import java.util.function.Consumer; + /** - * Interface for a registry . + * An interface that defines the methods for registering and unregistering service instances with a registry. */ public interface Registry { @@ -30,7 +34,17 @@ public interface Registry { * * @param instance the service instance to be registered */ - void register(ServiceInstance instance); + default void register(ServiceInstance instance) { + register(instance, null); + } + + /** + * Registers a service instance with the registry. + * + * @param instance the service instance to be registered + * @param callback a callback function that will be called if the registration is successful + */ + void register(ServiceInstance instance, Callable callback); /** * Unregisters a service instance from the registry. @@ -38,5 +52,21 @@ public interface Registry { * @param instance the service instance to be unregistered */ void unregister(ServiceInstance instance); + + /** + * Subscribes a specific service policy based on its name. + * + * @param service The service name of the policy to subscribe to. + * @return A {@link CompletableFuture} that completes when the subscription is successful. + */ + CompletableFuture subscribe(String service); + + /** + * Subscribes to endpoint events for a specific service. + * + * @param service the service name to subscribe to + * @param consumer the consumer that will receive endpoint events + */ + void subscribe(String service, Consumer consumer); } diff --git a/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/registry/RegistryEvent.java b/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/registry/RegistryEvent.java index b39a37020..5ef674919 100644 --- a/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/registry/RegistryEvent.java +++ b/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/registry/RegistryEvent.java @@ -34,15 +34,15 @@ public class RegistryEvent implements Serializable { private ServiceInstance instance; - public static RegistryEvent register(ServiceInstance instance) { + public static RegistryEvent ofRegister(ServiceInstance instance) { return new RegistryEvent(EventType.REGISTER, instance); } - public static RegistryEvent unregister(ServiceInstance instance) { + public static RegistryEvent ofUnregister(ServiceInstance instance) { return new RegistryEvent(EventType.UNREGISTER, instance); } - public static RegistryEvent heartbeat(ServiceInstance instance) { + public static RegistryEvent ofHeartbeat(ServiceInstance instance) { return new RegistryEvent(EventType.HEARTBEAT, instance); } diff --git a/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/registry/RegistrySupervisor.java b/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/registry/RegistrySupervisor.java new file mode 100644 index 000000000..4dfb5ecdb --- /dev/null +++ b/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/registry/RegistrySupervisor.java @@ -0,0 +1,42 @@ +/* + * Copyright © ${year} ${owner} (${email}) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.jd.live.agent.governance.registry; + +import com.jd.live.agent.governance.instance.Endpoint; + +import java.util.List; + +/** + * An interface that defines the method for updating a service group's endpoints in the registry. + */ +public interface RegistrySupervisor extends Registry { + + /** + * Updates the endpoints for a specific service group in the registry. + * + * @param service the service + * @param endpoints the new list of endpoints for the service group + */ + void update(String service, List endpoints); + + /** + * Checks if the registry is subscribed to a specific service. + * + * @param service the service + * @return true if the registry is subscribed to the service, false otherwise + */ + boolean isSubscribed(String service); +} diff --git a/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/service/sync/AbstractServiceSyncer.java b/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/service/sync/AbstractServiceSyncer.java index 247fff567..eae2c43aa 100644 --- a/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/service/sync/AbstractServiceSyncer.java +++ b/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/service/sync/AbstractServiceSyncer.java @@ -31,7 +31,7 @@ import com.jd.live.agent.core.util.Close; import com.jd.live.agent.core.util.Waiter; import com.jd.live.agent.core.util.template.Template; -import com.jd.live.agent.governance.policy.PolicySubscriber; +import com.jd.live.agent.governance.policy.PolicySubscription; import com.jd.live.agent.governance.policy.PolicySupervisor; import com.jd.live.agent.governance.subscription.policy.listener.ServiceEvent; import com.jd.live.agent.governance.policy.service.Service; @@ -61,15 +61,15 @@ public abstract class AbstractServiceSyncer extends Abstra protected PolicySupervisor policySupervisor; @Inject(Publisher.POLICY_SUBSCRIBER) - protected Publisher publisher; + protected Publisher publisher; protected ExecutorService executorService; - protected final Queue subscribers = new ConcurrentLinkedQueue<>(); + protected final Queue subscribers = new ConcurrentLinkedQueue<>(); protected final Waiter.MutexWaiter waiter = new Waiter.MutexWaiter(); - protected final EventHandler handler = this::onEvent; + protected final EventHandler handler = this::onEvent; @Override public String getType() { @@ -85,9 +85,9 @@ protected void startSync() throws Exception { for (int i = 0; i < concurrency; i++) { executorService.submit(() -> { while (isStarted()) { - PolicySubscriber subscriber = subscribers.poll(); - if (subscriber != null) { - syncAndUpdate(subscriber); + PolicySubscription subscription = subscribers.poll(); + if (subscription != null) { + syncAndUpdate(subscription); } else { try { waiter.await(1000, TimeUnit.MILLISECONDS); @@ -97,7 +97,7 @@ protected void startSync() throws Exception { } }); } - addTasks(policySupervisor.getSubscribers()); + addTasks(policySupervisor.getSubscriptions()); } @Override @@ -129,7 +129,7 @@ protected Template createTemplate() { * @param subscriber The PolicySubscriber for which to create the subscription. * @return A new Subscription object for synchronizing Service objects. */ - protected Subscription createSubscription(PolicySubscriber subscriber) { + protected Subscription createSubscription(PolicySubscription subscriber) { Subscription result = new Subscription<>(getName(), createServiceKey(subscriber)); result.setListener(r -> onResponse(result, r)); return result; @@ -141,14 +141,14 @@ protected Subscription createSubscription(PolicySubscriber subscribe * @param subscriber The PolicySubscriber for which to create the ServiceKey object. * @return A new ServiceKey object representing the PolicySubscriber. */ - protected abstract K createServiceKey(PolicySubscriber subscriber); + protected abstract K createServiceKey(PolicySubscription subscriber); /** * Synchronizes and updates the service based on the given subscriber. * * @param subscriber the policy subscriber to synchronize and update. */ - protected void syncAndUpdate(PolicySubscriber subscriber) { + protected void syncAndUpdate(PolicySubscription subscriber) { Subscription subscription = subscriptions.computeIfAbsent(subscriber.getUniqueName(), name -> createSubscription(subscriber)); if (subscription.lock()) { @@ -199,7 +199,7 @@ protected void onSuccess(Subscription subscription, Service service) if (service == null) { onNotFound(subscription); } else { - PolicySubscriber subscriber = subscription.getKey().getSubscriber(); + PolicySubscription subscriber = subscription.getKey().getSubscriber(); if (update(subscriber.getName(), service)) { subscription.setVersion(service.getVersion()); subscriber.complete(subscription.getOwner()); @@ -214,7 +214,7 @@ protected void onSuccess(Subscription subscription, Service service) * @param subscription The subscription for which an error occurred. */ protected void onNotModified(Subscription subscription) { - PolicySubscriber subscriber = subscription.getKey().getSubscriber(); + PolicySubscription subscriber = subscription.getKey().getSubscriber(); subscriber.complete(getName()); if (subscription.shouldPrint()) { logger.info(subscription.getSuccessMessage(SyncStatus.NOT_MODIFIED)); @@ -227,7 +227,7 @@ protected void onNotModified(Subscription subscription) { * @param subscription The subscription for which an error occurred. */ protected void onNotFound(Subscription subscription) { - PolicySubscriber subscriber = subscription.getKey().getSubscriber(); + PolicySubscription subscriber = subscription.getKey().getSubscriber(); if (subscription.getVersion() > 0) { if (update(subscriber.getName(), null)) { // Retry from version 0 after data is recovered. @@ -251,7 +251,7 @@ protected void onNotFound(Subscription subscription) { */ protected void onError(Subscription subscription, String error, Throwable e) { e = e != null ? e : new SyncException(error); - PolicySubscriber subscriber = subscription.getKey().getSubscriber(); + PolicySubscription subscriber = subscription.getKey().getSubscriber(); if (subscriber.completeExceptionally(e)) { logger.error(subscription.getErrorMessage(SyncStatus.ERROR, error), e); } else if (subscription.shouldPrint()) { @@ -295,7 +295,7 @@ protected boolean update(String name, Service service) { * * @param events the list of events containing policy subscribers. */ - protected void onEvent(List> events) { + protected void onEvent(List> events) { events.forEach(e -> addTask(e.getData(), t -> !subscriptions.containsKey(t.getName()))); } @@ -304,7 +304,7 @@ protected void onEvent(List> events) { * * @param tasks the list of policy subscriber tasks to be added. */ - protected void addTasks(List tasks) { + protected void addTasks(List tasks) { if (tasks != null) { tasks.forEach(task -> addTask(task, t -> !subscriptions.containsKey(t.getName()))); } @@ -315,7 +315,7 @@ protected void addTasks(List tasks) { * * @param task the policy subscriber task to be added. */ - protected void addTask(PolicySubscriber task) { + protected void addTask(PolicySubscription task) { addTask(task, null); } @@ -325,7 +325,7 @@ protected void addTask(PolicySubscriber task) { * @param task the policy subscriber task to be added. * @param predicate an optional predicate to test the task before adding it to the queue. */ - protected void addTask(PolicySubscriber task, Predicate predicate) { + protected void addTask(PolicySubscription task, Predicate predicate) { if (task != null && PolicyWatcher.TYPE_SERVICE_SPACE.equals(task.getType()) && isStarted() diff --git a/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/service/sync/SyncKey.java b/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/service/sync/SyncKey.java index 584297f36..fe0760fb1 100644 --- a/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/service/sync/SyncKey.java +++ b/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/service/sync/SyncKey.java @@ -15,7 +15,7 @@ */ package com.jd.live.agent.governance.service.sync; -import com.jd.live.agent.governance.policy.PolicySubscriber; +import com.jd.live.agent.governance.policy.PolicySubscription; import com.jd.live.agent.governance.policy.service.ServiceName; import com.jd.live.agent.governance.service.sync.http.HttpResource; import lombok.Getter; @@ -38,9 +38,9 @@ interface HttpSyncKey extends SyncKey, HttpResource { @Getter class ServiceKey implements SyncKey, ServiceName { - protected final PolicySubscriber subscriber; + protected final PolicySubscription subscriber; - public ServiceKey(PolicySubscriber subscriber) { + public ServiceKey(PolicySubscription subscriber) { this.subscriber = subscriber; } diff --git a/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/subscription/policy/listener/ServiceListener.java b/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/subscription/policy/listener/ServiceListener.java index 6227cdee6..4ebf96b89 100644 --- a/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/subscription/policy/listener/ServiceListener.java +++ b/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/subscription/policy/listener/ServiceListener.java @@ -22,7 +22,7 @@ import com.jd.live.agent.core.event.Publisher; import com.jd.live.agent.core.parser.ObjectParser; import com.jd.live.agent.governance.policy.GovernancePolicy; -import com.jd.live.agent.governance.policy.PolicySubscriber; +import com.jd.live.agent.governance.policy.PolicySubscription; import com.jd.live.agent.governance.policy.PolicySupervisor; import com.jd.live.agent.governance.policy.service.Service; @@ -34,7 +34,7 @@ */ public class ServiceListener extends AbstractListener { - private final Map subscribers = new ConcurrentHashMap<>(); + private final Map subscribers = new ConcurrentHashMap<>(); private Set loadedServices = new HashSet<>(); @@ -44,10 +44,10 @@ public class ServiceListener extends AbstractListener { public ServiceListener(PolicySupervisor supervisor, ObjectParser parser, - Publisher publisher) { + Publisher publisher) { super(Service.class, supervisor, parser); publisher.addHandler(this::onEvent); - supervisor.getSubscribers().forEach(this::subscribe); + supervisor.getSubscriptions().forEach(this::subscribe); } @Override @@ -126,7 +126,7 @@ protected synchronized void onSuccess(PolicyEvent event) { * * @param events A list of events containing policy subscribers to subscribe to. */ - private void onEvent(List> events) { + private void onEvent(List> events) { events.forEach(e -> subscribe(e.getData())); } @@ -135,10 +135,10 @@ private void onEvent(List> events) { * * @param subscriber The policy subscriber to subscribe to. */ - private void subscribe(PolicySubscriber subscriber) { + private void subscribe(PolicySubscription subscriber) { if (subscriber != null && PolicyWatcher.TYPE_SERVICE_SPACE.equals(subscriber.getType())) { String name = subscriber.getName(); - PolicySubscriber old = subscribers.putIfAbsent(name, subscriber); + PolicySubscription old = subscribers.putIfAbsent(name, subscriber); if (old != null && old != subscriber) { old.trigger((v, t) -> { if (t == null) { diff --git a/joylive-implement/joylive-service/joylive-service-microservice/src/main/java/com/jd/live/agent/implement/service/policy/microservice/ServiceHttpSyncer.java b/joylive-implement/joylive-service/joylive-service-microservice/src/main/java/com/jd/live/agent/implement/service/policy/microservice/ServiceHttpSyncer.java index 5e3d0f986..f57d0a30d 100644 --- a/joylive-implement/joylive-service/joylive-service-microservice/src/main/java/com/jd/live/agent/implement/service/policy/microservice/ServiceHttpSyncer.java +++ b/joylive-implement/joylive-service/joylive-service-microservice/src/main/java/com/jd/live/agent/implement/service/policy/microservice/ServiceHttpSyncer.java @@ -21,7 +21,7 @@ import com.jd.live.agent.core.inject.annotation.Config; import com.jd.live.agent.core.inject.annotation.Injectable; import com.jd.live.agent.governance.config.GovernanceConfig; -import com.jd.live.agent.governance.policy.PolicySubscriber; +import com.jd.live.agent.governance.policy.PolicySubscription; import com.jd.live.agent.governance.subscription.policy.listener.ServiceEvent; import com.jd.live.agent.governance.policy.service.MergePolicy; import com.jd.live.agent.governance.service.sync.SyncKey.ServiceKey; @@ -50,7 +50,7 @@ protected SyncConfig getSyncConfig() { } @Override - protected ServiceKey createServiceKey(PolicySubscriber subscriber) { + protected ServiceKey createServiceKey(PolicySubscription subscriber) { return new ServiceKey(subscriber); } diff --git a/joylive-implement/joylive-service/joylive-service-multilive/src/main/java/com/jd/live/agent/implement/service/policy/multilive/LiveServiceHttpSyncer.java b/joylive-implement/joylive-service/joylive-service-multilive/src/main/java/com/jd/live/agent/implement/service/policy/multilive/LiveServiceHttpSyncer.java index 2276866e0..994219fff 100644 --- a/joylive-implement/joylive-service/joylive-service-multilive/src/main/java/com/jd/live/agent/implement/service/policy/multilive/LiveServiceHttpSyncer.java +++ b/joylive-implement/joylive-service/joylive-service-multilive/src/main/java/com/jd/live/agent/implement/service/policy/multilive/LiveServiceHttpSyncer.java @@ -24,7 +24,7 @@ import com.jd.live.agent.core.util.http.HttpResponse; import com.jd.live.agent.core.util.http.HttpUtils; import com.jd.live.agent.governance.config.GovernanceConfig; -import com.jd.live.agent.governance.policy.PolicySubscriber; +import com.jd.live.agent.governance.policy.PolicySubscription; import com.jd.live.agent.governance.subscription.policy.listener.ServiceEvent; import com.jd.live.agent.governance.policy.service.MergePolicy; import com.jd.live.agent.governance.policy.service.Service; @@ -60,7 +60,7 @@ protected SyncConfig getSyncConfig() { } @Override - protected ServiceKey createServiceKey(PolicySubscriber subscriber) { + protected ServiceKey createServiceKey(PolicySubscription subscriber) { return new ServiceKey(subscriber); } diff --git a/joylive-implement/joylive-service/joylive-service-nacos/src/main/java/com/jd/live/agent/implement/service/policy/nacos/LiveServiceNacosSyncer.java b/joylive-implement/joylive-service/joylive-service-nacos/src/main/java/com/jd/live/agent/implement/service/policy/nacos/LiveServiceNacosSyncer.java index 87d6ba439..edfecdc6f 100644 --- a/joylive-implement/joylive-service/joylive-service-nacos/src/main/java/com/jd/live/agent/implement/service/policy/nacos/LiveServiceNacosSyncer.java +++ b/joylive-implement/joylive-service/joylive-service-nacos/src/main/java/com/jd/live/agent/implement/service/policy/nacos/LiveServiceNacosSyncer.java @@ -25,7 +25,7 @@ import com.jd.live.agent.core.util.Futures; import com.jd.live.agent.core.util.template.Template; import com.jd.live.agent.governance.config.GovernanceConfig; -import com.jd.live.agent.governance.policy.PolicySubscriber; +import com.jd.live.agent.governance.policy.PolicySubscription; import com.jd.live.agent.governance.subscription.policy.listener.ServiceEvent; import com.jd.live.agent.governance.policy.service.MergePolicy; import com.jd.live.agent.governance.policy.service.Service; @@ -86,7 +86,7 @@ protected Template createTemplate() { } @Override - protected NacosServiceKey createServiceKey(PolicySubscriber subscriber) { + protected NacosServiceKey createServiceKey(PolicySubscription subscriber) { Map context = new HashMap<>(); context.put("name", subscriber.getName()); context.put("space", application.getService().getNamespace()); diff --git a/joylive-implement/joylive-service/joylive-service-nacos/src/main/java/com/jd/live/agent/implement/service/policy/nacos/NacosServiceKey.java b/joylive-implement/joylive-service/joylive-service-nacos/src/main/java/com/jd/live/agent/implement/service/policy/nacos/NacosServiceKey.java index 160a5d775..ef743cd8c 100644 --- a/joylive-implement/joylive-service/joylive-service-nacos/src/main/java/com/jd/live/agent/implement/service/policy/nacos/NacosServiceKey.java +++ b/joylive-implement/joylive-service/joylive-service-nacos/src/main/java/com/jd/live/agent/implement/service/policy/nacos/NacosServiceKey.java @@ -15,7 +15,7 @@ */ package com.jd.live.agent.implement.service.policy.nacos; -import com.jd.live.agent.governance.policy.PolicySubscriber; +import com.jd.live.agent.governance.policy.PolicySubscription; import com.jd.live.agent.governance.service.sync.SyncKey.ServiceKey; import lombok.Getter; @@ -26,7 +26,7 @@ public class NacosServiceKey extends ServiceKey implements NacosSyncKey { private final String group; - public NacosServiceKey(PolicySubscriber subscriber, String dataId, String group) { + public NacosServiceKey(PolicySubscription subscriber, String dataId, String group) { super(subscriber); this.dataId = dataId; this.group = group; diff --git a/joylive-implement/joylive-service/joylive-service-nacos/src/main/java/com/jd/live/agent/implement/service/policy/nacos/ServiceNacosSyncer.java b/joylive-implement/joylive-service/joylive-service-nacos/src/main/java/com/jd/live/agent/implement/service/policy/nacos/ServiceNacosSyncer.java index fb92ee954..278ea0664 100644 --- a/joylive-implement/joylive-service/joylive-service-nacos/src/main/java/com/jd/live/agent/implement/service/policy/nacos/ServiceNacosSyncer.java +++ b/joylive-implement/joylive-service/joylive-service-nacos/src/main/java/com/jd/live/agent/implement/service/policy/nacos/ServiceNacosSyncer.java @@ -25,7 +25,7 @@ import com.jd.live.agent.core.util.Futures; import com.jd.live.agent.core.util.template.Template; import com.jd.live.agent.governance.config.GovernanceConfig; -import com.jd.live.agent.governance.policy.PolicySubscriber; +import com.jd.live.agent.governance.policy.PolicySubscription; import com.jd.live.agent.governance.subscription.policy.listener.ServiceEvent; import com.jd.live.agent.governance.policy.service.MergePolicy; import com.jd.live.agent.governance.policy.service.Service; @@ -85,7 +85,7 @@ protected Template createTemplate() { } @Override - protected NacosServiceKey createServiceKey(PolicySubscriber subscriber) { + protected NacosServiceKey createServiceKey(PolicySubscription subscriber) { Map context = new HashMap<>(); context.put("name", subscriber.getName()); context.put("space", application.getService().getNamespace()); diff --git a/joylive-plugin/joylive-registry/joylive-registry-dubbo2.6/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v2_6/definition/ReferenceConfigDefinition.java b/joylive-plugin/joylive-registry/joylive-registry-dubbo2.6/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v2_6/definition/ReferenceConfigDefinition.java index b8c86ac10..6277718ad 100644 --- a/joylive-plugin/joylive-registry/joylive-registry-dubbo2.6/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v2_6/definition/ReferenceConfigDefinition.java +++ b/joylive-plugin/joylive-registry/joylive-registry-dubbo2.6/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v2_6/definition/ReferenceConfigDefinition.java @@ -25,7 +25,7 @@ import com.jd.live.agent.core.plugin.definition.InterceptorDefinitionAdapter; import com.jd.live.agent.core.plugin.definition.PluginDefinition; import com.jd.live.agent.core.plugin.definition.PluginDefinitionAdapter; -import com.jd.live.agent.governance.policy.PolicySupplier; +import com.jd.live.agent.governance.registry.Registry; import com.jd.live.agent.plugin.registry.dubbo.v2_6.condition.ConditionalOnDubbo26GovernanceEnabled; import com.jd.live.agent.plugin.registry.dubbo.v2_6.interceptor.ReferenceConfigInterceptor; @@ -49,8 +49,8 @@ public class ReferenceConfigDefinition extends PluginDefinitionAdapter { @Inject(Application.COMPONENT_APPLICATION) private Application application; - @Inject(PolicySupplier.COMPONENT_POLICY_SUPPLIER) - private PolicySupplier policySupplier; + @Inject(Registry.COMPONENT_REGISTRY) + private Registry registry; public ReferenceConfigDefinition() { this.matcher = () -> MatcherBuilder.named(TYPE_REFERENCE_CONFIG); @@ -58,7 +58,7 @@ public ReferenceConfigDefinition() { new InterceptorDefinitionAdapter( MatcherBuilder.named(METHOD_CREATE_PROXY). and(MatcherBuilder.arguments(ARGUMENT_CREATE_PROXY)), - () -> new ReferenceConfigInterceptor(application, policySupplier)) + () -> new ReferenceConfigInterceptor(application, registry)) }; } } diff --git a/joylive-plugin/joylive-registry/joylive-registry-dubbo2.6/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v2_6/definition/RegistryDefinition.java b/joylive-plugin/joylive-registry/joylive-registry-dubbo2.6/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v2_6/definition/RegistryDefinition.java index 4738b46a2..ee5e11a91 100644 --- a/joylive-plugin/joylive-registry/joylive-registry-dubbo2.6/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v2_6/definition/RegistryDefinition.java +++ b/joylive-plugin/joylive-registry/joylive-registry-dubbo2.6/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v2_6/definition/RegistryDefinition.java @@ -15,7 +15,6 @@ */ package com.jd.live.agent.plugin.registry.dubbo.v2_6.definition; -import com.jd.live.agent.core.bootstrap.AgentLifecycle; import com.jd.live.agent.core.bytekit.matcher.MatcherBuilder; import com.jd.live.agent.core.extension.annotation.ConditionalOnClass; import com.jd.live.agent.core.extension.annotation.Extension; @@ -50,9 +49,6 @@ public class RegistryDefinition extends PluginDefinitionAdapter { @Inject(Application.COMPONENT_APPLICATION) private Application application; - @Inject(AgentLifecycle.COMPONENT_AGENT_LIFECYCLE) - private AgentLifecycle lifecycle; - @Inject(Registry.COMPONENT_REGISTRY) private Registry registry; @@ -65,7 +61,7 @@ public RegistryDefinition() { MatcherBuilder.named(METHOD_REGISTER) .and(MatcherBuilder.arguments(ARGUMENT_REGISTER)) .and(MatcherBuilder.not(MatcherBuilder.isAbstract())), - () -> new RegistryInterceptor(application, lifecycle, registry)) + () -> new RegistryInterceptor(application, registry)) }; } } diff --git a/joylive-plugin/joylive-registry/joylive-registry-dubbo2.6/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v2_6/definition/ServiceConfigDefinition.java b/joylive-plugin/joylive-registry/joylive-registry-dubbo2.6/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v2_6/definition/ServiceConfigDefinition.java index 1ab4275af..9e1dd60ad 100644 --- a/joylive-plugin/joylive-registry/joylive-registry-dubbo2.6/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v2_6/definition/ServiceConfigDefinition.java +++ b/joylive-plugin/joylive-registry/joylive-registry-dubbo2.6/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v2_6/definition/ServiceConfigDefinition.java @@ -25,7 +25,7 @@ import com.jd.live.agent.core.plugin.definition.InterceptorDefinitionAdapter; import com.jd.live.agent.core.plugin.definition.PluginDefinition; import com.jd.live.agent.core.plugin.definition.PluginDefinitionAdapter; -import com.jd.live.agent.governance.policy.PolicySupplier; +import com.jd.live.agent.governance.registry.Registry; import com.jd.live.agent.plugin.registry.dubbo.v2_6.condition.ConditionalOnDubbo26GovernanceEnabled; import com.jd.live.agent.plugin.registry.dubbo.v2_6.interceptor.ServiceConfigInterceptor; @@ -51,8 +51,8 @@ public class ServiceConfigDefinition extends PluginDefinitionAdapter { @Inject(Application.COMPONENT_APPLICATION) private Application application; - @Inject(PolicySupplier.COMPONENT_POLICY_SUPPLIER) - private PolicySupplier policySupplier; + @Inject(Registry.COMPONENT_REGISTRY) + private Registry registry; public ServiceConfigDefinition() { this.matcher = () -> MatcherBuilder.named(TYPE_SERVICE_CONFIG); @@ -60,7 +60,7 @@ public ServiceConfigDefinition() { new InterceptorDefinitionAdapter( MatcherBuilder.named(METHOD_FIND_CONFIGED_HOSTS). and(MatcherBuilder.arguments(ARGUMENT_FIND_CONFIGED_HOSTS)), - () -> new ServiceConfigInterceptor(application, policySupplier)) + () -> new ServiceConfigInterceptor(application, registry)) }; } } diff --git a/joylive-plugin/joylive-registry/joylive-registry-dubbo2.6/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v2_6/interceptor/AbstractConfigInterceptor.java b/joylive-plugin/joylive-registry/joylive-registry-dubbo2.6/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v2_6/interceptor/AbstractConfigInterceptor.java index 9efd0405e..5e54a4a00 100644 --- a/joylive-plugin/joylive-registry/joylive-registry-dubbo2.6/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v2_6/interceptor/AbstractConfigInterceptor.java +++ b/joylive-plugin/joylive-registry/joylive-registry-dubbo2.6/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v2_6/interceptor/AbstractConfigInterceptor.java @@ -19,7 +19,7 @@ import com.jd.live.agent.bootstrap.bytekit.context.ExecutableContext; import com.jd.live.agent.core.instance.Application; import com.jd.live.agent.core.plugin.definition.InterceptorAdaptor; -import com.jd.live.agent.governance.policy.PolicySupplier; +import com.jd.live.agent.governance.registry.Registry; import java.util.Map; @@ -32,11 +32,11 @@ public abstract class AbstractConfigInterceptor getContext(ExecutableContext ctx); protected abstract String getService(T config); + protected void subscribe(String service) { + registry.subscribe(service); + } + } diff --git a/joylive-plugin/joylive-registry/joylive-registry-dubbo2.6/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v2_6/interceptor/ReferenceConfigInterceptor.java b/joylive-plugin/joylive-registry/joylive-registry-dubbo2.6/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v2_6/interceptor/ReferenceConfigInterceptor.java index 9d09a7a2f..e822db75b 100644 --- a/joylive-plugin/joylive-registry/joylive-registry-dubbo2.6/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v2_6/interceptor/ReferenceConfigInterceptor.java +++ b/joylive-plugin/joylive-registry/joylive-registry-dubbo2.6/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v2_6/interceptor/ReferenceConfigInterceptor.java @@ -18,7 +18,7 @@ import com.alibaba.dubbo.config.ReferenceConfig; import com.jd.live.agent.bootstrap.bytekit.context.ExecutableContext; import com.jd.live.agent.core.instance.Application; -import com.jd.live.agent.governance.policy.PolicySupplier; +import com.jd.live.agent.governance.registry.Registry; import java.util.Map; @@ -27,8 +27,8 @@ */ public class ReferenceConfigInterceptor extends AbstractConfigInterceptor> { - public ReferenceConfigInterceptor(Application application, PolicySupplier policySupplier) { - super(application, policySupplier); + public ReferenceConfigInterceptor(Application application, Registry registry) { + super(application, registry); } @SuppressWarnings("unchecked") diff --git a/joylive-plugin/joylive-registry/joylive-registry-dubbo2.6/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v2_6/interceptor/RegistryInterceptor.java b/joylive-plugin/joylive-registry/joylive-registry-dubbo2.6/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v2_6/interceptor/RegistryInterceptor.java index 91a7c7c45..a6109e994 100644 --- a/joylive-plugin/joylive-registry/joylive-registry-dubbo2.6/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v2_6/interceptor/RegistryInterceptor.java +++ b/joylive-plugin/joylive-registry/joylive-registry-dubbo2.6/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v2_6/interceptor/RegistryInterceptor.java @@ -17,7 +17,6 @@ import com.alibaba.dubbo.common.URL; import com.jd.live.agent.bootstrap.bytekit.context.MethodContext; -import com.jd.live.agent.core.bootstrap.AgentLifecycle; import com.jd.live.agent.core.instance.Application; import com.jd.live.agent.governance.interceptor.AbstractRegistryInterceptor; import com.jd.live.agent.governance.registry.Registry; @@ -32,8 +31,8 @@ */ public class RegistryInterceptor extends AbstractRegistryInterceptor { - public RegistryInterceptor(Application application, AgentLifecycle lifecycle, Registry registry) { - super(application, lifecycle, registry); + public RegistryInterceptor(Application application, Registry registry) { + super(application, registry); } @Override diff --git a/joylive-plugin/joylive-registry/joylive-registry-dubbo2.6/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v2_6/interceptor/ServiceConfigInterceptor.java b/joylive-plugin/joylive-registry/joylive-registry-dubbo2.6/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v2_6/interceptor/ServiceConfigInterceptor.java index fab9cecf8..3df230967 100644 --- a/joylive-plugin/joylive-registry/joylive-registry-dubbo2.6/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v2_6/interceptor/ServiceConfigInterceptor.java +++ b/joylive-plugin/joylive-registry/joylive-registry-dubbo2.6/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v2_6/interceptor/ServiceConfigInterceptor.java @@ -18,7 +18,7 @@ import com.alibaba.dubbo.config.ServiceConfig; import com.jd.live.agent.bootstrap.bytekit.context.ExecutableContext; import com.jd.live.agent.core.instance.Application; -import com.jd.live.agent.governance.policy.PolicySupplier; +import com.jd.live.agent.governance.registry.Registry; import java.util.Map; @@ -27,8 +27,8 @@ */ public class ServiceConfigInterceptor extends AbstractConfigInterceptor> { - public ServiceConfigInterceptor(Application application, PolicySupplier policySupplier) { - super(application, policySupplier); + public ServiceConfigInterceptor(Application application, Registry registry) { + super(application, registry); } @SuppressWarnings("unchecked") @@ -41,4 +41,9 @@ protected Map getContext(ExecutableContext ctx) { protected String getService(ServiceConfig config) { return config.getInterface(); } + + @Override + protected void subscribe(String service) { + super.subscribe(service); + } } diff --git a/joylive-plugin/joylive-registry/joylive-registry-dubbo2.7/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v2_7/definition/ReferenceConfigDefinition.java b/joylive-plugin/joylive-registry/joylive-registry-dubbo2.7/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v2_7/definition/ReferenceConfigDefinition.java index 609dd16b8..039afbdee 100644 --- a/joylive-plugin/joylive-registry/joylive-registry-dubbo2.7/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v2_7/definition/ReferenceConfigDefinition.java +++ b/joylive-plugin/joylive-registry/joylive-registry-dubbo2.7/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v2_7/definition/ReferenceConfigDefinition.java @@ -25,7 +25,7 @@ import com.jd.live.agent.core.plugin.definition.InterceptorDefinitionAdapter; import com.jd.live.agent.core.plugin.definition.PluginDefinition; import com.jd.live.agent.core.plugin.definition.PluginDefinitionAdapter; -import com.jd.live.agent.governance.policy.PolicySupplier; +import com.jd.live.agent.governance.registry.Registry; import com.jd.live.agent.plugin.registry.dubbo.v2_7.condition.ConditionalOnDubbo27GovernanceEnabled; import com.jd.live.agent.plugin.registry.dubbo.v2_7.interceptor.ReferenceConfigInterceptor; @@ -49,15 +49,15 @@ public class ReferenceConfigDefinition extends PluginDefinitionAdapter { @Inject(Application.COMPONENT_APPLICATION) private Application application; - @Inject(PolicySupplier.COMPONENT_POLICY_SUPPLIER) - private PolicySupplier policySupplier; + @Inject(Registry.COMPONENT_REGISTRY) + private Registry registry; public ReferenceConfigDefinition() { this.matcher = () -> MatcherBuilder.named(TYPE_REFERENCE_CONFIG); this.interceptors = new InterceptorDefinition[]{ new InterceptorDefinitionAdapter( MatcherBuilder.named(METHOD_CREATE_PROXY).and(MatcherBuilder.arguments(ARGUMENT_CREATE_PROXY)), - () -> new ReferenceConfigInterceptor(application, policySupplier)) + () -> new ReferenceConfigInterceptor(application, registry)) }; } } diff --git a/joylive-plugin/joylive-registry/joylive-registry-dubbo2.7/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v2_7/definition/RegistryDefinition.java b/joylive-plugin/joylive-registry/joylive-registry-dubbo2.7/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v2_7/definition/RegistryDefinition.java index 588fed5f5..c3dd7079d 100644 --- a/joylive-plugin/joylive-registry/joylive-registry-dubbo2.7/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v2_7/definition/RegistryDefinition.java +++ b/joylive-plugin/joylive-registry/joylive-registry-dubbo2.7/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v2_7/definition/RegistryDefinition.java @@ -15,7 +15,6 @@ */ package com.jd.live.agent.plugin.registry.dubbo.v2_7.definition; -import com.jd.live.agent.core.bootstrap.AgentLifecycle; import com.jd.live.agent.core.bytekit.matcher.MatcherBuilder; import com.jd.live.agent.core.extension.annotation.ConditionalOnClass; import com.jd.live.agent.core.extension.annotation.Extension; @@ -52,9 +51,6 @@ public class RegistryDefinition extends PluginDefinitionAdapter { @Inject(Application.COMPONENT_APPLICATION) private Application application; - @Inject(AgentLifecycle.COMPONENT_AGENT_LIFECYCLE) - private AgentLifecycle lifecycle; - @Inject(Registry.COMPONENT_REGISTRY) private Registry registry; @@ -76,7 +72,7 @@ public RegistryDefinition() { MatcherBuilder.named(METHOD_REGISTER) .and(MatcherBuilder.arguments(ARGUMENT_REGISTER)) .and(MatcherBuilder.not(MatcherBuilder.isAbstract())), - () -> new RegistryInterceptor(application, lifecycle, registry)) + () -> new RegistryInterceptor(application, registry)) }; } } diff --git a/joylive-plugin/joylive-registry/joylive-registry-dubbo2.7/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v2_7/definition/ServiceConfigDefinition.java b/joylive-plugin/joylive-registry/joylive-registry-dubbo2.7/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v2_7/definition/ServiceConfigDefinition.java index 822bf1b23..887c9d6a7 100644 --- a/joylive-plugin/joylive-registry/joylive-registry-dubbo2.7/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v2_7/definition/ServiceConfigDefinition.java +++ b/joylive-plugin/joylive-registry/joylive-registry-dubbo2.7/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v2_7/definition/ServiceConfigDefinition.java @@ -25,7 +25,7 @@ import com.jd.live.agent.core.plugin.definition.InterceptorDefinitionAdapter; import com.jd.live.agent.core.plugin.definition.PluginDefinition; import com.jd.live.agent.core.plugin.definition.PluginDefinitionAdapter; -import com.jd.live.agent.governance.policy.PolicySupplier; +import com.jd.live.agent.governance.registry.Registry; import com.jd.live.agent.plugin.registry.dubbo.v2_7.condition.ConditionalOnDubbo27GovernanceEnabled; import com.jd.live.agent.plugin.registry.dubbo.v2_7.interceptor.ServiceConfigInterceptor; @@ -51,8 +51,8 @@ public class ServiceConfigDefinition extends PluginDefinitionAdapter { @Inject(Application.COMPONENT_APPLICATION) private Application application; - @Inject(PolicySupplier.COMPONENT_POLICY_SUPPLIER) - private PolicySupplier policySupplier; + @Inject(Registry.COMPONENT_REGISTRY) + private Registry registry; public ServiceConfigDefinition() { this.matcher = () -> MatcherBuilder.named(TYPE_SERVICE_CONFIG); @@ -60,7 +60,7 @@ public ServiceConfigDefinition() { new InterceptorDefinitionAdapter( MatcherBuilder.named(METHOD_FIND_CONFIGED_HOSTS) .and(MatcherBuilder.arguments(ARGUMENT_FIND_CONFIGED_HOSTS)), - () -> new ServiceConfigInterceptor(application, policySupplier)) + () -> new ServiceConfigInterceptor(application, registry)) }; } } diff --git a/joylive-plugin/joylive-registry/joylive-registry-dubbo2.7/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v2_7/interceptor/AbstractConfigInterceptor.java b/joylive-plugin/joylive-registry/joylive-registry-dubbo2.7/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v2_7/interceptor/AbstractConfigInterceptor.java index 039b03c40..8e3ee5d35 100644 --- a/joylive-plugin/joylive-registry/joylive-registry-dubbo2.7/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v2_7/interceptor/AbstractConfigInterceptor.java +++ b/joylive-plugin/joylive-registry/joylive-registry-dubbo2.7/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v2_7/interceptor/AbstractConfigInterceptor.java @@ -18,7 +18,7 @@ import com.jd.live.agent.bootstrap.bytekit.context.ExecutableContext; import com.jd.live.agent.core.instance.Application; import com.jd.live.agent.core.plugin.definition.InterceptorAdaptor; -import com.jd.live.agent.governance.policy.PolicySupplier; +import com.jd.live.agent.governance.registry.Registry; import org.apache.dubbo.config.AbstractInterfaceConfig; import java.util.Map; @@ -40,11 +40,11 @@ public abstract class AbstractConfigInterceptor ctx) { switch (type) { case REGISTRY_TYPE_SERVICE: ctx.put(REGISTRY_TYPE_KEY, SERVICE_REGISTRY_TYPE); - policySupplier.subscribe(config.getApplication().getName()); + registry.subscribe(config.getApplication().getName()); break; case REGISTRY_TYPE_ALL: ctx.put(REGISTRY_TYPE_KEY, "all"); - policySupplier.subscribe(config.getApplication().getName()); - policySupplier.subscribe(service); + registry.subscribe(config.getApplication().getName()); + registry.subscribe(service); case REGISTRY_TYPE_INTERFACE: default: - policySupplier.subscribe(service); + registry.subscribe(service); } } diff --git a/joylive-plugin/joylive-registry/joylive-registry-dubbo2.7/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v2_7/interceptor/ReferenceConfigInterceptor.java b/joylive-plugin/joylive-registry/joylive-registry-dubbo2.7/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v2_7/interceptor/ReferenceConfigInterceptor.java index c65dd4f98..fe97b540b 100644 --- a/joylive-plugin/joylive-registry/joylive-registry-dubbo2.7/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v2_7/interceptor/ReferenceConfigInterceptor.java +++ b/joylive-plugin/joylive-registry/joylive-registry-dubbo2.7/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v2_7/interceptor/ReferenceConfigInterceptor.java @@ -17,7 +17,7 @@ import com.jd.live.agent.bootstrap.bytekit.context.ExecutableContext; import com.jd.live.agent.core.instance.Application; -import com.jd.live.agent.governance.policy.PolicySupplier; +import com.jd.live.agent.governance.registry.Registry; import org.apache.dubbo.config.ReferenceConfig; import java.util.Map; @@ -27,8 +27,8 @@ */ public class ReferenceConfigInterceptor extends AbstractConfigInterceptor> { - public ReferenceConfigInterceptor(Application application, PolicySupplier policySupplier) { - super(application, policySupplier); + public ReferenceConfigInterceptor(Application application, Registry registry) { + super(application, registry); } @SuppressWarnings("unchecked") diff --git a/joylive-plugin/joylive-registry/joylive-registry-dubbo2.7/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v2_7/interceptor/RegistryInterceptor.java b/joylive-plugin/joylive-registry/joylive-registry-dubbo2.7/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v2_7/interceptor/RegistryInterceptor.java index 5f34f6f0d..0e887e40f 100644 --- a/joylive-plugin/joylive-registry/joylive-registry-dubbo2.7/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v2_7/interceptor/RegistryInterceptor.java +++ b/joylive-plugin/joylive-registry/joylive-registry-dubbo2.7/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v2_7/interceptor/RegistryInterceptor.java @@ -16,7 +16,6 @@ package com.jd.live.agent.plugin.registry.dubbo.v2_7.interceptor; import com.jd.live.agent.bootstrap.bytekit.context.MethodContext; -import com.jd.live.agent.core.bootstrap.AgentLifecycle; import com.jd.live.agent.core.instance.Application; import com.jd.live.agent.governance.interceptor.AbstractRegistryInterceptor; import com.jd.live.agent.governance.registry.Registry; @@ -31,8 +30,8 @@ */ public class RegistryInterceptor extends AbstractRegistryInterceptor { - public RegistryInterceptor(Application application, AgentLifecycle lifecycle, Registry registry) { - super(application, lifecycle, registry); + public RegistryInterceptor(Application application, Registry registry) { + super(application, registry); } @Override diff --git a/joylive-plugin/joylive-registry/joylive-registry-dubbo2.7/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v2_7/interceptor/ServiceConfigInterceptor.java b/joylive-plugin/joylive-registry/joylive-registry-dubbo2.7/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v2_7/interceptor/ServiceConfigInterceptor.java index d5fa7697e..35e83b408 100644 --- a/joylive-plugin/joylive-registry/joylive-registry-dubbo2.7/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v2_7/interceptor/ServiceConfigInterceptor.java +++ b/joylive-plugin/joylive-registry/joylive-registry-dubbo2.7/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v2_7/interceptor/ServiceConfigInterceptor.java @@ -17,7 +17,7 @@ import com.jd.live.agent.bootstrap.bytekit.context.ExecutableContext; import com.jd.live.agent.core.instance.Application; -import com.jd.live.agent.governance.policy.PolicySupplier; +import com.jd.live.agent.governance.registry.Registry; import org.apache.dubbo.config.RegistryConfig; import org.apache.dubbo.config.ServiceConfig; @@ -31,8 +31,8 @@ */ public class ServiceConfigInterceptor extends AbstractConfigInterceptor> { - public ServiceConfigInterceptor(Application application, PolicySupplier policySupplier) { - super(application, policySupplier); + public ServiceConfigInterceptor(Application application, Registry registry) { + super(application, registry); } @SuppressWarnings("unchecked") diff --git a/joylive-plugin/joylive-registry/joylive-registry-dubbo3/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v3/definition/ReferenceConfigDefinition.java b/joylive-plugin/joylive-registry/joylive-registry-dubbo3/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v3/definition/ReferenceConfigDefinition.java index b9eb0af60..a02f7c925 100644 --- a/joylive-plugin/joylive-registry/joylive-registry-dubbo3/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v3/definition/ReferenceConfigDefinition.java +++ b/joylive-plugin/joylive-registry/joylive-registry-dubbo3/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v3/definition/ReferenceConfigDefinition.java @@ -25,7 +25,7 @@ import com.jd.live.agent.core.plugin.definition.InterceptorDefinitionAdapter; import com.jd.live.agent.core.plugin.definition.PluginDefinition; import com.jd.live.agent.core.plugin.definition.PluginDefinitionAdapter; -import com.jd.live.agent.governance.policy.PolicySupplier; +import com.jd.live.agent.governance.registry.Registry; import com.jd.live.agent.plugin.registry.dubbo.v3.condition.ConditionalOnDubbo3GovernanceEnabled; import com.jd.live.agent.plugin.registry.dubbo.v3.interceptor.ReferenceConfigInterceptor; @@ -45,8 +45,8 @@ public class ReferenceConfigDefinition extends PluginDefinitionAdapter { @Inject(Application.COMPONENT_APPLICATION) private Application application; - @Inject(PolicySupplier.COMPONENT_POLICY_SUPPLIER) - private PolicySupplier policySupplier; + @Inject(Registry.COMPONENT_REGISTRY) + private Registry registry; public ReferenceConfigDefinition() { this.matcher = () -> MatcherBuilder.named(TYPE_REFERENCE_CONFIG); @@ -54,7 +54,7 @@ public ReferenceConfigDefinition() { new InterceptorDefinitionAdapter( MatcherBuilder.named(METHOD_APPEND_CONFIG). and(MatcherBuilder.arguments(0)), - () -> new ReferenceConfigInterceptor(application, policySupplier)) + () -> new ReferenceConfigInterceptor(application, registry)) }; } } diff --git a/joylive-plugin/joylive-registry/joylive-registry-dubbo3/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v3/definition/RegistryDefinition.java b/joylive-plugin/joylive-registry/joylive-registry-dubbo3/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v3/definition/RegistryDefinition.java index 07d52c006..35216a117 100644 --- a/joylive-plugin/joylive-registry/joylive-registry-dubbo3/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v3/definition/RegistryDefinition.java +++ b/joylive-plugin/joylive-registry/joylive-registry-dubbo3/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v3/definition/RegistryDefinition.java @@ -15,7 +15,6 @@ */ package com.jd.live.agent.plugin.registry.dubbo.v3.definition; -import com.jd.live.agent.core.bootstrap.AgentLifecycle; import com.jd.live.agent.core.bytekit.matcher.MatcherBuilder; import com.jd.live.agent.core.extension.annotation.ConditionalOnClass; import com.jd.live.agent.core.extension.annotation.Extension; @@ -52,9 +51,6 @@ public class RegistryDefinition extends PluginDefinitionAdapter { @Inject(Application.COMPONENT_APPLICATION) private Application application; - @Inject(AgentLifecycle.COMPONENT_AGENT_LIFECYCLE) - private AgentLifecycle lifecycle; - @Inject(Registry.COMPONENT_REGISTRY) private Registry registry; @@ -70,7 +66,7 @@ public RegistryDefinition() { MatcherBuilder.named(METHOD_REGISTER) .and(MatcherBuilder.arguments(ARGUMENT_REGISTER)) .and(MatcherBuilder.not(MatcherBuilder.isAbstract())), - () -> new RegistryInterceptor(application, lifecycle, registry)) + () -> new RegistryInterceptor(application, registry)) }; } } diff --git a/joylive-plugin/joylive-registry/joylive-registry-dubbo3/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v3/definition/ServiceConfigDefinition.java b/joylive-plugin/joylive-registry/joylive-registry-dubbo3/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v3/definition/ServiceConfigDefinition.java index 8b5b5e095..6391d8443 100644 --- a/joylive-plugin/joylive-registry/joylive-registry-dubbo3/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v3/definition/ServiceConfigDefinition.java +++ b/joylive-plugin/joylive-registry/joylive-registry-dubbo3/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v3/definition/ServiceConfigDefinition.java @@ -25,7 +25,7 @@ import com.jd.live.agent.core.plugin.definition.InterceptorDefinitionAdapter; import com.jd.live.agent.core.plugin.definition.PluginDefinition; import com.jd.live.agent.core.plugin.definition.PluginDefinitionAdapter; -import com.jd.live.agent.governance.policy.PolicySupplier; +import com.jd.live.agent.governance.registry.Registry; import com.jd.live.agent.plugin.registry.dubbo.v3.condition.ConditionalOnDubbo3GovernanceEnabled; import com.jd.live.agent.plugin.registry.dubbo.v3.interceptor.ServiceConfigInterceptor; @@ -49,8 +49,8 @@ public class ServiceConfigDefinition extends PluginDefinitionAdapter { @Inject(Application.COMPONENT_APPLICATION) private Application application; - @Inject(PolicySupplier.COMPONENT_POLICY_SUPPLIER) - private PolicySupplier policySupplier; + @Inject(Registry.COMPONENT_REGISTRY) + private Registry registry; public ServiceConfigDefinition() { this.matcher = () -> MatcherBuilder.named(TYPE_SERVICE_CONFIG); @@ -58,7 +58,7 @@ public ServiceConfigDefinition() { new InterceptorDefinitionAdapter( MatcherBuilder.named(METHOD_BUILD_ATTRIBUTES). and(MatcherBuilder.arguments(ARGUMENT_BUILD_ATTRIBUTES)), - () -> new ServiceConfigInterceptor(application, policySupplier)) + () -> new ServiceConfigInterceptor(application, registry)) }; } } diff --git a/joylive-plugin/joylive-registry/joylive-registry-dubbo3/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v3/interceptor/AbstractConfigInterceptor.java b/joylive-plugin/joylive-registry/joylive-registry-dubbo3/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v3/interceptor/AbstractConfigInterceptor.java index e36eefa22..906b49f5b 100644 --- a/joylive-plugin/joylive-registry/joylive-registry-dubbo3/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v3/interceptor/AbstractConfigInterceptor.java +++ b/joylive-plugin/joylive-registry/joylive-registry-dubbo3/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v3/interceptor/AbstractConfigInterceptor.java @@ -18,7 +18,7 @@ import com.jd.live.agent.bootstrap.bytekit.context.ExecutableContext; import com.jd.live.agent.core.instance.Application; import com.jd.live.agent.core.plugin.definition.InterceptorAdaptor; -import com.jd.live.agent.governance.policy.PolicySupplier; +import com.jd.live.agent.governance.registry.Registry; import org.apache.dubbo.config.AbstractInterfaceConfig; import java.util.Map; @@ -40,11 +40,11 @@ public abstract class AbstractConfigInterceptor ctx) { switch (type) { case REGISTRY_TYPE_SERVICE: ctx.put(REGISTRY_TYPE_KEY, SERVICE_REGISTRY_TYPE); - policySupplier.subscribe(config.getApplication().getName()); + registry.subscribe(config.getApplication().getName()); break; case REGISTRY_TYPE_ALL: ctx.put(REGISTRY_TYPE_KEY, "all"); - policySupplier.subscribe(config.getApplication().getName()); - policySupplier.subscribe(service); + registry.subscribe(config.getApplication().getName()); + registry.subscribe(service); case REGISTRY_TYPE_INTERFACE: default: - policySupplier.subscribe(service); + registry.subscribe(service); } } diff --git a/joylive-plugin/joylive-registry/joylive-registry-dubbo3/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v3/interceptor/ReferenceConfigInterceptor.java b/joylive-plugin/joylive-registry/joylive-registry-dubbo3/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v3/interceptor/ReferenceConfigInterceptor.java index ab30fe418..bdb0a7100 100644 --- a/joylive-plugin/joylive-registry/joylive-registry-dubbo3/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v3/interceptor/ReferenceConfigInterceptor.java +++ b/joylive-plugin/joylive-registry/joylive-registry-dubbo3/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v3/interceptor/ReferenceConfigInterceptor.java @@ -18,7 +18,7 @@ import com.jd.live.agent.bootstrap.bytekit.context.ExecutableContext; import com.jd.live.agent.bootstrap.bytekit.context.MethodContext; import com.jd.live.agent.core.instance.Application; -import com.jd.live.agent.governance.policy.PolicySupplier; +import com.jd.live.agent.governance.registry.Registry; import org.apache.dubbo.config.ReferenceConfig; import java.util.Map; @@ -28,14 +28,13 @@ */ public class ReferenceConfigInterceptor extends AbstractConfigInterceptor> { - public ReferenceConfigInterceptor(Application application, PolicySupplier policySupplier) { - super(application, policySupplier); + public ReferenceConfigInterceptor(Application application, Registry registry) { + super(application, registry); } - @SuppressWarnings("unchecked") @Override protected Map getContext(ExecutableContext ctx) { - return (Map) ((MethodContext) ctx).getResult(); + return ((MethodContext) ctx).getResult(); } @Override diff --git a/joylive-plugin/joylive-registry/joylive-registry-dubbo3/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v3/interceptor/RegistryInterceptor.java b/joylive-plugin/joylive-registry/joylive-registry-dubbo3/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v3/interceptor/RegistryInterceptor.java index 6f023b6c9..769db3bfd 100644 --- a/joylive-plugin/joylive-registry/joylive-registry-dubbo3/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v3/interceptor/RegistryInterceptor.java +++ b/joylive-plugin/joylive-registry/joylive-registry-dubbo3/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v3/interceptor/RegistryInterceptor.java @@ -16,7 +16,6 @@ package com.jd.live.agent.plugin.registry.dubbo.v3.interceptor; import com.jd.live.agent.bootstrap.bytekit.context.MethodContext; -import com.jd.live.agent.core.bootstrap.AgentLifecycle; import com.jd.live.agent.core.instance.Application; import com.jd.live.agent.governance.interceptor.AbstractRegistryInterceptor; import com.jd.live.agent.governance.registry.Registry; @@ -32,8 +31,8 @@ */ public class RegistryInterceptor extends AbstractRegistryInterceptor { - public RegistryInterceptor(Application application, AgentLifecycle lifecycle, Registry registry) { - super(application, lifecycle, registry); + public RegistryInterceptor(Application application, Registry registry) { + super(application, registry); } @Override diff --git a/joylive-plugin/joylive-registry/joylive-registry-dubbo3/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v3/interceptor/ServiceConfigInterceptor.java b/joylive-plugin/joylive-registry/joylive-registry-dubbo3/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v3/interceptor/ServiceConfigInterceptor.java index efc41b0e0..2ea317317 100644 --- a/joylive-plugin/joylive-registry/joylive-registry-dubbo3/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v3/interceptor/ServiceConfigInterceptor.java +++ b/joylive-plugin/joylive-registry/joylive-registry-dubbo3/src/main/java/com/jd/live/agent/plugin/registry/dubbo/v3/interceptor/ServiceConfigInterceptor.java @@ -18,7 +18,7 @@ import com.jd.live.agent.bootstrap.bytekit.context.ExecutableContext; import com.jd.live.agent.bootstrap.bytekit.context.MethodContext; import com.jd.live.agent.core.instance.Application; -import com.jd.live.agent.governance.policy.PolicySupplier; +import com.jd.live.agent.governance.registry.Registry; import org.apache.dubbo.config.ApplicationConfig; import org.apache.dubbo.config.ServiceConfig; @@ -32,8 +32,8 @@ */ public class ServiceConfigInterceptor extends AbstractConfigInterceptor> { - public ServiceConfigInterceptor(Application application, PolicySupplier policySupplier) { - super(application, policySupplier); + public ServiceConfigInterceptor(Application application, Registry registry) { + super(application, registry); } @SuppressWarnings("unchecked") diff --git a/joylive-plugin/joylive-registry/joylive-registry-grpc/src/main/java/com/jd/live/agent/plugin/registry/grpc/definition/DiscoveryClientNameResolverDefinition.java b/joylive-plugin/joylive-registry/joylive-registry-grpc/src/main/java/com/jd/live/agent/plugin/registry/grpc/definition/DiscoveryClientNameResolverDefinition.java index e418b77aa..4d3cf03e2 100644 --- a/joylive-plugin/joylive-registry/joylive-registry-grpc/src/main/java/com/jd/live/agent/plugin/registry/grpc/definition/DiscoveryClientNameResolverDefinition.java +++ b/joylive-plugin/joylive-registry/joylive-registry-grpc/src/main/java/com/jd/live/agent/plugin/registry/grpc/definition/DiscoveryClientNameResolverDefinition.java @@ -25,7 +25,7 @@ import com.jd.live.agent.core.plugin.definition.PluginDefinition; import com.jd.live.agent.core.plugin.definition.PluginDefinitionAdapter; import com.jd.live.agent.governance.annotation.ConditionalOnGovernanceEnabled; -import com.jd.live.agent.governance.policy.PolicySupplier; +import com.jd.live.agent.governance.registry.Registry; import com.jd.live.agent.plugin.registry.grpc.interceptor.DiscoveryClientConstructorInterceptor; /** @@ -39,14 +39,14 @@ public class DiscoveryClientNameResolverDefinition extends PluginDefinitionAdapt protected static final String TYPE_DISCOVERY_CLIENT_NAME_RESOLVER = "net.devh.boot.grpc.client.nameresolver.DiscoveryClientNameResolver"; - @Inject(PolicySupplier.COMPONENT_POLICY_SUPPLIER) - private PolicySupplier policySupplier; + @Inject(Registry.COMPONENT_REGISTRY) + private Registry registry; public DiscoveryClientNameResolverDefinition() { this.matcher = () -> MatcherBuilder.named(TYPE_DISCOVERY_CLIENT_NAME_RESOLVER); this.interceptors = new InterceptorDefinition[]{ new InterceptorDefinitionAdapter( - MatcherBuilder.isConstructor(), () -> new DiscoveryClientConstructorInterceptor(policySupplier)), + MatcherBuilder.isConstructor(), () -> new DiscoveryClientConstructorInterceptor(registry)), }; } } diff --git a/joylive-plugin/joylive-registry/joylive-registry-grpc/src/main/java/com/jd/live/agent/plugin/registry/grpc/interceptor/DiscoveryClientConstructorInterceptor.java b/joylive-plugin/joylive-registry/joylive-registry-grpc/src/main/java/com/jd/live/agent/plugin/registry/grpc/interceptor/DiscoveryClientConstructorInterceptor.java index 78c666334..8ce02520a 100644 --- a/joylive-plugin/joylive-registry/joylive-registry-grpc/src/main/java/com/jd/live/agent/plugin/registry/grpc/interceptor/DiscoveryClientConstructorInterceptor.java +++ b/joylive-plugin/joylive-registry/joylive-registry-grpc/src/main/java/com/jd/live/agent/plugin/registry/grpc/interceptor/DiscoveryClientConstructorInterceptor.java @@ -19,7 +19,7 @@ import com.jd.live.agent.bootstrap.logger.Logger; import com.jd.live.agent.bootstrap.logger.LoggerFactory; import com.jd.live.agent.core.plugin.definition.InterceptorAdaptor; -import com.jd.live.agent.governance.policy.PolicySupplier; +import com.jd.live.agent.governance.registry.Registry; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; @@ -32,17 +32,18 @@ public class DiscoveryClientConstructorInterceptor extends InterceptorAdaptor { private static final Logger logger = LoggerFactory.getLogger(DiscoveryClientConstructorInterceptor.class); - private final PolicySupplier policySupplier; + private final Registry registry; - public DiscoveryClientConstructorInterceptor(PolicySupplier policySupplier) { - this.policySupplier = policySupplier; + public DiscoveryClientConstructorInterceptor(Registry registry) { + this.registry = registry; } @Override public void onSuccess(ExecutableContext ctx) { + // gRPC is triggered on the first request String serviceId = ctx.getArgument(0); try { - policySupplier.subscribe(serviceId).get(5000, TimeUnit.MILLISECONDS); + registry.subscribe(serviceId).get(5000, TimeUnit.MILLISECONDS); } catch (InterruptedException ignore) { } catch (ExecutionException e) { Throwable cause = e.getCause() != null ? e.getCause() : e; diff --git a/joylive-plugin/joylive-registry/joylive-registry-nacos/pom.xml b/joylive-plugin/joylive-registry/joylive-registry-nacos/pom.xml index 575953af5..ac0cff420 100644 --- a/joylive-plugin/joylive-registry/joylive-registry-nacos/pom.xml +++ b/joylive-plugin/joylive-registry/joylive-registry-nacos/pom.xml @@ -12,6 +12,7 @@ joylive-registry-nacos 3.1.8 + 2.4.3 @@ -21,6 +22,11 @@ ${spring-cloud-commons.version} provided + + com.alibaba.nacos + nacos-client + ${nacos-client.version} + \ No newline at end of file diff --git a/joylive-plugin/joylive-registry/joylive-registry-nacos/src/main/java/com/jd/live/agent/plugin/registry/nacos/definition/NacosInstanceChangeDefinition.java b/joylive-plugin/joylive-registry/joylive-registry-nacos/src/main/java/com/jd/live/agent/plugin/registry/nacos/definition/NacosInstanceChangeDefinition.java new file mode 100644 index 000000000..f632fd83f --- /dev/null +++ b/joylive-plugin/joylive-registry/joylive-registry-nacos/src/main/java/com/jd/live/agent/plugin/registry/nacos/definition/NacosInstanceChangeDefinition.java @@ -0,0 +1,56 @@ +/* + * Copyright © ${year} ${owner} (${email}) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.jd.live.agent.plugin.registry.nacos.definition; + +import com.jd.live.agent.core.bytekit.matcher.MatcherBuilder; +import com.jd.live.agent.core.extension.annotation.ConditionalOnClass; +import com.jd.live.agent.core.extension.annotation.Extension; +import com.jd.live.agent.core.inject.annotation.Inject; +import com.jd.live.agent.core.inject.annotation.Injectable; +import com.jd.live.agent.core.plugin.definition.InterceptorDefinition; +import com.jd.live.agent.core.plugin.definition.InterceptorDefinitionAdapter; +import com.jd.live.agent.core.plugin.definition.PluginDefinition; +import com.jd.live.agent.core.plugin.definition.PluginDefinitionAdapter; +import com.jd.live.agent.governance.annotation.ConditionalOnGovernanceEnabled; +import com.jd.live.agent.governance.registry.Registry; +import com.jd.live.agent.governance.registry.RegistrySupervisor; +import com.jd.live.agent.plugin.registry.nacos.interceptor.NacosInstanceChangeInterceptor; + +/** + * NacosInstanceChangeDefinition + */ +@Injectable +@Extension(value = "NacosInstanceChangeDefinition", order = PluginDefinition.ORDER_REGISTRY) +@ConditionalOnGovernanceEnabled +@ConditionalOnClass(NacosInstanceChangeDefinition.TYPE_INSTANCES_CHANGE_NOTIFIER) +public class NacosInstanceChangeDefinition extends PluginDefinitionAdapter { + + protected static final String TYPE_INSTANCES_CHANGE_NOTIFIER = "com.alibaba.nacos.client.naming.event.InstancesChangeNotifier"; + + private static final String METHOD_ON_EVENT = "onEvent"; + + @Inject(Registry.COMPONENT_REGISTRY) + private RegistrySupervisor registry; + + public NacosInstanceChangeDefinition() { + this.matcher = () -> MatcherBuilder.named(TYPE_INSTANCES_CHANGE_NOTIFIER); + this.interceptors = new InterceptorDefinition[]{ + new InterceptorDefinitionAdapter( + MatcherBuilder.named(METHOD_ON_EVENT), + () -> new NacosInstanceChangeInterceptor(registry)), + }; + } +} diff --git a/joylive-plugin/joylive-registry/joylive-registry-nacos/src/main/java/com/jd/live/agent/plugin/registry/nacos/instance/NacosEndpoint.java b/joylive-plugin/joylive-registry/joylive-registry-nacos/src/main/java/com/jd/live/agent/plugin/registry/nacos/instance/NacosEndpoint.java new file mode 100644 index 000000000..574055416 --- /dev/null +++ b/joylive-plugin/joylive-registry/joylive-registry-nacos/src/main/java/com/jd/live/agent/plugin/registry/nacos/instance/NacosEndpoint.java @@ -0,0 +1,75 @@ +/* + * Copyright © ${year} ${owner} (${email}) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.jd.live.agent.plugin.registry.nacos.instance; + +import com.alibaba.nacos.api.naming.pojo.Instance; +import com.jd.live.agent.bootstrap.util.AbstractAttributes; +import com.jd.live.agent.core.Constants; +import com.jd.live.agent.core.util.option.Converts; +import com.jd.live.agent.governance.instance.Endpoint; +import com.jd.live.agent.governance.instance.EndpointState; +import com.jd.live.agent.governance.request.ServiceRequest; + +import java.util.Map; + +/** + * A class that represents an endpoint in the Nacos registry. + */ +public class NacosEndpoint extends AbstractAttributes implements Endpoint { + + /** + * The instance associated with this endpoint. + */ + private final Instance instance; + + /** + * Creates a new NacosEndpoint object with the specified instance. + * + * @param instance the instance associated with this endpoint + */ + public NacosEndpoint(Instance instance) { + this.instance = instance; + } + + @Override + public String getHost() { + return instance.getIp(); + } + + @Override + public int getPort() { + return instance.getPort(); + } + + @Override + public String getLabel(String key) { + Map metadata = instance.getMetadata(); + return metadata == null ? null : metadata.get(key); + } + + @Override + public EndpointState getState() { + if (!instance.isEnabled()) { + return EndpointState.DISABLE; + } + return instance.isHealthy() ? EndpointState.HEALTHY : EndpointState.WEAK; + } + + @Override + public Integer getWeight(ServiceRequest request) { + return Converts.getInteger(getLabel(Constants.LABEL_WEIGHT), (int) (instance.getWeight() * DEFAULT_WEIGHT)); + } +} diff --git a/joylive-plugin/joylive-registry/joylive-registry-nacos/src/main/java/com/jd/live/agent/plugin/registry/nacos/interceptor/NacosInstanceChangeInterceptor.java b/joylive-plugin/joylive-registry/joylive-registry-nacos/src/main/java/com/jd/live/agent/plugin/registry/nacos/interceptor/NacosInstanceChangeInterceptor.java new file mode 100644 index 000000000..a64eb2f04 --- /dev/null +++ b/joylive-plugin/joylive-registry/joylive-registry-nacos/src/main/java/com/jd/live/agent/plugin/registry/nacos/interceptor/NacosInstanceChangeInterceptor.java @@ -0,0 +1,46 @@ +/* + * Copyright © ${year} ${owner} (${email}) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.jd.live.agent.plugin.registry.nacos.interceptor; + +import com.alibaba.nacos.client.naming.event.InstancesChangeEvent; +import com.jd.live.agent.bootstrap.bytekit.context.ExecutableContext; +import com.jd.live.agent.bootstrap.bytekit.context.MethodContext; +import com.jd.live.agent.core.plugin.definition.InterceptorAdaptor; +import com.jd.live.agent.governance.registry.RegistrySupervisor; +import com.jd.live.agent.plugin.registry.nacos.instance.NacosEndpoint; + +import static com.jd.live.agent.core.util.CollectionUtils.toList; + +/** + * NacosInstanceChangeInterceptor + */ +public class NacosInstanceChangeInterceptor extends InterceptorAdaptor { + + private final RegistrySupervisor supervisor; + + public NacosInstanceChangeInterceptor(RegistrySupervisor supervisor) { + this.supervisor = supervisor; + } + + @Override + public void onSuccess(ExecutableContext ctx) { + MethodContext mc = (MethodContext) ctx; + InstancesChangeEvent event = mc.getArgument(0); + if (supervisor.isSubscribed(event.getServiceName())) { + supervisor.update(event.getServiceName(), toList(event.getHosts(), NacosEndpoint::new)); + } + } +} diff --git a/joylive-plugin/joylive-registry/joylive-registry-nacos/src/main/resources/META-INF/services/com.jd.live.agent.core.plugin.definition.PluginDefinition b/joylive-plugin/joylive-registry/joylive-registry-nacos/src/main/resources/META-INF/services/com.jd.live.agent.core.plugin.definition.PluginDefinition index ce27ade6b..c24f3ea80 100644 --- a/joylive-plugin/joylive-registry/joylive-registry-nacos/src/main/resources/META-INF/services/com.jd.live.agent.core.plugin.definition.PluginDefinition +++ b/joylive-plugin/joylive-registry/joylive-registry-nacos/src/main/resources/META-INF/services/com.jd.live.agent.core.plugin.definition.PluginDefinition @@ -1 +1,2 @@ -com.jd.live.agent.plugin.registry.nacos.definition.NacosServiceDiscoveryDefinition \ No newline at end of file +com.jd.live.agent.plugin.registry.nacos.definition.NacosServiceDiscoveryDefinition +com.jd.live.agent.plugin.registry.nacos.definition.NacosInstanceChangeDefinition \ No newline at end of file diff --git a/joylive-plugin/joylive-registry/joylive-registry-sofarpc/src/main/java/com/jd/live/agent/plugin/registry/sofarpc/definition/ConsumerBootstrapDefinition.java b/joylive-plugin/joylive-registry/joylive-registry-sofarpc/src/main/java/com/jd/live/agent/plugin/registry/sofarpc/definition/ConsumerBootstrapDefinition.java index a51290fa1..43ff5a163 100644 --- a/joylive-plugin/joylive-registry/joylive-registry-sofarpc/src/main/java/com/jd/live/agent/plugin/registry/sofarpc/definition/ConsumerBootstrapDefinition.java +++ b/joylive-plugin/joylive-registry/joylive-registry-sofarpc/src/main/java/com/jd/live/agent/plugin/registry/sofarpc/definition/ConsumerBootstrapDefinition.java @@ -23,7 +23,7 @@ import com.jd.live.agent.core.instance.Application; import com.jd.live.agent.core.plugin.definition.*; import com.jd.live.agent.governance.annotation.ConditionalOnGovernanceEnabled; -import com.jd.live.agent.governance.policy.PolicySupplier; +import com.jd.live.agent.governance.registry.Registry; import com.jd.live.agent.plugin.registry.sofarpc.interceptor.ConsumerBootstrapInterceptor; import java.util.HashMap; @@ -45,8 +45,8 @@ public class ConsumerBootstrapDefinition extends PluginDefinitionAdapter impleme @Inject(Application.COMPONENT_APPLICATION) private Application application; - @Inject(PolicySupplier.COMPONENT_POLICY_SUPPLIER) - private PolicySupplier policySupplier; + @Inject(Registry.COMPONENT_REGISTRY) + private Registry registry; public ConsumerBootstrapDefinition() { this.matcher = () -> MatcherBuilder.isSubTypeOf(TYPE_CONSUMER_BOOTSTRAP) @@ -55,7 +55,7 @@ public ConsumerBootstrapDefinition() { new InterceptorDefinitionAdapter( MatcherBuilder.named(METHOD_REFER). and(MatcherBuilder.arguments(0)), - () -> new ConsumerBootstrapInterceptor(application, policySupplier)) + () -> new ConsumerBootstrapInterceptor(application, registry)) }; } diff --git a/joylive-plugin/joylive-registry/joylive-registry-sofarpc/src/main/java/com/jd/live/agent/plugin/registry/sofarpc/definition/ProviderBootstrapDefinition.java b/joylive-plugin/joylive-registry/joylive-registry-sofarpc/src/main/java/com/jd/live/agent/plugin/registry/sofarpc/definition/ProviderBootstrapDefinition.java index 9c6d95843..9cc3264d1 100644 --- a/joylive-plugin/joylive-registry/joylive-registry-sofarpc/src/main/java/com/jd/live/agent/plugin/registry/sofarpc/definition/ProviderBootstrapDefinition.java +++ b/joylive-plugin/joylive-registry/joylive-registry-sofarpc/src/main/java/com/jd/live/agent/plugin/registry/sofarpc/definition/ProviderBootstrapDefinition.java @@ -23,7 +23,7 @@ import com.jd.live.agent.core.instance.Application; import com.jd.live.agent.core.plugin.definition.*; import com.jd.live.agent.governance.annotation.ConditionalOnGovernanceEnabled; -import com.jd.live.agent.governance.policy.PolicySupplier; +import com.jd.live.agent.governance.registry.Registry; import com.jd.live.agent.plugin.registry.sofarpc.interceptor.ProviderBootstrapInterceptor; import java.util.HashMap; @@ -45,8 +45,8 @@ public class ProviderBootstrapDefinition extends PluginDefinitionAdapter impleme @Inject(Application.COMPONENT_APPLICATION) private Application application; - @Inject(PolicySupplier.COMPONENT_POLICY_SUPPLIER) - private PolicySupplier policySupplier; + @Inject(Registry.COMPONENT_REGISTRY) + private Registry registry; public ProviderBootstrapDefinition() { this.matcher = () -> MatcherBuilder.isSubTypeOf(TYPE_PROVIDER_BOOTSTRAP). @@ -55,7 +55,7 @@ public ProviderBootstrapDefinition() { new InterceptorDefinitionAdapter( MatcherBuilder.named(METHOD_EXPORT). and(MatcherBuilder.arguments(0)), - () -> new ProviderBootstrapInterceptor(application, policySupplier)) + () -> new ProviderBootstrapInterceptor(application, registry)) }; } diff --git a/joylive-plugin/joylive-registry/joylive-registry-sofarpc/src/main/java/com/jd/live/agent/plugin/registry/sofarpc/definition/RegistryDefinition.java b/joylive-plugin/joylive-registry/joylive-registry-sofarpc/src/main/java/com/jd/live/agent/plugin/registry/sofarpc/definition/RegistryDefinition.java index ae3388724..4b25b1d83 100644 --- a/joylive-plugin/joylive-registry/joylive-registry-sofarpc/src/main/java/com/jd/live/agent/plugin/registry/sofarpc/definition/RegistryDefinition.java +++ b/joylive-plugin/joylive-registry/joylive-registry-sofarpc/src/main/java/com/jd/live/agent/plugin/registry/sofarpc/definition/RegistryDefinition.java @@ -15,7 +15,6 @@ */ package com.jd.live.agent.plugin.registry.sofarpc.definition; -import com.jd.live.agent.core.bootstrap.AgentLifecycle; import com.jd.live.agent.core.bytekit.matcher.MatcherBuilder; import com.jd.live.agent.core.extension.annotation.ConditionalOnClass; import com.jd.live.agent.core.extension.annotation.Extension; @@ -52,9 +51,6 @@ public class RegistryDefinition extends PluginDefinitionAdapter { @Inject(Application.COMPONENT_APPLICATION) private Application application; - @Inject(AgentLifecycle.COMPONENT_AGENT_LIFECYCLE) - private AgentLifecycle lifecycle; - @Inject(Registry.COMPONENT_REGISTRY) private Registry registry; @@ -76,7 +72,7 @@ public RegistryDefinition() { MatcherBuilder.named(METHOD_REGISTER) .and(MatcherBuilder.arguments(ARGUMENT_REGISTER)) .and(MatcherBuilder.not(MatcherBuilder.isAbstract())), - () -> new RegistryInterceptor(application, lifecycle, registry)) + () -> new RegistryInterceptor(application, registry)) }; } } diff --git a/joylive-plugin/joylive-registry/joylive-registry-sofarpc/src/main/java/com/jd/live/agent/plugin/registry/sofarpc/interceptor/AbstractBootstrapInterceptor.java b/joylive-plugin/joylive-registry/joylive-registry-sofarpc/src/main/java/com/jd/live/agent/plugin/registry/sofarpc/interceptor/AbstractBootstrapInterceptor.java index 99c37c7f2..1a9c79558 100644 --- a/joylive-plugin/joylive-registry/joylive-registry-sofarpc/src/main/java/com/jd/live/agent/plugin/registry/sofarpc/interceptor/AbstractBootstrapInterceptor.java +++ b/joylive-plugin/joylive-registry/joylive-registry-sofarpc/src/main/java/com/jd/live/agent/plugin/registry/sofarpc/interceptor/AbstractBootstrapInterceptor.java @@ -19,7 +19,7 @@ import com.jd.live.agent.bootstrap.bytekit.context.ExecutableContext; import com.jd.live.agent.core.instance.Application; import com.jd.live.agent.core.plugin.definition.InterceptorAdaptor; -import com.jd.live.agent.governance.policy.PolicySupplier; +import com.jd.live.agent.governance.registry.Registry; /** * AbstractBootstrapInterceptor @@ -28,11 +28,11 @@ public abstract class AbstractBootstrapInterceptor> { - public ConsumerBootstrapInterceptor(Application application, PolicySupplier policySupplier) { - super(application, policySupplier); + public ConsumerBootstrapInterceptor(Application application, Registry registry) { + super(application, registry); } @Override diff --git a/joylive-plugin/joylive-registry/joylive-registry-sofarpc/src/main/java/com/jd/live/agent/plugin/registry/sofarpc/interceptor/ProviderBootstrapInterceptor.java b/joylive-plugin/joylive-registry/joylive-registry-sofarpc/src/main/java/com/jd/live/agent/plugin/registry/sofarpc/interceptor/ProviderBootstrapInterceptor.java index f915f1b03..73c5565e3 100644 --- a/joylive-plugin/joylive-registry/joylive-registry-sofarpc/src/main/java/com/jd/live/agent/plugin/registry/sofarpc/interceptor/ProviderBootstrapInterceptor.java +++ b/joylive-plugin/joylive-registry/joylive-registry-sofarpc/src/main/java/com/jd/live/agent/plugin/registry/sofarpc/interceptor/ProviderBootstrapInterceptor.java @@ -19,15 +19,15 @@ import com.alipay.sofa.rpc.config.ProviderConfig; import com.jd.live.agent.bootstrap.bytekit.context.ExecutableContext; import com.jd.live.agent.core.instance.Application; -import com.jd.live.agent.governance.policy.PolicySupplier; +import com.jd.live.agent.governance.registry.Registry; /** * ProviderBootstrapInterceptor */ public class ProviderBootstrapInterceptor extends AbstractBootstrapInterceptor> { - public ProviderBootstrapInterceptor(Application application, PolicySupplier policySupplier) { - super(application, policySupplier); + public ProviderBootstrapInterceptor(Application application, Registry registry) { + super(application, registry); } @Override diff --git a/joylive-plugin/joylive-registry/joylive-registry-sofarpc/src/main/java/com/jd/live/agent/plugin/registry/sofarpc/interceptor/RegistryInterceptor.java b/joylive-plugin/joylive-registry/joylive-registry-sofarpc/src/main/java/com/jd/live/agent/plugin/registry/sofarpc/interceptor/RegistryInterceptor.java index 958947e16..561ad89e1 100644 --- a/joylive-plugin/joylive-registry/joylive-registry-sofarpc/src/main/java/com/jd/live/agent/plugin/registry/sofarpc/interceptor/RegistryInterceptor.java +++ b/joylive-plugin/joylive-registry/joylive-registry-sofarpc/src/main/java/com/jd/live/agent/plugin/registry/sofarpc/interceptor/RegistryInterceptor.java @@ -19,7 +19,6 @@ import com.alipay.sofa.rpc.config.ServerConfig; import com.alipay.sofa.rpc.registry.utils.RegistryUtils; import com.jd.live.agent.bootstrap.bytekit.context.MethodContext; -import com.jd.live.agent.core.bootstrap.AgentLifecycle; import com.jd.live.agent.core.instance.Application; import com.jd.live.agent.core.util.StringUtils; import com.jd.live.agent.governance.interceptor.AbstractRegistryInterceptor; @@ -35,8 +34,8 @@ */ public class RegistryInterceptor extends AbstractRegistryInterceptor { - public RegistryInterceptor(Application application, AgentLifecycle lifecycle, Registry registry) { - super(application, lifecycle, registry); + public RegistryInterceptor(Application application, Registry registry) { + super(application, registry); } @Override diff --git a/joylive-plugin/joylive-registry/joylive-registry-springcloud3/src/main/java/com/jd/live/agent/plugin/registry/springcloud/v3/definition/DiscoveryClientDefinition.java b/joylive-plugin/joylive-registry/joylive-registry-springcloud3/src/main/java/com/jd/live/agent/plugin/registry/springcloud/v3/definition/DiscoveryClientDefinition.java index 168226692..ab0075700 100644 --- a/joylive-plugin/joylive-registry/joylive-registry-springcloud3/src/main/java/com/jd/live/agent/plugin/registry/springcloud/v3/definition/DiscoveryClientDefinition.java +++ b/joylive-plugin/joylive-registry/joylive-registry-springcloud3/src/main/java/com/jd/live/agent/plugin/registry/springcloud/v3/definition/DiscoveryClientDefinition.java @@ -25,7 +25,7 @@ import com.jd.live.agent.core.plugin.definition.PluginDefinition; import com.jd.live.agent.core.plugin.definition.PluginDefinitionAdapter; import com.jd.live.agent.governance.annotation.ConditionalOnGovernanceEnabled; -import com.jd.live.agent.governance.policy.PolicySupplier; +import com.jd.live.agent.governance.registry.Registry; import com.jd.live.agent.plugin.registry.springcloud.v3.interceptor.DiscoveryClientConstructorInterceptor; import com.jd.live.agent.plugin.registry.springcloud.v3.interceptor.DiscoveryClientGetInterceptor; @@ -42,18 +42,18 @@ public class DiscoveryClientDefinition extends PluginDefinitionAdapter { private static final String METHOD_GET = "get"; - @Inject(PolicySupplier.COMPONENT_POLICY_SUPPLIER) - private PolicySupplier policySupplier; + @Inject(Registry.COMPONENT_REGISTRY) + private Registry registry; public DiscoveryClientDefinition() { this.matcher = () -> MatcherBuilder.named(TYPE_DISCOVERY_CLIENT); this.interceptors = new InterceptorDefinition[]{ new InterceptorDefinitionAdapter( - MatcherBuilder.isConstructor(), () -> new DiscoveryClientConstructorInterceptor(policySupplier)), + MatcherBuilder.isConstructor(), () -> new DiscoveryClientConstructorInterceptor(registry)), new InterceptorDefinitionAdapter( MatcherBuilder.named(METHOD_GET) .and(MatcherBuilder.arguments(0)), - () -> new DiscoveryClientGetInterceptor(policySupplier)) + () -> new DiscoveryClientGetInterceptor(registry)) }; } } diff --git a/joylive-plugin/joylive-registry/joylive-registry-springcloud3/src/main/java/com/jd/live/agent/plugin/registry/springcloud/v3/definition/FeignClientFactoryBeanDefinition.java b/joylive-plugin/joylive-registry/joylive-registry-springcloud3/src/main/java/com/jd/live/agent/plugin/registry/springcloud/v3/definition/FeignClientFactoryBeanDefinition.java index 26ac5e74b..5bf19daec 100644 --- a/joylive-plugin/joylive-registry/joylive-registry-springcloud3/src/main/java/com/jd/live/agent/plugin/registry/springcloud/v3/definition/FeignClientFactoryBeanDefinition.java +++ b/joylive-plugin/joylive-registry/joylive-registry-springcloud3/src/main/java/com/jd/live/agent/plugin/registry/springcloud/v3/definition/FeignClientFactoryBeanDefinition.java @@ -25,7 +25,7 @@ import com.jd.live.agent.core.plugin.definition.PluginDefinition; import com.jd.live.agent.core.plugin.definition.PluginDefinitionAdapter; import com.jd.live.agent.governance.annotation.ConditionalOnGovernanceEnabled; -import com.jd.live.agent.governance.policy.PolicySupplier; +import com.jd.live.agent.governance.registry.Registry; import com.jd.live.agent.plugin.registry.springcloud.v3.interceptor.FeignClientFactoryBeanInterceptor; /** @@ -51,8 +51,8 @@ public class FeignClientFactoryBeanDefinition extends PluginDefinitionAdapter { "org.springframework.cloud.openfeign.FeignClientFactory" }; - @Inject(PolicySupplier.COMPONENT_POLICY_SUPPLIER) - private PolicySupplier policySupplier; + @Inject(Registry.COMPONENT_REGISTRY) + private Registry registry; public FeignClientFactoryBeanDefinition() { this.matcher = () -> MatcherBuilder.named(TYPE_FEIGN_CLIENT_FACTORY_BEAN); @@ -60,11 +60,11 @@ public FeignClientFactoryBeanDefinition() { new InterceptorDefinitionAdapter( MatcherBuilder.named(METHOD_FEIGN). and(MatcherBuilder.arguments(ARGUMENT_FEIGN3)), - () -> new FeignClientFactoryBeanInterceptor(policySupplier)), + () -> new FeignClientFactoryBeanInterceptor(registry)), new InterceptorDefinitionAdapter( MatcherBuilder.named(METHOD_FEIGN). and(MatcherBuilder.arguments(ARGUMENT_FEIGN4)), - () -> new FeignClientFactoryBeanInterceptor(policySupplier)) + () -> new FeignClientFactoryBeanInterceptor(registry)) }; } } diff --git a/joylive-plugin/joylive-registry/joylive-registry-springcloud3/src/main/java/com/jd/live/agent/plugin/registry/springcloud/v3/definition/RegistryDefinition.java b/joylive-plugin/joylive-registry/joylive-registry-springcloud3/src/main/java/com/jd/live/agent/plugin/registry/springcloud/v3/definition/RegistryDefinition.java index 6988181a5..96cc97cc7 100644 --- a/joylive-plugin/joylive-registry/joylive-registry-springcloud3/src/main/java/com/jd/live/agent/plugin/registry/springcloud/v3/definition/RegistryDefinition.java +++ b/joylive-plugin/joylive-registry/joylive-registry-springcloud3/src/main/java/com/jd/live/agent/plugin/registry/springcloud/v3/definition/RegistryDefinition.java @@ -15,7 +15,6 @@ */ package com.jd.live.agent.plugin.registry.springcloud.v3.definition; -import com.jd.live.agent.core.bootstrap.AgentLifecycle; import com.jd.live.agent.core.bytekit.matcher.MatcherBuilder; import com.jd.live.agent.core.extension.annotation.ConditionalOnClass; import com.jd.live.agent.core.extension.annotation.Extension; @@ -27,7 +26,6 @@ import com.jd.live.agent.core.plugin.definition.PluginDefinition; import com.jd.live.agent.core.plugin.definition.PluginDefinitionAdapter; import com.jd.live.agent.governance.annotation.ConditionalOnGovernanceEnabled; -import com.jd.live.agent.governance.policy.PolicySupplier; import com.jd.live.agent.governance.registry.Registry; import com.jd.live.agent.plugin.registry.springcloud.v3.interceptor.RegistryInterceptor; @@ -52,12 +50,6 @@ public class RegistryDefinition extends PluginDefinitionAdapter { @Inject(Application.COMPONENT_APPLICATION) private Application application; - @Inject(AgentLifecycle.COMPONENT_AGENT_LIFECYCLE) - private AgentLifecycle lifecycle; - - @Inject(PolicySupplier.COMPONENT_POLICY_SUPPLIER) - private PolicySupplier policySupplier; - @Inject(Registry.COMPONENT_REGISTRY) private Registry registry; @@ -67,7 +59,7 @@ public RegistryDefinition() { new InterceptorDefinitionAdapter( MatcherBuilder.named(METHOD_REGISTER). and(MatcherBuilder.arguments(MatcherBuilder.isSubTypeOf(ARGUMENT_REGISTER))), - () -> new RegistryInterceptor(application, lifecycle, registry, policySupplier)) + () -> new RegistryInterceptor(application, registry)) }; } } diff --git a/joylive-plugin/joylive-registry/joylive-registry-springcloud3/src/main/java/com/jd/live/agent/plugin/registry/springcloud/v3/interceptor/DiscoveryClientConstructorInterceptor.java b/joylive-plugin/joylive-registry/joylive-registry-springcloud3/src/main/java/com/jd/live/agent/plugin/registry/springcloud/v3/interceptor/DiscoveryClientConstructorInterceptor.java index 4cfb42c87..d59495e8b 100644 --- a/joylive-plugin/joylive-registry/joylive-registry-springcloud3/src/main/java/com/jd/live/agent/plugin/registry/springcloud/v3/interceptor/DiscoveryClientConstructorInterceptor.java +++ b/joylive-plugin/joylive-registry/joylive-registry-springcloud3/src/main/java/com/jd/live/agent/plugin/registry/springcloud/v3/interceptor/DiscoveryClientConstructorInterceptor.java @@ -19,7 +19,7 @@ import com.jd.live.agent.bootstrap.logger.Logger; import com.jd.live.agent.bootstrap.logger.LoggerFactory; import com.jd.live.agent.core.plugin.definition.InterceptorAdaptor; -import com.jd.live.agent.governance.policy.PolicySupplier; +import com.jd.live.agent.governance.registry.Registry; import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier; import java.util.concurrent.ExecutionException; @@ -33,10 +33,10 @@ public class DiscoveryClientConstructorInterceptor extends InterceptorAdaptor { private static final Logger logger = LoggerFactory.getLogger(DiscoveryClientConstructorInterceptor.class); - private final PolicySupplier policySupplier; + private final Registry registry; - public DiscoveryClientConstructorInterceptor(PolicySupplier policySupplier) { - this.policySupplier = policySupplier; + public DiscoveryClientConstructorInterceptor(Registry registry) { + this.registry = registry; } @Override @@ -46,7 +46,7 @@ public void onSuccess(ExecutableContext ctx) { // Built at runtime, cannot intercept and obtain the required service during the startup phase // restTemplate.getForObject("http://service-provider/echo/" + str, String.class) try { - policySupplier.subscribe(serviceId).get(5000, TimeUnit.MILLISECONDS); + registry.subscribe(serviceId).get(5000, TimeUnit.MILLISECONDS); } catch (InterruptedException ignore) { } catch (ExecutionException e) { Throwable cause = e.getCause() != null ? e.getCause() : e; diff --git a/joylive-plugin/joylive-registry/joylive-registry-springcloud3/src/main/java/com/jd/live/agent/plugin/registry/springcloud/v3/interceptor/DiscoveryClientGetInterceptor.java b/joylive-plugin/joylive-registry/joylive-registry-springcloud3/src/main/java/com/jd/live/agent/plugin/registry/springcloud/v3/interceptor/DiscoveryClientGetInterceptor.java index 2161941a7..3f6b136a1 100644 --- a/joylive-plugin/joylive-registry/joylive-registry-springcloud3/src/main/java/com/jd/live/agent/plugin/registry/springcloud/v3/interceptor/DiscoveryClientGetInterceptor.java +++ b/joylive-plugin/joylive-registry/joylive-registry-springcloud3/src/main/java/com/jd/live/agent/plugin/registry/springcloud/v3/interceptor/DiscoveryClientGetInterceptor.java @@ -18,7 +18,7 @@ import com.jd.live.agent.bootstrap.bytekit.context.ExecutableContext; import com.jd.live.agent.bootstrap.bytekit.context.MethodContext; import com.jd.live.agent.core.plugin.definition.InterceptorAdaptor; -import com.jd.live.agent.governance.policy.PolicySupplier; +import com.jd.live.agent.governance.registry.Registry; import org.springframework.cloud.loadbalancer.core.DiscoveryClientServiceInstanceListSupplier; import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier; import org.springframework.http.HttpHeaders; @@ -27,16 +27,17 @@ import reactor.core.publisher.Flux; import java.nio.charset.StandardCharsets; +import java.util.concurrent.CompletableFuture; /** * DiscoveryClientGetInterceptor */ public class DiscoveryClientGetInterceptor extends InterceptorAdaptor { - private final PolicySupplier policySupplier; + private final Registry registry; - public DiscoveryClientGetInterceptor(PolicySupplier policySupplier) { - this.policySupplier = policySupplier; + public DiscoveryClientGetInterceptor(Registry registry) { + this.registry = registry; } /** @@ -48,7 +49,8 @@ public DiscoveryClientGetInterceptor(PolicySupplier policySupplier) { public void onEnter(ExecutableContext ctx) { MethodContext mc = (MethodContext) ctx; ServiceInstanceListSupplier supplier = (ServiceInstanceListSupplier) mc.getTarget(); - if (!policySupplier.isDone(supplier.getServiceId())) { + CompletableFuture future = registry.subscribe(supplier.getServiceId()); + if (!future.isDone() || future.isCompletedExceptionally()) { mc.setResult(Flux.error(HttpClientErrorException.create( HttpStatus.INTERNAL_SERVER_ERROR, "Governance policy is not synchronized.", diff --git a/joylive-plugin/joylive-registry/joylive-registry-springcloud3/src/main/java/com/jd/live/agent/plugin/registry/springcloud/v3/interceptor/FeignClientFactoryBeanInterceptor.java b/joylive-plugin/joylive-registry/joylive-registry-springcloud3/src/main/java/com/jd/live/agent/plugin/registry/springcloud/v3/interceptor/FeignClientFactoryBeanInterceptor.java index 111f44c9d..fdefe8e93 100644 --- a/joylive-plugin/joylive-registry/joylive-registry-springcloud3/src/main/java/com/jd/live/agent/plugin/registry/springcloud/v3/interceptor/FeignClientFactoryBeanInterceptor.java +++ b/joylive-plugin/joylive-registry/joylive-registry-springcloud3/src/main/java/com/jd/live/agent/plugin/registry/springcloud/v3/interceptor/FeignClientFactoryBeanInterceptor.java @@ -17,7 +17,7 @@ import com.jd.live.agent.bootstrap.bytekit.context.ExecutableContext; import com.jd.live.agent.core.plugin.definition.InterceptorAdaptor; -import com.jd.live.agent.governance.policy.PolicySupplier; +import com.jd.live.agent.governance.registry.Registry; import org.springframework.cloud.openfeign.FeignClientFactoryBean; /** @@ -25,15 +25,15 @@ */ public class FeignClientFactoryBeanInterceptor extends InterceptorAdaptor { - private final PolicySupplier policySupplier; + private final Registry registry; - public FeignClientFactoryBeanInterceptor(PolicySupplier policySupplier) { - this.policySupplier = policySupplier; + public FeignClientFactoryBeanInterceptor(Registry registry) { + this.registry = registry; } @Override public void onEnter(ExecutableContext ctx) { FeignClientFactoryBean factoryBean = (FeignClientFactoryBean) ctx.getTarget(); - policySupplier.subscribe(factoryBean.getName()); + registry.subscribe(factoryBean.getName()); } } diff --git a/joylive-plugin/joylive-registry/joylive-registry-springcloud3/src/main/java/com/jd/live/agent/plugin/registry/springcloud/v3/interceptor/RegistryInterceptor.java b/joylive-plugin/joylive-registry/joylive-registry-springcloud3/src/main/java/com/jd/live/agent/plugin/registry/springcloud/v3/interceptor/RegistryInterceptor.java index 8bb69fa35..57720b52d 100644 --- a/joylive-plugin/joylive-registry/joylive-registry-springcloud3/src/main/java/com/jd/live/agent/plugin/registry/springcloud/v3/interceptor/RegistryInterceptor.java +++ b/joylive-plugin/joylive-registry/joylive-registry-springcloud3/src/main/java/com/jd/live/agent/plugin/registry/springcloud/v3/interceptor/RegistryInterceptor.java @@ -18,10 +18,8 @@ import com.jd.live.agent.bootstrap.bytekit.context.ExecutableContext; import com.jd.live.agent.bootstrap.bytekit.context.MethodContext; import com.jd.live.agent.core.Constants; -import com.jd.live.agent.core.bootstrap.AgentLifecycle; import com.jd.live.agent.core.instance.Application; import com.jd.live.agent.governance.interceptor.AbstractRegistryInterceptor; -import com.jd.live.agent.governance.policy.PolicySupplier; import com.jd.live.agent.governance.registry.Registry; import com.jd.live.agent.governance.registry.ServiceInstance; import com.jd.live.agent.governance.registry.ServiceProtocol; @@ -38,11 +36,8 @@ */ public class RegistryInterceptor extends AbstractRegistryInterceptor { - private final PolicySupplier policySupplier; - - public RegistryInterceptor(Application application, AgentLifecycle lifecycle, Registry registry, PolicySupplier policySupplier) { - super(application, lifecycle, registry); - this.policySupplier = policySupplier; + public RegistryInterceptor(Application application, Registry registry) { + super(application, registry); } /** @@ -58,7 +53,7 @@ public void onEnter(ExecutableContext ctx) { application.labelRegistry(metadata::putIfAbsent, true); metadata.put(Constants.LABEL_FRAMEWORK, "spring-boot-" + SpringBootVersion.getVersion()); } - policySupplier.subscribe(registration.getServiceId()); + registry.subscribe(registration.getServiceId()); super.onEnter(ctx); } diff --git a/joylive-plugin/joylive-registry/joylive-registry-springgateway3/src/main/java/com/jd/live/agent/plugin/registry/springgateway/v3/definition/RouteDefinition.java b/joylive-plugin/joylive-registry/joylive-registry-springgateway3/src/main/java/com/jd/live/agent/plugin/registry/springgateway/v3/definition/RouteDefinition.java index 565a09458..663e2d62d 100644 --- a/joylive-plugin/joylive-registry/joylive-registry-springgateway3/src/main/java/com/jd/live/agent/plugin/registry/springgateway/v3/definition/RouteDefinition.java +++ b/joylive-plugin/joylive-registry/joylive-registry-springgateway3/src/main/java/com/jd/live/agent/plugin/registry/springgateway/v3/definition/RouteDefinition.java @@ -24,7 +24,7 @@ import com.jd.live.agent.core.plugin.definition.InterceptorDefinitionAdapter; import com.jd.live.agent.core.plugin.definition.PluginDefinition; import com.jd.live.agent.core.plugin.definition.PluginDefinitionAdapter; -import com.jd.live.agent.governance.policy.PolicySupplier; +import com.jd.live.agent.governance.registry.Registry; import com.jd.live.agent.plugin.registry.springgateway.v3.condition.ConditionalOnSpringGateway3GovernanceEnabled; import com.jd.live.agent.plugin.registry.springgateway.v3.interceptor.RouteInterceptor; @@ -45,8 +45,8 @@ public class RouteDefinition extends PluginDefinitionAdapter { "org.springframework.cloud.gateway.route.RouteDefinition" }; - @Inject(PolicySupplier.COMPONENT_POLICY_SUPPLIER) - private PolicySupplier policySupplier; + @Inject(Registry.COMPONENT_REGISTRY) + private Registry registry; public RouteDefinition() { this.matcher = () -> MatcherBuilder.named(TYPE_ROUTE_DEFINITION_ROUTE_LOCATOR); @@ -54,7 +54,7 @@ public RouteDefinition() { new InterceptorDefinitionAdapter( MatcherBuilder.named(METHOD_CONVERT_TO_ROUTE). and(MatcherBuilder.arguments(ARGUMENT_CONVERT_TO_ROUTE)), - () -> new RouteInterceptor(policySupplier)) + () -> new RouteInterceptor(registry)) }; } } diff --git a/joylive-plugin/joylive-registry/joylive-registry-springgateway3/src/main/java/com/jd/live/agent/plugin/registry/springgateway/v3/interceptor/RouteInterceptor.java b/joylive-plugin/joylive-registry/joylive-registry-springgateway3/src/main/java/com/jd/live/agent/plugin/registry/springgateway/v3/interceptor/RouteInterceptor.java index bcef831ca..dcf0b0509 100644 --- a/joylive-plugin/joylive-registry/joylive-registry-springgateway3/src/main/java/com/jd/live/agent/plugin/registry/springgateway/v3/interceptor/RouteInterceptor.java +++ b/joylive-plugin/joylive-registry/joylive-registry-springgateway3/src/main/java/com/jd/live/agent/plugin/registry/springgateway/v3/interceptor/RouteInterceptor.java @@ -17,7 +17,7 @@ import com.jd.live.agent.bootstrap.bytekit.context.ExecutableContext; import com.jd.live.agent.core.plugin.definition.InterceptorAdaptor; -import com.jd.live.agent.governance.policy.PolicySupplier; +import com.jd.live.agent.governance.registry.Registry; import org.springframework.cloud.gateway.route.RouteDefinition; import java.net.URI; @@ -29,10 +29,10 @@ public class RouteInterceptor extends InterceptorAdaptor { private static final String SCHEMA_LB = "lb"; - private final PolicySupplier policySupplier; + private final Registry registry; - public RouteInterceptor(PolicySupplier policySupplier) { - this.policySupplier = policySupplier; + public RouteInterceptor(Registry registry) { + this.registry = registry; } @Override @@ -40,7 +40,7 @@ public void onEnter(ExecutableContext ctx) { RouteDefinition definition = (RouteDefinition) ctx.getArguments()[0]; URI uri = definition.getUri(); if (SCHEMA_LB.equals(uri.getScheme())) { - policySupplier.subscribe(uri.getHost()); + registry.subscribe(uri.getHost()); } } } diff --git a/joylive-plugin/joylive-registry/joylive-registry-springgateway4/src/main/java/com/jd/live/agent/plugin/registry/springgateway/v4/definition/RouteDefinition.java b/joylive-plugin/joylive-registry/joylive-registry-springgateway4/src/main/java/com/jd/live/agent/plugin/registry/springgateway/v4/definition/RouteDefinition.java index b54bf573b..9701ce205 100644 --- a/joylive-plugin/joylive-registry/joylive-registry-springgateway4/src/main/java/com/jd/live/agent/plugin/registry/springgateway/v4/definition/RouteDefinition.java +++ b/joylive-plugin/joylive-registry/joylive-registry-springgateway4/src/main/java/com/jd/live/agent/plugin/registry/springgateway/v4/definition/RouteDefinition.java @@ -24,7 +24,7 @@ import com.jd.live.agent.core.plugin.definition.InterceptorDefinitionAdapter; import com.jd.live.agent.core.plugin.definition.PluginDefinition; import com.jd.live.agent.core.plugin.definition.PluginDefinitionAdapter; -import com.jd.live.agent.governance.policy.PolicySupplier; +import com.jd.live.agent.governance.registry.Registry; import com.jd.live.agent.plugin.registry.springgateway.v4.condition.ConditionalOnSpringGateway4GovernanceEnabled; import com.jd.live.agent.plugin.registry.springgateway.v4.interceptor.RouteInterceptor; @@ -45,8 +45,8 @@ public class RouteDefinition extends PluginDefinitionAdapter { "org.springframework.cloud.gateway.route.RouteDefinition" }; - @Inject(PolicySupplier.COMPONENT_POLICY_SUPPLIER) - private PolicySupplier policySupplier; + @Inject(Registry.COMPONENT_REGISTRY) + private Registry registry; public RouteDefinition() { this.matcher = () -> MatcherBuilder.named(TYPE_ROUTE_DEFINITION_ROUTE_LOCATOR); @@ -54,7 +54,7 @@ public RouteDefinition() { new InterceptorDefinitionAdapter( MatcherBuilder.named(METHOD_CONVERT_TO_ROUTE). and(MatcherBuilder.arguments(ARGUMENT_CONVERT_TO_ROUTE)), - () -> new RouteInterceptor(policySupplier)) + () -> new RouteInterceptor(registry)) }; } } diff --git a/joylive-plugin/joylive-registry/joylive-registry-springgateway4/src/main/java/com/jd/live/agent/plugin/registry/springgateway/v4/interceptor/RouteInterceptor.java b/joylive-plugin/joylive-registry/joylive-registry-springgateway4/src/main/java/com/jd/live/agent/plugin/registry/springgateway/v4/interceptor/RouteInterceptor.java index 10a4260a4..236a72f03 100644 --- a/joylive-plugin/joylive-registry/joylive-registry-springgateway4/src/main/java/com/jd/live/agent/plugin/registry/springgateway/v4/interceptor/RouteInterceptor.java +++ b/joylive-plugin/joylive-registry/joylive-registry-springgateway4/src/main/java/com/jd/live/agent/plugin/registry/springgateway/v4/interceptor/RouteInterceptor.java @@ -17,7 +17,7 @@ import com.jd.live.agent.bootstrap.bytekit.context.ExecutableContext; import com.jd.live.agent.core.plugin.definition.InterceptorAdaptor; -import com.jd.live.agent.governance.policy.PolicySupplier; +import com.jd.live.agent.governance.registry.Registry; import org.springframework.cloud.gateway.route.RouteDefinition; import java.net.URI; @@ -29,10 +29,10 @@ public class RouteInterceptor extends InterceptorAdaptor { private static final String SCHEMA_LB = "lb"; - private final PolicySupplier policySupplier; + private final Registry registry; - public RouteInterceptor(PolicySupplier policySupplier) { - this.policySupplier = policySupplier; + public RouteInterceptor(Registry registry) { + this.registry = registry; } @Override @@ -40,7 +40,7 @@ public void onEnter(ExecutableContext ctx) { RouteDefinition definition = (RouteDefinition) ctx.getArguments()[0]; URI uri = definition.getUri(); if (SCHEMA_LB.equals(uri.getScheme())) { - policySupplier.subscribe(uri.getHost()); + registry.subscribe(uri.getHost()); } } } diff --git a/joylive-plugin/joylive-router/joylive-router-grpc/src/main/java/com/jd/live/agent/plugin/router/gprc/definition/LoadbalancerDefinition.java b/joylive-plugin/joylive-router/joylive-router-grpc/src/main/java/com/jd/live/agent/plugin/router/gprc/definition/LoadbalancerDefinition.java index 474b118bb..7f6daaa0b 100644 --- a/joylive-plugin/joylive-router/joylive-router-grpc/src/main/java/com/jd/live/agent/plugin/router/gprc/definition/LoadbalancerDefinition.java +++ b/joylive-plugin/joylive-router/joylive-router-grpc/src/main/java/com/jd/live/agent/plugin/router/gprc/definition/LoadbalancerDefinition.java @@ -18,11 +18,13 @@ import com.jd.live.agent.core.bytekit.matcher.MatcherBuilder; import com.jd.live.agent.core.extension.annotation.ConditionalOnClass; import com.jd.live.agent.core.extension.annotation.Extension; +import com.jd.live.agent.core.inject.annotation.Inject; import com.jd.live.agent.core.inject.annotation.Injectable; import com.jd.live.agent.core.plugin.definition.InterceptorDefinition; import com.jd.live.agent.core.plugin.definition.InterceptorDefinitionAdapter; import com.jd.live.agent.core.plugin.definition.PluginDefinition; import com.jd.live.agent.core.plugin.definition.PluginDefinitionAdapter; +import com.jd.live.agent.governance.registry.Registry; import com.jd.live.agent.plugin.router.gprc.condition.ConditionalOnGrpcGovernanceEnabled; import com.jd.live.agent.plugin.router.gprc.interceptor.LoadbalancerInterceptor; @@ -40,12 +42,15 @@ public class LoadbalancerDefinition extends PluginDefinitionAdapter { "java.lang.String" }; + @Inject(Registry.COMPONENT_REGISTRY) + private Registry registry; + public LoadbalancerDefinition() { this.matcher = () -> MatcherBuilder.named(TYPE); this.interceptors = new InterceptorDefinition[]{ new InterceptorDefinitionAdapter( MatcherBuilder.named(METHOD).and(MatcherBuilder.arguments(ARGUMENTS)), - LoadbalancerInterceptor::new) + () -> new LoadbalancerInterceptor(registry)) }; } diff --git a/joylive-plugin/joylive-router/joylive-router-grpc/src/main/java/com/jd/live/agent/plugin/router/gprc/interceptor/LoadbalancerInterceptor.java b/joylive-plugin/joylive-router/joylive-router-grpc/src/main/java/com/jd/live/agent/plugin/router/gprc/interceptor/LoadbalancerInterceptor.java index cec20eec5..c82288441 100644 --- a/joylive-plugin/joylive-router/joylive-router-grpc/src/main/java/com/jd/live/agent/plugin/router/gprc/interceptor/LoadbalancerInterceptor.java +++ b/joylive-plugin/joylive-router/joylive-router-grpc/src/main/java/com/jd/live/agent/plugin/router/gprc/interceptor/LoadbalancerInterceptor.java @@ -18,6 +18,7 @@ import com.jd.live.agent.bootstrap.bytekit.context.ExecutableContext; import com.jd.live.agent.bootstrap.bytekit.context.MethodContext; import com.jd.live.agent.core.plugin.definition.InterceptorAdaptor; +import com.jd.live.agent.governance.registry.Registry; import com.jd.live.agent.plugin.router.gprc.loadbalance.LiveLoadBalancerProvider; /** @@ -25,9 +26,15 @@ */ public class LoadbalancerInterceptor extends InterceptorAdaptor { + private final Registry registry; + + public LoadbalancerInterceptor(Registry registry) { + this.registry = registry; + } + @Override public void onEnter(ExecutableContext ctx) { MethodContext mc = (MethodContext) ctx; - mc.skipWithResult(new LiveLoadBalancerProvider()); + mc.skipWithResult(new LiveLoadBalancerProvider(registry)); } } diff --git a/joylive-plugin/joylive-router/joylive-router-grpc/src/main/java/com/jd/live/agent/plugin/router/gprc/loadbalance/LiveLoadBalancer.java b/joylive-plugin/joylive-router/joylive-router-grpc/src/main/java/com/jd/live/agent/plugin/router/gprc/loadbalance/LiveLoadBalancer.java index 8bbefa3bf..0415a00f0 100644 --- a/joylive-plugin/joylive-router/joylive-router-grpc/src/main/java/com/jd/live/agent/plugin/router/gprc/loadbalance/LiveLoadBalancer.java +++ b/joylive-plugin/joylive-router/joylive-router-grpc/src/main/java/com/jd/live/agent/plugin/router/gprc/loadbalance/LiveLoadBalancer.java @@ -15,6 +15,7 @@ */ package com.jd.live.agent.plugin.router.gprc.loadbalance; +import com.jd.live.agent.governance.registry.EndpointEvent; import com.jd.live.agent.plugin.router.gprc.instance.GrpcEndpoint; import io.grpc.*; @@ -43,6 +44,21 @@ public LiveLoadBalancer(Helper helper) { this.serviceName = getServiceName(helper); } + public String getServiceName() { + return serviceName; + } + + /** + * Handles an endpoint event by refreshing the name resolution if the event is for the same service. + * + * @param event the endpoint event to handle + */ + protected void handle(EndpointEvent event) { + if (serviceName.equals(event.getService())) { + helper.getSynchronizationContext().execute(helper::refreshNameResolution); + } + } + @Override public void handleResolvedAddresses(ResolvedAddresses resolvedAddresses) { Set latestAddresses = deDup(resolvedAddresses); @@ -82,7 +98,7 @@ public void handleResolvedAddresses(ResolvedAddresses resolvedAddresses) { @Override public void handleNameResolutionError(Status error) { -// helper.updateBalancingState(TRANSIENT_FAILURE, new LiveSubchannelPicker(PickResult.withError(error))); + updatePicker(TRANSIENT_FAILURE, new LiveSubchannelPicker(PickResult.withDrop(error))); handleResolvedAddresses(null); } @@ -157,22 +173,23 @@ private GrpcEndpoint createEndpoint(EquivalentAddressGroup addressGroup) { private void pickReady() { List readies = new ArrayList<>(); endpoints.values().forEach(endpoint -> { - if (endpoint.getConnectivityState() == ConnectivityState.READY) { + if (endpoint.getConnectivityState() == READY) { readies.add(endpoint); } }); if (readies.isEmpty()) { - PickResult result = PickResult.withDrop(Status.UNAVAILABLE.withDescription(NO_ENDPOINT_AVAILABLE)); - LiveSubchannelPicker picker = new LiveSubchannelPicker(result); - helper.updateBalancingState(ConnectivityState.CONNECTING, picker); - LiveDiscovery.setSubchannelPicker(serviceName, picker); + updatePicker(TRANSIENT_FAILURE, new LiveSubchannelPicker(PickResult.withDrop( + Status.UNAVAILABLE.withDescription(NO_ENDPOINT_AVAILABLE)))); } else { - LiveSubchannelPicker picker = new LiveSubchannelPicker(readies); - helper.updateBalancingState(ConnectivityState.READY, picker); - LiveDiscovery.setSubchannelPicker(serviceName, picker); + updatePicker(READY, new LiveSubchannelPicker(readies)); } } + private void updatePicker(ConnectivityState state, LiveSubchannelPicker picker) { + helper.updateBalancingState(state, picker); + LiveDiscovery.setSubchannelPicker(serviceName, picker); + } + private class LiveStateListener implements SubchannelStateListener { private final GrpcEndpoint endpoint; diff --git a/joylive-plugin/joylive-router/joylive-router-grpc/src/main/java/com/jd/live/agent/plugin/router/gprc/loadbalance/LiveLoadBalancerProvider.java b/joylive-plugin/joylive-router/joylive-router-grpc/src/main/java/com/jd/live/agent/plugin/router/gprc/loadbalance/LiveLoadBalancerProvider.java index 64f763f50..53ed8460d 100644 --- a/joylive-plugin/joylive-router/joylive-router-grpc/src/main/java/com/jd/live/agent/plugin/router/gprc/loadbalance/LiveLoadBalancerProvider.java +++ b/joylive-plugin/joylive-router/joylive-router-grpc/src/main/java/com/jd/live/agent/plugin/router/gprc/loadbalance/LiveLoadBalancerProvider.java @@ -15,6 +15,7 @@ */ package com.jd.live.agent.plugin.router.gprc.loadbalance; +import com.jd.live.agent.governance.registry.Registry; import io.grpc.LoadBalancer; import io.grpc.LoadBalancerProvider; @@ -23,6 +24,12 @@ */ public class LiveLoadBalancerProvider extends LoadBalancerProvider { + private final Registry registry; + + public LiveLoadBalancerProvider(Registry registry) { + this.registry = registry; + } + @Override public boolean isAvailable() { return true; @@ -40,6 +47,8 @@ public String getPolicyName() { @Override public LoadBalancer newLoadBalancer(LoadBalancer.Helper helper) { - return new LiveLoadBalancer(helper); + LiveLoadBalancer balancer = new LiveLoadBalancer(helper); + registry.subscribe(balancer.getServiceName(), balancer::handle); + return balancer; } } diff --git a/joylive-plugin/joylive-router/joylive-router-grpc/src/main/resources/META-INF/services/com.jd.live.agent.core.plugin.definition.PluginDefinition b/joylive-plugin/joylive-router/joylive-router-grpc/src/main/resources/META-INF/services/com.jd.live.agent.core.plugin.definition.PluginDefinition index dbeaf30fb..0bead3a18 100644 --- a/joylive-plugin/joylive-router/joylive-router-grpc/src/main/resources/META-INF/services/com.jd.live.agent.core.plugin.definition.PluginDefinition +++ b/joylive-plugin/joylive-router/joylive-router-grpc/src/main/resources/META-INF/services/com.jd.live.agent.core.plugin.definition.PluginDefinition @@ -3,4 +3,4 @@ com.jd.live.agent.plugin.router.gprc.definition.LoadbalancerDefinition com.jd.live.agent.plugin.router.gprc.definition.NameResolverDefinition com.jd.live.agent.plugin.router.gprc.definition.ChannelFactoryDefinition com.jd.live.agent.plugin.router.gprc.definition.GrpcClientBeanDefinition -com.jd.live.agent.plugin.router.gprc.definition.ServerObserverDefinition \ No newline at end of file +com.jd.live.agent.plugin.router.gprc.definition.ServerObserverDefinition