使用vertx共享數據


Using Shared Data with Vert.x

io.vertx.core.shareddata

接口計數器



  • 公共接口櫃台
    一個異步計數器,可用於跨集群維護一致的計數。

     

    作者:
    蒂姆·福克斯
    • 方法細節

      • 得到

        無效get(處理程序 < AsyncResult <  >> resultHandler)
        獲取計數器的當前值
        參數:
        resultHandler  -將傳遞值的處理程序
      • IncrementAndGet

        無效的crementAndGet 處理程序 < AsyncResult <  >> resultHandler)
        自動增加計數器並返回新計數
        參數:
        resultHandler  -將傳遞值的處理程序
      • getAndIncrement

        void getAndIncrement(Handler < AsyncResult < Long >> resultHandler)
        以原子方式遞增計數器,並在遞增之前返回值。
        參數:
        resultHandler  -將傳遞值的處理程序
      • 遞減並獲取

        void decrementAndGet(Handler < AsyncResult < Long >> resultHandler)
        自動減少計數器並返回新計數
        參數:
        resultHandler  -將傳遞值的處理程序
      • addAndGet

        void addAndGet(long value,
                        Handler < AsyncResult < Long >> resultHandler)
        將值自動添加到計數器,然后返回新的計數
        參數:
        value  -要添加的值
        resultHandler  -將傳遞值的處理程序
      • getAndAdd

        void getAndAdd(long value,
                        Handler < AsyncResult < Long >> resultHandler)
        將值原子地添加到計數器,然后在添加之前返回值
        參數:
        value  -要添加的值
        resultHandler  -將傳遞值的處理程序
      • compareAndSet

        void compareAndSet(期待已久,
                           長值
                           Handler < AsyncResult < Boolean >> resultHandler)
        僅當當前值為期望值時,才將計數器設置為指定值。這是原子發生的。
        參數:
        expected  -期望值
        value  -新價值
        resultHandler  -處理程序將在成功時傳遞true

使用vertx共享數據

Shared data contains functionality that allows you to safely share data between different parts of your application, or 

共享數據功能允許你安全的在不同的模塊、

different applications in the same Vert.x instance or across a cluster of Vert.x instances.

或不同的應用、或不同的分布式實例之間共享數據。

Shared data includes local shared maps, distributed, cluster-wide maps, asynchronous cluster-wide locks and 

共享數據有多個方式:包括本地共享maps、分布式、寬集群的maps、異步的寬集群鎖

asynchronous cluster-wide counters.

、異步的寬集群計數器。

Local shared maps

Local shared maps allow you to share data safely between different event loops (e.g. different verticles) in the same Vert.x instance.

本地共享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:

這里有個本地共享的例子。

  1.  
    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");

     

 

 

Cluster-wide asynchronous maps

寬集群異步共享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是異步返回的,你可以單獨處理返回結果。

  1.  
     
  2.  
     
  3.  
    SharedData sd = vertx.sharedData();

    sd.<String, String>getClusterWideMap("mymap", res -> { if (res.succeeded()) { AsyncMap<String, String> map = res.result(); } else { // Something went wrong! } });

     

 

 

Putting data in a map

設置共享數據。

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里會有通知一旦設置完成。

  1.  
    map.put("foo", "bar", resPut -> {
     
      if (resPut.succeeded()) {
     
        // Successfully put the value
     
      } else {
     
        // Something went wrong!
     
      }
     
    });

     

 

 

Getting data from a map

獲取數據從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里獲取結果。

  1.  
    map.get("foo", resGet -> {
     
      if (resGet.succeeded()) {
     
        // Successfully got the value
     
        Object val = resGet.result();
     
      } else {
     
        // Something went wrong!
     
      }
     
    });

     

 

Other map operations

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

寬集群的鎖。

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.

  1.  
    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。

  1.  
    sd.getLockWithTimeout("mylock", 10000, res -> {
     
      if (res.succeeded()) {
     
        // Got the lock!
     
        Lock lock = res.result();
     
     
     
      } else {
     
        // Failed to get lock
     
      }
     
    });

     

 

Cluster-wide counters

集群計數器。

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來獲得。

  1.  
    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.

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM