使用Niak的Riak提供的Erlang客户端库 [英] Using the Erlang client library from Riak in Nitrogen

查看:142
本文介绍了使用Niak的Riak提供的Erlang客户端库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题是>包括Erlang客户端库的继续这是之前被问到的。

This question is a continuation of Including the Erlang client lib which was asked before.

我可以通过以下方式包含该库:

I was able to include the library by changing:

{mimetypes,     ".*",   {git, "git://github.com/spawngrid/mimetypes",   {branch, master}}},

%% Uncomment the following lines and comment the bottom lines with specific
%% tags to always pull the latest versions
{nitrogen_core, ".*",   {git, "git://github.com/nitrogen/nitrogen_core",{branch, master}}},

至:

{mimetypes,     ".*",   {git, "git://github.com/spawngrid/mimetypes",   {branch, master}}},

{riakc, "1.4.1",
          {git, "git://github.com/basho/riak-erlang-client", 
                       {tag, "1.4.1"}}},


%% Uncomment the following lines and comment the bottom lines with specific
%% tags to always pull the latest versions
{nitrogen_core, ".*",   {git, "git://github.com/nitrogen/nitrogen_core",{branch, master}}},

rel / nitrogen / rebar.config 并使用make

现在我已经将它们安装在 lib 文件夹下,我不是确定我应该在哪里将 riakc_pb_socket 库实现为建议在文档中

Now that I have those installed under the lib folder, I'm not sure where I should be implementing the riakc_pb_socket lib as suggested in the docs

我尝试放入

{ok, Pid} = riakc_pb_socket:start_link("127.0.0.1", 8087),

进入 nitrogen_sup:init(),但我收到此错误消息:

into nitrogen_sup:init() but i get this error message:

application: nitrogen
exited: {{{badmatch,{error,{tcp,econnrefused}}},
          [{nitrogen_sup,init,1,
                         [{file,"/home/neil/proj/nitrogen/rel/nitrogen/site/src/nitrogen_sup.erl"},
                          {line,43}]},
           {supervisor,init,1,[{file,"supervisor.erl"},{line,239}]},
           {gen_server,init_it,6,[{file,"gen_server.erl"},{line,304}]},
           {proc_lib,init_p_do_apply,3,
                     [{file,"proc_lib.erl"},{line,239}]}]},
         {nitrogen_app,start,[normal,[]]}}
type: temporary

我应该在应用程序初始化期间一次或在处理新请求时与riak数据库建立此连接。我看到一些关于连接池的话题,这是我在应用程序初始化期间进行一次设置,然后链接到新进程的事情。

Am I supposed to make this connection to riak database once during the app initialization, or often as new requests are handled. I saw some talk of connection pools, would this be something that I setup once during app initialization, then link to with new processes.

我是erlang / OTP的新手

I'm new to erlang/OTP and this framework, so any direction will be greatly appreciated.

添加的:

通过 bin /氮气控制台运行氮气应用程序时,我可以运行 {ok,Pid} = riakc_pb_socket:start_link( 127.0。 0.1,8087)。,而我确实设法从 riakc_pb_socket:ping(Pid)返回了 pong

When I run the nitrogen app via bin/nitrogen console, I am able to run {ok, Pid} = riakc_pb_socket:start_link("127.0.0.1",8087). and I do manage to get a pong back from riakc_pb_socket:ping(Pid).

我想现在的问题是:关于查询/读取/写入,通常在哪个文件中建立/管理riak连接?

I guess the question now is: In which files does one generally setup/manage riak connections with respect to queries/reads/writes?

推荐答案

获取事物的一种方法是:

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.

这篇关于使用Niak的Riak提供的Erlang客户端库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆