dubbo实现了分布式远程调用框架,多运行节点既能提高可靠性,又能提升负载能力。dubbo配置主要有注册中心(推荐zookeeper或redis)、提供者provider、消费者consumer,注册中心是第三方实现,所以主要配置好服务提供者和消费者就可以了。实际上服务接口和实现都是需要我们自己设计和实现的,dubbo做的事情就是将服务实现发布到注册中心,然后消费者从注册中心订阅服务接口,之后对接口的调用就由dubbo调度提供者去执行并返回结果。以下配置都有源码,见右侧“免费资源”。
提供者provider的配置:提供者是独立运行的节点,可以多实例运行,将服务注册到注册中心
必须要有application name,注册中心配置zookeeper,协议dubbo,超时6秒失败不重试,提供者加载repository和service层bean,然后发布接口service。

assembly.xml内容,可以切换dir或tar.gz两种格式
实际上本地调试开发时,可以不必启用分布式配置,只需要更改web.xml即可,所有的服务都已经是配置好了的。
提供者provider的配置:提供者是独立运行的节点,可以多实例运行,将服务注册到注册中心
必须要有application name,注册中心配置zookeeper,协议dubbo,超时6秒失败不重试,提供者加载repository和service层bean,然后发布接口service。
<dubbo:application name="ite-provider" /> <dubbo:registry address="zookeeper://127.0.0.1:2181"/> <dubbo:protocol name="dubbo" port="20880" /> <dubbo:provider timeout="6000" retries="0"/> <import resource="classpath:cache.xml"/> <import resource="classpath:ite-repository.xml"/> <import resource="classpath:ite-service.xml"/> <import resource="classpath:ite-provider.xml"/>ite-provider.xml,ref引用的bean是ite-service.xml已经定义好的接口实现,dubbo:service就是把接口实现发布到注册中心
<dubbo:service ref="codeListService" interface="com.itecheast.ite.domain.service.CodeListService" /> <dubbo:service ref="idService" interface="com.itecheast.ite.domain.service.IdService" /> <dubbo:service ref="passwordService" interface="com.itecheast.ite.domain.service.PasswordService" /> <dubbo:service ref="rolePermissionService" interface="com.itecheast.ite.domain.service.RolePermissionService" />provider是可以独立运行的,dubbo.jar里面有assembly目录,运行mvn assembly:directory就可以生成能直接运行的provider目录

assembly.xml内容,可以切换dir或tar.gz两种格式
<assembly>dubbo.properties,运行start.bat或start.sh时,将从属性文件读取dubbo配置信息,provider节点可以多处复制并运行。
<id>assembly</id>
<formats>
<!-- <format>tar.gz</format> -->
<format>dir</format>
</formats>
<includeBaseDirectory>true</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>src/main/assembly/bin</directory>
<outputDirectory>bin</outputDirectory>
<fileMode>0755</fileMode>
</fileSet>
<fileSet>
<directory>src/main/assembly/conf</directory>
<outputDirectory>conf</outputDirectory>
<fileMode>0644</fileMode>
</fileSet>
<fileSet>
<directory>src/test/resources</directory>
<outputDirectory>conf</outputDirectory>
<fileMode>0644</fileMode>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<outputDirectory>lib</outputDirectory>
</dependencySet>
</dependencySets>
</assembly>
dubbo.container=log4j,spring dubbo.application.name=ite-provider dubbo.registry.address=zookeeper://127.0.0.1:2181 dubbo.monitor.protocol=registry dubbo.protocol.name=dubbo dubbo.protocol.port=20880 dubbo.spring.config=provider.xml dubbo.log4j.file=logs/ite-provider.log dubbo.log4j.level=WARN消费者consumer的配置,使用dubbo:reference订阅注册中心里的服务即可,然后就可以@Autowired注入服务接口了。
<dubbo:application name="ite-consumer" /> <dubbo:registry address="zookeeper://127.0.0.1:2181"/> <dubbo:reference id="codeListService" interface="com.itecheast.ite.domain.service.CodeListService" /> <dubbo:reference id="idService" interface="com.itecheast.ite.domain.service.IdService" /> <dubbo:reference id="passwordService" interface="com.itecheast.ite.domain.service.PasswordService" /> <dubbo:reference id="rolePermissionService" interface="com.itecheast.ite.domain.service.RolePermissionService" />如果前端项目是一个消费者,就可以在web.xml里直接加载consumer.xml订阅服务了。
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:consumer.xml,classpath:cache.xml,classpath:shiro.xml,classpath:front.xml</param-value> </context-param>
实际上本地调试开发时,可以不必启用分布式配置,只需要更改web.xml即可,所有的服务都已经是配置好了的。
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:ite-repository.xml,classpath:ite-service.xml,classpath:cache.xml,classpath:shiro.xml,classpath:front.xml</param-value> </context-param>zookeeper的配置很简单,
wget http://tool.xlongwei.com/softwares/zookeeper-3.4.6.tar.gz
tar -zxvf zookeeper-3.4.6.tar.gz
cd zookeeper-3.4.6/conf
cp zoo_sample.cfg zoo.cfg
vi zoo.cfg #配置zookeeper参数
单机配置(集群配置待研究)tickTime=2000
initLimit=10
syncLimit=5
dataDir=/home/dubbo/zookeeper-3.3.3/data
clientPort=2181 运行或停止zookeeper sh zkServer.sh start | stop
dubbo不支持分布式事务,接口嵌套不宜过多避免调用超时
请问是shiro做的权限控制么?
@朱无分 #1 是的,可选:shiro,spring security,或者自定义ServletFilter都可以,shiro有时带来一些不明白的问题还不如自定义方便
请我楼主 我在自定义Realm 无法注入需要调用 dubbo的服务 来做用户认证这是为什么呢,我是用的是注解@Reference来调用service服务,每次进去都是发现service都是空的
源码地址:https://www.xlongwei.com/repos/evergreen/trunk/,点击右侧免费资源还可以检出源码,https://www.xlongwei.com/repos/evergreen/trunk/ite-domain/src/main/java/com/itecheast/ite/domain/shiro/UserRealm.java,UserReam里面的service需要在consumer.xml里面定义bean,而权限filter是在web.xml和front.xml里面配置的。
Dubbo实战与源码分析 课程观看地址:http://www.xuetuwuyou.com/course/90
Dubbo项目实战:http://www.roncoo.com/details?cid=f614343765bc4aac8597c6d8b38f06fd