我们摒弃其它教程粗陋的思路,当你看到这篇文章之时,代表已经做出选择;这里只是做细微讲解,demo请到
dubbo github:https://github.com/alibaba/dubbo
dubbox github:https://github.com/dangdangdotcom/dubbox
下载详细代码,查看事例
大纲
- 官方文档
- 项目结构
- 环境配置
- 提供者
- 消费者
- dubbo协议
- 默认Hessian序列化
- kyro序列化
一、官方文档
dubbo:http://dubbo.io/
二、项目结构
- ems-***-api:包含model数据对象、service层接口
- ems-***-provider:是对service接口的实现和dao层
- ems--rest:restful协议实现层,引用了ems--api
为什么会是这样的结构?
项目中项目让其它消费端使用该接口,但是不想让对方了解业务的具体实现;当项目发布部署后,我们只需要把ems-***-api给消费端使用,dubbo即可调用远程实现层;
项目架构说明:dubbo进入到我的项目
二、环境配置
提供者
ems-cic-provider的pom.xml中加和ems-cic-api的包
- src/main/java:java代码
- src/main/assembly:
assembly.xml打包配置文件
conf包含数据库连接、dubbo、redis等相关参数(properties)文件 - src/main/resources
META-INF/spring/dubbo-provider.xml:dubbo配置文件
//dubbo-provider.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: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">
<context:property-placeholder location="classpath:conf/*.properties"/>
<import resource="spring-config-db.xml"/>
<!--应用相关,name:应用名称、owner:所有者、organization:组织-->
<dubbo:application name="ems-cic" owner="cic" organization="ems" />
<!--协议相关,name:协议名称、serialization:序列化方式、host:提供者所在服务器ip、port:提供者端口号-->
<dubbo:protocol name="dubbo" serialization="kryo" host="${dubbo.protocol.host}" port="${dubbo.protocol.port}" />
<!--对协议进行设置,delay:延时、timeout:接口调用过期时间、retries:重度次数-->
<dubbo:provider protocol="dubbo" delay="-1" timeout="20000" retries="0" />
<!--注册地址,一般情况下使用zookeeper,如:zookeeper://127.0.0.1:2181-->
<dubbo:registry address="${dubbo.registry.address}" />
<!--接口暴露,interface:ems-***-api中所定义的java interface、ref:spring扫描的service实现别名、protocol:指定以什么协议暴露接口、validation:true表示验证接口参数-->
<dubbo:service interface="net.slans.ems.cic.service.SignService" ref="signService" protocol="dubbo" validation="true" />
<dubbo:service interface="net.slans.ems.cic.service.DepartmentService" ref="departmentService" protocol="dubbo" validation="true" />
<dubbo:service interface="net.slans.ems.cic.service.UserService" ref="userService" protocol="dubbo" validation="true" />
<dubbo:service interface="net.slans.ems.cic.service.RoleService" ref="roleService" protocol="dubbo" validation="true" />
<dubbo:service interface="net.slans.ems.cic.manager.OmpCodeManager" ref="ompCodeManager" protocol="dubbo" validation="true" />
<dubbo:service interface="net.slans.ems.cic.manager.OmpTokenManager" ref="ompTokenManager" protocol="dubbo" validation="true" />
<dubbo:service interface="net.slans.ems.cic.manager.OmpRegCodeManager" ref="ompRegCodeManager" protocol="dubbo" validation="true" />
</beans>
//dubbo.properties
dubbo.application.name=ems-uic-provider
dubbo.protocol.host=127.0.0.1
dubbo.protocol.port=20810
dubbo.protocol.timeout=2000000
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.log4j.file=logs/dubbo.log
dubbo.log4j.level=WARN
这个地方我需要唠叨一下,
- dubbo.provider timeout:当提供者没有在指定时间内返回数据就会报错,当数据源查询数据比较慢就会出现此类问题
- dubbo.provider retries:当因指定时间内未返回数据,是否重复调用此接口,当插入数据时,重复执行会造成数据重复
- dubbo.protocol host:在这里指定内部ip在内网才可以使用,最开始我们没有指定此项,跨服务器消费者启动会出现接口无法访问
此处无法列举出所有问题,有其它问题可以评论
消费者
<?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">
<context:property-placeholder location="classpath:conf/*.properties"/>
<import resource="spring-config.xml"/>
<dubbo:application name="ems-omp-rest" owner="uic" organization="ems" />
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!--注意:消费者和提供者需要使用共同的序列化方法-->
<dubbo:protocol name="dubbo" serialization="kryo" />
<!-- 访问的接口 -->
<dubbo:reference id="snsCodeManager" interface="net.slans.ems.uic.manager.SnsCodeManager" protocol="dubbo" validation="true" />
<dubbo:reference id="snsTokenManager" interface="net.slans.ems.uic.manager.SnsTokenManager" protocol="dubbo" validation="true" />
<dubbo:reference id="ompCodeManager" interface="net.slans.ems.cic.manager.OmpCodeManager" protocol="dubbo" validation="true" />
<dubbo:reference id="ompTokenManager" interface="net.slans.ems.cic.manager.OmpTokenManager" protocol="dubbo" validation="true" />
<dubbo:reference id="ompRegCodeManager" interface="net.slans.ems.cic.manager.OmpRegCodeManager" protocol="dubbo" validation="true" />
<dubbo:reference id="verifyManager" interface="net.slans.ems.uic.manager.VerifyManager" protocol="dubbo" validation="true" />
<dubbo:reference id="signService" interface="net.slans.ems.cic.service.SignService" protocol="dubbo" validation="true" />
<dubbo:reference id="departmentService" interface="net.slans.ems.cic.service.DepartmentService" protocol="dubbo" validation="true" />
<dubbo:reference id="userService" interface="net.slans.ems.cic.service.UserService" protocol="dubbo" validation="true" />
<dubbo:reference id="roleService" interface="net.slans.ems.cic.service.RoleService" protocol="dubbo" validation="true" />
<dubbo:reference id="slansService" interface="net.slans.ems.uic.service.SlansService" protocol="dubbo" validation="true" />
</beans>
三、dubbo协议
- 默认Hessian序列化
//默认是Hessian不指定即可
<dubbo:protocol name="dubbo" host="${dubbo.protocol.host}" port="${dubbo.protocol.port}" />
- kyro序列化
//serialization="kryo"
<dubbo:protocol name="dubbo" serialization="kryo" host="${dubbo.protocol.host}" port="${dubbo.protocol.port}" />
接下来,会涉及到dubbox restful、mycat、jstorm、redis、mongodb、react+redux
很高兴认识你,我们都一样,有过迷茫却从未放弃;害怕孤独可从不寂寞。