我是Vertx的新手并尝试了一些事情来更好地理解vertx。我已经编写了一个应用程序,需要一些帮助来部署相同的多个实例,我已粘贴下面的代码,任何人都可以告诉我这是否正确和正确的方法。提前谢谢。
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.eventbus.EventBus;
import io.vertx.core.http.HttpMethod;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.BodyHandler;
import io.vertx.ext.web.handler.CorsHandler;
import com.test.handler.ApiRequestHandler;
import com.test.handler.PushToRedisApiHandler;
import com.test.handler.RequestValidationHandler;
import com.test.utils.JedisLocalConnectionPool;
import com.test.utils.JedisMasterConnectionPool;
public class TestServer extends AbstractVerticle {
@Override
public void start(Future<Void> future) {
JedisLocalConnectionPool localJedisPool = new JedisLocalConnectionPool(
config().getString("redis.local.host", "127.0.0.1"), config()
.getInteger("redis.port", 6379), config().getString(
"redis.password", ""));
JedisMasterConnectionPool masterJedisPool = new JedisMasterConnectionPool(
config().getString("redis.master.host", "127.0.0.1"), config()
.getInteger("redis.port", 6379), config().getString(
"redis.password", ""));
EventBus eventBus = vertx.eventBus();
Router router = Router.router(vertx);
router.route().consumes("application/json");
router.route().produces("application/json");
router.route().handler(
CorsHandler.create("*").allowedMethod(HttpMethod.POST));
router.route().handler(BodyHandler.create());
router.post("/api/process-api-request").handler(
new ApiRequestHandler(eventBus, localJedisPool));
PushToRedisApiHandler pushToRedisHandler = new PushToRedisApiHandler(
masterJedisPool);
RequestValidationHandler validationHandler = new RequestValidationHandler(
localJedisPool, masterJedisPool);
eventBus.consumer("push.to.redis", pushToRedisHandler);
eventBus.consumer("push.to.redis", validationHandler);
// for running multiple instances
for (int i = 0; i < 4; i++) {
vertx.createHttpServer().requestHandler(router::accept)
.listen(config().getInteger("http.port", 80));
}
}
}
答案 0 :(得分:2)
多数民众赞成错误,您不必多次在Verticle内实例化http服务器。您必须多次实例化Verticle本身。
您可以使用vertx二进制文件或fatjar启动您的Verticle(这是一个示例https://github.com/floriankammermann/vertx-examples/tree/master/hello-world-java)
您可以添加此命令行参数以部署多个实例:--instances 2
您还可以以编程方式部署Verticle并传递实例数。您可以在此处找到示例:https://github.com/floriankammermann/vertx-examples/blob/master/verticle-instances/src/main/java/org/swisspush/vertx/examples/CryptoVerticleStarter.java