且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用Niak的Riak提供的Erlang客户端库

更新时间:2023-11-22 08:21:28

获取事物的一种方法是:

A way to get-things-going is:


  1. 创建一个 gen_server ,将其添加到应用程序的主管树中。***包括您自己的应用程序。

  2. gen_server 初始化时,它将建立一个Riak连接。每当您想使用连接时,只要调用 gen_server ,由于它具有连接,因此可以

  3. 句柄重新连接。如果连接丢失,则会使 gen_server 崩溃。如果初始化时连接被拒绝,则请稍等片刻,然后重试。

  1. Create a gen_server which you add to the supervisor tree of an application. Preferably your own application which you include.
  2. When the gen_server initializes, it sets up a Riak connection. Whenever you want to use the connection, you call the gen_server and since it has the connection, it will be able to issue queries.
  3. Handle reconnects. If the connection is lost, you crash the gen_server. If the connection is refused upon initialization, you wait a bit and try again.

通常,您会看到一个单独的应用程序在运行后端的东西,然后是另一个应用程序(例如氮气)来处理Web内容。您的 gen_server 将属于后端部分。

Often, you will see a separate application running the "backend" stuff of an Erlang system and then another application, like nitrogen, to handle the Web-stuff. Your gen_server would belong to the "backend" part.

在此基础上您可以进一步扩展它:

Once this basis works you can extend it further:


  • 您的gen_server将生活在名为 foo 的模块中。每当您想使用riak时,都调用 foo:f(...)。重写 foo:f(...)以使用 http:/ /github.com/devinus/poolboy 或其他支持Riak集群的连接池的东西。但是只有在证明单个连接太慢之后才这样做。

  • Your gen_server will be living in a module named foo. Whenever you want to use riak, you call foo:f(...). Rewrite foo:f(...) to use http://github.com/devinus/poolboy or something such to support connection pooling toward the Riak cluster. But only do this once it proves that a single connection is too slow.

这很好地显示了Erlang程序中松散耦合的思想。 foo 模块的作用类似于一个接口,但是您不知道该接口背后的实现。如果需要,您可以稍后将其更改为更快的速度。您唯一需要做的就是实现正确的协议,在这种情况下,这是一个函数调用。

This neatly shows the idea of loose coupling in Erlang programs. The foo module acts like an interface, but you don't know the implementation behind that interface. You can change it later for something faster should the need arise. The only thing you need to do is to implement the correct protocol, which is a function call in this case.