Skip to content
Permalink
Browse files
dev reference ref process
  • Loading branch information
DLive committed Jun 23, 2019
1 parent 216b5a7 commit e0b122b375aadbf1524789d54360728a7d90c027
Showing 9 changed files with 113 additions and 38 deletions.
@@ -17,6 +17,7 @@
]
},
{dubboerl,[
{registry,zookeeper},
{zookeeper_list,[{"127.0.0.1",2181}]},
{application,<<"testdubboerl">>},
{registry,true},
@@ -100,6 +100,7 @@
}).


-record(interface_info, {interface, loadbalance}).

-record(interface_list, {interface, pid, connection_info}).
%%-record(provider_node_list, {host_flag, pid, weight, readonly = false}).
@@ -18,7 +18,7 @@

-include("dubboerl.hrl").
%% API
-export([local_ip_v4/0, local_ip_v4_str/0, parse_url/1, map_to_url/1]).
-export([local_ip_v4/0, local_ip_v4_str/0, parse_url/1, url_to_binary/1]).

local_ip_v4() ->
{ok, Addrs} = inet:getifaddrs(),
@@ -67,7 +67,7 @@ parse_url_parameter([Item | Rest], Parameters) ->
end.


map_to_url(UrlInfo) ->
url_to_binary(UrlInfo) ->
ParameterStr =
case UrlInfo#dubbo_url.parameters of
undefined ->
@@ -79,10 +79,11 @@ map_to_url(UrlInfo) ->
ParameterStr2 = ["?" | ParameterStr1],
list_to_binary(ParameterStr2)
end,
Value = io_lib:format(<<"~s://~s/~s?~s">>,
Value = io_lib:format(<<"~s://~s:~p/~s?~s">>,
[
UrlInfo#dubbo_url.scheme,
UrlInfo#dubbo_url.host,
UrlInfo#dubbo_url.port,
UrlInfo#dubbo_url.path,
ParameterStr
]),
@@ -18,6 +18,8 @@

-behaviour(gen_server).
-include("dubboerl.hrl").
-include("dubbo.hrl").

-export([subscribe/2,notify/2]).
%% API
-export([start_link/0]).
@@ -92,15 +94,15 @@ refresh_invoker(UrlList)->
case pick_interface(UrlList) of
{error,Reason}->
fail;
{"empty",Interface}->
{"empty",Interface,_}->
todo_destroy;
{_,Interface} ->
{_,Interface,LoadBalance} ->
OldProviderHosts = dubbo_provider_consumer_reg_table:get_interface_provider_node(Interface),
NewInvokers = refresh_invoker(UrlList,[]),
NewProviderHosts = [Item#dubbo_invoker.host_flag || Item <- NewInvokers],
DeleteProverList = OldProviderHosts -- NewProviderHosts,
dubbo_provider_consumer_reg_table:clean_invalid_provider(DeleteProverList)

dubbo_provider_consumer_reg_table:clean_invalid_provider(DeleteProverList),
dubbo_provider_consumer_reg_table:update_connection_info(#interface_info{interface = Interface,loadbalance = LoadBalance})
end.
%% OldProviderHosts =

@@ -119,7 +121,8 @@ pick_interface([Url | _]) ->
case dubbo_common_fun:parse_url(Url) of
{ok,UrlInfo}->
Interface = maps:get("interface",UrlInfo#dubbo_url.parameters),
{UrlInfo#dubbo_url.scheme,Interface};
LoadBalance = list_to_atom("dubbo_loadbalance_" ++ maps:get("loadbalance",UrlInfo#dubbo_url.parameters,"random")),
{UrlInfo#dubbo_url.scheme,Interface,LoadBalance};
{error,Reason} ->
{error,Reason}
end.
@@ -0,0 +1,20 @@
%%------------------------------------------------------------------------------
%% Licensed to the Apache Software Foundation (ASF) under one or more
%% contributor license agreements. See the NOTICE file distributed with
%% this work for additional information regarding copyright ownership.
%% The ASF licenses this file to You under the Apache License, Version 2.0
%% (the "License"); you may not use this file except in compliance with
%% the License. You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%------------------------------------------------------------------------------
-module(dubbo_loadbalance_random).

%% API
-export([]).
@@ -49,5 +49,5 @@ gen_consumer_url(UrlInfo)->
path = Interface,
parameters = Parameters2
},
ConsumerUrl = dubbo_common_fun:map_to_url(ConsumerUrlInfo),
ConsumerUrl = dubbo_common_fun:url_to_binary(ConsumerUrlInfo),
ConsumerUrl.
@@ -36,6 +36,9 @@
-define(SERVER, ?MODULE).

-define(INTERFCE_LIST_TABLE, interface_list).

-define(INTERFAE_INFO_TABLE,dubbo_interface_info).

-define(PROVIDER_NODE_LIST_TABLE, provider_node_list).

-record(state, {}).
@@ -87,16 +90,21 @@ init_ets_table() ->
ok
catch
_Type:Reason ->
logger:error("new ets table error ~p", [Reason]),
error
logger:error("new ets table INTERFCE_LIST_TABLE error ~p", [Reason])
end,
try ets:new(?PROVIDER_NODE_LIST_TABLE, [bag, public, named_table, {keypos, 2}]) of
?PROVIDER_NODE_LIST_TABLE ->
ok
catch
_Type1:Reason1 ->
logger:error("new ets table error ~p", [Reason1]),
error
logger:error("new ets table PROVIDER_NODE_LIST_TABLE error ~p", [Reason1])
end,
try ets:new(?INTERFAE_INFO_TABLE, [public, named_table, {keypos, 2}]) of
?INTERFAE_INFO_TABLE ->
ok
catch
_Type1:Reason1 ->
logger:error("new ets table PROVIDER_NODE_LIST_TABLE error ~p", [Reason1])
end,
ok.
%%--------------------------------------------------------------------
@@ -196,6 +204,9 @@ get_host_connections(Host, Port) ->
List = ets:lookup(?PROVIDER_NODE_LIST_TABLE, HostFlag),
List.

update_interface_info(InterfaceInfo)->
ets:insert(?INTERFAE_INFO_TABLE,InterfaceInfo).


%%%===================================================================
%%% Internal functions
@@ -16,24 +16,28 @@
%%------------------------------------------------------------------------------
-module(dubbo_reference_config).

-include("dubbo.hrl").
-include("dubboerl.hrl").

-record(dubbo_interface_info,{}).

%% API
-export([]).

init_reference()->
InitConfigMap= #{
-export([init_reference/1]).

},
init_reference(ConsumerInfo)->
%% InitConfigMap= #{
%%
%% },
%% 组装各类需要数据
create_proxy(ConsumerInfo),
ok.


create_proxy(InitConfigMap)->
create_proxy(ConsumerInfo)->



InterfaceClassInfo = #{},
Para = gen_parameter(),
Para = gen_parameter(ConsumerInfo),
Url = gen_registry_url(Para),
dubbo_extension:run(protocol_wapper,refer,[Url]),
ok.
@@ -43,32 +47,65 @@ create_proxy(InitConfigMap)->

gen_registry_url(Para)->
%%todo 组装para & url
{Host,Port} = get_registry_host_port(),
UrlInfo = #dubbo_url{
scheme = <<"registry">>,
host = list_to_binary(Host),
port = integer_to_binary(Port),
path = <<"org.apache.dubbo.registry.RegistryService">>,
parameters = Para
},
dubbo_common_fun:url_to_binary(UrlInfo).
%% Url = "registry://127.0.0.1:2181/org.apache.dubbo.registry.RegistryService?application=hello-world&dubbo=2.0.2&pid=68901&refer=application%3Dhello-world%26default.check%3Dfalse%26default.lazy%3Dfalse%26default.retries%3D0%26default.sticky%3Dfalse%26default.timeout%3D300000%26dubbo%3D2.0.2%26interface%3Dorg.apache.dubbo.erlang.sample.service.facade.UserOperator%26lazy%3Dfalse%26methods%3DqueryUserInfo%2CqueryUserList%2CgenUserId%2CgetUserInfo%26pid%3D68901%26register.ip%3D127.0.0.1%26release%3D2.7.1%26retries%3D0%26side%3Dconsumer%26sticky%3Dfalse%26timestamp%3D1559727789953&registry=zookeeper&release=2.7.1&timestamp=1559727842451",
%% Url.

get_registry_host_port()->
%% @todo need adapter other registry
RegistryList = application:get_env(dubboerl,zookeeper_list,[{"127.0.0.1",2181}]),
[Item|_] = RegistryList,
Item.

Url = "registry://127.0.0.1:2181/org.apache.dubbo.registry.RegistryService?application=hello-world&dubbo=2.0.2&pid=68901&refer=application%3Dhello-world%26default.check%3Dfalse%26default.lazy%3Dfalse%26default.retries%3D0%26default.sticky%3Dfalse%26default.timeout%3D300000%26dubbo%3D2.0.2%26interface%3Dorg.apache.dubbo.erlang.sample.service.facade.UserOperator%26lazy%3Dfalse%26methods%3DqueryUserInfo%2CqueryUserList%2CgenUserId%2CgetUserInfo%26pid%3D68901%26register.ip%3D127.0.0.1%26release%3D2.7.1%26retries%3D0%26side%3Dconsumer%26sticky%3Dfalse%26timestamp%3D1559727789953&registry=zookeeper&release=2.7.1&timestamp=1559727842451",
Url.
gen_parameter()->
gen_parameter(ConsumerInfo)->
Para = #{
<<"application">> => get_appname(),
<<"application">> => get_appname(ConsumerInfo),
<<"dubbo">> => <<"2.0.2">>,
<<"pid">> => get_pid(),
<<"refer">> => get_refinfo(),
<<"refer">> => get_refinfo(ConsumerInfo),
<<"registry">> => get_registry_type(),
<<"release">> => <<"2.7.1">>,
<<"timestamp">> => <<"1559727842451">>
<<"timestamp">> => integer_to_binary(dubbo_time_util:timestamp_ms())
},

Para.

get_appname()->
%%todo
<<"hello-world">>.
get_appname(ConsumerInfo)->
ConsumerInfo#consumer_config.application.
get_pid()->
%%todo
<<"68901">>.
get_refinfo()->
%%todo
<<"application%3Dhello-world%26default.check%3Dfalse%26default.lazy%3Dfalse%26default.retries%3D0%26default.sticky%3Dfalse%26default.timeout%3D300000%26dubbo%3D2.0.2%26interface%3Dorg.apache.dubbo.erlang.sample.service.facade.UserOperator%26lazy%3Dfalse%26methods%3DqueryUserInfo%2CqueryUserList%2CgenUserId%2CgetUserInfo%26pid%3D68901%26register.ip%3D127..0.1%26release%3D2.7.1%26retries%3D0%26side%3Dconsumer%26sticky%3Dfalse%26timestamp%3D1559727789953">>.
os:getpid().
get_refinfo(ConsumerInfo)->
KeyValues=[
{"application",ConsumerInfo#consumer_config.application},
{"default.check",ConsumerInfo#consumer_config.check},
{"default.lazy","false"},
{"default.retries","0"},
{"default.sticky","false"},
{"default.timeout","300000"},
{"dubbo","2.0.2"},
{"interface",ConsumerInfo#consumer_config.interface},
{"lazy","false"},
{"methods",ConsumerInfo#consumer_config.methods},
{"register.ip",ConsumerInfo#consumer_config.application},
{"release","2.7.1"},
{"pid",get_pid()},
{"side","consumer"},
{"sticky","false"},
{"timestamp",dubbo_time_util:timestamp_ms()}
],
KeyValues2 = [io_lib:format("~s=~p", [Key, Value]) || {Key, Value} <= KeyValues],
ParameterStr1 = string:join(KeyValues2, "&"),
list_to_binary(http_uri:encode(ParameterStr1)).
%% <<"application%3Dhello-world%26default.check%3Dfalse%26default.lazy%3Dfalse%26default.retries%3D0%26default.sticky%3Dfalse%26default.timeout%3D300000%26dubbo%3D2.0.2%26interface%3Dorg.apache.dubbo.erlang.sample.service.facade.UserOperator%26lazy%3Dfalse%26methods%3DqueryUserInfo%2CqueryUserList%2CgenUserId%2CgetUserInfo%26pid%3D68901%26register.ip%3D127..0.1%26release%3D2.7.1%26retries%3D0%26side%3Dconsumer%26sticky%3Dfalse%26timestamp%3D1559727789953">>.

get_registry_type()->
%%todo
<<"zookeeper">>.
atom_to_binary(application:get_env(dubboerl,registry,zookeeper)).
@@ -33,8 +33,9 @@ start_consumer() ->
ApplicationName = application:get_env(dubboerl, application, <<"defaultApplication">>),
lists:map(fun({Interface, Option}) ->
ConsumerInfo = dubbo_config_util:gen_consumer(ApplicationName, Interface, Option),
dubbo_zookeeper:register_consumer(ConsumerInfo),
logger:info("register consumer success ~p", [Interface])
%% dubbo_zookeeper:register_consumer(ConsumerInfo),
dubbo_reference_config:init_reference(ConsumerInfo),
logger:info("consumer refer success ~p", [Interface])
end, ConsumerList),
ok.

0 comments on commit e0b122b

Please sign in to comment.