Skip to content

#213 Support spring cloud http3 #220

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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 @@ -16,6 +16,7 @@
<spring-boot.version>3.4.0</spring-boot.version>
<spring-cloud-context.version>3.1.9</spring-cloud-context.version>
<nacos-discovery.version>2023.0.3.2</nacos-discovery.version>
<reactor-netty-http.version>1.0.32</reactor-netty-http.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -44,6 +45,12 @@
<version>${nacos-discovery.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.projectreactor.netty</groupId>
<artifactId>reactor-netty-http</artifactId>
<version>${reactor-netty-http.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>

</project>
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.application.springboot.v2.definition;

import com.jd.live.agent.core.bytekit.matcher.MatcherBuilder;
import com.jd.live.agent.core.event.AgentEvent;
import com.jd.live.agent.core.event.Publisher;
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.plugin.application.springboot.v2.interceptor.SpringCloudHttp3Interceptor;

@Injectable
@Extension(value = "SpringApplicationDefinition_v5", order = PluginDefinition.ORDER_APPLICATION)
@ConditionalOnGovernanceEnabled
@ConditionalOnClass(SpringNettyWebServerDefinition.TYPE_SPRING_NETTY_WEB_SERVER)
@ConditionalOnClass(SpringNettyWebServerDefinition.TYPE_REACTOR_HTTP_SERVER)
public class SpringNettyWebServerDefinition extends PluginDefinitionAdapter {

protected static final String TYPE_SPRING_NETTY_WEB_SERVER = "org.springframework.boot.web.embedded.netty.NettyWebServer";
protected static final String TYPE_REACTOR_HTTP_SERVER = "reactor.netty.http.server.HttpServer";

private static final String METHOD_START = "start";

@Inject(Publisher.SYSTEM)
private Publisher<AgentEvent> publisher;

public SpringNettyWebServerDefinition() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

publisher 注入未做空值校验,可能导致 NPE。
只拦截了 NettyWebServer 的 start 方法,未考虑其他生命周期方法,可能遗漏部分启动场景。

this.matcher = () -> MatcherBuilder.named(TYPE_SPRING_NETTY_WEB_SERVER);
this.interceptors = new InterceptorDefinition[]{
new InterceptorDefinitionAdapter(MatcherBuilder.named(METHOD_START),
() -> new SpringCloudHttp3Interceptor(publisher)),
};
}
}
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.plugin.application.springboot.v2.interceptor;

import com.jd.live.agent.bootstrap.bytekit.context.ExecutableContext;
import com.jd.live.agent.core.event.AgentEvent;
import com.jd.live.agent.core.event.Publisher;
import com.jd.live.agent.core.plugin.definition.InterceptorAdaptor;
import org.springframework.boot.web.embedded.netty.NettyWebServer;
import reactor.netty.http.HttpProtocol;
import reactor.netty.http.server.HttpServer;

import java.lang.reflect.Field;

public class SpringCloudHttp3Interceptor extends InterceptorAdaptor {

private static final String PROTOCOL_HTTP3 = "HTTP3";

private final Publisher<AgentEvent> publisher;

public SpringCloudHttp3Interceptor(Publisher<AgentEvent> publisher) {
this.publisher = publisher;
}

@Override
public void onSuccess(ExecutableContext ctx) {
NettyWebServer nettyWebServer = (NettyWebServer) ctx.getTarget();
try {
Field httpServerField = nettyWebServer.getClass().getDeclaredField("httpServer");
httpServerField.setAccessible(true);
HttpServer httpServer = (HttpServer) httpServerField.get(nettyWebServer);
HttpProtocol[] protocols = httpServer.configuration().protocols();
for (HttpProtocol protocol : protocols) {
if (PROTOCOL_HTTP3.equals(protocol.name())) {
publisher.offer(AgentEvent.onApplicationStarted("Spring http3 web server started"));
break;
}
}
} catch (Exception e) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

直接吞掉了所有的异常,应该细化为小的异常,从具体到公共。
反射获取字段未做类型和存在性校验,易导致运行时错误。
publisher.offer 没有判断 publisher 是否为 null。

//Not needing to throw exceptions
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
com.jd.live.agent.plugin.application.springboot.v2.definition.SpringApplicationDefinition
com.jd.live.agent.plugin.application.springboot.v2.definition.SpringApplicationRunListenersDefinition
com.jd.live.agent.plugin.application.springboot.v2.definition.SpringApplicationContextDefinition
com.jd.live.agent.plugin.application.springboot.v2.definition.NacosRegistrationDefinition
com.jd.live.agent.plugin.application.springboot.v2.definition.NacosRegistrationDefinition
com.jd.live.agent.plugin.application.springboot.v2.definition.SpringNettyWebServerDefinition