请问,dubbo rpc rest maven实现rest的maven配置是什么

&&&& 本文使用官方项目示例,基于dubbox发布restful服务。jdk,eclipse、maven神马环境的就不讲了,自行baidu之。dubbox下载、编译下载地址:/dangdangdotcom/dubbox下载方式可以用git下载,也可以打包下载。下载之后,用maven导入(eclipse-&import-&Maven)。dubbo项目结构:650) this.width=650;" src="/d/file/kafa//31ddbdabcbce61.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="1240" />使用maven打包,版本2.8.4。zookeeper安装本文使用的是单节点的注册中心,在实际项目中需要考虑高可用,需要搭建集群注册中心,集群环境搭建在以后的文章中会讲到。下载https://zookeeper.apache.org/这里用的是zookeeper-3.5.1-alpha版本,下载之后,解压到本地。配置进入conf目录,修改dataDir(数据)和dataLogDir(日志)文件目录到你本地的目录:dataDir=E:\\zookeeper-3.5.1-alpha\\data
dataLogDir=E:\\zookeeper-3.5.1-alpha\\logs其它不用改动,需要注意的是clientPort=2181。这个就是dubbo连接注册中心需要使用的端口。启动进入zookeeper\bin目录,运行zkServer.cmd:650) this.width=650;" src="/d/file/kafa//5b8838ccd6e63c9a3d94722bccd6095e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="1240" />运行zookeeper出现以下信息,启动成功:&10:53:29,604&[myid:]&-&INFO&&[main:NIOServerCnxnFactory@673]&-&
Configuring&NIO&connection&handler&with&10s&sessionless&connection&timeout,&
2&selector&thread(s),&16&worker&threads,&and&64&kB&direct&buffers.
&10:53:29,609&[myid:]&-&INFO&&[main:NIOServerCnxnFactory@686]&-&
binding&to&port&0.0.0.0/0.0.0.0:2181dubbo示例demo导入dubbo官方自带的示例,包括三个工程,api\product(服务提供者)和consumer(服务消费者)。导入后的项目结构:650) this.width=650;" src="/d/file/kafa//1d3dffddcbad840caa01d70.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="1240" title="" style="width:485height:564" width="485" vspace="0" hspace="0" height="564" border="0" />dubbo示例项目结构demo解剖dubbo-demo-api:定义的服务接口dubbo-demo-provider:服务发布dubbo-demo-consumer:服务消费配置文件&!--连接本地注册中心--&&dubbo:registry&address="zookeeper://127.0.0.1:2181"/&服务接口public&interface&UserService&{&&&&User&getUser(Long&id);
&&&&Long&registerUser(User&user);
}定义了一个用户接口,两个方法。服务发布&!--对接口的实现--&
&bean&id="userService"&class="com.alibaba.dubbo.demo.user.UserServiceImpl"&/&
&!--dubbo服务--&
&dubbo:service&interface="com.alibaba.dubbo.demo.user.UserService"&ref="userService"&&protocol="dubbo"&group="xmlConfig"/&
&!--rest服务实现--&
&bean&id="userRestService"&class="com.alibaba.dubbo.demo.user.facade.UserRestServiceImpl"&
&&&&&property&name="userService"&ref="userService"/&&/bean&
&!--dubbo发布的rest服务--&
&dubbo:service&interface="com.alibaba.dubbo.demo.user.facade.UserRestService"&ref="userRestService"&&protocol="rest"&validation="true"/&UserRestServiceImpl.java调用dubbo服务,实现rest服务的发布:public&class&UserServiceImpl&implements&UserService&{&&&&
private&final&AtomicLong&idGen&=&new&AtomicLong();&&&&
public&User&getUser(Long&id)&{&&&&&&&&
&&&&return&new&User(id,&"username"&+&id);
&&&&}&&&&public&Long&registerUser(User&user)&{//&&&&&&&&System.out.println("Username&is&"&+&user.getName());
&&&&&&&&return&idGen.incrementAndGet();
&&&&}UserServiceImpl.java对接口的实现:@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&{&&&&//使用dubbo服务
&&&&private&UserService&userS&&&&public&void&setUserService(UserService&userService)&{&&&&&&&&this.userService&=&userS
&&&&}&&&&@GET
&&&&@Path("{id&:&\\d+}")
&&&&public&User&getUser(@PathParam("id")&Long&id/*,&@Context&HttpServletRequest&request*/)&{&&&&//System.out.println("Client&address&from&@Context&injection:&"&+&(request&!=&null&?&request.getRemoteAddr()&:&""));
&&&&//System.out.println("Client&address&from&RpcContext:&"&+&RpcContext.getContext().getRemoteAddressString());
&&&&&&&&if&(RpcContext.getContext().getRequest(HttpServletRequest.class)&!=&null)&{
&&&&&&&&&&&&System.out.println("Client&IP&address&from&RpcContext:&"&+&RpcContext.getContext().getRequest(HttpServletRequest.class).getRemoteAddr());
&&&&&&&&}&&&&&&&&if&(RpcContext.getContext().getResponse(HttpServletResponse.class)&!=&null)&{
&&&&&&&&&&&&System.out.println("Response&object&from&RpcContext:&"&+&RpcContext.getContext().getResponse(HttpServletResponse.class));
&&&&&&&&}&&&&&&&&return&userService.getUser(id);
}服务消费启动服务提供者使用测试用例DemoProvider.java启动,控制台出现以下信息,说明服务启动成功:[20/05/17&11:32:02:002&CST]&main&&INFO&container.Main:&&[DUBBO]&Dubbo&SpringContainer&started!,&dubbo&version:&2.0.0,&current&host:&127.0.0.1[&11:32:02]&Dubbo&service&server&started!查看服务管理界面(启动dobbo-admin工程):650) this.width=650;" src="/d/file/kafa//c9fafc632fdd7c0c6b672f21bc611810.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="1240" />dubbo管理界面可以看到发布的服务。调用发布的restful调用地址:http://localhost:8888/services/users/5.json返回数据:{"id":5,"username":"username5"}本文首发于/p/fcc
本文出处:,转载请注明出处(春哥QQ:
微信号:wocgtblog)SpringMVC+mybatis+shiro+Restful+dubbo+maven分布式框架设计_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
SpringMVC+mybatis+shiro+Restful+dubbo+maven分布式框架设计
&&j2ee 框架设计资料
阅读已结束,下载文档到电脑
想免费下载更多文档?
定制HR最喜欢的简历
下载文档到电脑,方便使用
还剩5页未读,继续阅读
定制HR最喜欢的简历
你可能喜欢Dubbo与Spring的配合使用 - Event driven - ITeye博客
博客分类:
Dubbo是一个服务治理框架,对分布式服务的管理和治理、监控有较好的实现。
本案举例说明Dubbo与Spring的配合使用:
Maven引入dubbo的配置
&dependency&
&groupId&com.alibaba&/groupId&
&artifactId&dubbo&/artifactId&
&version&2.5.3&/version&
&/dependency&
增加Spring的Dubbo provider配置文件 spring-dubbo-provider.xml:
&?xml version="1.0" encoding="UTF-8"?&
&beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
/schema/dubbo
/schema/dubbo/dubbo.xsd
&!-- 提供方应用名称信息,这个相当于起一个名字,在dubbo管理页面比较清晰是哪个应用暴露出来的 --&
&dubbo:application name="user_provider"/&
&!-- 使用zookeeper注册中心注册服务 --&
&dubbo:registry address="${zookeeper.registry}"/&
&dubbo:protocol port="${dubbo.provider.port}"/&
&dubbo:monitor protocol="registry"/&
&!-- 暴露的服务接口 --&
&dubbo:service interface="com.test.api.UserService"
ref="userService" timeout="5000" retries="2" /&
&bean id="userService" class="com.test.service.UserServiceImpl" /&
3. 接口和实现类代码:
UserService接口:
public interface UserService {
public String hello(String name);
UserServiceImpl类:
public interface UserServiceImpl implement UserService {
public String hello(String name){
return "hello"+
4. 消费方的配置文件
&?xml version="1.0" encoding="UTF-8"?&
&beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
/schema/dubbo
/schema/dubbo/dubbo.xsd
&!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 --&
&dubbo:application name="user_consumer" /&
&dubbo:registry address="${zookeeper.registry}"/&
&dubbo:monitor protocol="registry"/&
&!-- 注册中心配置 --&
&dubbo:reference id="userPointsFacade" interface="com.dmall.points.api.UserPointsFacade" timeout="10000" check="false" /&
public class UserPonitsTest {
public static void main(String[] args) throws InterruptedException{
public void testApp() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"spring/dubbo-consumer.xml"});
context.start();
System.in.read();
为保证服务一直开着,利用输入流的阻塞来模拟
}catch (Exception e){
e.printStackTrace();
6. 使用dubbo-admin包可以对dubbo的服务进行管理和监控。
浏览: 634353 次
来自: 北京
基于 Spring + Dubbo 开发分布式REST服务实战 ...
学习中,赞一个
toknowme 写道现在需要重新intellij IDEAdubbo(1)
上一章,我们详细介绍了如何搭建dubbo服务,本章节我们将在dubbo服务的基础上,利用dubbox,对外提供rest服务。
1. 修改maven 依赖
目前的最新版是&dubbox-2.8.4&,其实groupId和artifactId还是原来的不变,我们升级一下dubbo版本,并且新增dubbox依赖,修改后的pom文件如下
&dependencies&
&!-- spring framework --&
&dependency&
&groupId&org.springframework&/groupId&
&artifactId&spring-context&/artifactId&
&version&3.2.9.RELEASE&/version&
&/dependency&
&!-- dubbo --&
&dependency&
&groupId&com.alibaba&/groupId&
&artifactId&dubbo&/artifactId&
&version&2.8.4&/version&
&/dependency&
&!-- zokeeper register center --&
&dependency&
&groupId&org.apache.zookeeper&/groupId&
&artifactId&zookeeper&/artifactId&
&version&3.3.3&/version&
&/dependency&
&dependency&
&groupId&com.github.sgroschupf&/groupId&
&artifactId&zkclient&/artifactId&
&version&0.1&/version&
&/dependency&
&!-- dubbox自身依赖--&
&dependency&
&groupId&org.jboss.resteasy&/groupId&
&artifactId&resteasy-jaxrs&/artifactId&
&version&3.0.7.Final&/version&
&/dependency&
&dependency&
&groupId&org.jboss.resteasy&/groupId&
&artifactId&resteasy-client&/artifactId&
&version&3.0.7.Final&/version&
&/dependency&
&dependency&
&groupId&javax.validation&/groupId&
&artifactId&validation-api&/artifactId&
&version&1.0.0.GA&/version&
&/dependency&
&dependency&
&groupId&org.apache.tomcat.embed&/groupId&
&artifactId&tomcat-embed-core&/artifactId&
&version&8.0.11&/version&
&/dependency&
&dependency&
&groupId&org.apache.tomcat.embed&/groupId&
&artifactId&tomcat-embed-logging-juli&/artifactId&
&version&8.0.11&/version&
&/dependency&
&/dependencies&
2.修改provider.xml,增加rest协议
&!-- 提供方应用信息,用于计算依赖关系 --&
&dubbo:application name=&dubbo-test-service&
&!-- 使用multicast广播注册中心暴露服务地址 --&
&dubbo:registry address=&zookeeper://127.0.0.1:2181& client=&zkclient& /&
&!-- 用dubbo协议在20880端口暴露服务 --&
&dubbo:protocol name=&dubbo& port=&20880& /&
&!-- 新增rest 协议--&
&dubbo:protocol name=&rest& server=&tomcat& port=&9090&/&
&!-- 声明需要暴露的服务接口 --&
&dubbo:service interface=&net.xiaocun.DemoService& ref=&demoService& /&
&!-- 和本地bean一样实现服务 --&
&bean id=&demoService& class=&net.xiaocun.impl.DemoServiceImpl& /&
3.修改dubbo service实现,提供rest访问路径
@Path(&demo&)
public class DemoServiceImpl implements DemoService {
@Path(&greeting&)
@Produces(&application/ charset=UTF-8&)
public String sayHello(@QueryParam(&name&) String name) {
return &Hello & +
4.测试,输入路径localhost:9090/demo/greeting?name=xiaocun,看到返回结果hello xiaocun
上一章,我们详细介绍了如何搭建dubbo服务,本章节我们将在dubbo服务的基础上,利用dubbox,对外提供rest服务。
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:8439次
排名:千里之外
原创:14篇
(1)(4)(1)(2)(6)

我要回帖

更多关于 dubbox rest 的文章

 

随机推荐