This is an automated email from the ASF dual-hosted git repository. dlive pushed a commit to branch 0.4.0 in repository https://gitbox.apache.org/repos/asf/dubbo-erlang.git
commit e0b122b375aadbf1524789d54360728a7d90c027 Author: DLive <[email protected]> AuthorDate: Sun Jun 23 23:39:36 2019 +0800 dev reference ref process --- config_example/sys.config | 1 + include/dubbo.hrl | 1 + src/dubbo_common_fun.erl | 7 +-- src/dubbo_directory.erl | 13 +++-- src/dubbo_loadbalance_random.erl | 20 ++++++++ src/dubbo_protocol_registry.erl | 2 +- src/dubbo_provider_consumer_reg_table.erl | 19 +++++-- src/dubbo_reference_config.erl | 83 ++++++++++++++++++++++--------- src/dubboerl.erl | 5 +- 9 files changed, 113 insertions(+), 38 deletions(-) diff --git a/config_example/sys.config b/config_example/sys.config index 0ed8a87..07cfaf9 100644 --- a/config_example/sys.config +++ b/config_example/sys.config @@ -17,6 +17,7 @@ ] }, {dubboerl,[ + {registry,zookeeper}, {zookeeper_list,[{"127.0.0.1",2181}]}, {application,<<"testdubboerl">>}, {registry,true}, diff --git a/include/dubbo.hrl b/include/dubbo.hrl index f2d4048..282e0da 100644 --- a/include/dubbo.hrl +++ b/include/dubbo.hrl @@ -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}). diff --git a/src/dubbo_common_fun.erl b/src/dubbo_common_fun.erl index 6717fac..5f38fbd 100644 --- a/src/dubbo_common_fun.erl +++ b/src/dubbo_common_fun.erl @@ -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 ]), diff --git a/src/dubbo_directory.erl b/src/dubbo_directory.erl index 07a6dff..c667f29 100644 --- a/src/dubbo_directory.erl +++ b/src/dubbo_directory.erl @@ -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. diff --git a/src/dubbo_loadbalance_random.erl b/src/dubbo_loadbalance_random.erl new file mode 100644 index 0000000..21d4f61 --- /dev/null +++ b/src/dubbo_loadbalance_random.erl @@ -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([]). diff --git a/src/dubbo_protocol_registry.erl b/src/dubbo_protocol_registry.erl index 8277c7e..b2fde17 100644 --- a/src/dubbo_protocol_registry.erl +++ b/src/dubbo_protocol_registry.erl @@ -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. \ No newline at end of file diff --git a/src/dubbo_provider_consumer_reg_table.erl b/src/dubbo_provider_consumer_reg_table.erl index d47260d..1c193b8 100644 --- a/src/dubbo_provider_consumer_reg_table.erl +++ b/src/dubbo_provider_consumer_reg_table.erl @@ -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 diff --git a/src/dubbo_reference_config.erl b/src/dubbo_reference_config.erl index 6c58a50..7ab7f86 100644 --- a/src/dubbo_reference_config.erl +++ b/src/dubbo_reference_config.erl @@ -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% [...] +%% 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%26 [...] - 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">>. \ No newline at end of file + atom_to_binary(application:get_env(dubboerl,registry,zookeeper)). \ No newline at end of file diff --git a/src/dubboerl.erl b/src/dubboerl.erl index 0a5f5bc..03cc8a0 100644 --- a/src/dubboerl.erl +++ b/src/dubboerl.erl @@ -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.
