Java 需要扩展Vertex Http服务器的帮助吗
我是Vertx新手,尝试了一些事情来更好地理解Vertx。我已经编写了一个应用程序,在部署同一个应用程序的多个实例时需要一些帮助,我已经粘贴了下面的代码。有人能告诉我这是否正确以及正确的方法吗。提前谢谢Java 需要扩展Vertex Http服务器的帮助吗,java,vert.x,Java,Vert.x,我是Vertx新手,尝试了一些事情来更好地理解Vertx。我已经编写了一个应用程序,在部署同一个应用程序的多个实例时需要一些帮助,我已经粘贴了下面的代码。有人能告诉我这是否正确以及正确的方法吗。提前谢谢 import io.vertx.core.AbstractVerticle; import io.vertx.core.Future; import io.vertx.core.eventbus.EventBus; import io.vertx.core.http.HttpMethod; im
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));
}
}
}
import io.vertx.core.AbstractVerticle;
导入io.vertx.core.Future;
导入io.vertx.core.eventbus.eventbus;
导入io.vertx.core.http.HttpMethod;
导入io.vertx.ext.web.Router;
导入io.vertx.ext.web.handler.BodyHandler;
导入io.vertx.ext.web.handler.CorsHandler;
导入com.test.handler.ApiRequestHandler;
导入com.test.handler.PushToRedisApiHandler;
导入com.test.handler.RequestValidationHandler;
导入com.test.utils.JedisLocalConnectionPool;
导入com.test.utils.JedisMasterConnectionPool;
公共类TestServer扩展了AbstractVerticle{
@凌驾
公共无效启动(未来){
JedisLocalConnectionPool localJedisPool=新的JedisLocalConnectionPool(
config().getString(“redis.local.host”,“127.0.0.1”),config()
.getInteger(“redis.port”,6379),config().getString(
“redis.password”,”);
JedisMasterConnectionPool masterJedisPool=新的JedisMasterConnectionPool(
config().getString(“redis.master.host”,“127.0.0.1”),config()
.getInteger(“redis.port”,6379),config().getString(
“redis.password”,”);
EventBus EventBus=vertx.EventBus();
路由器=路由器。路由器(vertx);
route.route()消耗(“应用程序/json”);
route()生成(“应用程序/json”);
router.route()处理程序(
create(“*”).allowedMethod(HttpMethod.POST));
router.route().handler(BodyHandler.create());
router.post(“/api/processapi请求”).handler(
新的APIRestHandler(eventBus,localspool));
PushToredisapHandler pushToRedisHandler=新的PushToredisapHandler(
大师级(卷轴);
RequestValidationHandler validationHandler=新建RequestValidationHandler(
localJedisPool,masterJedisPool);
消费者(“push.to.redis”,pushToRedisHandler);
consumer(“push.to.redis”,validationHandler);
//用于运行多个实例
对于(int i=0;i<4;i++){
createHttpServer().requestHandler(路由器::接受)
.listen(config().getInteger(“http.port”,80));
}
}
}
这是错误的,您不必在垂直链接中多次实例化http服务器。您必须多次实例化垂直体本身
您可以使用vertx二进制文件或作为fatjar启动verticle(以下是一个示例)
您可以添加此命令行参数来部署多个实例:--instances 2
您还可以通过编程方式部署verticle,并传递实例数。您可以在这里找到一个示例:这是错误的,您不必在垂直体中多次实例化http服务器。您必须多次实例化verticle本身 您可以使用vertx二进制文件或作为fatjar启动verticle(以下是一个示例) 您可以添加此命令行参数来部署多个实例:
--instances 2
您还可以通过编程方式部署verticle,并传递实例数。您可以在这里找到一个示例: