tag | c0caf5cb6846e601c0fa15fcd91f50c7ffba2286 | |
---|---|---|
tagger | cicode <cicode@ofcard.com> | Sun Apr 12 21:27:15 2015 +0800 |
object | 709fb2c847848f5f0ad4c0db3967130263348ec9 |
[maven-release-plugin] copy for tag dubbo-rpc-jsonrpc-1.0.0-RELEASE
commit | 709fb2c847848f5f0ad4c0db3967130263348ec9 | [log] [tgz] |
---|---|---|
author | cicode <cicode@ofcard.com> | Sun Apr 12 21:27:12 2015 +0800 |
committer | cicode <cicode@ofcard.com> | Sun Apr 12 21:27:12 2015 +0800 |
tree | d02324771af31db9eaa0e084712348d822ceb093 | |
parent | bbadca68462d373bc8d6cc34aef972ce0b27c3ec [diff] |
[maven-release-plugin] prepare release dubbo-rpc-jsonrpc-1.0.0-RELEASE
在互联网快速迭代的大潮下,越来越多的公司选择nodejs、django、rails这样的快速脚本框架来开发web端应用 而后端的服务用Java又是最合适的,这就产生了大量的跨语言的调用需求。
而http、json是天然合适作为跨语言的标准,各种语言都有成熟的类库
虽然Dubbo的异步长连接协议效率很高,但是在脚本语言中,这点效率的损失并不重要。
Dubbox 在RESTful接口上已经做出了尝试,但是REST架构和dubbo原有的RPC架构是有区别的,
区别在于REST架构需要有资源(Resources)的定义, 需要用到HTTP协议的基本操作GET、POST、PUT、DELETE对资源进行操作。
Dubbox需要重新定义接口的属性,这对原有的Dubbo接口迁移是一个较大的负担。
相比之下,RESTful更合适互联网系统之间的调用,而RPC更合适一个系统内的调用,
所以我们使用了和Dubbo理念较为一致的JsonRPC
<dependency> <groupId>com.ofpay</groupId> <artifactId>dubbo-rpc-jsonrpc</artifactId> <version>1.0.0-SNAPSHOT</version> </dependency>
Define jsonrpc protocol:
<dubbo:protocol name="jsonrpc" port="8080" server="jetty" />
Set default protocol:
<dubbo:provider protocol="jsonrpc" />
Set service protocol:
<dubbo:service protocol="jsonrpc" />
Multi port:
<dubbo:protocol id="jsonrpc1" name="jsonrpc" port="8080" /> <dubbo:protocol id="jsonrpc2" name="jsonrpc" port="8081" />
Multi protocol:
<dubbo:protocol name="dubbo" port="20880" /> <dubbo:protocol name="jsonrpc" port="8080" />
<dubbo:service id="helloService" interface="com.alibaba.hello.api.HelloService" version="1.0.0" protocol="dubbo,jsonrpc" />
Jetty Server: (default)
<dubbo:protocol ... server="jetty" />
Maven:
<dependency> <groupId>org.mortbay.jetty</groupId> <artifactId>jetty</artifactId> <version>6.1.26</version> </dependency>
Servlet Bridge Server: (recommend)
<dubbo:protocol ... server="servlet" />
web.xml:
<servlet> <servlet-name>dubbo</servlet-name> <servlet-class>com.alibaba.dubbo.remoting.http.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dubbo</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping>
注意,如果使用servlet派发请求:
协议的端口<dubbo:protocol port=“8080” />必须与servlet容器的端口相同, 协议的上下文路径<dubbo:protocol contextpath=“foo” />必须与servlet应用的上下文路径相同。
JAVA API
public interface PhoneNoCheckProvider { /** * 校验号码是否受限 * @param operators 运营商 * @param no 号码 * @param userid 用户编号 * */ boolean isPhoneNoLimit(Operators operators, String no, String userid); }
Client
curl -i -H 'content-type: application/json' -X POST -d '{"jsonrpc": "2.0", "method": "isPhoneNoLimit", "params": [ "MOBILE", "130000", "A001"], "id": 1 }' 'http://127.0.0.1:18080/com.ofpay.api.PhoneNoCheckProvider'
Python Client Example
import httplib import json __author__ = 'caozupeng' def raw_client(app_params): headers = {"Content-type": "application/json-rpc", "Accept": "text/json"} h1 = httplib.HTTPConnection('172.19.32.135', port=18080) h1.request("POST", '/com.ofpay.ofdc.api.phone.PhoneNoCheckProvider', json.dumps(app_params), headers) response = h1.getresponse() return response.read() if __name__ == '__main__': app_params = { "jsonrpc": "2.0", "method": "isPhoneNoLimit", "params": ["MOBILE", "130000", "A001"], "id": 1 } print json.loads(raw_client(app_params), encoding='utf-8')
开发Python、Ruby、NodeJs的客户端,可以支持客户端的负载均衡。