一、将工程改造为基于SOA架构css
二、商品列表查询功能实现。前端
因为商城是基于soa的架构,表现层和服务层是不一样的工程。因此要实现商品列表查询须要两个系统之间进行通讯。java
如何实现远程通讯?mysql
1、Webservice:效率不高基于soap协议。项目中不推荐使用。linux
2、使用restful形式的服务:http+json。不少项目中应用。若是服务太多,服务之间调用关系混乱,须要治疗服务。git
3、使用dubbo。使用rpc协议进行远程调用,直接使用socket通讯。传输效率高,而且能够统计出系统之间的调用关系、调用次数。github
随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已没法应对,分布式服务架构以及流动计算架构势在必行,亟需一个治理系统确保架构有条不紊的演进。web
单一应用架构 算法
当网站流量很小时,只需一个应用,将全部功能都部署在一块儿,以减小部署节点和成本。spring
此时,用于简化增删改查工做量的 数据访问框架(ORM) 是关键。
垂直应用架构
当访问量逐渐增大,单一应用增长机器带来的加速度愈来愈小,将应用拆成互不相干的几个应用,以提高效率。
此时,用于加速前端页面开发的 Web框架(MVC) 是关键。
分布式服务架构
当垂直应用愈来愈多,应用之间交互不可避免,将核心业务抽取出来,做为独立的服务,逐渐造成稳定的服务中心,使前端应用能更快速的响应多变的市场需求。
此时,用于提升业务复用及整合的 分布式服务框架(RPC) 是关键。
流动计算架构
当服务愈来愈多,容量的评估,小服务资源的浪费等问题逐渐显现,此时需增长一个调度中心基于访问压力实时管理集群容量,提升集群利用率。
此时,用于提升机器利用率的 资源调度和治理中心(SOA) 是关键。
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>
官方推荐使用zookeeper注册中心。
注册中心负责服务地址的注册与查找,至关于目录服务,服务提供者和消费者只在启动时与注册中心交互,注册中心不转发请求,压力较小。使用dubbo-2.3.3以上版本,建议使用zookeeper注册中心。
Zookeeper是Apacahe Hadoop的子项目,是一个树型的目录服务,支持变动推送,适合做为Dubbo服务的注册中心,工业强度较高,可用于生产环境,并推荐使用
Zookeeper:
一、能够做为集群的管理工具使用。
二、能够集中管理配置文件。
安装环境:
Linux:centos6.4
Jdk:1.7以上版本
Zookeeper是java开发的能够运行在windows、linux环境。须要先安装jdk。
安装步骤:
第一步:安装jdk
第二步:把zookeeper的压缩包上传到linux系统。
第三步:解压缩压缩包
tar -zxvf zookeeper-3.4.6.tar.gz
第四步:进入zookeeper-3.4.6目录,建立data文件夹。
第五步:把conf目录下的zoo_sample.cfg更名为zoo.cfg
[root@localhost conf]# mv zoo_sample.cfg zoo.cfg
第六步:修改data属性:dataDir=/heima/zookeeper-3.4.6/data
第七步:启动zookeeper
[root@localhost bin]# ./zkServer.sh start
关闭:[root@localhost bin]# ./zkServer.sh stop
查看状态:[root@localhost bin]# ./zkServer.sh status
注意:须要关闭防火墙。
service iptables stop
永久关闭修改配置开机不启动防火墙:
chkconfig iptables off
若是不能成功启动zookeeper,须要删除data目录下的zookeeper_server.pid文件。
1)将表现层工程独立出来:
e3-manager-web
2)将原来的e3-manager改成以下结构
e3-manager
|--e3-manager-dao
|--e3-manager-interface
|--e3-manager-pojo
|--e3-manager-service(打包方式改成war)
第一步:把e3-manager的pom文件中删除e3-manager-web模块。
第二步:把e3-manager-web文件夹移动到e3-manager同一级目录。
第三步:e3-manager-service的pom文件修改打包方式,
<packaging>war</packaging>
第四步:在e3-manager-service工程中添加web.xml文件,而后更新
因为打包方式为war须要补全目录结构,须要在webapp目录下添加WEB-INF目录,和web.xml文件。
第五步:从新导入e3-manager-web,并修改pom.xml
第六步:把e3-manager-web的配置文件复制到e3-manager-service中。
删除springmvc.xml
第七步:把e3-manager-web的web.xml复制到e3-manager-service,它的web.xml中只配置spring容器,删除前端控制器和过滤器。
第八步:发布服务
一、在e3-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的配置文件applicationContext-service.xml中添加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="cn.e3mall.service"></context:component-scan> <!-- 使用dubbo发布服务 --> <!-- 提供方应用信息,用于计算依赖关系 --> <dubbo:application name="e3-manager" /> <dubbo:registry protocol="zookeeper" address="192.168.25.128:2181" /> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 声明须要暴露的服务接口 --> <dubbo:service interface="cn.e3mall.service.itemService" ref="itemServiceImpl" /> </beans>
第一步:把e3-manager-web的配置文件删除,只留springmvc.xml,在web.xml中删除spring容器
第二步:修改e3-manager-web的pom文件
加入如下依赖
<!-- spring的依赖 --> <!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> </dependency>
<!-- 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>
第三步:修改springmvc.xml,在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="cn.e3mall.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="e3-manager-web"/> <dubbo:registry protocol="zookeeper" address="192.168.65.128:2181"/> <dubbo:reference interface="cn.e3mall.service.itemService" id="itemService" /> </beans>
第四步:在e3-manager-web工程中添加tomcat插件配置。
<build> <plugins> <!-- 配置Tomcat插件 --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <configuration> <path>/</path> <port>8082</port> </configuration> </plugin> </plugins> </build>
把e3-manager更新到本地仓库
启动l两个服务
进行页面测试(记得zookeeper要启动,否则会连不上)
war包:
需先要安装tomcat,而后部署监控中心便可。
1、使用SecureFx上传文件
二、启动tomcat
[root@localhost bin]# ./startup.sh
三、访问http://192.168.25.128:8080/dubbo-admin-2.5.4
用户名:root
密码:root
若是监控中心和注册中心在同一台服务器上,能够不须要任何配置。
若是不在同一台服务器,须要修改配置文件:
/root/apache-tomcat-7.0.47/webapps/dubbo-admin-2.5.4/WEB-INF/dubbo.properties
请求的url:/
参数:无
返回值:逻辑视图String
@Controller public class PageController { @RequestMapping("/") public String showIndex() { return "index"; } @RequestMapping("/{page}") public String showPage(@PathVariable String page) { return page; } }
静态页面位置:
把静态页面导入到工程中:
因为在web.xml中定义的url拦截形式为“/”表示拦截全部的url请求,包括静态资源例如css、js等。因此须要在springmvc.xml中添加资源映射标签:
<mvc:resources location="/WEB-INF/js/" mapping="/js/**"/> <mvc:resources location="/WEB-INF/css/" mapping="/css/**"/>
对应的jsp为:
item-list.jsp
请求的url:
/item/list
请求的参数:
page=1&rows=30
响应的json数据格式:
Easyui中datagrid控件要求的数据格式为:
{total:”2”,rows:[{“id”:”1”,”name”:”张三”},{“id”:”2”,”name”:”李四”}]}
package cn.e3mall.common.pojo; import java.io.Serializable; import java.util.List; public class EasyUIDataGridResult implements Serializable{ private Long total; private List rows; public Long getTotal() { return total; } public void setTotal(Long total) { this.total = total; } public List getRows() { return rows; } public void setRows(List rows) { this.rows = rows; } }
逆向工程生成的代码是不支持分页处理的,若是想进行分页须要本身编写mapper,这样就失去逆向工程的意义了。为了提升开发效率可使用mybatis的分页插件PageHelper。
若是你也在用Mybatis,建议尝试该分页插件,这个必定是最方便使用的分页插件。
该插件目前支持Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库分页。
第一步:导入PageHelper
第二步:把生成 PageHelper依赖的jar包添加到工程中
第三步:在Mybatis配置SqlMapConfig.xml中配置拦截器插件。
<plugins> <!-- com.github.pagehelper为PageHelper类所在包名 --> <plugin interceptor="com.github.pagehelper.PageHelper"> <!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库--> <property name="dialect" value="mysql"/> </plugin> </plugins>
@Test public void testPageHelper() throws Exception { //初始化spring容器 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-*.xml"); //得到Mapper的代理对象 TbItemMapper itemMapper = applicationContext.getBean(TbItemMapper.class); //设置分页信息 PageHelper.startPage(1, 30); //执行查询 TbItemExample example = new TbItemExample(); List<TbItem> list = itemMapper.selectByExample(example); //取分页信息 PageInfo<TbItem> pageInfo = new PageInfo<>(list); System.out.println(pageInfo.getTotal()); System.out.println(pageInfo.getPages()); System.out.println(pageInfo.getPageNum()); System.out.println(pageInfo.getPageSize()); }
参数:int page ,int rows
业务逻辑:查询全部商品列表,要进行分页处理。
返回值:EasyUIDataGridResult
@Override public EasyUIDataGridResult getItemList(int page, int rows) { //设置分页信息 PageHelper.startPage(page, rows); //执行查询 TbItemExample example = new TbItemExample(); List<TbItem> list = itemMapper.selectByExample(example); //取分页信息 PageInfo<TbItem> pageInfo = new PageInfo<>(list); //建立返回结果对象 EasyUIDataGridResult result = new EasyUIDataGridResult(); result.setTotal(pageInfo.getTotal()); result.setRows(list); return result; }
以前已经发布过了,因此不用再发布服务
引用服务:
1、初始化表格请求的url:/item/list
2、Datagrid默认请求参数:
page:当前的页码,从1开始。
rows:每页显示的记录数。
三、响应的数据:json数据。EasyUIDataGridResult
@RequestMapping("/item/list") @ResponseBody public EasyUIDataGridResult getItemList(Integer page,Integer rows){ EasyUIDataGridResult result=service.getItemList(page, rows); return result; }
4.启动服务,测试
问题:java.lang.RuntimeException: Serialized class cn.e3mall.pojo.TbItem must implement java.io.Serializable
解决: