[英]CompletableFuture across microservices(JVM)
步骤1:我想在微服务A中启动一个CompletableFuture<String> asyncFuture
该服务通过say supplyAsync
运行异步任务。
步骤2:然后通过从不同的微服务B手动调用asyncFuture.complete(T value)
来手动完成相同的将来对象,这将由某些异步事件触发。
显然, 微服务A和微服务B具有不同的JVM。 实际上,微服务A和微服务B是在kubernetes中的不同Pod上运行的同一微服务的不同实例。
在步骤1和步骤2之间,将来的对象将存储在Redis中,微服务B可以安全地检索它。
快速浏览后,我想我将尝试以下几种解决方案:
1> HazelCast的分布式执行器服务,可以在调用时作为第二个参数传入
static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier, Executor executor)
参考: http : //docs.hazelcast.org/docs/2.3/manual/html/ch09.html
2>使用apache ignite的共享executorService
参考: https : //apacheignite.readme.io/v1.2/docs/executor-service
不知道两者是否都可以工作? 我也想知道以前有没有人处理过类似的事情? 如果是这样,我希望您能与我分享您的解决方案。
关于Apache Ignite,有很多选择如何协作节点(微服务)。 其中之一是连续查询[1],它允许侦听高速缓存上发生的数据修改。
例如,在服务A上,您可以创建ContinuousQuery并等待缓存中的值更改:
private String waitForValueChanged(IgniteCache<Integer, String> cache, Integer key) throws InterruptedException {
ContinuousQuery<Integer, String> qry = new ContinuousQuery<>();
qry.setInitialQuery(new ScanQuery<>((k, v) -> k == key));
final CountDownLatch waitForValueChanged = new CountDownLatch(1);
final AtomicReference<String> result = new AtomicReference<>();
CacheEntryUpdatedListener<Integer, String> listener = new CacheEntryUpdatedListener<Integer, String>() {
@Override public void onUpdated(Iterable<CacheEntryEvent<? extends Integer, ? extends String>> iterable) throws CacheEntryListenerException {
for (CacheEntryEvent<? extends Integer, ? extends String> entry: iterable) {
result.set(entry.getValue());
}
waitForValueChanged.countDown();
}
};
qry.setLocalListener(listener);
try (QueryCursor<Cache.Entry<Integer, String>> cur = cache.query(qry);) {
waitForValueChanged.await(60000, TimeUnit.MILLISECONDS);
}
return result.get();
}
在服务B上,您只需要将值放入高速缓存即可“完成未来”:
private void completeFuture(IgniteCache<Integer, String> cache, Integer key, String value) {
cache.put(key, value);
}
这是一个示例项目,显示了连续查询的工作方式[2]。
[1] https://apacheignite.readme.io/docs#section-continuous-queries
[2] https://github.com/gromtech/ignite-continuous-query-example
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.