Skip to content

Commit

Permalink
Retrieve the consumer service name in Spring Cloud and subscribe to g…
Browse files Browse the repository at this point in the history
…overnance policies
  • Loading branch information
hexiaofeng committed May 22, 2024
1 parent 09aba99 commit 93d5b51
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 3 deletions.
2 changes: 2 additions & 0 deletions joylive-package/src/main/assembly/config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ agent:
counter:
enabled: true
governance:
service:
warmups: ${CONFIG_WARMUP_SERVICES:}
initializeTimeout: ${CONFIG_POLICY_INITIALIZE_TIMEOUT:10000}
router:
virtual: ${CONFIG_VIRTUAL_SIZE:500}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,28 @@
<artifactId>joylive-registry-springcloud3</artifactId>

<properties>
<package.plugin.type>plugin</package.plugin.type>
<spring.cloud.version>3.1.8</spring.cloud.version>
<spring.version>5.3.31</spring.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-commons</artifactId>
<version>${spring.cloud.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-loadbalancer</artifactId>
<version>${spring.cloud.version}</version>
<exclusions>
<exclusion>
<groupId>org.glassfish</groupId>
<artifactId>jakarta.el</artifactId>
</exclusion>
</exclusions>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-openfeign-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* 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.springcloud.v3.definition;

import com.jd.live.agent.core.bytekit.matcher.MatcherBuilder;
import com.jd.live.agent.core.extension.annotation.*;
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.config.GovernanceConfig;
import com.jd.live.agent.governance.policy.PolicySupplier;
import com.jd.live.agent.plugin.registry.springcloud.v3.interceptor.DiscoveryClientInterceptor;

/**
* ServiceRegistryDefinition
*
* @author Zhiguo.Chen
* @since 1.0.0
*/
@Injectable
@Extension(value = "DiscoveryClientDefinition_v3", order = PluginDefinition.ORDER_REGISTRY)
@ConditionalOnProperties(value = {
@ConditionalOnProperty(value = GovernanceConfig.CONFIG_LIVE_ENABLED, matchIfMissing = true),
@ConditionalOnProperty(value = GovernanceConfig.CONFIG_LANE_ENABLED, matchIfMissing = true),
@ConditionalOnProperty(value = GovernanceConfig.CONFIG_FLOW_CONTROL_ENABLED, matchIfMissing = true)
}, relation = ConditionalRelation.OR)
@ConditionalOnClass(DiscoveryClientDefinition.TYPE_DISCOVERY_CLIENT)
public class DiscoveryClientDefinition extends PluginDefinitionAdapter {

protected static final String TYPE_DISCOVERY_CLIENT = "org.springframework.cloud.loadbalancer.core.DiscoveryClientServiceInstanceListSupplier";

@Inject(PolicySupplier.COMPONENT_POLICY_SUPPLIER)
private PolicySupplier policySupplier;

public DiscoveryClientDefinition() {
this.matcher = () -> MatcherBuilder.isImplement(TYPE_DISCOVERY_CLIENT);
this.interceptors = new InterceptorDefinition[]{
new InterceptorDefinitionAdapter(
MatcherBuilder.isConstructor(),
() -> new DiscoveryClientInterceptor(policySupplier))
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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.springcloud.v3.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 com.jd.live.agent.governance.policy.PolicyType;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
* ServiceRegistryInterceptor
*
* @author Zhiguo.Chen
* @since 1.0.0
*/
public class DiscoveryClientInterceptor extends InterceptorAdaptor {

private static final Logger logger = LoggerFactory.getLogger(ServiceRegistryInterceptor.class);

private final PolicySupplier policySupplier;

public DiscoveryClientInterceptor(PolicySupplier policySupplier) {
this.policySupplier = policySupplier;
}

@Override
public void onSuccess(ExecutableContext ctx) {
ServiceInstanceListSupplier supplier = (ServiceInstanceListSupplier) ctx.getTarget();
subscribePolicy(supplier.getServiceId());
}

private void subscribePolicy(String serviceId) {
try {
policySupplier.subscribe(serviceId, PolicyType.SERVICE_POLICY).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 " + e.getMessage(), e);
} 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
@@ -1,2 +1,3 @@
com.jd.live.agent.plugin.registry.springcloud.v3.definition.ServiceRegistryDefinition
com.jd.live.agent.plugin.registry.springcloud.v3.definition.FeignClientFactoryBeanDefinition
com.jd.live.agent.plugin.registry.springcloud.v3.definition.FeignClientFactoryBeanDefinition
com.jd.live.agent.plugin.registry.springcloud.v3.definition.DiscoveryClientDefinition

0 comments on commit 93d5b51

Please sign in to comment.