自学vertx也快一个月了,给自己总结一下目前所得、以及成果展示:
基于jvm的异步驱动开发,主要是开发app接口、以及做网关使用,个人学习观点,仅供参考,
集成了zk、mybatis,代码只贴部分:
public class Application {
public static void main(String[] args) {
ClusterManager mgr = new ZookeeperClusterManager();
VertxOptions options = new VertxOptions().setClusterManager(mgr);
options.setClusterHost("10.12.12.210");//本机zk地址
Vertx.clusteredVertx(options, res -> {
if (res.succeeded()) {
System.out.println("success.");
Vertx vertx = res.result();
DeploymentOptions option = new DeploymentOptions();
vertx.deployVerticle(new GateWayVerticle1(),option);
} else {
}
});
}
}
实例化网关端口:根据服务名分发服务
public class GateWayVerticle1 extends AbstractVerticle {
private static Logger LOG = Logger.getLogger(GateWayVerticle.class);
@Override
public void start() throws Exception {
Router router = Router.router(vertx);
router.route().handler(LoggerHandler.create());
router.route().handler(CookieHandler.create());
router.route().handler(BodyHandler.create());
router.route().handler(StaticHandler.create());
router.route("/gateway.do").handler(context -> {
HttpServerResponse response = context.response();
response.putHeader("content-type", "application/json");
JsonObject result = new JsonObject();
try {
JsonObject request = context.getBodyAsJson();
String ip = context.request().remoteAddress().host();
request.put("ip", ip);
LOG.info(request);
String service = request.getString("service");
String channel = request.getString("channel");
String sv = request.getString("sv");
String cv = request.getString("cv");
String pn = request.getString("pn");
String sign_type = request.getString("sign_type");
String sign = request.getString("sign");
String serial_number = request.getString("serial_number");
String timestamp = request.getString("timestamp");
JsonObject request_data = request.getJsonObject("request_data");
request_data.put("serial_number", serial_number);
result.put("serial_number", serial_number);
if (!request.containsKey("skip")) {
//skip all check.
}
DeliveryOptions options = new DeliveryOptions();
options.addHeader("cmd", "save");
options.setSendTimeout(5000);
vertx.eventBus().send(service, request_data, res -> {
if (res.succeeded()) {
JsonObject _res = (JsonObject) res.result().body();
result.put("code", _res.getValue("code"));
result.put("message", _res.getValue("message"));
result.put("timestamp", System.currentTimeMillis());
result.put("response_data", _res.getValue("response_data"));
} else {
result.put("code", 500);
result.put("message", res.cause().getMessage());
result.put("timestamp", System.currentTimeMillis());
}
LOG.info(result.toString());
response.end(result.toString());
});
} catch (Exception ex) {
result.put("code", 500);
result.put("message", ex.getMessage());
result.put("timestamp", System.currentTimeMillis());
LOG.info(result.toString());
response.end(result.toString());
}
});
vertx.createHttpServer().requestHandler(router::accept).listen(8080);
}
}
服务处理类:
orm层使用mybatis
@Component
public class Test4 extends AbstractVerticle{
private static Logger logger = Logger.getLogger(Test4.class);
@Autowired
private TransactionTemplate sjc;
@Autowired
private DataSource dataSource;
@Qualifier(value="jdbcTemplate11")
private JdbcTemplate jdbcTemplate;
@Autowired
private AppUserService appUserService;
public Test4() {
}
@Override
public void start() throws Exception {
vertx.eventBus().consumer("chinamcom.watch.login", message -> {
JsonObject request = (JsonObject) message.body();
logger.info("登陆请求:" + message.body());
JsonObject response = new JsonObject();
int userId = request.getInteger("id");
// int userId = Integer.parseInt(request.getString("id"));
AppUser user = this.appUserService.getUserById(userId);
int id = user.getId();
String username = user.getName();
System.out.println("id=="+id+"...name="+username);
message.reply(response);
});
}
}