Skip to content
Permalink
Browse files
增加dubbo client的实现
  • Loading branch information
JoeCao committed Apr 8, 2015
1 parent 2dd3531 commit b902a898876a9c140a217bed1c71f70022761029
Showing 5 changed files with 68 additions and 1 deletion.
@@ -1 +1,5 @@
__author__ = 'caozupeng'

from rpc import (
DubboClient,
)
@@ -1,4 +1,4 @@
# encoding=utf-8
# coding=utf-8
import urllib
from urlparse import urlparse, parse_qsl

@@ -1,5 +1,7 @@
import httplib
import json
import random
from pyjsonrpc import HttpClient

from dubbo_client.registry import service_provides, add_provider_listener

@@ -21,6 +23,43 @@ def raw_client(service_interface, app_params):
return None, 'can not find the provide of {0}'.format(service_interface)


class DubboClient(object):
clients = []

class _Method(object):

def __init__(self, client_instance, method):
self.client_instance = client_instance
self.method = method

def __call__(self, *args, **kwargs):
return self.client_instance.call(self.method, *args, **kwargs)

def __init__(self, interface):
add_provider_listener(interface)
provides = service_provides.get(interface, ())
if len(provides) > 0:
for location, provide in provides.items():
self.clients.append(HttpClient(url="http://{0}{1}".format(location, provide.path)))

def call(self, method, *args, **kwargs):
client = random.choice(self.clients)
return client.call(method, *args, **kwargs)

def __call__(self, method, *args, **kwargs):
"""
Redirects the direct call to *self.call*
"""
return self.call(method, *args, **kwargs)

def __getattr__(self, method):
"""
Allows the usage of attributes as *method* names.
"""

return self._Method(client_instance=self, method=method)


if __name__ == '__main__':
app_params = {
"jsonrpc": "2.0",
@@ -0,0 +1,11 @@
from dubbo_client.registry import zk

__author__ = 'caozupeng'

if __name__ == '__main__':
if zk.exists("/dubbo"):
# Print the version of a node and its data
children = zk.get_children("/dubbo")
print "There are {0} children".format(len(children))
for node in children:
print node
@@ -0,0 +1,13 @@
# coding=utf-8
from dubbo_client import DubboClient

__author__ = 'caozupeng'

if __name__ == '__main__':
service_interface = 'com.ofpay.demo.api.UserProvider'
dubbo_client = DubboClient(service_interface)
print dubbo_client.getUser('A003')
print dubbo_client.queryUser(
{u'age': 18, u'time': 1428463514153, u'sex': u'MAN', u'id': u'A003', u'name': u'zhangsan'})
print dubbo_client.queryAll()
print dubbo_client.isLimit('MAN', 'Joe')

0 comments on commit b902a89

Please sign in to comment.