dubbox rest服务
1.xml配置: web.xml
定义监听的contextpath,见rest协议定义
<servlet>
<servlet-name>dispatcher</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>dispatcher</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
2.xml配置:dubbo-demo-provider.xml
##rest协议定义:
##port:和服务器端口保持一致
##contextpath:和web服务器web.xml定义保持一致
##server:默认servlet,在没有在web服务器中运行的时候,可以写tomcat等由应用启动一个web服务器
<dubbo:protocol name="rest" port="8888" contextpath="services" server="servlet"/> ##远程服务定义
##interface : 服务接口
##ref:服务实际实现的service,引用其它定义的spring bean
##protocol:服务的协议模式
<dubbo:service interface="com.alibaba.dubbo.demo.user.facade.UserRestService" ref="userRestService" protocol="rest" validation="true"/> ##实际服务实现
<bean id="userRestService" class="com.alibaba.dubbo.demo.user.facade.UserRestServiceImpl">
<property name="userService" ref="userService"/>
</bean>
3.java代码:UserRestServiceImpl
class定义:
##@Path:整体服务的访问路径
本样例http访问就应该是: http:/IP:PORT/应用名称/contextpath/users
contextpath:web.xml和resf服务中定义的
##可以接受的数据格式。json和简单xml
@Path("users")
@Consumes({MediaType.APPLICATION_JSON, MediaType.TEXT_XML})
@Produces({ContentType.APPLICATION_JSON_UTF_8, ContentType.TEXT_XML_UTF_8})
public class UserRestServiceImpl implements UserRestService {
Method定义:
##@POST:http接受的方法,可以GET,POST,delete等
本样例http访问就应该是:
http:/IP:PORT/应用名称/contextpath/users/register.json
http:/IP:PORT/应用名称/contextpath/users/register.xml
##@Path:服务路径
@POST
@Path("register")
public RegistrationResult registerUser(User user) {
return new RegistrationResult(userService.registerUser(user));
}
客户端:
见RestClient.java
js调用
<!DOCTYPE html>
<html>
<head>
<script src="jquery-1.10.2.min.js">
</script>
<script>
$(document).ready(function(){
$("#btn1").click(function(){
$('#test').load('http://127.0.0.1:8888/services/u/1.json');
})
})
</script>
</head> <body> <h3 id="test">请点击下面的按钮,通过 jQuery AJAX 改变这段文本。</h3>
<button id="btn1" type="button">获得外部的内容</button> </body>
</html>
参考:http://www.ganps.net/dubboxie-yi/
http://dangdangdotcom.github.io/dubbox/rest.html
dubbox rest服务的更多相关文章
- dubbox REST服务使用fastjson替换jackson
上一节讲解了resteasy如何使用fastjson来替换默认的jackson,虽然dubbox内部采用的就是resteasy,但是大多数情况下,dubbox服务是一个独立的app,并不需要以war包 ...
- Dubbox服务demo
一.安装虚拟机,安装所需要的jdk.zookeeper并启动zookeeper,虚拟机的ip+zookeeper默认端口号2181 二.编写Service服务方 1.创建Maven项目 2.编写接口 ...
- dubbox微服务实例及引发的“血案”
Dubbo 是阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的 RPC 实现服务的输出和输入功能,可以和 Spring框架无缝集成. 主要核心部件: Remoting: 网络通信框架 ...
- dubbox 编译 和 测试
因为 dubbox 并没有发布到maven中央仓库仓库中,所以需要我们自己到官网下载,自己编译,install 到本地. 1. 首先安装git客户端工具 TortoiseGit, 然后使用它将 dub ...
- java框架之SpringCloud(1)-微服务及SpringCloud介绍
微服务概述 是什么 业界大牛 Martin Fowler 这样描述微服务: 参考[微服务(Microservices)-微服务原作者Martin Flower博客翻译]. 下面是关于上述博客中的部分重 ...
- dubbox消费者启动成功,却无法连接注册中心
使用dubbox作为服务提供端很好实现,因为git的说明和网上有很多的例子可供参考,但是消费端都一笔带过,简单得很,初学者往往以为只要配置如下3样东西就够了: <?xml version=&qu ...
- web常用服务架构
架构风格就是一种项目的设计模式.常见的架构风格有基于客户端与服务端的.基于组件模型的(EJB).分层架构(MVC).面向服务架构(SOA)等. 一.单体架构 单体架构也称为单体系统或单体应用,就是一种 ...
- dubox首次调用消费者执行两次问题
dubbox+zookeeper服务开启后 Dubbo的超时重试机制为服务容错.服务稳定提供了比较好的框架支持,但是在一些比较特殊的网络环境下(网络传输慢,并发多)可能由于服务响应慢,Dubbo自身的 ...
- spring cloud 学习(11) - 用fastson替换jackson及用gb2312码输出
前几天遇到一个需求,因为要兼容旧项目的编码格式,需要spring-cloud的rest接口,输出gb2312编码,本以为是一个很容易的事情,比如下面这样: @RequestMapping(method ...
随机推荐
- 奇异值分解 SVD
一基本知识 A是一个m*n的矩阵,那么A的SVD分解为\(A_{mn} = U_{mm}\Sigma _{mn}V^T_{nn}\),其中\(U^TU = I\),\(V^TV = I\),UV的列向 ...
- c#委托----我的一点笔记
public partial class ucBloodLabs { public delegate void ShowBloodEvent(); public ShowBloodEvent Show ...
- JS for循环
正常的for循环代码: var arr = ['Apple', 'Google', 'Microsoft']; var i, x; for (i=0; i<arr.length; i++) { ...
- install scrapy-redis on centos
wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpmrpm -ivh epel-release- ...
- Steamroller
FCC题目:对嵌套的数组进行扁平化处理.你必须考虑到不同层级的嵌套. 示例: steamroller([[["a"]], [["b"]]]) 应该返回 [&qu ...
- JS中使用MD5加密
下载 MD5 使用MD5加密的方法:下载md5.js文件,在网页中引用该文件: < script type="text/javascript" src="md5.j ...
- 在不损坏C盘的情况下为C盘扩容,适用于Win
2016年12月29日14:29:27 参考原文:http://jingyan.baidu.com/article/90808022a6c6b7fd91c80fc8.html 在不损坏磁盘的情况下给某 ...
- iframe用js设定自定义高度
JS代码 function SetWinHeight(obj){ var win=obj; if (document.getElementById){ if (win && !wind ...
- js loaclstorage和sessionstorage
这里需要注意的是这两种储存方式只能以字符串的形式来存取 html5中的Web Storage包括了两种存储方式:sessionStorage和localStorage.sessionStorage用于 ...
- js
1.判断是否为空 if (typeof(a)!="undefined") 2. pop()移除数组最后一个元素 var arr = []; $('#tableid tr').f ...