国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

dubbo zookeeper spring mvc簡單整合的工程例子demo

2019-11-08 01:55:01
字體:
來源:轉載
供稿:網友
該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
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 保靖县| 东宁县| 朝阳市| 灵石县| 无为县| 临江市| 新源县| 东方市| 深泽县| 正镶白旗| 渭南市| 万全县| 太仆寺旗| 平泉县| 丰都县| 荆门市| 五华县| 札达县| 武冈市| 绍兴市| 都江堰市| 虞城县| 晋中市| 上高县| 峨眉山市| 白沙| 卓资县| 青州市| 祥云县| 如东县| 崇明县| 商城县| 广西| 宁强县| 武定县| 长泰县| 苍溪县| 石柱| 武隆县| 通海县| 扶风县|