Skip to content

Commit

Permalink
Merge pull request #209 from jd-opensource/grpc
Browse files Browse the repository at this point in the history
Add registry adaptor for grpc
  • Loading branch information
hexiaofeng authored Jan 14, 2025
2 parents 93460f2 + 34d0ca7 commit e99de22
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 0 deletions.
5 changes: 5 additions & 0 deletions joylive-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,11 @@
<artifactId>joylive-application-springboot2</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>com.jd.live</groupId>
<artifactId>joylive-registry-grpc</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>com.jd.live</groupId>
<artifactId>joylive-registry-springcloud3</artifactId>
Expand Down
4 changes: 4 additions & 0 deletions joylive-package/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@
<groupId>com.jd.live</groupId>
<artifactId>joylive-application-springboot2</artifactId>
</dependency>
<dependency>
<groupId>com.jd.live</groupId>
<artifactId>joylive-registry-grpc</artifactId>
</dependency>
<dependency>
<groupId>com.jd.live</groupId>
<artifactId>joylive-registry-springcloud3</artifactId>
Expand Down
1 change: 1 addition & 0 deletions joylive-package/src/main/assembly/assembly.xml
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@
<includes>
<include>com.jd.live:joylive-transmission-grpc</include>
<include>com.jd.live:joylive-router-grpc</include>
<include>com.jd.live:joylive-registry-grpc</include>
</includes>
</dependencySet>

Expand Down
27 changes: 27 additions & 0 deletions joylive-plugin/joylive-registry/joylive-registry-grpc/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.jd.live</groupId>
<artifactId>joylive-registry</artifactId>
<version>${revision}</version>
</parent>

<artifactId>joylive-registry-grpc</artifactId>

<properties>
<spring.grpc.version>2.15.0.RELEASE</spring.grpc.version>
</properties>

<dependencies>
<dependency>
<groupId>net.devh</groupId>
<artifactId>grpc-client-spring-boot-starter</artifactId>
<version>${spring.grpc.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -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)),
};
}
}
Original file line number Diff line number Diff line change
@@ -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.");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.jd.live.agent.plugin.registry.grpc.definition.DiscoveryClientNameResolverDefinition
1 change: 1 addition & 0 deletions joylive-plugin/joylive-registry/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<packaging>pom</packaging>
<modules>
<module>joylive-registry-springcloud3</module>
<module>joylive-registry-grpc</module>
<module>joylive-registry-dubbo3</module>
<module>joylive-registry-dubbo2.7</module>
<module>joylive-registry-dubbo2.6</module>
Expand Down

0 comments on commit e99de22

Please sign in to comment.