dubbo zookeeper spring mvc简单整合的工程例子demo

2/22/2017来源:ASP.NET技巧人气:1946

该demo只是简单的集成,包括了5个工程(都是maven结构的),如下图所示: 服务端: dubbo-demo-server-api :服务接口定义工程 dubbo-demo-server-biz :服务接口实现工程 web-dubbo-server:服务接口发布工程 客户端: dubbo-demo-client-biz :客户端调用接口业务处理工程 web-dubbo-client :客户端web工程 首先先在api工程(dubbo-demo-server-api )定义服务接口。 package org.zhq.dubbo.demo.server.api; /**  * 服务接口  * @author zhq  *  */ public interface IDubboDemoServerService {      public static final String BEAN_ID="dubboDemoServerServiceImpl";      /**       * say hello + msg       * @param msg       * @return       */      String hello(String msg); } 然后在api的实现工程(dubbo-demo-server-biz )进行接口实现 package org.zhq.dubbo.server.dubbo_demo_server_biz; import org.sPRingframework.stereotype.Service; import org.zhq.dubbo.demo.server.api.IDubboDemoServerService; /**  * 接口服务实现类  * @author zhq  *  */ @Service(IDubboDemoServerService.BEAN_ID) public class DubboDemoServerServiceImpl implements IDubboDemoServerService{      @Override      public String hello(String msg) {            String outMsg="Hello "+msg;            System.out.println(outMsg);            return outMsg;      } } 再进行dubbo和zookeeper配置,引入dubbo和zookeeper的mavn包 <!-- 引入dubbo和zookeepr -->     <dependency>          <groupId>com.alibaba</groupId>          <artifactId>dubbo</artifactId>          <version>2.5.3</version>      </dependency>       <dependency>          <groupId>org.apache.zookeeper</groupId>            <artifactId>zookeeper</artifactId>            <version>3.4.6</version>        </dependency>        <dependency>            <groupId>com.github.sgroschupf</groupId>            <artifactId>zkclient</artifactId>            <version>0.1</version>       </dependency> 接口发布配置 <?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:jee="http://www.springframework.org/schema/jee"     xmlns:tx="http://www.springframework.org/schema/tx"     xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"     xmlns:context="http://www.springframework.org/schema/context"     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd     http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd     http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"     default-lazy-init="false" >     <context:component-scan base-package="org.zhq"></context:component-scan>    <!-- 提供方应用名称信息,这个相当于起一个名字,我们dubbo管理页面比较清晰是哪个应用暴露出来的 -->    <dubbo:application name="dubbo_provider"></dubbo:application>    <!-- 使用zookeeper注册中心暴露服务地址 -->    <dubbo:registry address="zookeeper://127.0.0.1:2181" check="false" subscribe="false" register=""></dubbo:registry>   <!-- 要暴露的服务接口 -->   <dubbo:service interface="org.zhq.dubbo.demo.server.api.IDubboDemoServerService" ref="dubboDemoServerServiceImpl" /> </beans> 配置说明 dubbo:registry 标签一些属性的说明: 1)register 向该注册中心注册服务,如果设为false,将只订阅,不注册。       2)check 该注册中心不存在时,是否报错。       3)subscribe 是否向此注册中心订阅服务,如果设为false,将只注册,不订阅。       4)timeout 注册中心请求超时时间(毫秒)。       5)address 可以Zookeeper集群配置,地址可以多个以逗号隔开等。就是我们上一章配置的zookeeper服务地址 dubbo:service标签的一些属性说明:      1)interface 服务api接口的路径,包括接口类名称。      2)ref 引用对应的实现类的Bean的ID      3)registry 向指定注册中心注册,在多个注册中心时使用,值为<dubbo:registry>的id属性,多个注册中心ID用逗号分隔,如果不想将该服务注册到任何registry,可将值设为N/A      4)register 默认true ,该协议的服务是否注册到注册中心。 详细说明见官网:http://dubbo.io/User+Guide-zh.htm#UserGuide-zh-%E5%90%AF%E5%8A%A8%E6%97%B6%E6%A3%80%E6%9F%A5 代码位置示图: 在web-dubbo-server应用引用实现工程,这样服务应用基本就已经开发完毕。 客户端业务类调用接口服务实现 在工程(dubbo-demo-client-biz )引入dubbo和zookeeper和api工程 配置消费 <?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:jee="http://www.springframework.org/schema/jee"     xmlns:tx="http://www.springframework.org/schema/tx"     xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"     xmlns:context="http://www.springframework.org/schema/context"     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd     http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd     http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"     default-lazy-init="false" >     <context:component-scan base-package="org.zhq"></context:component-scan>    <!-- 提供方应用名称信息,这个相当于起一个名字,我们dubbo管理页面比较清晰是哪个应用暴露出来的 -->    <dubbo:application name="dubbo_provider"></dubbo:application>    <!-- 使用zookeeper注册中心暴露服务地址 -->    <dubbo:registry address="zookeeper://127.0.0.1:2181" check="false" subscribe="false" register=""></dubbo:registry>   <!-- 要暴露的服务接口 -->   <dubbo:service interface="org.zhq.dubbo.demo.server.api.IDubboDemoServerService" ref="dubboDemoServerServiceImpl" /> </beans> 调用类实现 package org.zhq.dubbo.server.dubbo_demo_server_biz; import org.springframework.stereotype.Service; import org.zhq.dubbo.demo.server.api.IDubboDemoServerService; /**  * 接口服务实现类  * @author zhq  *  */ @Service(IDubboDemoServerService.BEAN_ID) public class DubboDemoServerServiceImpl implements IDubboDemoServerService{      @Override      public String hello(String msg) {            String outMsg="Hello "+msg;            System.out.println(outMsg);            return outMsg;      } } we-dubbo-client 引入业务实现调用工程 spring-mvc的controller实现调用 package org.zhq.dubbo.common.portal.controller; import java.util.HashMap; import java.util.Map; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.zhq.dubbo.demo.client.biz.DubboDemoServerClientBiz; /**  * 测试控制器类  * @author zhq  *  */ @Controller @RequestMapping(value = "/dubboDemoClientController") public class DubboDemoClientController { /*    @Resource(name=IDubboDemoServerService.BEAN_ID)     private IDubboDemoServerService dubboDemoServerService;*/     //测试通过业务类来调用,也可以直接用上面的api接口直接调用     @Resource(name=DubboDemoServerClientBiz.BEAN_ID)     private DubboDemoServerClientBiz dubboDemoServerClientBiz;     /**      * @param request      * @param response      * @return      */     @RequestMapping(value = "/helloDemo.do")     public @ResponseBody Map<String,Object> hello(HttpServletRequest request, HttpServletResponse response){         Map<String,Object> resultData = new HashMap<String,Object>(); //        String msg=dubboDemoServerService.hello("world");         String msg=dubboDemoServerClientBiz.sayHelloMsg("world");         resultData.put("msg", msg);         return resultData;     } } 启动web-dubbo-server和web-dubbo-client工程 访问http://localhost:8099/dubboClient/dubboDemoClientController/helloDemo.do 可以看到调用成功,服务访问Hello world字符串 dubbo-admin监控后台效果 提供者 消费者 完整工程代码下载: 链接:http://pan.baidu.com/s/1nuF30DV 密码:pcam