1.为了解Vertx的微服务,这几天做了点测试,项目地址
https://github.com/peijie1662/vertx-cluser
目录结构
vertx_cluser_01和vertx_cluser_02模拟集群的二个节点。
maven install时注意修改
<main.verticle>pj.com.cn.vertx_cluser_02.MainVerticle</main.verticle>
分别编译为2个cluser的jar,cluser01.jar,cluser02.jar
运行时分别在2个命令窗口中运行java -jar cluserXX.jar即可。
- 步骤
2.1 创建简单的服务供测试
public class MyFirstVerticle extends AbstractVerticle {
public void start() {
Router router = Router.router(vertx);
router.get("/").handler(rc -> {
rc.response().end("Welcome use first-service!");
});
vertx.createHttpServer().requestHandler(router).listen(8081);
}
}
2.2 在cluser01节点,得到集群模式下的clusteredVertx,并在这个vertx实例上发布服务first-service和second-service。
然后将2个服务注册到服务发现(ServiceDiscovery)实例。
public class MainVerticle extends AbstractVerticle {
public void start() {
ClusterManager mgr = new HazelcastClusterManager();
VertxOptions options = new VertxOptions().setClusterManager(mgr);
Vertx.clusteredVertx(options, res -> {
if (res.succeeded()) {
vertx = res.result();
vertx.deployVerticle(new MyFirstVerticle());
vertx.deployVerticle(new MySecondVerticle());
ServiceDiscovery.create(vertx, sd -> {
sd.publish(HttpEndpoint.createRecord("first-service", "localhost", 8081, "/"), ar -> {
System.out.println("cluser01 - first-service publish success!");
});
sd.publish(HttpEndpoint.createRecord("second-service", "localhost", 8082, "/"), ar -> {
System.out.println("cluser01 - second-service publish success!");
});
});
} else {
res.cause().printStackTrace();
}
});
}
}
2.3 在cluser02节点,略微复杂,通过服务发现实例去调用cluser01节点中的first-service。
public class MyThirdVerticle extends AbstractVerticle {
private CircuitBreaker breaker;
public void start() {
breaker = CircuitBreaker
.create("third-circuit-breaker", vertx, new CircuitBreakerOptions().setMaxFailures(5).setTimeout(2000))
.openHandler(v -> {
System.out.println("Circuit opened");
}).closeHandler(v -> {
System.out.println("Circuit closed");
});
// 重试策略
// .retryPolicy(retryCount -> retryCount * 100L)
Router router = Router.router(vertx);
router.get("/").handler(rc -> {
rc.response().end("Welcome use third-service!");
});
router.get("/cluser01/first").handler(rc -> {
ServiceDiscovery sd = ServiceDiscovery.create(vertx);
sd.getRecord(new JsonObject().put("name", "first-service"), r -> {
if (r.succeeded()) {
// System.out.println("record:"+r.result().toJson());
breaker.execute(f -> {
Record rd = r.result();
ServiceReference ref = sd.getReference(rd);
WebClient wc = ref.getAs(WebClient.class);
wc.get("/").send(res -> {
if (res.succeeded()) {
f.complete("call first-service:" + res.result().body().toString());
} else {
f.fail("first-service unvalid! " + breaker.failureCount());
}
});
ref.release();
}).setHandler(ar -> {
if (ar.succeeded()) {
rc.response().end(
"breaker status:" + breaker.state().toString() + " " + ar.result().toString());
} else {
rc.response().end(
"breaker status:" + breaker.state().toString() + " " + ar.cause().getMessage());
}
});
} else {
rc.response().end("could not found first-service!");
}
});
sd.close();
});
vertx.createHttpServer().requestHandler(router).listen(8083);
}
}
另外在这里顺便测试了CircuitBreaker断路器。
结论:
1.运行cluser01.jar和cluser02.jar,会发现二者可以做到实时互相发现,对方上下线都可以及时发现,(因为默认使用了UDP在同网段中组播)
2.通过http://localhost:8083/cluser01/first可以通过third-service访问到first-service。说明服务发现运行正常。
3.此时将cluser01关掉,打印服务发现实例中first-service的状态仍旧是UP,虽然cluser02的窗口已提示cluser01掉线.,所以也不知道说啥好了。
4.断路器还是比较好用的,关掉cluser01后,猛刷几次http://localhost:8083/cluser01/first,即可看到断路效果,要注意的是,必须在指定时间窗口内(默认10秒,可通过setFailuresRollingWindow设置)失败次数超setMaxFailures值才会开路(open)。
5.还少个API Gateway,对于测试来说意义也不大,总体感觉有些简陋,有些点还没覆盖到,做做“小”的微服务应该问题不大:)