Git Product home page Git Product logo

spring-boot-starter-vertx's Introduction

Vertx Starter

Spring Boot无缝集成vertx-web框架。

使用方法

添加依赖:

<dependency>
	<groupId>cn.fh</groupId>
	<artifactId>spring-boot-starter-vertx</artifactId>
	<version>1.5-SNAPSHOT</version>
</dependency>

添加配置:

server:
  port: 9000

vertx:
  # 事件循环处理线程数
  nio-thread-count: 6
  # worker线程池大小
  worker-thread-count: 10
  # HTTP Server Verticle部署数量
  verticle-count: 6
  # 允许NIO线程处理单个请求的最长时间, 毫秒
  max-event-loop-execute-time: 2000

  handler-mappings:
    # path为/demo的请求, 会依次经过homeHander, demoHandler
    - path: /demo
      method: POST
      # spring bean名
      beanNames: homeHandler,demoHandler

编写Handler:

@Component
// @BlockedHandler
@Slf4j
public class DemoHandler implements Handler<RoutingContext> {
    @Override
    public void handle(RoutingContext route) {
        log.info("invoke DemoHandler, path: {}", route.request().path());
        route.response().end("ok");
    }
}

所有的Handler都会在vertx的NIO线程中执行,所以不要在handler中出现阻塞调用。

如需调用会阻塞线程的方法,可以在Handler上添加@BlockedHandler注解,带有此注解的Handler会在vertx的worker线程池中执行; 或者使用vertx的异步编程方式:

        Future<String> fut1 = Future.future();
        Future<String> fut2 = Future.future();


        // 执行block调用
        route.vertx()
                .executeBlocking(
                        fut -> {
                            String result = demoService.blockingLogic(1);
                            fut.complete(result);
                        },
                        fut1.completer()
                );

        // 执行block调用
        route.vertx()
                .executeBlocking(
                        fut -> {
                            String result = demoService.blockingLogic(2);
                            fut.complete(result);
                        },
                        fut2.completer()
                );

        // 组合结果
        CompositeFuture.all(fut1, fut2).setHandler(ar -> {
            if (!ar.succeeded()) {
                log.error("", ar.cause());
                route.response().end("error");
                return;
            }

            log.info("final step");

            List<String> resultList = ar.result().list();
            route.response().end(resultList.toString());
        });

spring-boot-starter-vertx's People

Contributors

wanghongfei avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.