Vert.x实战一:Vert.x通过Http发布数据(或文件)

18 篇文章 5 订阅

Vert.x系列:
Vert.x介绍:https://blog.csdn.net/haoranhaoshi/article/details/89279096
Vert.x实战一:Vert.x通过Http发布数据:https://blog.csdn.net/haoranhaoshi/article/details/89284847
Vert.x实战二:TCP通信:https://blog.csdn.net/haoranhaoshi/article/details/89296522
Vert.x实战三:TCP客户端之间以角色通过服务端转接通信:https://mp.csdn.net/postedit/89296606
Vert.x实战四:TCP客户端之间以角色和同一角色连接顺序通过服务端转接通信:https://blog.csdn.net/haoranhaoshi/article/details/89296665
Vert.x实战五:TCP客户端之间以ID通过服务端转接通信:https://blog.csdn.net/haoranhaoshi/article/details/89296754
Vert.x实战六:TCP客户端之间以功能名通过服务端转接通信:https://blog.csdn.net/haoranhaoshi/article/details/89296841
Vert.x实战七:TCP设置超时断开:https://blog.csdn.net/haoranhaoshi/article/details/89296986
Vert.x的TCP服务端和客户端配置:https://blog.csdn.net/haoranhaoshi/article/details/89297022
Vert.x的Http和TCP实战代码下载:https://download.csdn.net/download/haoranhaoshi/11114611

本篇:WebService、Node.js、SpringBoot都能通过Http发布数据。Vert.x作为下一代异步、可伸缩、并发性企业级应用的服务器端框架,Http、TCP、WebSocket皆可发布。WebService、Node.js需先安装Tomcat等Web容器,SpringBoot内置Tomcat,Vert.x内置Vert.x容器。

package VertxHttpTest;

import java.io.IOException;
import java.util.function.Consumer;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.core.http.HttpServerRequest;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.handler.BodyHandler;

// verticle是vert.x中的组件,一个组件可有多个实例,创建verticle都要继承AbstractVerticle
// 依赖vertx-web-3.7.0.jar、vertx-core-3.7.0.jar、slf4j-api-1.7.21.jar、slf4j-log4j12-1.7.5.jar、log4j.properties(src下),前两个必须,后三个推荐加入
public class VertxHttpTest extends AbstractVerticle {

    public static void main(String[] args) throws IOException {
        String verticleID = VertxHttpTest.class.getName();
        runExample(verticleID);
    }

    @Override
    public void start() throws Exception {
        final Router router = Router.router(vertx);
        router.route().handler(BodyHandler.create());
        // router.get("/hello")表示所监听URL路径
        router.get("/hello").handler(new Handler<RoutingContext>() {
            public void handle(RoutingContext event) {
                // 文本
                //event.response().putHeader("content-type", "text/html").end("Hello Vert.x");
                // 文件
                //event.response().sendFile(文件绝对路径);
            }
        });
        // 传递方法引用,监听端口
        vertx.createHttpServer().requestHandler(new Handler<HttpServerRequest>() {
            public void handle(HttpServerRequest event) {
                router.accept(event);
            }
        }).listen("localhost", 33322);// 监听
        // 访问http://localhost:33322/hello可查看结果
    }

    public static void runExample(String verticleID) {
        VertxOptions options = new VertxOptions();
        Consumer<Vertx> runner = vertx -> {
            vertx.deployVerticle(verticleID);
        };
        // Vert.x实例是vert.x api的入口点,我们调用vert.x中的核心服务时,均要先获取vert.x实例,
        // 通过该实例来调用相应的服务,例如部署verticle、创建http server
        Vertx vertx = Vertx.vertx(options);
        runner.accept(vertx);
    }
}

改良版:

package VertxHttpTest;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.BodyHandler;

import java.util.function.Consumer;

// verticle是vert.x中的组件,一个组件可有多个实例,创建verticle都要继承AbstractVerticle
// 依赖vertx-web-3.7.0.jar、vertx-core-3.7.0.jar、slf4j-api-1.7.21.jar、slf4j-log4j12-1.7.5.jar、log4j.properties(src下),前两个必须,后三个推荐加入
public class VertxHttpTest extends AbstractVerticle {

    public static void main(String[] args) {
        String verticleID = VertxHttpTest.class.getName();
        runExample(verticleID);
    }

    @Override
    public void start() {
        final Router router = Router.router(vertx);
        router.route().handler(BodyHandler.create());
        // router.get("/hello")表示所监听URL路径
        router.get("/hello").handler(event -> {
            // 文本
            //event.response().putHeader("content-type", "text/html").end("Hello Vert.x");
            // 文件
            //event.response().sendFile(文件绝对路径);
        });
        // 传递方法引用,监听端口
        vertx.createHttpServer().requestHandler(event -> router.handle(event)).listen(33322, "localhost");// 监听
        // 访问http://localhost:33322/hello可查看结果
    }

    public static void runExample(String verticleID) {
        VertxOptions options = new VertxOptions();
        Consumer<Vertx> runner = vertx -> vertx.deployVerticle(verticleID);
        // Vert.x实例是vert.x api的入口点,我们调用vert.x中的核心服务时,均要先获取vert.x实例,
        // 通过该实例来调用相应的服务,例如部署verticle、创建http server
        Vertx vertx = Vertx.vertx(options);
        runner.accept(vertx);
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风铃峰顶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值