新建3個maven項目


package com.cl.user.serviceImpl;import org.springframework.stereotype.Component;import org.springframework.stereotype.Service;import com.cl.user.servicei.UserService;@Service("userService")@Componentpublic class UserServiceImpl implements UserService{ public String sayHello(String id) { System.out.println("hello world----------------------------"); StringBuffer sb=new StringBuffer();; for (int i = 0; i < 10; i++) { sb=sb.append("hello world-->"+i+"===="+id+"/n"); } return sb.toString(); } public String test(int a,int b) { return (a+b)+""; } public String test2() { String str="hello dubbo"; return str; }}有了實現類,還需要接口,也就是項目test-public-interface 
這個項目很簡單 只需要 接口 就行了
package com.cl.user.servicei;public interface UserService { public String sayHello(String id); public String test(int a,int b); public String test2();}當然 項目 1 要 依賴 項目 2
所以在1 的 pom.xml 中 有
<dependency> <groupId>test-web</groupId> <artifactId>test-pubilc-interface</artifactId> <version>0.0.1-SNAPSHOT</version> <scope>test</scope></dependency>這樣 1 中的 UserServiceImpl 就可以實現 UserService接口當然我們用到 dubbo,就要有dubbo的核心jar包和zookeeper的jar包,所以在1 的 pom.xml 中有
<dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> </exclusion> </exclusions></dependency><dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.3.3</version></dependency>下面介紹一下項目1 中 的 ApplicationContent-dubbo.xml 和 ApplicationContent.xmlApplicationContent.xml 主要是spring的配置不多說<?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 啟用注解 --> <context:annotation-config/> <!-- 啟動組件掃描,排除@Controller組件,該組件由SpringMVC配置文件掃描 --> <context:component-scan base-package="com.cl.user.serviceImpl"/> <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <!-- value>/WEB-INF/classes/dbconfig.properties</value--> <value>dbconfig.properties</value> </list> </property> </bean> <!-- 阿里 druid數據庫連接池 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <!-- 數據庫基本信息配置 --> <property name="url" value="${url}" /> <property name="username" value="${username}" /> <property name="passWord" value="${password}" /> <property name="driverClassName" value="${driverClassName}" /> <!-- 初始化連接大小 --> <property name="initialSize" value="${jdbc.initialSize}"></property> <!-- 連接池最大數量 --> <property name="maxActive" value="${jdbc.maxActive}"></property> <!-- 連接池最大空閑 --> <property name="maxIdle" value="${jdbc.maxIdle}"></property> <!-- 連接池最小空閑 --> <property name="minIdle" value="${jdbc.minIdle}"></property> <!-- 獲取連接最大等待時間 --> <property name="maxWait" value="${jdbc.maxWait}"></property> </bean> <!-- 配置mybatis --> <bean id="sqlsessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"></property> <!-- mapper掃描 --> <property name="mapperLocations" value="classpath:mybatis/*/*.xml"></property> </bean> <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg ref="sqlSessionFactory" /> </bean> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="delete*" propagation="REQUIRED" read-only="false" rollback-for="Exception"/> <tx:method name="insert*" propagation="REQUIRED" read-only="false" rollback-for="Exception" /> <tx:method name="update*" propagation="REQUIRED" read-only="false" rollback-for="Exception" /> <tx:method name="save*" propagation="REQUIRED" read-only="false" rollback-for="Exception" /> <tx:method name="*" rollback-for="Exception"/> </tx:attributes> </tx:advice> <aop:aspectj-autoproxy proxy-target-class="true"/> <!-- 事物處理 --> <aop:config> <aop:pointcut id="pc" expression="execution(* com.um.*.services..*(..))" /> <aop:advisor pointcut-ref="pc" advice-ref="txAdvice" /> </aop:config></beans>ApplicationContent-dubbo.xml dubbo服務的配置<?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:context="http://www.springframework.org/schema/context" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 提供方應用信息,用于計算依賴關系 --> <dubbo:application name="hehe_provider" /> <!-- 使用zookeeper注冊中心暴露服務地址 --> <dubbo:registry address="zookeeper://172.30.9.173:2181"/> <!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> --> <!-- 用dubbo協議在20880端口暴露服務 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 具體的實現bean --> <bean id="userService" class="com.cl.user.serviceImpl.UserServiceImpl" /> <!-- 聲明需要暴露的服務接口 --> <dubbo:service interface="com.cl.user.servicei.UserService" ref="userService" /></beans>注意:有可能你的配置文件中不識別 <dubbo:> 解決辦法(缺少dubbo.xsd,具體怎么導入到編譯器,上網百度,這里不多說了)
到這里我們的 “提供者” 即 服務已經開發完了,那么如何啟動服務呢? 在項目1中有個start.java package com.test;import org.apache.log4j.PropertyConfigurator;public class start { static{ PropertyConfigurator.configure("src/main/resources/log4j.properties"); } public static void main(String[] args) { com.alibaba.dubbo.container.Main.main(args); }}注意里面有main方法,可以直接運行啟動。別急! 之前還有個問題就是為什么ApplicationContent-dubbo.xml 和 ApplicationContent.xml 這兩個配置文件 必須要放到/MATE-INF/spring/下面
因為 com.alibaba.dubbo.container.Main.main(args) 默認就會去加載 /MATE-INF/spring/ 下的配置文件
我們來看一下源碼

看完源碼 這下明白為什么了吧!!!!!!!!!!!!!!!!!
啟動服務后我們在 dubbo-admin中就能看到我們剛才開發的的 “提供者”


192.168.139.127 是本地局域網地址

這是 “提供者” 此時還沒有“消費者” 接下來我們就要開發 “消費者”

UserController.java
package com.cl.user.controller;import javax.annotation.Resource;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import com.cl.user.servicei.UserService;@Controllerpublic class UserController { @Resource(name="userService") private UserService userService; @RequestMapping("/hello/test/world") public void sayHello(){ System.out.println(userService.sayHello("hello")+"**************************"); }}ApplicationContext-mvc.xml 都是springmvc的配置 如下,具體不解釋了<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <mvc:annotation-driven/> <mvc:default-servlet-handler/> <context:component-scan base-package="com" /> <!-- 對靜態資源文件的訪問 restful--> <mvc:resources mapping="/js/**" location="/js/" /> <mvc:resources mapping="/lib/**" location="/lib/" /> <mvc:resources mapping="/plugins/**" location="/plugins/" /> <mvc:resources mapping="/uploadFiles/**" location="/uploadFiles/" /> <mvc:resources mapping="/WEB-INF/html/**" location="/WEB-INF/html/" /> <!-- 配置SpringMVC的視圖解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/html"/> <property name="suffix" value=".jsp"/> </bean> <!-- 上傳攔截,如最大上傳值及最小上傳值 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" > <property name="maxUploadSize"> <value>104857600</value> </property> <property name="maxInMemorySize"> <value>4096</value> </property> <property name="defaultEncoding"> <value>utf-8</value> </property> </bean> </beans>ApplicationContext-dubbo.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="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 消費方應用名,用于計算依賴關系,不是匹配條件,不要與提供方一樣 --> <dubbo:application name="hehe_consumer" /> <!-- 使用zookeeper注冊中心暴露服務地址 --> <dubbo:registry address="zookeeper://172.30.9.173:2181"/> <!-- 組播注冊 --> <!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> --> <!-- 生成遠程服務代理,可以像使用本地bean一樣使用demoService --> <dubbo:reference id="userService" interface="com.cl.user.servicei.UserService" /> </beans>開發完消費者后,啟動消費者(啟動MyTest類),可以在管控臺看到

本次教程教導這里!!Dubbo更多詳細的東西還去官網的用戶手冊尋找吧!! http://dubbo.io/User+Guide-zh.htm http://dubbo.io/ 用戶手冊說的挺詳細的!這里僅供參考!
源碼:http://pan.baidu.com/s/1pLg4ivx 密碼:a05p
新聞熱點
疑難解答