最近加班不太多, 琢磨着把之前做的基于SOA架构的《商城项目》简单整理下。记得当时做的时候涉及到dubbo, redis, activeMQ, solr等觉着挺六的技术,现在每天写写接口,偶尔客串写写前端,分布式相关的那些东西忘的差不多了,等在用到的时候估计还得从头来过,想想都费劲。所以这篇算是简单开个头,今后琢磨啥新的东西的时候,有必要将学习过程简单做个记录,当再次用到的时候只需要看看记录就能较快的上手。
一, 项目架构
1,初始架构
2,改造之后的SOA面向服务架构
面向服务,在这就是将service服务层单独拿出来细化为会员、订单等各个子系统,供web层系统选择性调用。在这里web层系统对service层系统的远程调用,用的是dubbo技术实现的。
二,dubbo的应用
1,先借用一个简单的dubbo工作流程图
节点角色说明:
Provider: 暴露服务的服务提供方。
Consumer: 调用远程服务的服务消费方。
Registry: 服务注册与发现的注册中心。
Monitor: 统计服务的调用次调和调用时间的监控中心。
Container: 服务运行容器
在这里,service层系统(Provider)将服务发布到注册中心Registry(项目中用的zookeeper),web层系统根据连接参数到注册中心Registry调用已发布的服务。
2,zookeeper的安装配置
2.1 解压安装包
链接:https://pan.baidu.com/s/1NxoCs8W-12IFvuDSMf07Wg
提取码:zfrl
2.2 在zookeeper-3.4.9目录下新建名为data的文件夹
2.3 修改配置文件 conf/zoo.cfg(复制于zoo_sample.cfg)
2.4 配置相关参数
dataDir: 2.2中新建的data文件夹路径(改成自己的)
clientPort: 运行端口号
2.5 启动测试
zookeeper-3.4.9/ bin/ zkServer.cmd -->ok
3,在项目中的具体应用
3.1 在对应的应用中添加需要依赖的jar包; ( e3-service/e3-web pom.xml )
<!-- dubbo相关 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
<exclusion>
<groupId>org.jboss.netty</groupId>
<artifactId>netty</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
</dependency>
3.2 发布方应用进行服务发布相关配置; ( e3-service --applicationContext*.xml)
<应用/注册中心/dubbo端口/发布服务>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
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.2.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-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
<!-- 配置包扫描器 -->
<!-- 对应的是e3-interface中的包 -->
<context:component-scan base-package="cn.e3mall.service"></context:component-scan>
<!-- 使用dubbo发布服务 -->
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="e3-manager" />
<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" />
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 声明需要暴露的服务接口; timeout用于设置debug超时时间(30秒) -->
<dubbo:service interface="cn.e3mall.service.ItemService" ref="itemServiceImpl" timeout="30000" />
<dubbo:service interface="cn.e3mall.service.ItemCatService" ref="itemCatServiceImpl" timeout="30000" />
</beans>
3.3 调用方应用进行服务调用相关配置; ( e3-manager-web )
<应用/注册中心/调用服务>
其中命xmlns名空间同上,
<!-- 引用dubbo服务 -->
<dubbo:application name="e3-manager-web"/>
<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"/>
<!-- 相当于创建一个名为itemService的bean -->
<dubbo:reference interface="cn.e3mall.service.ItemService" id="itemService" />
<dubbo:reference interface="cn.e3mall.service.ItemCatService" id="itemCatServiceTest007" />
需要注意一点:有两行是后期额外添加的:
3.4, 启动应用, 测试远程调用服务的效果;
( e3-web调用e3-service发布的对应接口)
三、Dubbo-admin的应用
1, Dubbo-admin的安装配置
1.1 可新建一Tomcat安装包,运行Dubbo-admin
1.2 将dubbo-admin.war复制到对应Tomcat安装包的webapps目录下
(在此附上dubbo-admin.war下载链接 https://pan.baidu.com/s/19sAuIaLl01Kl_D1W4RI3dw 提取码:35ll)
1.3 运行Tomcat直接访问
(http://localhost:8080/dubbo-admin/),其中端口8080可自定义修改
此为注册中心监控, 可查看对应的服务发布者和消费者
登录: root/root,能出现下图即操作ok!!!
到此,使用dubbo进行系统之间的远程调用算是ok了,到点了洗洗碎觉。