diff --git a/joylive-bom/pom.xml b/joylive-bom/pom.xml index b4f4e01b..b6908268 100644 --- a/joylive-bom/pom.xml +++ b/joylive-bom/pom.xml @@ -183,6 +183,11 @@ joylive-application-springboot2 ${revision} + + com.jd.live + joylive-registry-grpc + ${revision} + com.jd.live joylive-registry-springcloud3 diff --git a/joylive-package/pom.xml b/joylive-package/pom.xml index 1a027957..d522749e 100644 --- a/joylive-package/pom.xml +++ b/joylive-package/pom.xml @@ -123,6 +123,10 @@ com.jd.live joylive-application-springboot2 + + com.jd.live + joylive-registry-grpc + com.jd.live joylive-registry-springcloud3 diff --git a/joylive-package/src/main/assembly/assembly.xml b/joylive-package/src/main/assembly/assembly.xml index 4aae8f25..c51ee10b 100644 --- a/joylive-package/src/main/assembly/assembly.xml +++ b/joylive-package/src/main/assembly/assembly.xml @@ -207,6 +207,7 @@ com.jd.live:joylive-transmission-grpc com.jd.live:joylive-router-grpc + com.jd.live:joylive-registry-grpc diff --git a/joylive-plugin/joylive-registry/joylive-registry-grpc/pom.xml b/joylive-plugin/joylive-registry/joylive-registry-grpc/pom.xml new file mode 100644 index 00000000..04a2f9f5 --- /dev/null +++ b/joylive-plugin/joylive-registry/joylive-registry-grpc/pom.xml @@ -0,0 +1,27 @@ + + + 4.0.0 + + com.jd.live + joylive-registry + ${revision} + + + joylive-registry-grpc + + + 2.15.0.RELEASE + + + + + net.devh + grpc-client-spring-boot-starter + ${spring.grpc.version} + provided + + + + \ No newline at end of file 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 new file mode 100644 index 00000000..e418b77a --- /dev/null +++ b/joylive-plugin/joylive-registry/joylive-registry-grpc/src/main/java/com/jd/live/agent/plugin/registry/grpc/definition/DiscoveryClientNameResolverDefinition.java @@ -0,0 +1,52 @@ +/* + * 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.grpc.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.policy.PolicySupplier; +import com.jd.live.agent.plugin.registry.grpc.interceptor.DiscoveryClientConstructorInterceptor; + +/** + * DiscoveryClientNameResolverDefinition + */ +@Injectable +@Extension(value = "DiscoveryClientNameResolverDefinition", order = PluginDefinition.ORDER_REGISTRY) +@ConditionalOnGovernanceEnabled +@ConditionalOnClass(DiscoveryClientNameResolverDefinition.TYPE_DISCOVERY_CLIENT_NAME_RESOLVER) +public class DiscoveryClientNameResolverDefinition extends PluginDefinitionAdapter { + + protected static final String TYPE_DISCOVERY_CLIENT_NAME_RESOLVER = "net.devh.boot.grpc.client.nameresolver.DiscoveryClientNameResolver"; + + @Inject(PolicySupplier.COMPONENT_POLICY_SUPPLIER) + private PolicySupplier policySupplier; + + public DiscoveryClientNameResolverDefinition() { + this.matcher = () -> MatcherBuilder.named(TYPE_DISCOVERY_CLIENT_NAME_RESOLVER); + this.interceptors = new InterceptorDefinition[]{ + new InterceptorDefinitionAdapter( + MatcherBuilder.isConstructor(), () -> new DiscoveryClientConstructorInterceptor(policySupplier)), + }; + } +} 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 new file mode 100644 index 00000000..78c66633 --- /dev/null +++ b/joylive-plugin/joylive-registry/joylive-registry-grpc/src/main/java/com/jd/live/agent/plugin/registry/grpc/interceptor/DiscoveryClientConstructorInterceptor.java @@ -0,0 +1,54 @@ +/* + * 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.grpc.interceptor; + +import com.jd.live.agent.bootstrap.bytekit.context.ExecutableContext; +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 java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +/** + * DiscoveryClientConstructorInterceptor + */ +public class DiscoveryClientConstructorInterceptor extends InterceptorAdaptor { + + private static final Logger logger = LoggerFactory.getLogger(DiscoveryClientConstructorInterceptor.class); + + private final PolicySupplier policySupplier; + + public DiscoveryClientConstructorInterceptor(PolicySupplier policySupplier) { + this.policySupplier = policySupplier; + } + + @Override + public void onSuccess(ExecutableContext ctx) { + String serviceId = ctx.getArgument(0); + try { + policySupplier.subscribe(serviceId).get(5000, TimeUnit.MILLISECONDS); + } catch (InterruptedException ignore) { + } catch (ExecutionException e) { + Throwable cause = e.getCause() != null ? e.getCause() : e; + logger.warn("Failed to get governance policy for " + serviceId + ", caused by " + cause.getMessage(), cause); + } catch (TimeoutException e) { + logger.warn("Failed to get governance policy for " + serviceId + ", caused by it's timeout."); + } + } +} diff --git a/joylive-plugin/joylive-registry/joylive-registry-grpc/src/main/resources/META-INF/services/com.jd.live.agent.core.plugin.definition.PluginDefinition b/joylive-plugin/joylive-registry/joylive-registry-grpc/src/main/resources/META-INF/services/com.jd.live.agent.core.plugin.definition.PluginDefinition new file mode 100644 index 00000000..14c5840f --- /dev/null +++ b/joylive-plugin/joylive-registry/joylive-registry-grpc/src/main/resources/META-INF/services/com.jd.live.agent.core.plugin.definition.PluginDefinition @@ -0,0 +1 @@ +com.jd.live.agent.plugin.registry.grpc.definition.DiscoveryClientNameResolverDefinition diff --git a/joylive-plugin/joylive-registry/pom.xml b/joylive-plugin/joylive-registry/pom.xml index 0052e8c4..5946ddac 100644 --- a/joylive-plugin/joylive-registry/pom.xml +++ b/joylive-plugin/joylive-registry/pom.xml @@ -13,6 +13,7 @@ pom joylive-registry-springcloud3 + joylive-registry-grpc joylive-registry-dubbo3 joylive-registry-dubbo2.7 joylive-registry-dubbo2.6