项目作者: snchengqi

项目描述 :
A library for http2 protocol based on netty
高级语言: Java
项目地址: git://github.com/snchengqi/http2-qing.git
创建时间: 2020-06-28T05:55:06Z
项目社区:https://github.com/snchengqi/http2-qing

开源协议:Apache License 2.0

下载


http2-qing

http2-qing is a library for http2 protocol based on netty

中文文档


GitHub release (latest by date)

Features

  • Develop based on netty,with good performance
  • Encapsulate the http2 protocol and provide simple and easy-to-use APIs
  • Basically all asynchronous methods
  • Support manage connections of server and server-push
  • Provides client disconnection and reconnection and support customized reconnect policy

How to use

program codes for server

Firstly, import maven dependency about http2-server to your project

  1. <dependency>
  2. <groupId>com.github.snchengqi</groupId>
  3. <artifactId>http2-qing-server</artifactId>
  4. <version>1.0.1</version>
  5. </dependency>

Construct the environment of server, and config necessary options. Firstly, you need to new a Environment4Server object, and then set propeties such as port,ConnectionManager,RequestHandler, as follows excample

  1. SimpleConnectionManager connectionManager = new SimpleConnectionManager();
  2. Environment4Server env = new Environment4Server();
  3. env.setPort(8080).setConnectionManager(connectionManager).setRequestHandler(context -> {
  4. if (context.isSuccess()) {
  5. ConnectionFacade connection = context.connection();
  6. Http2Stream stream = context.stream();
  7. HttpRequest request = context.request();
  8. String responseMessage = "server response for client request";
  9. System.out.println("=============================");
  10. System.out.println("time:" + new Date());
  11. System.out.println("server receive client request message:" + new String(request.getBody()));
  12. System.out.println("stream id:" + stream.id());
  13. System.out.println("server send response message:" + responseMessage);
  14. System.out.println("=============================");
  15. HttpResponse response = new HttpResponse(EmptyHttp2Headers.INSTANCE, responseMessage.getBytes());
  16. connection.sendResponse(response, stream.id());
  17. return;
  18. }
  19. context.cause().printStackTrace();
  20. });

Using Http2ServerFactoryBuilder to build the object which is a factory method object of Http2Server, so you can create a Http2Server Object, then start the server

  1. Http2ServerFactory factory = new Http2ServerFactoryBuilder(env).build();
  2. Http2Server server = factory.createServer();
  3. CompletableFuture<Void> bindFuture = server.start();
  4. bindFuture.get();

And then, could get the connection by connectionManager which you defined, and send server-push to remote endpoint, for example, like this

  1. connectionManager.forEach(connection -> {
  2. CompletableFuture<HttpPushAck> future = connection.sendPushMessage(new HttpPushEntity(EmptyHttp2Headers.INSTANCE,pushMessage.getBytes()));
  3. future.whenComplete((r, cause) -> {
  4. if (cause == null) {
  5. System.out.println("=============================");
  6. System.out.println("time:" + new Date());
  7. System.out.println("server send push message:" + pushMessage);
  8. System.out.println("receive client ack:" + new String(r.getBody()));
  9. System.out.println("=============================");
  10. return;
  11. }
  12. cause.printStackTrace();
  13. });
  14. };

Finally, don`t forget close the Http2Server to release resources

  1. server.close();

program code for client

Firstly, import maven dependency about http2-client to your project

  1. <dependency>
  2. <groupId>com.github.snchengqi</groupId>
  3. <artifactId>http2-qing-client</artifactId>
  4. <version>1.0.1</version>
  5. </dependency>

Construct the environment of client, and config necessary options. Firstly, you need to new a Environment4Client object, and then set propeties such as remote ip and port,ServerPushHandler, as follows excample

  1. Environment4Client env = new Environment4Client();
  2. env.setIp("127.0.0.1").setPort(8080).setServerPushHandler(context -> {
  3. if (context.isSuccess()) {
  4. Http2Client client = context.client();
  5. HttpPushEntity pushEntity = context.pushEntity();
  6. Http2Stream stream = context.stream();
  7. String ackMessage = "client ack for server push";
  8. System.out.println("=============================");
  9. System.out.println("time:" + new Date());
  10. System.out.println("client receive server push:" + (pushEntity.getBody() == null? "null": new String(pushEntity.getBody())));
  11. System.out.println("stream id:" + stream.id());
  12. System.out.println("client ack message:" + ackMessage);
  13. System.out.println("=============================");
  14. HttpPushAck ack = new HttpPushAck(EmptyHttp2Headers.INSTANCE, ackMessage.getBytes());
  15. client.sendPushAck(ack, stream.id());
  16. return;
  17. }
  18. context.cause().printStackTrace();
  19. });

Using Http2ClientFactoryBuilder to build the object which is a factory method object of Http2Client, so you can create a Http2Client Object, then start it to connect to remote server

  1. Http2ClientFactory factory = new Http2ClientFactoryBuilder(env).build();
  2. Http2Client client = factory.createClient();
  3. CompletableFuture<Void> connectFuture = client.connect();
  4. connectFuture.get();

After connected to server, you can user the client to send a request and receive response asynchronously

  1. String requestContent = "request message of client";
  2. HttpRequest request = new HttpRequest(EmptyHttp2Headers.INSTANCE, requestContent.getBytes());
  3. CompletableFuture<HttpResponse> requestFuture = client.sendRequest(request);
  4. requestFuture.whenComplete((r, cause) -> {
  5. if (cause == null) {
  6. System.out.println("=============================");
  7. System.out.println("time:" + new Date());
  8. System.out.println("client send request message:" + requestContent);
  9. System.out.println("receive server response:" + new String(r.getBody()));
  10. System.out.println("=============================");
  11. return;
  12. }
  13. cause.printStackTrace();
  14. });

Finally,remember to close the client

  1. client.close();