您的位置:首页 > 运维架构 > 网站架构

框架 day88 涛涛商城项目(补)-soa架构及服务中间件Dubbo

2017-02-10 19:03 671 查看

1.1.1.  基于soa的架构

SOA:Service Oriented Architecture面向服务的架构。也就是把工程拆分成服务层、表现层两个工程。服务层中包含业务逻辑,只需要对外提供服务即可。表现层只需要处理和页面的交互,业务逻辑都是调用服务层的服务来实现。



淘淘商城系统架构


 

 

后台工程搭建

Maven的常见打包方式:jar、war、pom

Pom工程一般都是父工程,管理jar包的版本、maven插件的版本、统一的依赖管理。聚合工程。

 

Taotao-parent:父工程,打包方式pom,管理jar包的版本号。

    |          项目中所有工程都应该继承父工程。

|--Taotao-common:通用的工具类通用的pojo。打包方式jar

|--Taotao-manager:服务层工程。聚合工程。Pom工程

|--taotao-manager-dao:打包方式jar

|--taotao-manager-pojo:打包方式jar

|--taotao-manager-interface:打包方式jar

|--taotao-manager-service:打包方式:war

|--taotao-manager-web:表现层工程。打包方式war

 

 

1.1.2.  系统间通信(dubbo)

分析

由于淘淘商城是基于soa的架构,表现层和服务层是不同的工程。所以要实现商品列表查询需要两个系统之间进行通信。

如何实现远程通信?
1、Webservice:效率不高基于soap协议。项目中不推荐使用。
2、使用restful形式的服务:http+json。很多项目中应用。如果服务太多,服务之间调用关系混乱,需要治疗服务。
3、使用dubbo。使用rpc协议进行远程调用,直接使用socket通信。传输效率高,并且可以统计出系统之间的调用关系、调用次数。

什么是dubbo

随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,亟需一个治理系统确保架构有条不紊的演进。



单一应用架构
当网站流量很小时,只需一个应用,将所有功能都部署在一起,以减少部署节点和成本。
此时,用于简化增删改查工作量的 数据访问框架(ORM) 是关键。

垂直应用架构
当访问量逐渐增大,单一应用增加机器带来的加速度越来越小,将应用拆成互不相干的几个应用,以提升效率。
此时,用于加速前端页面开发的 Web框架(MVC) 是关键。
分布式服务架构
当垂直应用越来越多,应用之间交互不可避免,将核心业务抽取出来,作为独立的服务,逐渐形成稳定的服务中心,使前端应用能更快速的响应多变的市场需求。
此时,用于提高业务复用及整合的 分布式服务框架(RPC) 是关键。
流动计算架构
当服务越来越多,容量的评估,小服务资源的浪费等问题逐渐显现,此时需增加一个调度中心基于访问压力实时管理集群容量,提高集群利用率。
此时,用于提高机器利用率的 资源调度和治理中心(SOA) 是关键。
 

Dubbo就是资源调度和治理中心的管理工具。

 

Dubbo的架构



节点角色说明:

Provider: 暴露服务的服务提供方。

Consumer: 调用远程服务的服务消费方。

Registry: 服务注册与发现的注册中心。

Monitor: 统计服务的调用次调和调用时间的监控中心。

Container: 服务运行容器。

调用关系说明:

0. 服务容器负责启动,加载,运行服务提供者。

1. 服务提供者在启动时,向注册中心注册自己提供的服务。

2. 服务消费者在启动时,向注册中心订阅自己所需的服务。

3. 注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。

4. 服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。

5. 服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。

使用方法

Dubbo采用全Spring配置方式,透明化接入应用,对应用没有任何API侵入,只需用Spring加载Dubbo的配置即可,Dubbo基于Spring的Schema扩展进行加载。

 

单一工程中spring的配置

<bean id="xxxService" class="com.xxx.XxxServiceImpl" />

<bean id="xxxAction" class="com.xxx.XxxAction">

       <property name="xxxService" ref="xxxService" />

</bean>

 

远程服务:

在本地服务的基础上,只需做简单配置,即可完成远程化:

 

将上面的local.xml配置拆分成两份,将服务定义部分放在服务提供方remote-provider.xml,将服务引用部分放在服务消费方remote-consumer.xml。

并在提供方增加暴露服务配置<dubbo:service>,在消费方增加引用服务配置<dubbo:reference>。

发布服务:

<!-- 和本地服务一样实现远程服务 -->

<bean id="xxxService" class="com.xxx.XxxServiceImpl" />

<!-- 增加暴露远程服务配置 -->

<dubbo:service interface="com.xxx.XxxService" ref="xxxService" />

 

调用服务:

<!-- 增加引用远程服务配置 -->

<dubbo:reference id="xxxService" interface="com.xxx.XxxService" />

<!-- 和本地服务一样使用远程服务 -->

<bean id="xxxAction" class="com.xxx.XxxAction">

       <property name="xxxService" ref="xxxService" />

</bean>

 

注册中心

注册中心负责服务地址的注册与查找,相当于目录服务,服务提供者和消费者只在启动时与注册中心交互,注册中心不转发请求,压力较小。使用dubbo-2.3.3以上版本,建议使用zookeeper注册中心。

Zookeeper是Apacahe Hadoop的子项目,是一个树型的目录服务,支持变更推送,适合作为Dubbo服务的注册中心,工业强度较高,可用于生产环境,并推荐使用

 

Zookeeper的安装:

第一步:安装jdk

第二步:解压缩zookeeper压缩包

第三步:将conf文件夹下zoo_sample.cfg复制一份,改名为zoo.cfg

第四步:修改配置dataDir属性,指定一个真实目录

第五步:

启动zookeeper:bin/zkServer.sh start

关闭zookeeper:bin/zkServer.sh stop

查看zookeeper状态:bin/zkServer.sh status

注意要关闭linux的防火墙。

 

 

1.1.3.  整合测试

需求

根据商品id查询商品信息,并将商品信息使用json数据返回。

分析

请求的url:/item/{itemId}

参数:商品id,从请求的url中获得

返回值:TbItem对象,逆向工程生成的pojo(响应json数据)

Dao层

根据商品id查询商品信息,单表查询可以使用逆向工程生成的代码。

Service层

1、在taotao-manager-interface工程中创建一个ItemService接口

2、在taotao-manager-Service工程中创建一个itemSeviceImpl的实现类。

@Service
public
class
ItemServiceImpl implements ItemService {
 
     @Autowired
     private TbItemMapper
itemMapper;
    
     @Override
     public TbItem getItemById(long
itemId) {
          TbItem tbItem =
itemMapper.selectByPrimaryKey(itemId);
          return
tbItem;
     }

}

发布服务

1、在taotao-manager-Service工程中添加dubbo依赖的jar包。

<!-- 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>

 

2、在spring的配置文件中添加dubbo的约束,然后使用dubbo:service发布服务。

<?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:dubbo="http://code.alibabatech.com/schema/dubbo"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.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://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
 
     <context:component-scan
base-package="com.taotao.service"></context:component-scan>
 
    
<!-- 使用dubbo发布服务 -->
    
<!-- 提供方应用信息,用于计算依赖关系 -->
    
<dubbo:application
name="taotao-manager"
/>
    
<dubbo:registry protocol="zookeeper"
          address="192.168.25.154:2181,192.168.25.154:2182,192.168.25.154:2183" />
    
<!-- 用dubbo协议在20880端口暴露服务 -->
    
<dubbo:protocol
name="dubbo"
port="20880"
/>
    
<!-- 声明需要暴露的服务接口 -->
    
<dubbo:service
interface="com.taotao.service.ItemService"
ref="itemServiceImpl"
/>
 
</beans>

引用服务

1、在taotao-manager-web工程中添加dubbo依赖的jar包

<!-- 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>

2、在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:p="http://www.springframework.org/schema/p"
     xmlns:context="http://www.springframework.org/schema/context"
     xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
     xmlns:mvc="http://www.springframework.org/schema/mvc"
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-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">
 
     <context:component-scan
base-package="com.taotao.controller"
/>
     <mvc:annotation-driven
/>
     <bean
          class="org.springframework.web.servlet.view.InternalResourceViewResolver">
          <property
name="prefix"
value="/WEB-INF/jsp/"
/>
          <property
name="suffix"
value=".jsp"
/>
     </bean>
    
    
<!-- 引用dubbo服务 -->
    
<dubbo:application
name="taotao-manager-web"/>
    
<dubbo:registry protocol="zookeeper" address="192.168.25.154:2181,192.168.25.154:2182,192.168.25.154:2183"/>   

    
<dubbo:reference
interface="com.taotao.service.ItemService"
id="itemService"
/>
    
</beans>

Controller

@Controller
public
class
ItemController {
 
     @Autowired
     private ItemService
itemService;
    
     @RequestMapping("/item/{itemId}")
     @ResponseBody
     public TbItem getItemById(@PathVariable Long
itemId) {
          //根据商品id查询商品信息
          TbItem tbItem =
itemService.getItemById(itemId);
          return
tbItem;
     }

}

 

 

Dubbo监控中心



需要安装tomcat,然后部署监控中心即可。

 

1、部署监控中心:

[root@localhost ~]# cpdubbo-admin-2.5.4.war apache-tomcat-7.0.47/webapps/dubbo-admin.war

 

2、启动tomcat

 

3、访问http://192.168.25.167:8080/dubbo-admin/

用户名:root

密码:root

 

如果监控中心和注册中心在同一台服务器上,可以不需要任何配置。

如果不在同一台服务器,需要修改配置文件:

/root/apache-tomcat-7.0.47/webapps/dubbo-admin/WEB-INF/dubbo.properties

 


 

Dubbo直连

 

dubbo-consumer.xml

<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"

         xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"

         xmlns:task="http://www.springframework.org/schema/task" 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.0.xsd
                  http://www.springframework.org/schema/mvc
                  http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
                  http://www.springframework.org/schema/context
                  http://www.springframework.org/schema/context/spring-context-4.0.xsd
                  http://www.springframework.org/schema/aop
                  http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
                  http://www.springframework.org/schema/tx
                  http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
                  http://www.springframework.org/schema/task
               http://www.springframework.org/schema/task/spring-task-4.0.xsd
                  http://code.alibabatech.com/schema/dubbo       
                  http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
         <!-- dubbo 3步 -->

         <!-- 服务消费方 名称 -->

         <dubbo:application name="taotao-manager-web" />

         <!-- 注册中心 注册地址 连接zookeeper <dubbo:registry address="192.168.200.128:2181"

                  protocol="zookeeper"/> -->

         <dubbo:registry address="N/A" />

         <!-- 调用暴露接口

                  <dubbo:reference interface="com.itheima.core.service.buyer.SessionService"

                  id="sessionService" url="127.0.0.1:20880" />

          -->

          <dubbo:reference interface="com.taotao.service.ItemService"

                  id="itemService" url="127.0.0.1:20880" />

         <!-- 使用debug跑 所以连接超时 我这里使用10分钟 启动时不校验服务提供方 -->

         <dubbo:consumer timeout="600000" check="false" />

 

</beans>

 

 

dubbo-provider.xml

<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"

         xmlns:aop="http://www.springframework.org/schema/aop"

         xmlns:tx="http://www.springframework.org/schema/tx"

         xmlns:task="http://www.springframework.org/schema/task"

         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.0.xsd
                  http://www.springframework.org/schema/mvc
                  http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
                  http://www.springframework.org/schema/context
                  http://www.springframework.org/schema/context/spring-context-4.0.xsd
                  http://www.springframework.org/schema/aop
                  http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
                  http://www.springframework.org/schema/tx
                  http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
                  http://www.springframework.org/schema/task
               http://www.springframework.org/schema/task/spring-task-4.0.xsd
                  http://code.alibabatech.com/schema/dubbo       
                  http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
                  <!-- dubbo  4步 -->

                  <!-- 服务提供方 名称 唯一标示 -->

                  <dubbo:application name="taotao-manager-service"/>

                  <!-- 注册中心 注册地址 连接zookeeper

                  <dubbo:registry address="192.168.200.128:2181" protocol="zookeeper"/>

                  -->

                  <dubbo:registry address="N/A"/>

                  <!-- dubbo   主机  端口 -->

                  <dubbo:protocol host="127.0.0.1" port="20880"/>

                  <!-- 暴露接口

                  <dubbo:service interface="com.itheima.core.service.TestTbService" ref="testTbService"/>

                   -->

                   <dubbo:service interface="com.taotao.service.ItemService" ref="itemService"/>

                 

                 

</beans>

 

 

升级版逆向工程

 

mybatis:动态sql语句

逆向工程 缺点:

1.不能查询动态sql语句(所有的字段都查)

2.不支持分页

3.pojo类没有实现序列化接口

4.没注释

5.toString方法

6.可以改名为dao

7.pojo类查询(query)

升级版逆向工程 以上都解决了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐