Skip to content
Permalink
Browse files
first upload
  • Loading branch information
JoeCao committed Apr 7, 2015
0 parents commit cb520b0947faf8be2678bc0f1cb7f1a8656beb03
Showing 7 changed files with 133 additions and 0 deletions.
@@ -0,0 +1,8 @@
### Python Dubbo Client

##Python调用Dubbo接口的jsonrpc协议
请使用dubbo-rpc-jsonrpc

##在客户端实现负载均衡,服务发现
通过注册中心的zookeeper,获取服务的注册信息
然后通过代理实现负载均衡算法,调用服务端
@@ -0,0 +1 @@
__author__ = 'caozupeng'
@@ -0,0 +1 @@
__author__ = 'caozupeng'
@@ -0,0 +1,85 @@
# encoding=utf-8
import urllib
from urlparse import urlparse, parse_qsl

from kazoo.protocol.states import KazooState


__author__ = 'caozupeng'
from kazoo.client import KazooClient
import logging

logging.basicConfig()
zk = KazooClient(hosts='172.19.65.33:2181', read_only=True)
service_provides = {}


def state_listener(state):
if state == KazooState.LOST:
# Register somewhere that the session was lost
print 'session lost'
elif state == KazooState.SUSPENDED:
# Handle being disconnected from Zookeeper
print 'disconnect from zookeeper'
else:
# Handle being connected/reconnected to Zookeeper
print 'connected'


zk.add_listener(state_listener)


class JsonProvide(object):
protocol = 'jsonrpc'
location = ''
path = ''
ip = '127.0.0.1'
port = '9090'

def __init__(self, url):
result = urlparse(url)
self.protocol = result[0]
self.location = result[1]
self.path = result[2]
if self.location.find(':') > -1:
self.ip, self.port = result[1].split(':')
params = parse_qsl(result[4])
for key, value in params:
# url has a default.timeout property, but it can not add in python object
# so keep the last one
pos = key.find('.')
if pos > -1:
key = key[pos + 1:]
print key
self.__dict__[key] = value


def node_listener(event):
print event


def add_provider_listener(provide_name):
children = zk.get_children('{0}/{1}/{2}'.format('dubbo', provide_name, 'providers'), watch=node_listener)
for child_node in children:
url = urllib.unquote(child_node).decode('utf8')
if url.startswith('jsonrpc'):
provide = JsonProvide(url)
service_key = service_provides.get(provide.interface)
if service_key:
service_key[provide.location] = provide
else:
service_provides[provide.interface] = {provide.location: provide}


zk.start()

if __name__ == '__main__':
zk.start()
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
add_provider_listener('com.ofpay.demo.api.UserProvider')

@@ -0,0 +1,35 @@
import httplib
import json

from dubbo_client.registry import service_provides, add_provider_listener


__author__ = 'caozupeng'


def raw_client(service_interface, app_params):
headers = {"Content-type": "application/json-rpc",
"Accept": "text/json"}
provides = service_provides.get(service_interface, ())
if len(provides) > 0:
location, first = provides.items().pop()
h1 = httplib.HTTPConnection(first.ip, port=int(first.port))
h1.request("POST", first.path, json.dumps(app_params), headers)
response = h1.getresponse()
return response.read(), None
else:
return None, 'can not find the provide of {0}'.format(service_interface)


if __name__ == '__main__':
app_params = {
"jsonrpc": "2.0",
"method": "getUser",
"params": ["A001"],
"id": 1
}
service_interface = 'com.ofpay.demo.api.UserProvider'
add_provider_listener(service_interface)
ret, error = raw_client(service_interface, app_params)
if not error:
print json.loads(ret, encoding='utf-8')
@@ -0,0 +1,2 @@
#encoding=utf-8
__author__ = 'caozupeng'
@@ -0,0 +1 @@
__author__ = 'caozupeng'

0 comments on commit cb520b0

Please sign in to comment.