新建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
新闻热点
疑难解答