- Verticle实现类ServiceVerticle
public class ServiceVerticle extends AbstractVerticle
{
private static final int CORE_NUM = 2;
public static void main(String[] args)
{
Vertx vertx = Vertx.vertx();
DeploymentOptions deploymentOptions = new DeploymentOptions()
.setWorker(true)
.setInstances(CORE_NUM)
.setWorkerPoolSize(CORE_NUM)
.setWorkerPoolName("vertx-thread-pool");
vertx.deployVerticle(ServiceVerticle.class, deploymentOptions, asyncResult ->
{
if (asyncResult.failed())
{
System.out.println("Start server failed!");
}
else
{
System.out.println("Start server success!");
}
});
}
@Override
public void start(Future<Void> startFuture) throws Exception
{
JksOptions jksOptions = new JksOptions().setPath("localhost.jks").setPassword("localhost");
HttpServerOptions options = new HttpServerOptions().setSsl(true).setKeyStoreOptions(jksOptions);
HttpServer server = vertx.createHttpServer(options);
Router router = Router.router(vertx);
// We need request bodies
router.route().handler(BodyHandler.create());
// We consume application/json, also produce it
router.route().consumes("application/json").produces("application/json");
// Register API
registerResource(router);
// Start the HTTPS server
server.requestHandler(router::accept).listen(8080, asyncResult ->
{
if (asyncResult.failed())
{
System.out.println("start server failed! " + asyncResult.cause());
}
else
{
System.out.println("start server success!");
}
});
}
private void registerResource(Router router)
{
DemoResource demoResource = new DemoResource();
demoResource.registerResource(router);
}
}
public class DemoResource extends BaseResource
{
// 省略部分代码
@Override
public void registerResource(Router router)
{
router.get(VERSION +"/resource/:resource_id").handler(this::queryResource).failureHandler(this::failedHandler);
router.get(VERSION + "/resource").handler(this::queryAllResource).failureHandler(this::failedHandler);
router.post(VERSION + "/resource").handler(this::createResource).failureHandler(this::failedHandler);
}
}
代码Github地址