原文及更多文章請見我的博客:http://heartlifes.comhtml
在第2偏筆記中,咱們寫了第一個vert.x的hello world代碼,這裏,咱們把代碼中用到的幾個重要概念解釋下。java
Vertx類,是全部vert.x代碼的入口,官方代碼註釋爲:c++
The entry point into the Vert.x Core API.
即該類是全部vert.x core包API的總入口,簡單理解就是,全部核心功能的API,都須要該類去調用,全部的核心功能也都須要該類提供環境上下文。web
官方註釋:spring
An HTTP and WebSockets server
http/https/websockets服務器,vert.x發佈restful服務,不須要tomcat提供servlet容器,它本身就是一臺性能強大的web服務器,而且原生支持負載均衡,後面咱們會講到。json
先看官方代碼註釋:api
A router receives request from an HttpServer and routes it to the first matching Route that it contains. A router can contain many routes.
Router類能夠理解爲一個路由器,他接收httpserver帶來的請求,並將不一樣的請求分發到不一樣的路由中,若是簡單對比一下spring mvc的話,能夠將router理解爲spring mvc中的dispatcher。瀏覽器
route表明一條路由,一樣,對比spring mvc,至關於spring中的@RequestMapping,他指定了restful api的請求接口路徑,並將其交給handler來處理該條路由。tomcat
首先來看官方代碼註釋:服務器
Specify a request handler for the route. The router routes requests to handlers depending on whether the various criteria such as method, path, etc match. There can be only one request handler for a route. If you set this more than once it will overwrite the previous handler.
handler處理具體的路由請求,字面上講就是處理某個具體的restful api。他與httpserver,router,route的關係能夠用以下流程表示:
來自httpserver的request請求-->交由路由器作分發處理-->路由器匹配到具體的路由規則-->路由到最終的handler去處理請求
vert.x默認提供了不少處理器,包括但不侷限於如下:
AuthHandler 處理權限校驗支持 BodyHandler 提供全部請求上下文 CookieHandler 提供cookie支持 SessionHandler 提供session支持
官方代碼註釋:
Represents the context for the handling of a request in Vert.x-Web.
很簡單,請求上下文,能夠理解爲servlet中的httprequest和httpresponse
A verticle is a piece of code that can be deployed by Vert.x.
verticle是vert.x中,可被部署運行的最小代碼塊,能夠理解爲一個verticle就是一個最小化的業務處理引擎。
verticle被髮布部署後,會調用其內部的start方法,開始業務邏輯處理,完成後會調用stop方法,對該代碼塊執行銷燬動做
新建類:RestServer,代碼以下
package com.heartlifes.vertx.demo.simple; import io.vertx.core.AbstractVerticle; import io.vertx.core.Vertx; import io.vertx.core.json.JsonObject; import io.vertx.ext.web.Router; import io.vertx.ext.web.RoutingContext; import io.vertx.ext.web.handler.BodyHandler; public class RestServer extends AbstractVerticle { public static void main(String[] args) { // 獲取vertx基類 Vertx vertx = Vertx.vertx(); // 部署發佈rest服務 vertx.deployVerticle(new RestServer()); } // 重寫start方法,加入咱們的rest服務處理邏輯 @Override public void start() throws Exception { // 實例化一個路由器出來,用來路由不一樣的rest接口 Router router = Router.router(vertx); // 增長一個處理器,將請求的上下文信息,放到RoutingContext中 router.route().handler(BodyHandler.create()); // 處理一個post方法的rest接口 router.post("/post/:param1/:param2").handler(this::handlePost); // 處理一個get方法的rest接口 router.get("/get/:param1/:param2").handler(this::handleGet); // 建立一個httpserver,監聽8080端口,並交由路由器分發處理用戶請求 vertx.createHttpServer().requestHandler(router::accept).listen(8080); } // 處理post請求的handler private void handlePost(RoutingContext context) { // 從上下文獲取請求參數,相似於從httprequest中獲取parameter同樣 String param1 = context.request().getParam("param1"); String param2 = context.request().getParam("param2"); if (isBlank(param1) || isBlank(param2)) { // 若是參數空,交由httpserver提供默認的400錯誤界面 context.response().setStatusCode(400).end(); } JsonObject obj = new JsonObject(); obj.put("method", "post").put("param1", param1).put("param2", param2); // 申明response類型爲json格式,結束response而且輸出json字符串 context.response().putHeader("content-type", "application/json") .end(obj.encodePrettily()); } // 邏輯同post方法 private void handleGet(RoutingContext context) { String param1 = context.request().getParam("param1"); String param2 = context.request().getParam("param2"); if (isBlank(param1) || isBlank(param2)) { context.response().setStatusCode(400).end(); } JsonObject obj = new JsonObject(); obj.put("method", "get").put("param1", param1).put("param2", param2); context.response().putHeader("content-type", "application/json") .end(obj.encodePrettily()); } private boolean isBlank(String str) { if (str == null || "".equals(str)) return true; return false; } }
執行代碼,打開瀏覽器,輸入如下接口
http://localhost:8080/get/1/2 http://localhost:8080/post/1/2
package com.heartlifes.vertx.demo.simple; import io.vertx.core.AbstractVerticle; import io.vertx.core.Vertx; import io.vertx.ext.web.Router; import io.vertx.ext.web.Session; import io.vertx.ext.web.handler.CookieHandler; import io.vertx.ext.web.handler.SessionHandler; import io.vertx.ext.web.sstore.LocalSessionStore; public class SessionServer extends AbstractVerticle { public static void main(String[] args) { Vertx vertx = Vertx.vertx(); vertx.deployVerticle(new SessionServer()); } @Override public void start() throws Exception { Router router = Router.router(vertx); // 增長cookies處理器,解碼cookies,並將其放到context上下文中 router.route().handler(CookieHandler.create()); // 增長session處理器,爲每次用戶請求,維護一個惟一的session,這裏使用內存session,後面會講分佈式的session存儲 router.route().handler( SessionHandler.create(LocalSessionStore.create(vertx))); router.route().handler(routingContext -> { // 從請求上下文獲取session Session session = routingContext.session(); Integer count = session.get("count"); if (count == null) count = 0; count++; session.put("count", count); routingContext.response() .putHeader("content-type", "text/html") .end("total visit count:" + session.get("count")); }); vertx.createHttpServer().requestHandler(router::accept).listen(8080); } }
package com.heartlifes.vertx.demo.simple; import io.vertx.core.AbstractVerticle; import io.vertx.core.Vertx; import io.vertx.ext.web.Cookie; import io.vertx.ext.web.Router; import io.vertx.ext.web.handler.CookieHandler; public class CookieServer extends AbstractVerticle { public static void main(String[] args) { Vertx vertx = Vertx.vertx(); vertx.deployVerticle(new CookieServer()); } @Override public void start() throws Exception { Router router = Router.router(vertx); router.route().handler(CookieHandler.create()); router.route().handler( routingContext -> { Cookie cookie = routingContext.getCookie("testCookie"); Integer c = 0; if (cookie != null) { String count = cookie.getValue(); try { c = Integer.valueOf(count); } catch (Exception e) { c = 0; } c++; } routingContext.addCookie(Cookie.cookie("testCookie", String.valueOf(c))); routingContext.response() .putHeader("content-type", "text/html") .end("total visit count:" + c); }); vertx.createHttpServer().requestHandler(router::accept).listen(8080); } }