gpt4 book ai didi

java - 如何 ping http2 服务器端口的活跃度(grpc 服务器)?

转载 作者:行者123 更新时间:2023-12-02 12:08:49 25 4
gpt4 key购买 nike

我正在尝试 ping grpc http2 服务器端口以获取活跃度。在 Http1.1 中,我们将使用 url.openConnection 并检查响应状态代码。

http2 中的等价物是什么?

下面的代码似乎没有错误,但我不太清楚如何使用它来检查请求是否成功?

import org.apache.commons.io.IOUtils;
import org.eclipse.jetty.http.HttpFields;
import org.eclipse.jetty.http.HttpURI;
import org.eclipse.jetty.http.HttpVersion;
import org.eclipse.jetty.http.MetaData;
import org.eclipse.jetty.http2.api.Session;
import org.eclipse.jetty.http2.api.Stream;
import org.eclipse.jetty.http2.api.server.ServerSessionListener;
import org.eclipse.jetty.http2.client.HTTP2Client;
import org.eclipse.jetty.http2.frames.DataFrame;
import org.eclipse.jetty.http2.frames.HeadersFrame;
import org.eclipse.jetty.util.Callback;
import org.eclipse.jetty.util.FuturePromise;
import org.eclipse.jetty.util.Jetty;
import org.eclipse.jetty.util.ssl.SslContextFactory;

import javax.net.ssl.HttpsURLConnection;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.URL;
import java.util.concurrent.TimeUnit;

public class HeartbeatCheckHttp2 {
public static void main(String [] args) throws Exception {
for (int i=0; i<50000; ++i) {
Thread.sleep(400L);

HTTP2Client client = new HTTP2Client();
client.start();

// Connect to host.
String host = "localhost";
int port = 50000;

FuturePromise<Session> sessionPromise = new FuturePromise<>();
client.connect(new InetSocketAddress(host, port), new ServerSessionListener.Adapter(), sessionPromise);


// Obtain the client Session object.
Session session = sessionPromise.get(5, TimeUnit.SECONDS);

// Prepare the HTTP request headers.
HttpFields requestFields = new HttpFields();
requestFields.put("User-Agent", client.getClass().getName() + "/" + Jetty.VERSION);
requestFields.put("Content-Type", "application/grpc");
// Prepare the HTTP request object.
MetaData.Request request = new MetaData.Request("POST", new HttpURI("http://" + host + ":" + port + "/"), HttpVersion.HTTP_2, requestFields);
// Create the HTTP/2 HEADERS frame representing the HTTP request.
HeadersFrame headersFrame = new HeadersFrame(request, null, true);

// Prepare the listener to receive the HTTP response frames.
Stream.Listener responseListener = new Stream.Listener.Adapter()
{
@Override
public void onData(Stream stream, DataFrame frame, Callback callback)
{
byte[] bytes = new byte[frame.getData().remaining()];
frame.getData().get(bytes);
callback.succeeded();
}
};

session.newStream(headersFrame, new FuturePromise<>(), responseListener);
session.newStream(headersFrame, new FuturePromise<>(), responseListener);
session.newStream(headersFrame, new FuturePromise<>(), responseListener);

Thread.sleep(TimeUnit.SECONDS.toMillis(20));


client.stop();
}
}
}

我可以使用 SocketAddress 来 ping Tcp 活跃度,但我想进行实际的 Http2 调用来执行活跃度检查。

最佳答案

HTTP/2 协议(protocol)具有内置的心跳帧,可用于仅“ping”其他对等点以了解 Activity 情况。

HTTP2Client client = new HTTP2Client();
client.start();

FuturePromise<Session> sessionPromise = new FuturePromise<>();
client.connect(new InetSocketAddress(host, port), new ServerSessionListener.Adapter() {
@Override
public void onPing(Session session, PingFrame frame) {
// The other peer replies to our pings.
}
}, sessionPromise);
Session session = sessionPromise.get(5, TimeUnit.SECONDS);

session.ping(new PingFrame(System.nanoTime(), false), Callback.NOOP);

说到内置的 ping 工具,如果您想检查 HTTP 请求的响应状态,您应该实现 onHeaders():

Stream.Listener responseListener = new Stream.Listener.Adapter() {
@Override
public void onHeaders(Stream stream, HeadersFrame frame) {
MetaData metaData = frame.getMetaData();
if (metaData.isResponse()) {
MetaData.Response response = (MetaData.Response)metaData;
int status = response.getStatus();
...
}
}

@Override
public void onData(Stream stream, DataFrame frame, Callback callback) {
...
}
};

这就是使用通过 HTTP/2 框架使用 HTTP 的服务器执行此操作的方法。

使用 gRPC,情况可能会有所不同,如 gRPC protocol通过 HTTP/2 帧传输,但它不是 HTTP。

关于java - 如何 ping http2 服务器端口的活跃度(grpc 服务器)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46703014/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com