Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add group filter #115

Merged
merged 5 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@
import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;

/**
* ServiceConfig is a configuration class that defines various settings for service behavior,
Expand Down Expand Up @@ -93,6 +90,21 @@ public class ServiceConfig {
@Setter
private Set<String> systemPaths;

/**
* Define the grouping matching relationship for calls between services.
* <p>k:v = service:group</p>
*/
@Getter
@Setter
private Map<String, String> invokeGroups;

/**
* If the target service is not configured with a group, should all groups be allowed to call it
*/
@Getter
@Setter
private boolean openAllGroups = true;

private final PathMatcherTrie<PrefixPath> systemPathTrie = new PathMatcherTrie<>(() -> {
List<PrefixPath> result = new ArrayList<>();
if (systemPaths != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ public interface RouteFilter {

int ORDER_READY = 0;

int ORDER_CIRCUIT_BREAKER = ORDER_READY + 100;
int ORDER_GROUP = ORDER_READY + 100;

int ORDER_CIRCUIT_BREAKER = ORDER_GROUP + 100;

int ORDER_STICKY = ORDER_CIRCUIT_BREAKER + 100;

Expand Down
Original file line number Diff line number Diff line change
@@ -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.governance.invoke.filter.route;

import com.jd.live.agent.core.Constants;
import com.jd.live.agent.core.extension.annotation.Extension;
import com.jd.live.agent.core.inject.annotation.Injectable;
import com.jd.live.agent.governance.config.ServiceConfig;
import com.jd.live.agent.governance.invoke.OutboundInvocation;
import com.jd.live.agent.governance.invoke.RouteTarget;
import com.jd.live.agent.governance.invoke.filter.RouteFilter;
import com.jd.live.agent.governance.invoke.filter.RouteFilterChain;
import com.jd.live.agent.governance.invoke.metadata.ServiceMetadata;
import com.jd.live.agent.governance.policy.PolicyId;
import com.jd.live.agent.governance.request.ServiceRequest.OutboundRequest;

/**
* Support service calls by configuring to filter instances of a specific group
*
* @since 1.4.0
*/
@Injectable
@Extension(value = "GroupFilter", order = RouteFilter.ORDER_GROUP)
public class GroupFilter implements RouteFilter {

@Override
public <T extends OutboundRequest> void filter(OutboundInvocation<T> invocation, RouteFilterChain chain) {
ServiceMetadata serviceMetadata = invocation.getServiceMetadata();
ServiceConfig serviceConfig = serviceMetadata.getServiceConfig();
String targetGroup = serviceMetadata.getServiceGroup();
if (targetGroup != null && !targetGroup.isEmpty()) {
RouteTarget target = invocation.getRouteTarget();
target.filter(endpoint -> endpoint.getLabel(Constants.LABEL_SERVICE_GROUP).equals(targetGroup));
} else if (serviceConfig != null && !serviceConfig.isOpenAllGroups()) {
RouteTarget target = invocation.getRouteTarget();
target.filter(endpoint -> endpoint.getLabel(Constants.LABEL_SERVICE_GROUP).equals(PolicyId.DEFAULT_GROUP)
|| endpoint.getLabel(Constants.LABEL_SERVICE_GROUP) == null
|| endpoint.getLabel(Constants.LABEL_SERVICE_GROUP).isEmpty()
);
}
chain.filter(invocation);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import com.jd.live.agent.governance.request.HttpRequest;
import com.jd.live.agent.governance.request.ServiceRequest;

import java.util.Map;

import static com.jd.live.agent.governance.policy.PolicyId.KEY_SERVICE_GROUP;
import static com.jd.live.agent.governance.policy.PolicyId.KEY_SERVICE_METHOD;

Expand Down Expand Up @@ -198,7 +200,12 @@ protected String parseServiceName() {

@Override
protected String parseServiceGroup() {
return request.getGroup();
String group = request.getGroup();
if (group == null || group.isEmpty()) {
Map<String, String> invokeMapping = serviceConfig.getInvokeGroups();
group = invokeMapping == null ? null : invokeMapping.get(parseServiceName());
}
return group;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ com.jd.live.agent.governance.invoke.filter.route.HealthyFilter
com.jd.live.agent.governance.invoke.filter.route.StickyFilter
com.jd.live.agent.governance.invoke.filter.route.VirtualFilter
com.jd.live.agent.governance.invoke.filter.route.CircuitBreakerFilter
com.jd.live.agent.governance.invoke.filter.route.GroupFilter
1 change: 1 addition & 0 deletions joylive-package/src/main/assembly/config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ agent:
type: Resilience4j
cleanInterval: 30000
expireTime: 60000
invokeGroups: ${CONFIG_INVOKE_GROUPS:}
live:
topics: ${CONFIG_LIVE_TOPICS}
modifyMQGroupEnabled: ${CONFIG_LIVE_MODIFY_MQ_GROUP:false}
Expand Down
Loading