vertx.x响应式编程

1、实例一、  软件IDEA   gradle

               compile 'io.vertx:vertx-core:3.5.0'

                   compile 'io.vertx:vertx-web:3.5.0'

2、实例二、 maven

<dependency>
    <groupId>io.vertx</groupId>
    <artifactId>vertx-core</artifactId>
    <version>3.5.0</version>
</dependency>
<!--vertx的web路由方式  web组件开发像是于springMVC-->

<dependency>
    <groupId>io.vertx</groupId>
    <artifactId>vertx-web</artifactId>
    <version>3.5.0</version>
</dependency>
(1)编写http服务器
package com.fei.vertxtest;

import io.vertx.core.Vertx;
import io.vertx.core.http.HttpServer;
import io.vertx.core.http.HttpServerRequest;
import io.vertx.ext.web.Router;
import org.junit.Test;


/**
 * Created by Thinkpad on 2018/3/3.
 */
public class VertxTest {
    @Test
    //vertx响应式实现http服务器
    public  void  vertxDemo01(){
        //vertx核心
        Vertx vertx=Vertx.vertx();
        //创建http的Server
      HttpServer server= vertx.createHttpServer();
      //服务器响应
        server.requestHandler(request->{
            request.response().end("hello world");
        });
      //监听端口
        server.listen(8080);
    }
    @Test
    public void  vertxWeb(){
        //创建vertx对象
     Vertx vertx=  Vertx.vertx();
     //web路由
        Router router=Router.router(vertx);
        router.route("/").handler(context->{
            //context.request();
            context.response().end("hello root");
        });
        router.route("/abc").handler(context->{
            HttpServerRequest request=context.request();
            String name=request.getParam("name");
            String handler=request.getHeader("fei");
            //context.request();
            context.response().end("hello abc"+name);
        });
     //创建httpserver的server服务器
    HttpServer   server=vertx.createHttpServer();
    server.requestHandler(request->{
        //处理request请求
        router.accept(request);
    });
    //监听端口8082
        server.listen(8082);
    }


}

2、Resultful 风格

package com.fei.vertxtest;

import io.vertx.core.Vertx;
import io.vertx.core.http.HttpServer;
import io.vertx.core.http.HttpServerRequest;
import io.vertx.ext.web.Router;

/**
 * Created by Thinkpad on 2018/3/3.
 */
public class VertxRestFul {  //restFul方式
    public  static  void  main(String[] args){
        //创建vertx对象
        Vertx vertx=  Vertx.vertx();
        //web路由
        Router router=Router.router(vertx);
        router.route("/").handler(context->{
            //context.request();
            context.response().end("hello root");
        });
        router.post("/post").handler(context->{
            //context.request();
            context.response().end("hello post");
        });
        router.get("/api/*").handler(context->{
            //context.request();
            context.response().end("hello *!");
        });
        //创建httpserver的server服务器
        HttpServer server=vertx.createHttpServer();
        server.requestHandler(request->{
            //处理request请求
            router.accept(request);
        });
        //监听端口8082
        server.listen(8082);
    }
}
(3)异步的路由
package com.fei.vertxtest;

import io.vertx.core.Vertx;
import io.vertx.core.http.HttpServer;
import io.vertx.ext.web.Router;

/**
 * Created by Thinkpad on 2018/3/3.
 */
public class VertxYiBu {
    public  static  void  main(String[] args){
        //创建vertx对象
        Vertx vertx=  Vertx.vertx();
        //web路由
        Router router=Router.router(vertx);

        router.post("/post").handler(context->{
            //context.request();
            context.response().end("hello post");
        });
        router.get("/api/*").handler(context->{
            //context.request();
            context.response().end("hello *!");
        });
        //同步请求
        router.get("/sync").handler(context->{
            //context.request();
            context.response().end("hello sync");
            System.out.print("sync"+Thread.currentThread().getName());
        });
        //异步请求
        //执行耗时操作
        //数据库访问
        //服务访问
        router.get("/async").blockingHandler(context->{

            //context.request();
            context.response().end("hello async");
            System.out.print("async"+Thread.currentThread().getName());
        });
        //创建httpserver的server服务器
        HttpServer server=vertx.createHttpServer();
        server.requestHandler(request->{
            //处理request请求
            router.accept(request);
        });
        //监听端口8082
        server.listen(8082);
    }
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

飞腾创客

你的鼓励是我最大的动力

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

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

打赏作者

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

抵扣说明:

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

余额充值