dburyak

vertx multiple http servers at same port

Jul 9th, 2020
376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 1.96 KB | None | 0 0
  1. @Log4j2
  2. class MultipleHttpServersApp {
  3.     static void main(String[] args) {
  4.         def vertx = Vertx.vertx()
  5.         def port = 9003
  6.         vertx.deployVerticle({ new HttpVerticle(port: port) }, new DeploymentOptions().setInstances(1000)) {
  7.             log.info 'done deploying verticles, let\'s run bunch of requests'
  8.             def client = WebClient.create(vertx, new WebClientOptions().setDefaultPort(port))
  9.             vertx.setTimer(5_000) {
  10.                 10_000.times {
  11.                     client.request(HttpMethod.GET, port, 'localhost', '/').send { resp ->
  12.                         if (resp.succeeded()) {
  13.                             log.info 'got resp : {}', resp.result().bodyAsString()
  14.                         } else {
  15.                             log.error 'got failed resp : {}', resp.cause()
  16.                         }
  17.                     }
  18.                 }
  19.             }
  20.         }
  21.     }
  22. }
  23.  
  24.  
  25.  
  26. @Log4j2
  27. class HttpVerticle extends AbstractVerticle {
  28.     private static final AtomicInteger numInstances = new AtomicInteger(0)
  29.     private HttpServer httpServer
  30.  
  31.     Integer port = 9003
  32.     Integer verticleId = numInstances.incrementAndGet()
  33.  
  34.     @Override
  35.     void start(Promise<Void> startPromise) throws Exception {
  36.         log.info 'creating new vertx http server: verticleId={}', verticleId
  37.         httpServer = vertx.createHttpServer()
  38.                 .requestHandler { r ->
  39.                     log.info 'got request : {}', verticleId
  40.                     r.response().end(verticleId as String)
  41.                 }
  42.                 .listen(9003) {
  43.                     log.info 'http server started : {}, {}', verticleId, it.succeeded()
  44.                     startPromise.handle(it.map(null))
  45.                 }
  46.     }
  47.  
  48.     @Override
  49.     void stop(Promise<Void> stopPromise) throws Exception {
  50.         httpServer.close {
  51.             log.info 'http server closed: {}', it.succeeded()
  52.             stopPromise.handle(it)
  53.         }
  54.     }
  55. }
  56.  
  57.  
  58.  
  59.  
  60. #build.gradle:
  61. plugins {
  62.     id 'groovy'
  63.     id 'application'
  64.     id 'com.github.johnrengelman.shadow' version '5.1.0'
  65. }
  66.  
  67. ext {
  68.     wrapperGradleVersion = gradleVersion
  69. }
  70.  
  71. group = 'dburyak.sandbox.vertx'
  72. version = '0.0.1-SNAPSHOT'
  73.  
  74. sourceCompatibility = 11
  75. targetCompatibility = 11
  76. mainClassName = 'dburyak.sandbox.vertx.EchoServerApp'
  77.  
  78. repositories {
  79.     mavenLocal()
  80.     jcenter()
  81.     mavenCentral()
  82. }
  83.  
  84. dependencies {
  85.     implementation "org.codehaus.groovy:groovy-all:${groovyVersion}"
  86.     implementation "io.vertx:vertx-core:${vertxVersion}"
  87.     implementation "io.vertx:vertx-lang-groovy:${vertxVersion}"
  88.     implementation "io.vertx:vertx-mysql-client:${vertxVersion}"
  89.     implementation "io.vertx:vertx-pg-client:${vertxVersion}"
  90.     implementation "io.vertx:vertx-web-client:${vertxVersion}"
  91.  
  92.     // logging
  93.     // log4j2 common configuration
  94.     implementation "org.apache.logging.log4j:log4j-core:${log4jVersion}"
  95.     implementation "org.apache.logging.log4j:log4j-slf4j-impl:${log4jVersion}"
  96. //    implementation "com.lmax:disruptor:${disruptorVersion}"
  97.     implementation "com.fasterxml.jackson.core:jackson-databind:${jacksonVersion}"
  98.     implementation "com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:${jacksonVersion}"
  99.  
  100.     // mongo
  101.  
  102. }
  103.  
  104. wrapper {
  105.     gradleVersion = wrapperGradleVersion
  106.     distributionType = 'ALL'
  107. }
  108.  
  109. shadowJar {
  110.     mergeServiceFiles {
  111.         include 'META-INF/services/io.vertx.core.spi.VerticleFactory'
  112.     }
  113. }
  114.  
  115. run {
  116.     systemProperties['vertx.logger-delegate-factory-class-name'] = 'io.vertx.core.logging.SLF4JLogDelegateFactory'
  117. }
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126. #gradle.properties:
  127. gradleVersion=6.5
  128. vertxVersion=3.9.1
  129. groovyVersion=3.0.4
  130. log4jVersion=2.13.0
  131. jacksonVersion=2.10.2
  132. disruptorVersion=3.4.2
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142. #log4j2.yml
  143. Configuration:
  144.   status: info
  145.   name: default
  146.   appenders:
  147.     Console:
  148.       name: CONSOLE
  149.       PatternLayout:
  150.         Pattern: "%style{\
  151.            %date{yyyy-MM-dd HH:mm:ss.SSS}\
  152.            }{white} \
  153.            \
  154.            %highlight{%-5level}{FATAL=Blinking bright red, ERROR=Red, WARN=Yellow, INFO=Cyan, DEBUG=White, TRACE=Black} \
  155.            \
  156.            [%style{\
  157.            %17.17thread\
  158.            }{yellow}] \
  159.            \
  160.            %style{\
  161.            %40.40logger\
  162.            }{cyan} - \
  163.            \
  164.            %msg%n%throwable\
  165.            "
  166.   Loggers:
  167.     Root:
  168.       level: debug
  169.       AppenderRef:
  170.         ref: CONSOLE
  171.     logger:
  172.       - name: com.athaydes.spockframework.report
  173.         level: warn
  174.       - name: io.netty
  175.         level: info
  176.       - name: io.vertx
  177.         level: info
  178.       - name: io.micronaut
  179.         level: info
  180.       - name: org.mongodb.driver
  181.         level: info
  182.       - name: org.mongodb.driver.connection
  183.         level: warn
  184.       - name: com.hazelcast
  185.         level: warn
  186.       - name: com.networknt.schema
  187.         level: warn
Add Comment
Please, Sign In to add comment