赞
踩
本dubbo使用教程基于zookeeper-3.4.9搭建,包含服务提供者DubboProvider、和消费者DubboConsumer,可直接在Tomcat上运行。DubboProvider对外开放2个接口,DubboConsumer调用DubboProvider,并对外暴露HTTP服务。DubboProvider还提供了对数据库的操作,基于SpringJDBC并加入了RowMapper通用类,这点在下文《SpringJDBC之RowMapper通用类》详讲。
项目完整源码见文末链接,下载即可用。
网上现有教程大多是直接照搬官网,用main函数启动,未使用Tomcat启动,直接应用于企业开发较为困难。当然我也是参照官网,学习了前辈的经验,哈哈。
开发环境:Windows10、Eclipse、JDK8、Spring4.3.7、zookeeper-3.4.9。
- package com.zxiaofan.dubboProvidder.service;
-
- import com.zxiaofan.dubboProvidder.model.HelloBo;
-
- /**
- *
- * @author xiaofan
- */
- public interface IProviderService {
- /**
- * 测试字符串传输.
- *
- * @param name
- * @return
- */
- String helloBoy(String name);
-
- /**
- * 测试bean传输.
- *
- * @param bo
- * @return
- */
- HelloBo helloGirl(HelloBo bo);
- }
接口IUserService略过,详见源码。
- package com.zxiaofan.dubboProvidder.service.impl;
-
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Component;
-
- import com.zxiaofan.dubboProvidder.model.HelloBo;
- import com.zxiaofan.dubboProvidder.service.IProviderService;
-
- /**
- *
- * @author xiaofan
- */
- @Component("providerService")
- public class ProviderServiceImpl implements IProviderService {
- @Value("${param.url}")
- private String url;
-
- /**
- * {@inheritDoc}.
- */
- @Override
- public String helloBoy(String name) {
- String result = "hello " + name + ", This is dubboProvider[" + url + "]";
- System.out.println(result);
- return result;
- }
-
- /**
- * {@inheritDoc}.
- */
- @Override
- public HelloBo helloGirl(HelloBo helloBo) {
- if (null == helloBo.getName()) {
- helloBo.setName("DefaultName");
- }
- helloBo.setUrl(helloBo.getName() + ":This is dubboProvider[csdn.zxiaofan.com]");
- return helloBo;
- }
-
- }
- <?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-4.3.xsd
- http://code.alibabatech.com/schema/dubbo
- http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
-
- <!-- 提供方应用信息,用于计算依赖关系 -->
- <dubbo:application name="${dubbo.applicationName}" />
-
- <!-- 使用zookeeper广播注册中心暴露服务地址 -->
- <dubbo:registry protocol="${dubbo.registryProtocol}" address="${dubbo.registryAddress}" />
-
- <!-- 用dubbo协议在20880端口暴露服务 -->
- <dubbo:protocol name="dubbo" port="${dubbo.port}" />
- <!-- <dubbo: protocol="registry"></dubbo:monitor> -->
-
-
- <!-- 和本地bean一样实现服务 -->
- <bean id="providerService" class="com.zxiaofan.dubboProvidder.service.impl.ProviderServiceImpl" />
- <!-- 和本地bean一样实现服务 -->
- <bean id="userService" class="com.zxiaofan.dubboProvidder.service.impl.UserServiceImpl" />
- <!-- 声明需要暴露的服务接口 -->
- <dubbo:service interface="com.zxiaofan.dubboProvidder.service.IProviderService"
- ref="providerService" retries="${dubbo.retries}" timeout="${dubbo.serverTimeout}"/>
- <!-- 声明需要暴露的服务接口 -->
- <dubbo:service interface="com.zxiaofan.dubboProvidder.service.IUserService"
- ref="userService" retries="${dubbo.retries}" timeout="${dubbo.serverTimeout}"/>
- </beans>
结合配置文件dubbo.properties来看
# dubbo param
dubbo.applicationName=dubboProvider
dubbo.registryProtocol=zookeeper
dubbo.registryAddress=127.0.0.1:2181
dubbo.port=20880
#服务端超过serverTimeout未返回结果则抛500异常
dubbo.serverTimeout=120000
#客户端超过clientTimeout未获取到数据则抛500异常
dubbo.clientTimeout=120000
#dubbo调用失败,默认重试2次
dubbo.retries=0
<bean id="providerService" class="com.zxiaofan.dubboProvidder.service.impl.ProviderServiceImpl" />声明了对外暴露的接口;
- <bean
- class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="locations">
- <list>
- <value>/WEB-INF/config/param.properties</value>
- <value>/WEB-INF/config/dubbo.properties</value>
- <value>/WEB-INF/config/jdbc.properties</value>
- </list>
- </property>
- <property name="fileEncoding" value="UTF8"></property>
- </bean>
- <bean
- class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
-
- <!-- Aspect -->
- <!-- <aop:aspectj-autoproxy proxy-target-class="true" />-->
- <context:component-scan base-package="com.zxiaofan"></context:component-scan>
- <import resource="classpath:com/zxiaofan/config/spring/dubbo-provider.xml"/>
web.xml一定要引入app-context.xml以及设置监听ContextLoaderListener。
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>
- classpath:/com/zxiaofan/config/spring/app-context.xml
- </param-value>
- </context-param>
- <!--初始化spring上下文 -->
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- package com.zxiaofan.dubboConsumer.service;
-
- /**
- *
- * @author zxiaofan
- */
- public interface IConsumerService {
- String hi(String name);
- }
- package com.zxiaofan.dubboConsumer.service.impl;
-
- import javax.annotation.Resource;
-
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Component;
-
- import com.zxiaofan.dubboConsumer.service.IConsumerService;
- import com.zxiaofan.dubboProvidder.model.HelloBo;
- import com.zxiaofan.dubboProvidder.service.IProviderService;
- import com.zxiaofan.dubboProvidder.service.IUserService;
-
- /**
- *
- * @author zxiaofan
- */
- @Component("consumerService")
- public class ConsumerServiceImpl implements IConsumerService {
- @Resource(name = "providerService")
- private IProviderService providerService;
-
- @Resource(name = "userService")
- private IUserService userService;
-
- @Value("${param.url}")
- private String url;
-
- /**
- * {@inheritDoc}.
- */
- @Override
- public String hi(String name) {
- String result = null;
- if (null != name && name.startsWith("boy")) {
- System.out.println("Hi Boy!");
- result = providerService.helloBoy(name.replace("boy", "~~~"));
- } else if (null != name && name.startsWith("select")) {
- System.out.println("hi select!");
- result = userService.selectByID(name.replace("select", ""));
- return result;
- } else {
- HelloBo helloBo = new HelloBo();
- helloBo.setName(name);
- HelloBo helloBoResult = null;
- helloBoResult = providerService.helloGirl(helloBo);
- if (null != helloBoResult) {
- result = helloBoResult.getUrl();
- }
- }
- result += "; This is dubboConsumer[" + url + "]";
- return result;
- }
-
- }
总共有3个分支:
- <?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-4.3.xsd
- http://code.alibabatech.com/schema/dubbo
- http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
-
-
- <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
- <dubbo:application name="${dubbo.applicationName}"/>
-
- <!-- 使用dubbo协议 注册中心暴露服务地址 -->
- <dubbo:registry protocol="${dubbo.registryProtocol}" address="${dubbo.registryAddress}"/>
-
- <!-- 生成远程服务代理,可以像使用本地bean一样使用demoService -->
- <dubbo:reference id="providerService" interface="com.zxiaofan.dubboProvidder.service.IProviderService" retries="${dubbo.retries}" timeout="${dubbo.clientTimeout}"/>
- <dubbo:reference id="userService" interface="com.zxiaofan.dubboProvidder.service.IUserService" retries="${dubbo.retries}" timeout="${dubbo.clientTimeout}"/>
- </beans>
该配置文件配置了2个服务的调用关系:providerService(入参为单字符串接口、入参为model接口)、userService(基于SpringJDBC的数据库服务)。
- <!-- <listener>
- <listener-class>
- org.springframework.web.util.Log4jConfigListener
- </listener-class>
- </listener> -->
-
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>
- classpath:/com/zxiaofan/config/spring/app-context.xml
- </param-value>
- </context-param>
-
- <!--初始化spring上下文 -->
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
-
- <!-- 必须有servlet -->
- <servlet>
- <servlet-name>HttpServer</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>
- classpath*:/com/zxiaofan/config/spring/httpServer-servlet.xml
- </param-value>
- </init-param>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>HttpServer</servlet-name>
- <url-pattern>/*</url-pattern>
- </servlet-mapping>
引入app-context.xml,监听ContextLoaderListener,注意加上servlet,方便暴露HTTP服务。
- 欢迎个人转载,但须在文章页面明显位置给出原文连接;
- 未经作者同意必须保留此段声明、不得随意修改原文、不得用于商业用途,否则保留追究法律责任的权利。
-
- 【 CSDN 】:csdn.zxiaofan.com
- 【GitHub】:github.zxiaofan.com
-
- 如有任何问题,欢迎留言。祝君好运!
- Life is all about choices!
- 将来的你一定会感激现在拼命的自己!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。