Skip to content
Permalink
Browse files
mod defined for plugin design
  • Loading branch information
DLive committed Jun 10, 2019
1 parent 03010f1 commit 07e743aa315bd5a08f9694337f0ebf8f4f3da507
Showing 23 changed files with 1,183 additions and 92 deletions.
@@ -23,7 +23,7 @@
{protocol,hessian},
{port,20881},
{consumer,[
{<<"org.apache.dubbo.erlang.sample.service.facade.UserOperator">>,[]}
% {<<"org.apache.dubbo.erlang.sample.service.facade.UserOperator">>,[]}
]},
{provider,[
{dubbo_service_user_impl,userOperator,<<"org.apache.dubbo.erlang.sample.service.facade.UserOperator">>,[]}
@@ -18,4 +18,7 @@

-define(PROVIDER_WORKER,provider_worker).

-define(TRAFFIC_CONTROL,traffic_control).
-define(TRAFFIC_CONTROL,traffic_control).


-record(dubbo_url,{scheme,user_info,host,port,path,parameters,fragment}).
@@ -21,9 +21,10 @@

{deps, [
{erlzk, ".*", {git, "https://github.com/huaban/erlzk.git", {tag, "v0.6.2"}}},
{ranch, ".*", {git, "https://github.com/ninenines/ranch.git", {tag, "1.4.0"}}},
{ranch, ".*", {git, "https://github.com/ninenines/ranch.git", {tag, "1.4.0"}}},
{poolboy, ".*", {git, "https://github.com/devinus/poolboy.git", {tag, "1.5.1"}}},
{jiffy, "0.15.1"}
{jiffy, "0.15.1"},
{hooks,{git,"https://github.com/benoitc/hooks.git",{tag,"2.1.0"}}}
]}.


@@ -3,6 +3,10 @@
{git,"https://github.com/huaban/erlzk.git",
{ref,"aa7190ee2343ac1341cea3edc9b9eea36c591708"}},
0},
{<<"hooks">>,
{git,"https://github.com/benoitc/hooks.git",
{ref,"d4872554a27c0ee9c2166d18000f725f8c3dc8a8"}},
0},
{<<"jiffy">>,{pkg,<<"jiffy">>,<<"0.15.1">>},0},
{<<"poolboy">>,
{git,"https://github.com/devinus/poolboy.git",
@@ -10,8 +10,8 @@

<dubbo:consumer check="false" timeout="300000" id="dubboConsumerConfig" retries="0"/>

<bean id="userService" class="org.apache.dubbo.erlang.sample.service.impl.UserOperatorImpl"/>
<dubbo:service interface="org.apache.dubbo.erlang.sample.service.facade.UserOperator" ref="userService"/>
<!-- <bean id="userService" class="org.apache.dubbo.erlang.sample.service.impl.UserOperatorImpl"/>-->
<!-- <dubbo:service interface="org.apache.dubbo.erlang.sample.service.facade.UserOperator" ref="userService"/>-->

<!-- <dubbo:reference id="userInterface" interface="UserOperator" retries="0" />-->
<dubbo:reference id="userInterface" interface="org.apache.dubbo.erlang.sample.service.facade.UserOperator" retries="0" />
</beans>
@@ -67,7 +67,7 @@ getUserInfo(Arg0, RequestOption)->
]
},
Request = dubbo_adapter:reference(Data),
dubbo_invoker:invoke_request(?CURRENT_CLASS_NAME,Request,RequestOption).
dubbo_invoker_old:invoke_request(?CURRENT_CLASS_NAME,Request,RequestOption).


-spec genUserId()->
@@ -96,7 +96,7 @@ genUserId( RequestOption)->
]
},
Request = dubbo_adapter:reference(Data),
dubbo_invoker:invoke_request(?CURRENT_CLASS_NAME,Request,RequestOption).
dubbo_invoker_old:invoke_request(?CURRENT_CLASS_NAME,Request,RequestOption).


-spec queryUserInfo(Arg0::#userInfoRequest{})->
@@ -127,7 +127,7 @@ queryUserInfo(Arg0, RequestOption)->
]
},
Request = dubbo_adapter:reference(Data),
dubbo_invoker:invoke_request(?CURRENT_CLASS_NAME,Request,RequestOption).
dubbo_invoker_old:invoke_request(?CURRENT_CLASS_NAME,Request,RequestOption).


-spec queryUserList(Arg0::list())->
@@ -158,5 +158,5 @@ queryUserList(Arg0, RequestOption)->
]
},
Request = dubbo_adapter:reference(Data),
dubbo_invoker:invoke_request(?CURRENT_CLASS_NAME,Request,RequestOption).
dubbo_invoker_old:invoke_request(?CURRENT_CLASS_NAME,Request,RequestOption).

@@ -0,0 +1,21 @@
%%------------------------------------------------------------------------------
%% 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_cluster).
-author("dlive").

%% API
-export([]).
@@ -0,0 +1,21 @@
%%------------------------------------------------------------------------------
%% 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_cluster_failfast).
-author("dlive").

%% API
-export([]).
@@ -16,8 +16,9 @@
%%------------------------------------------------------------------------------
-module(dubbo_common_fun).

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

local_ip_v4() ->
{ok, Addrs} = inet:getifaddrs(),
@@ -29,3 +30,60 @@ local_ip_v4() ->
local_ip_v4_str() ->
{V1, V2, V3, V4} = local_ip_v4(),
list_to_binary(io_lib:format("~p.~p.~p.~p", [V1, V2, V3, V4])).


-spec(parse_url(Url :: binary()|list()) -> {ok, map()}).
parse_url(Url) when is_binary(Url) ->
parse_url(binary_to_list(Url));
parse_url(Url) ->
case http_uri:parse(Url, []) of
{ok, {Scheme, _UserInfo, Host, Port, _Path, Query}} ->
QueryStr = case lists:prefix("?", Query) of
true ->
[_ | Query2] = Query,
Query2;
false ->
Query
end,
QueryListTmp = string:tokens(QueryStr, "&"),
Parameters = parse_url_parameter(QueryListTmp, #{}),
Result = #dubbo_url{scheme = Scheme, host = Host, port = Port, parameters = Parameters},
{ok, Result};
{error, R1} ->
{error, R1}
end.


parse_url_parameter([], Parameters) ->
Parameters;
parse_url_parameter([Item | Rest], Parameters) ->
case string:tokens(Item, "=") of
KeyPair when length(KeyPair) == 2 ->
[Key, Value] = KeyPair,
parse_url_parameter(Rest, maps:put(Key, Value, Parameters));
KeyPair2 ->
logger:error("parse parameter error, keypair ~p", [KeyPair2]),
parse_url_parameter(Rest, Parameters)
end.


map_to_url(UrlInfo) ->
ParameterStr =
case UrlInfo#dubbo_url.parameters of
undefined ->
"";
Parameter ->
KeyValues = maps:to_list(Parameter),
KeyValues2 = [io_lib:format("~s=~s", [Key, http_uri:encode(Value)]) || {Key, Value} <= KeyValues],
ParameterStr1 = string:join(KeyValues2, "&"),
ParameterStr2 = ["?" | ParameterStr1],
list_to_binary(ParameterStr2)
end,
Value = io_lib:format(<<"~s://~s/~s?~s">>,
[
UrlInfo#dubbo_url.scheme,
UrlInfo#dubbo_url.host,
UrlInfo#dubbo_url.path,
ParameterStr
]),
list_to_binary(Value).
@@ -0,0 +1,173 @@
%%------------------------------------------------------------------------------
%% 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_directory).

-behaviour(gen_server).

-export([subscribe/2,notify/1]).
%% API
-export([start_link/0]).

%% gen_server callbacks
-export([init/1,
handle_call/3,
handle_cast/2,
handle_info/2,
terminate/2,
code_change/3]).

-define(SERVER, ?MODULE).

-record(state, {}).

%%%===================================================================
%%% API
%%%===================================================================

%%--------------------------------------------------------------------
%% @doc
%% Starts the server
%%
%% @end
%%--------------------------------------------------------------------
-spec(start_link() ->
{ok, Pid :: pid()} | ignore | {error, Reason :: term()}).
start_link() ->
gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).

%%%===================================================================
%%% gen_server callbacks
%%%===================================================================

%%--------------------------------------------------------------------
%% @private
%% @doc
%% Initializes the server
%%
%% @spec init(Args) -> {ok, State} |
%% {ok, State, Timeout} |
%% ignore |
%% {stop, Reason}
%% @end
%%--------------------------------------------------------------------
-spec(init(Args :: term()) ->
{ok, State :: #state{}} | {ok, State :: #state{}, timeout() | hibernate} |
{stop, Reason :: term()} | ignore).
init([]) ->
{ok, #state{}}.

subscribe(RegistryName,SubcribeUrl)->
try gen_server:call(?SERVER,{subscribe,RegistryName,SubcribeUrl},5000) of
ok->
ok
catch
Error:Reason->
%% todo improve erro type
{error,Reason}
end.

notify(UrlList)->
dubbo_consumer_pool:start_consumer(Interface, UrlList),
ok.

%%--------------------------------------------------------------------
%% @private
%% @doc
%% Handling call messages
%%
%% @end
%%--------------------------------------------------------------------
-spec(handle_call(Request :: term(), From :: {pid(), Tag :: term()},
State :: #state{}) ->
{reply, Reply :: term(), NewState :: #state{}} |
{reply, Reply :: term(), NewState :: #state{}, timeout() | hibernate} |
{noreply, NewState :: #state{}} |
{noreply, NewState :: #state{}, timeout() | hibernate} |
{stop, Reason :: term(), Reply :: term(), NewState :: #state{}} |
{stop, Reason :: term(), NewState :: #state{}}).
handle_call({subscribe,RegistryName,SubcribeUrl}, _From, State) ->
NotifyFun= fun dubbo_directory:notify/1,
apply(RegistryName,subscribe,[SubcribeUrl,NotifyFun]),
{reply, ok, State};
handle_call(_Request, _From, State) ->
{reply, ok, State}.

%%--------------------------------------------------------------------
%% @private
%% @doc
%% Handling cast messages
%%
%% @end
%%--------------------------------------------------------------------
-spec(handle_cast(Request :: term(), State :: #state{}) ->
{noreply, NewState :: #state{}} |
{noreply, NewState :: #state{}, timeout() | hibernate} |
{stop, Reason :: term(), NewState :: #state{}}).
handle_cast(_Request, State) ->
{noreply, State}.

%%--------------------------------------------------------------------
%% @private
%% @doc
%% Handling all non call/cast messages
%%
%% @spec handle_info(Info, State) -> {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, State}
%% @end
%%--------------------------------------------------------------------
-spec(handle_info(Info :: timeout() | term(), State :: #state{}) ->
{noreply, NewState :: #state{}} |
{noreply, NewState :: #state{}, timeout() | hibernate} |
{stop, Reason :: term(), NewState :: #state{}}).
handle_info(_Info, State) ->
{noreply, State}.

%%--------------------------------------------------------------------
%% @private
%% @doc
%% This function is called by a gen_server when it is about to
%% terminate. It should be the opposite of Module:init/1 and do any
%% necessary cleaning up. When it returns, the gen_server terminates
%% with Reason. The return value is ignored.
%%
%% @spec terminate(Reason, State) -> void()
%% @end
%%--------------------------------------------------------------------
-spec(terminate(Reason :: (normal | shutdown | {shutdown, term()} | term()),
State :: #state{}) -> term()).
terminate(_Reason, _State) ->
ok.

%%--------------------------------------------------------------------
%% @private
%% @doc
%% Convert process state when code is changed
%%
%% @spec code_change(OldVsn, State, Extra) -> {ok, NewState}
%% @end
%%--------------------------------------------------------------------
-spec(code_change(OldVsn :: term() | {down, term()}, State :: #state{},
Extra :: term()) ->
{ok, NewState :: #state{}} | {error, Reason :: term()}).
code_change(_OldVsn, State, _Extra) ->
{ok, State}.

%%%===================================================================
%%% Internal functions
%%%===================================================================

0 comments on commit 07e743a

Please sign in to comment.