Search in sources :

Example 1 with Router

use of io.vertx.ext.web.Router in project yyl_example by Relucent.

the class HelloVertxVerticle method start.

/** 运行 */
@Override
public void start() throws Exception {
    Router router = Router.router(vertx);
    router.route().handler(BodyHandler.create());
    router.get("/hello").handler(new Handler<RoutingContext>() {

        public void handle(RoutingContext event) {
            event.response().putHeader("content-type", "text/html").end("Hello Vert.x");
        }
    });
    vertx.createHttpServer().requestHandler(new Handler<HttpServerRequest>() {

        public void handle(HttpServerRequest event) {
            router.accept(event);
        }
    }).listen(//监听端口号
    8080);
}
Also used : RoutingContext(io.vertx.ext.web.RoutingContext) HttpServerRequest(io.vertx.core.http.HttpServerRequest) Router(io.vertx.ext.web.Router) Handler(io.vertx.core.Handler) BodyHandler(io.vertx.ext.web.handler.BodyHandler)

Example 2 with Router

use of io.vertx.ext.web.Router in project java-chassis by ServiceComb.

the class RestServerVerticle method start.

@Override
public void start(Future<Void> startFuture) throws Exception {
    super.start();
    // 如果本地未配置地址,则表示不必监听,只需要作为客户端使用即可
    if (endpointObject == null) {
        LOGGER.warn("rest listen address is not configured, will not start.");
        startFuture.complete();
        return;
    }
    Router mainRouter = Router.router(vertx);
    mainRouter.route().handler(bodyHandler);
    VertxRestServer vertxRestServer = new VertxRestServer(mainRouter);
    vertxRestServer.setTransport(CseContext.getInstance().getTransportManager().findTransport(Const.RESTFUL));
    HttpServer httpServer = createHttpServer();
    httpServer.requestHandler(mainRouter::accept);
    startListen(httpServer, startFuture);
}
Also used : HttpServer(io.vertx.core.http.HttpServer) Router(io.vertx.ext.web.Router)

Example 3 with Router

use of io.vertx.ext.web.Router in project java-chassis by ServiceComb.

the class TestVertxRestServer method setUp.

@Before
public void setUp() throws Exception {
    Router mainRouter = Router.router(null);
    mainRouter.route().handler(BodyHandler.create());
    instance = new VertxRestServer(mainRouter) {

        @Override
        protected RestOperationMeta findRestOperation(RestServerRequestInternal restRequest) {
            return super.findRestOperation(restRequest);
        }

        @Override
        public void sendFailResponse(RestServerRequestInternal restRequest, HttpServerResponse httpResponse, Throwable throwable) {
        }
    };
}
Also used : RestOperationMeta(io.servicecomb.common.rest.definition.RestOperationMeta) RestServerRequestInternal(io.servicecomb.common.rest.codec.RestServerRequestInternal) HttpServerResponse(io.vertx.core.http.HttpServerResponse) Router(io.vertx.ext.web.Router) Before(org.junit.Before)

Example 4 with Router

use of io.vertx.ext.web.Router in project java-chassis by ServiceComb.

the class GrpcVerticle method startListen.

protected void startListen(Future<Void> startFuture) {
    // 如果本地未配置grpc地址,则表示不必监听,只需要作为客户端使用即可
    if (StringUtils.isEmpty(this.endpoint)) {
        LOGGER.warn("grpc listen address is not configured, will not listen.");
        startFuture.complete();
        return;
    }
    Router mainRouter = Router.router(vertx);
    mainRouter.route().handler(new GrpcBodyHandler());
    new GrpcServer(mainRouter);
    HttpServerOptions serverOptions = new HttpServerOptions();
    serverOptions.setAcceptBacklog(ACCEPT_BACKLOG);
    serverOptions.setSendBufferSize(SEND_BUFFER_SIZE);
    serverOptions.setReceiveBufferSize(RECEIVE_BUFFER_SIZE);
    serverOptions.setUsePooledBuffers(true);
    String key = System.getProperty("store.key");
    if (key != null && !key.isEmpty()) {
        serverOptions.setUseAlpn(true);
        serverOptions.setSsl(true);
        serverOptions.setKeyStoreOptions(new JksOptions().setPath(System.getProperty("store.key")).setPassword(System.getProperty("store.pass")));
    }
    HttpServer server = vertx.createHttpServer(serverOptions).requestHandler(mainRouter::accept);
    IpPort ipPort = NetUtils.parseIpPortFromURI(this.endpoint);
    if (ipPort == null) {
        LOGGER.error("wrong grpc listen address {}", this.endpoint);
        return;
    }
    startListen(server, ipPort, startFuture);
}
Also used : JksOptions(io.vertx.core.net.JksOptions) HttpServerOptions(io.vertx.core.http.HttpServerOptions) HttpServer(io.vertx.core.http.HttpServer) Router(io.vertx.ext.web.Router) IpPort(io.servicecomb.foundation.common.net.IpPort)

Aggregations

Router (io.vertx.ext.web.Router)4 HttpServer (io.vertx.core.http.HttpServer)2 RestServerRequestInternal (io.servicecomb.common.rest.codec.RestServerRequestInternal)1 RestOperationMeta (io.servicecomb.common.rest.definition.RestOperationMeta)1 IpPort (io.servicecomb.foundation.common.net.IpPort)1 Handler (io.vertx.core.Handler)1 HttpServerOptions (io.vertx.core.http.HttpServerOptions)1 HttpServerRequest (io.vertx.core.http.HttpServerRequest)1 HttpServerResponse (io.vertx.core.http.HttpServerResponse)1 JksOptions (io.vertx.core.net.JksOptions)1 RoutingContext (io.vertx.ext.web.RoutingContext)1 BodyHandler (io.vertx.ext.web.handler.BodyHandler)1 Before (org.junit.Before)1