单机模式牵涉 emqx_backend_redis_sub模块,该模块主要实现订阅redis数据。
-module(emqx_backend_redis_sub).
-behaviour(gen_server).
-export([start_link/1]).
-export([init/1,handle_call/3,handle_cast/2, handle_info/2, terminate/2, code_change/3]).
-record(state, {sub}).
start_link(Opts) ->
gen_server:start_link(emqx_backend_redis_sub, [Opts], []).
init([Opts]) ->
%% 获取配置通道
Channel = proplists:get_value(channel, Opts),
%% 流量防卫模式
Sentinel = proplists:get_value(sentinel, Opts),
%% 主机地址
Host = case Sentinel =:= "" of
true -> proplists:get_value(host, Opts);
false ->
eredis_sentinel:start_link(proplists:get_value(servers, Opts)),
"sentinel:" ++ Sentinel
end,
%% 启动连接redis,返回Sub
{ok, Sub} = eredis:start_link(Host, proplists:get_value(port, Opts),
%% 获取redis配置
proplists:get_value(database, Opts), proplists:get_value(pass