diff --git a/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/instance/Endpoint.java b/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/instance/Endpoint.java index f40835157..1078e2394 100644 --- a/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/instance/Endpoint.java +++ b/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/instance/Endpoint.java @@ -436,6 +436,16 @@ default boolean isAccessible() { return state == null || state.isAccessible(); } + /** + * Checks if the specified port matches the current port. + * + * @param port The port to check against the current port. + * @return {@code true} if the specified port matches the current port, {@code false} otherwise. + */ + default boolean isPort(int port) { + return getPort() == port; + } + /** * Evaluates the predicate associated with this endpoint, if any, to determine * if this endpoint satisfies the conditions defined by the predicate. diff --git a/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/invoke/filter/RouteFilter.java b/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/invoke/filter/RouteFilter.java index 7df57778d..be8eca7f2 100644 --- a/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/invoke/filter/RouteFilter.java +++ b/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/invoke/filter/RouteFilter.java @@ -45,7 +45,9 @@ public interface RouteFilter { int ORDER_LOCALHOST = ORDER_STICKY + 100; - int ORDER_HEALTH = ORDER_LOCALHOST + 100; + int ORDER_PORT = ORDER_LOCALHOST + 100; + + int ORDER_HEALTH = ORDER_PORT + 100; int ORDER_VIRTUAL = ORDER_HEALTH + 100; diff --git a/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/invoke/filter/route/PortFilter.java b/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/invoke/filter/route/PortFilter.java new file mode 100644 index 000000000..86ff6aa56 --- /dev/null +++ b/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/invoke/filter/route/PortFilter.java @@ -0,0 +1,47 @@ +/* + * 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.extension.annotation.Extension; +import com.jd.live.agent.governance.annotation.ConditionalOnFlowControlEnabled; +import com.jd.live.agent.governance.invoke.OutboundInvocation; +import com.jd.live.agent.governance.invoke.filter.RouteFilter; +import com.jd.live.agent.governance.invoke.filter.RouteFilterChain; +import com.jd.live.agent.governance.request.Portable; +import com.jd.live.agent.governance.request.ServiceRequest.OutboundRequest; + +/** + * A class that implements the {@link RouteFilter} interface. + * It filters outbound requests based on the port number. + * + * @since 1.6.0 + */ +@Extension(value = "PortFilter", order = RouteFilter.ORDER_PORT) +@ConditionalOnFlowControlEnabled +public class PortFilter implements RouteFilter { + + @Override + public void filter(OutboundInvocation invocation, RouteFilterChain chain) { + T request = invocation.getRequest(); + if (request instanceof Portable) { + Integer port = ((Portable) request).getPort(); + if (port != null) { + invocation.getRouteTarget().filter(e -> e.isPort(port)); + } + } + chain.filter(invocation); + } +} diff --git a/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/request/HttpRequest.java b/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/request/HttpRequest.java index 67b019635..9cab40b97 100644 --- a/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/request/HttpRequest.java +++ b/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/request/HttpRequest.java @@ -29,7 +29,7 @@ * inbound and outbound requests. *

*/ -public interface HttpRequest extends ServiceRequest { +public interface HttpRequest extends ServiceRequest, Portable { /** * Returns the URI of the request. @@ -45,13 +45,6 @@ public interface HttpRequest extends ServiceRequest { */ String getSchema(); - /** - * Returns the port number of the request. - * - * @return The port number. - */ - Integer getPort(); - /** * Returns the host name of the request. * diff --git a/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/request/Portable.java b/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/request/Portable.java new file mode 100644 index 000000000..db7086de4 --- /dev/null +++ b/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/request/Portable.java @@ -0,0 +1,31 @@ +/* + * 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.request; + +/** + * An interface representing an object that has a port. + * Implementing classes should provide a method to get the port number. + */ +public interface Portable { + + /** + * Returns the port number of the request. + * + * @return The port number. + */ + Integer getPort(); +} + diff --git a/joylive-core/joylive-governance-api/src/main/resources/META-INF/services/com.jd.live.agent.governance.invoke.filter.RouteFilter b/joylive-core/joylive-governance-api/src/main/resources/META-INF/services/com.jd.live.agent.governance.invoke.filter.RouteFilter index 119700c9e..0ee95c8c1 100644 --- a/joylive-core/joylive-governance-api/src/main/resources/META-INF/services/com.jd.live.agent.governance.invoke.filter.RouteFilter +++ b/joylive-core/joylive-governance-api/src/main/resources/META-INF/services/com.jd.live.agent.governance.invoke.filter.RouteFilter @@ -11,3 +11,4 @@ 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 +com.jd.live.agent.governance.invoke.filter.route.PortFilter