使用vertx共享數據html
Shared data contains functionality that allows you to safely share data between different parts of your application, or java
共享數據功能容許你安全的在不一樣的模塊、
node
different applications in the same Vert.x instance or across a cluster of Vert.x instances.web
或不一樣的應用、或不一樣的分佈式實例之間共享數據。api
Shared data includes local shared maps, distributed, cluster-wide maps, asynchronous cluster-wide locks and 安全
共享數據有多個方式:包括本地共享maps、分佈式、寬集羣的maps、異步的寬集羣鎖session
asynchronous cluster-wide counters.app
、異步的寬集羣計數器。異步
Local shared maps
allow you to share data safely between different event loops (e.g. different verticles) in the same Vert.x instance.async
本地共享maps容許你在不一樣的實例之間共享數據。
Local shared maps only allow certain data types to be used as keys and values. Those types must either be immutable, or certain other types that can be copied like Buffer
. In the latter case the key/value will be copied before putting it in the map.
本地存儲只容許某些數據類型被設置爲keys和values,這個類型必須不可變的或某些能夠被拷貝的類型例如Buffer,在最後keys/values被拷貝進map裏。
This way we can ensure there is no shared access to mutable state between different threads in your Vert.x application so
咱們能夠肯定不會有不一樣線程之間有多個狀態。因此
you don’t have to worry about protecting that state by synchronising access to it.
你不用擔憂異步訪問的問題。
Here’s an example of using a shared local map:
這裏有個本地共享的例子。
SharedData sd = vertx.sharedData(); LocalMap<String, String> map1 = sd.getLocalMap("mymap1"); map1.put("foo", "bar"); // Strings are immutable so no need to copyLocalMap<String, Buffer> map2 = sd.getLocalMap("mymap2"); map2.put("eek", Buffer.buffer().appendInt(123)); // This buffer will be copied before adding to map// Then... in another part of your application:map1 = sd.getLocalMap("mymap1"); String val = map1.get("foo"); map2 = sd.getLocalMap("mymap2"); Buffer buff = map2.get("eek");
寬集羣異步共享maps。
Cluster-wide asynchronous maps allow data to be put in the map from any node of the cluster and retrieved from any other node.
寬集羣共享map容許從任何集羣點、和任何節點上獲取共享數據。
This makes them really useful for things like storing session state in a farm of servers hosting a Vert.x web application.
You get an instance of AsyncMap
with getClusterWideMap
.
這個很是有用,例如存儲session狀態在一個web應用的集羣裏。你能夠得到一個AsyncMap從getClusterWideMap裏。
Getting the map is asynchronous and the result is returned to you in the handler that you specify. Here’s an example:
獲取map是異步返回的,你能夠單獨處理返回結果。
SharedData sd = vertx.sharedData(); sd.<String, String>getClusterWideMap("mymap", res -> { if (res.succeeded()) { AsyncMap<String, String> map = res.result(); } else { // Something went wrong! } });
設置共享數據。
You put data in a map with put
.
你能夠用put方法設置數據。
The actual put is asynchronous and the handler is notified once it is complete:
事實上put是異步的,hander裏會有通知一旦設置完成。
map.put("foo", "bar", resPut -> { if (resPut.succeeded()) { // Successfully put the value } else { // Something went wrong! } });
獲取數據從map裏。
You get data from a map with get
.
你能夠獲取數據用get。
The actual get is asynchronous and the handler is notified with the result some time later
實際上獲取也是異步的,能夠在hander裏獲取結果。
map.get("foo", resGet -> { if (resGet.succeeded()) { // Successfully got the value Object val = resGet.result(); } else { // Something went wrong! } });
map的其餘操做。
You can also remove entries from an asynchronous map, clear them and get the size.
你也能夠異步刪除、清空map裏的實例、和獲取map的size。
See the API docs
for more information.
查看更多。
寬集羣的鎖。
Cluster wide locks
allow you to obtain exclusive locks across the cluster - this is useful when you want to do something or access a resource on only one node of a cluster at any one time.
寬集羣鎖。
Cluster wide locks have an asynchronous API unlike most lock APIs which block the calling thread until the lock is obtained.
寬集羣鎖有一個異步的方法不像大部分鎖的api,它鎖的時候會阻止訪問線程直到鎖成功。
To obtain a lock use getLock
.
獲取一個鎖用getLock。
This won’t block, but when the lock is available, the handler will be called with an instance of Lock
, signifying that you now own the lock.
這個不阻止,當鎖有效,則handler會被執行,標示着你用了這個鎖。
While you own the lock no other caller, anywhere on the cluster will be able to obtain the lock.
When you’ve finished with the lock, you call release
to release it, so another caller can obtain it.
sd.getLock("mylock", res -> { if (res.succeeded()) { // Got the lock! Lock lock = res.result(); // 5 seconds later we release the lock so someone else can get it vertx.setTimer(5000, tid -> lock.release()); } else { // Something went wrong } });
You can also get a lock with a timeout. If it fails to obtain the lock within the timeout the handler will be called with a failure:
你能夠延時來獲取某個鎖,若是獲取鎖超時失敗了,會返回一個失敗failure。
sd.getLockWithTimeout("mylock", 10000, res -> { if (res.succeeded()) { // Got the lock! Lock lock = res.result(); } else { // Failed to get lock } });
集羣計數器。
It’s often useful to maintain an atomic counter across the different nodes of your application.
若是你常常用一個原子的計數器在不一樣的節點之間。
You can do this with Counter
.
你能夠用Counter來作。
You obtain an instance with getCounter
:
經過getCounter來得到。
sd.getCounter("mycounter", res -> { if (res.succeeded()) { Counter counter = res.result(); } else { // Something went wrong! } });
Once you have an instance you can retrieve the current count, atomically increment it, decrement and add a value to it using the various methods.
一旦你獲取了一個實例你能夠獲取當前計數,原子的,可增加,可減小的,可設置的經過各類方法。
See the API docs
for more information.