用SSM框架从零开始搭建一个分布式商城(二)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lkp1603645756/article/details/82254387

1. 计划

1、服务中间件dubbo

2、SSM框架整合

3、测试使用dubbo

4、后台系统商品列表查询功能实现

5、监控中心的搭建

2. 功能分析

2.1 后台系统所用的技术

框架:Spring+SpringMVC+Mybatis+dubbo

前端:EasyUI

数据库:mysql

2.2 创建数据库

导入数据库sql文件

链接:https://pan.baidu.com/s/1e6oC5rysgRxUcqtVjkcFRA 密码:6o4o

2.3. 系统间的通信

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

如何使用远程通信?

1、使用WebService:效率不高,它是基于soap协议(http+xml)。项目中不推荐使用。OA ERP可以使用

2、使用restful形式的服务:http+json。很多项目中应用。如果服务越来越多,服务于服务器之间的调用关系复杂,调用服务的URL管理复杂,什么添加机器难以确定。

3、使用dubbo。使用rpc协议进行远程调用,之间使用socket通信。传输效率高,并且可以统计出系统之间的调用关系、调用次数,管理服务。

3. Dubbo

3.1 什么是dubbo

dubbo.io 开源项目,非盈利项目用.io

DUBBO是一个分布式服务框架,致力于提高性能和透明化的RPC远程服务调用方案,是阿里巴巴SOA服务化治理方案的核心框架,每天2000+个服务体用3000000000+此访问量支持,并广泛应用于阿里巴巴集团的各成员站点。

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

Dubbo就是类十余webservice的关于系统之间通信的框架,并可以统计和管理服务之间的调用情况(包括服务被谁调用了,调用的次数是如何,以及服务的使用状况)

3.2 Dubbo的架构

节点角色说明:

  • Provider:暴露服务的服务提供方
  • Consumer:调用远程服务的服务消费方
  • Registry:服务注册与发现的注册中心
  • Monitor:统计服务的调用次数和调用时间的监控中心
  • Container:服务运行容器

调用关系说明:

  • 0.服务容器负责启动,加载,运行服务提供者。
  • 1.服务提供者在启动时,向注册中心注册自己提供的服务
  • 2.服务消费者在启动时,向注册中心订阅自己所需的服务。
  • 3.注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送给变更数据给消费者
  • 4.服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
  • 5.服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。

3.3使用方法

3.3.1 Spring配置

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

3.4 注册中心

3.4.1 zookeeper的介绍

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

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

在Linux系统中安装好zookeeper

启动:

启动之后一定要查看它的状态,status,出现Mode:standalone才是启动成功。

4.框架整合

4.1添加dubbo的依赖

加入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>

 

4.2整合思路

  1. Dao层:

mybatis整合spring,通过spring管理SqlSessionFactory、mapper代理对象。需要mybatis和spring的整合包,由spring创建数据库连接池。

整合内容

对应工程

Pojo

Taotao-manger-pojo

Mapper映射文件

Taotao-manger-dao

Mapper接口

Taotao-mangaer-dao

SqlMapConfig.xml(全局配置)

Taotao-manager-service

applicationContext-dao.xml(数据库连接池)

Taotao-manager-service

 

  1. Service层:

所有的实现类都放到spring容器中管理。并有spring管理事务;发布dubbo服务

整合内容

对应工程

Service接口

Taotao-mangaer-interface

Service实现类

Taotao-mangaer-service

applicationContext-service.xml

Taotao-manager-service

applicationContext-trans.xml

Taotao-manager-service

 

web.xml 配置:配置加载spring容器

 

  1. 表现层:

Springmvc整合spring框架,由springmvc管理controller;引入dubbo服务

整合内容

对应工程

springmvc.xml

Taotao-manager-web

Controller

Taotao-manager-web

 

web.xml 的配置:前端控制器的配置,配置URL拦截形式。

 

4.3 Dao整合

整合后的目录结构如下:后面的配置文件 可以参考此图片创建目录

 

 

1. 创建SqlMapConfig.xml配置文件

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE configuration

          PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

          "http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>

 

</configuration>

 

2. Spring整合mybatis

创建applicationContext-dao.xml

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

     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://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">

 

     <!-- 数据库连接池 -->

     <!-- 加载配置文件 -->

     <context:property-placeholder location="classpath:properties/*.properties" />

     <!-- 数据库连接池 -->

     <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"

          destroy-method="close">

          <property name="url" value="${jdbc.url}" />

          <property name="username" value="${jdbc.username}" />

          <property name="password" value="${jdbc.password}" />

          <property name="driverClassName" value="${jdbc.driver}" />

          <property name="maxActive" value="10" />

          <property name="minIdle" value="5" />

     </bean>

     <!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 -->

     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

          <!-- 数据库连接池 -->

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

          <!-- 加载mybatis的全局配置文件 -->

          <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />

     </bean>

     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

          <property name="basePackage" value="com.taotao.mapper" />

     </bean>

</beans>

db.properties

jdbc.driver=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://localhost:3306/taotao?characterEncoding=utf-8

jdbc.username=root

jdbc.password=itcast

 

备注:

Druid是目前最好的数据库连接池,在功能、性能、扩展性方面,都超过其他数据库连接池,包括DBCP、C3P0、BoneCP、Proxool、JBoss DataSource。

Druid已经在阿里巴巴部署了超过600个应用,经过多年多生产环境大规模部署的严苛考验。

 

4.4 Service整合

  1. 管理Service

applicationContext-service.xml:

<?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.128:2181" />

     <!-- dubbo协议在20880端口暴露服务 -->

     <dubbo:protocol name="dubbo" port="20880" />

     <!-- 声明需要暴露的服务接口 -->

     <!--<dubbo:service interface="com.taotao.service.TestService" ref="testServiceImpl" />-->

 

</beans>

 

 

2. 事务管理

创建applicationContext-trans.xml

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

     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://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">

     <!-- 事务管理器 -->

     <bean id="transactionManager"

          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

          <!-- 数据源 -->

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

     </bean>

     <!-- 通知 -->

     <tx:advice id="txAdvice" transaction-manager="transactionManager">

          <tx:attributes>

               <!-- 传播行为 -->

               <tx:method name="save*" propagation="REQUIRED" />

               <tx:method name="insert*" propagation="REQUIRED" />

               <tx:method name="add*" propagation="REQUIRED" />

               <tx:method name="create*" propagation="REQUIRED" />

               <tx:method name="delete*" propagation="REQUIRED" />

               <tx:method name="update*" propagation="REQUIRED" />

               <tx:method name="find*" propagation="SUPPORTS" read-only="true" />

               <tx:method name="select*" propagation="SUPPORTS" read-only="true" />

               <tx:method name="get*" propagation="SUPPORTS" read-only="true" />

          </tx:attributes>

     </tx:advice>

     <!-- 切面 -->

     <aop:config>

          <aop:advisor advice-ref="txAdvice"

               pointcut="execution(* com.taotao.service.*.*(..))" />

     </aop:config>

</beans>

 

3. Web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

      xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

      id="WebApp_ID" version="2.5">

      <display-name>taotao-manager</display-name>

      <!-- 加载spring容器 -->

      <context-param>

            <param-name>contextConfigLocation</param-name>

            <param-value>classpath:spring/applicationContext-*.xml</param-value>

      </context-param>

      <listener>

            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

      </listener>

</web-app>

 

4.5 表现层整合

整合后的效果如下:

 

 

1. Springmvc.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: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.128:2181"/>  

     <!--<dubbo:reference interface="com.taotao.service.TestService" id="testService" />-->

</beans>

 

如果没有添加interface的依赖,还需要在表现层工程的POM文件中添加 taotao-manager-interface工程的依赖。

<dependency>

          <groupId>com.taotao</groupId>

          <artifactId>taotao-manager-interface</artifactId>

          <version>0.0.1-SNAPSHOT</version>

     </dependency>

 

2. web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

     xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

     id="WebApp_ID" version="2.5">

     <display-name>taotao-manager-web</display-name>

     <welcome-file-list>

          <welcome-file>login.html</welcome-file>

     </welcome-file-list>

     <!-- 解决post乱码 -->

     <filter>

          <filter-name>CharacterEncodingFilter</filter-name>

          <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

          <init-param>

               <param-name>encoding</param-name>

               <param-value>utf-8</param-value>

          </init-param>

     </filter>

     <filter-mapping>

          <filter-name>CharacterEncodingFilter</filter-name>

          <url-pattern>/*</url-pattern>

     </filter-mapping>

 

 

     <!-- springmvc的前端控制器 -->

     <servlet>

          <servlet-name>taotao-manager-web</servlet-name>

          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

          <!-- contextConfigLocation不是必须的, 如果不配置contextConfigLocation, springmvc的配置文件默认在:WEB-INF/servlet的name+"-servlet.xml" -->

          <init-param>

               <param-name>contextConfigLocation</param-name>

               <param-value>classpath:spring/springmvc.xml</param-value>

          </init-param>

          <load-on-startup>1</load-on-startup>

     </servlet>

     <servlet-mapping>

          <servlet-name>taotao-manager-web</servlet-name>

          <url-pattern>/</url-pattern>

     </servlet-mapping>

</web-app>

5. dubbo配置测试

服务层开发:

1.编写TestMapper接口

2.编写TestMapper.xml

3.编写TestService接口

4.编写TestServiceImpl实现类

5.发布服务

表现层开发:

1.编写TestController

2.引用服务

测试:

安装taotao-manager

运行。

测试报错:

解决mapper映射文件不发布的问题

在taotao-manager-dao工程中的pom文件中添加如下内容:

<build>

          <!-- 如果不配置mybatis的配置文件会漏掉 -->

          <!-- 注意:配置了此方式,原来的默认的资源拷贝行为将无效 -->

          <resources>

               <resource>

                    <directory>src/main/java</directory>

                    <includes>

                         <include>**/*.properties</include>

                         <include>**/*.xml</include>

                    </includes>

                    <filtering>false</filtering>

               </resource>

          </resources>

     </build>

改好后需要选择taotao-manager-dao工程执行

run as maven install

测试效果:

使用tomcat插件启动服务层工程,启动表现层工程

 

6 Mybatis逆向工程

执行逆向工程

使用官方网站的mapper自动生成工具mybatis-generator-core-1.3.2来生成po类和mapper映射文件。

如图逆向工程导入到工程中,修改配置文件,生成POJO 及Mapper及映射文件。

 

 

注意:因为涉及到各个工程(系统)来回传递对象,所以使用时需要对涉及到的POJO实现序列化接口。

    1. 功能分析
      1. 整合静态页面展示首页

静态页面位置:如图:

 

使用方法:

把静态页面添加到taotao-manager-web工程中的WEB-INF下:

由于在web.xml中定义的url拦截形式为“/”表示拦截所有的url请求,包括静态资源例如css、js等。所以需要在springmvc.xml中添加资源映射标签:

 

     <mvc:resources location="/WEB-INF/js/" mapping="/js/**"/>

     <mvc:resources location="/WEB-INF/css/" mapping="/css/**"/>

 

 

分析:

首页的请求地址:/

请求参数:无

返回值:index.jsp (首页)

 

Controller

@Controller

public class PageController {

     /**

      * 展示首页

      * @return

      */

     @RequestMapping("/")

     public String showIndex(){

          return "index";

     }

     /**

      * 展示菜单页面

      * @param page

      * @return

      */

     @RequestMapping("/{page}")

     public String showItemList(@PathVariable String page){

          return page;

     }

}

 

 

删掉webapp下的index.jsp。

 

      1. 商品列表页面

 

 

对应的jsp

       item-list.jsp

请求的url

/item/list

请求的参数:

page=1&rows=30

响应的json数据格式:

Easyui中datagrid控件要求的数据格式为:

{total:”2”,rows:[{“id”:”1”,”name”:”张三”},{“id”:”2”,”name”:”李四”}]}

      1. 响应的json数据格式EasyUIDataGridResult

 

创建此类放入taotao-common中

/**

 * 商品列表查询的返回的数据类

 * @title EasyUIDataGridResult.java

 * <p>description</p>

 * <p>company: www.itheima.com</p>

 * @author ljh

 * @version 1.0

 */

public class EasyUIDataGridResult {

     private Integer total;

     private List rows;

     public Integer getTotal() {

          return total;

     }

     public void setTotal(Integer total) {

          this.total = total;

     }

     public List getRows() {

          return rows;

     }

     public void setRows(List rows) {

          this.rows = rows;

     }

    

}

      1. 分页处理

 

       逆向工程生成的代码是不支持分页处理的,如果想进行分页需要自己编写mapper,这样就失去逆向工程的意义了。为了提高开发效率可以使用mybatis的分页插件PageHelper。

 

    1. 分页插件PageHelper
      1. Mybatis分页插件 - PageHelper说明

       如果你也在用Mybatis,建议尝试该分页插件,这个一定是最方便使用的分页插件。该插件目前支持Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库分页。

 

      1. 使用方法

第一步:把PageHelper依赖的jar包添加到工程中。官方提供的代码对逆向工程支持的不好,使用参考资料中的pagehelper-fix。

 

 

 

使用参考资料中的工程 ,将其导入到eclipse 使用maven 安装到本地仓库。

 

第二步:在Mybatis的全局文件中配置SqlMapConfig.xml中配置拦截器插件:

<plugins>

    <!-- com.github.pagehelperPageHelper类所在包名 -->

    <plugin interceptor="com.github.pagehelper.PageHelper">

        <!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库-->       

        <property name="dialect" value="mysql"/>

    </plugin>

</plugins>

 

第三步:在代码中使用

1、设置分页信息:

    //获取第1页,10条内容,默认查询总数count

    PageHelper.startPage(1, 10);

 

    //紧跟着的第一个select方法会被分页

List<Country> list = countryMapper.selectIf(1);

 

2、取分页信息第一种方法

//分页后,实际返回的结果list类型是Page<E>,如果想取出分页信息,需要强制转换为Page<E>

Page<Country> listCountry = (Page<Country>)list;

listCountry.getTotal();

  1. 取分页信息的第二种方法,推荐使用第二种

//获取第1页,10条内容,默认查询总数count

PageHelper.startPage(1, 10);

List<Country> list = countryMapper.selectAll();

//PageInfo对结果进行包装

PageInfo page = new PageInfo(list);

//测试PageInfo全部属性

//PageInfo包含了非常全面的分页属性

assertEquals(1, page.getPageNum());

assertEquals(10, page.getPageSize());

assertEquals(1, page.getStartRow());

assertEquals(10, page.getEndRow());

assertEquals(183, page.getTotal());

assertEquals(19, page.getPages());

assertEquals(1, page.getFirstPage());

assertEquals(8, page.getLastPage());

assertEquals(true, page.isFirstPage());

assertEquals(false, page.isLastPage());

assertEquals(false, page.isHasPreviousPage());

assertEquals(true, page.isHasNextPage());

 

      1. 分页测试

@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());

     }

 

 

    1. Service层

参数:int page ,int rows

业务逻辑:查询所有商品列表,要进行分页处理。

返回值:EasyUIDataGridResult

      1. 接口

 

 

public interface ItemService {

     //查询商品列表

     public EasyUIDatagridResult getItemList(int page,int rows);

}

      1. 实现类

@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. 发布服务

在taotao-manager-service中的applicationContext-service.xml中发布服务:

注意address的值:使用自己的zookeeper所在的系统的ip地址和端口

 

    1. 表现层

在taotao-manager-web工程中的springmvc.xml中引入服务:

注意address的值:使用自己的zookeeper所在的系统的ip地址和端口

 

 

1、初始化表格请求的url:/item/list

2、Datagrid默认请求参数:

  1. page:当前的页码,从1开始。
  2. rows:每页显示的记录数。
  1. 响应的数据:json数据。EasyUIDataGridResult

@RequestMapping("/item/list")

     @ResponseBody

     public EasyUIDataGridResult getItemList(Integer page, Integer rows) {

          EasyUIDataGridResult result = itemService.getItemList(page, rows);

          return result;

     }

 

 

可以设置服务超时时间:

服务调用超时时间默认1秒,如下:在服务层和表现层都可以设置超时时间。

 

    1. 测试:

       需要先启动zookeeper,再启动服务层,再启动表现层。

如果先启动表现层,后启动服务层,会报错,但是不影响使用。

 

为了更方便的进行测试,表现层工程和服务层工程属于不同的工程,要debug的时候需要设置源码,如下:

Debug设置源代码,涉及到工程都要添加,为了方便,可以添加所有的工程。

 

 

 

 

 

 

    1. 解决错误原因

 

错误:

 

说明:反序列化时,使用Page<E>  ,在表现层没有这个CLASS ,在web层需要加入pagehelper的jar包即可。当然可以不管。

 

1.maven命令安装jar包时跳过测试:clean install -DskipTests   

表示先清理再安装,打包时跳过测试。

2.还可以使用跳过测试的插件:maven-sufire-plugin

 

  1. Dubbo监控中心

准备好war包

准备好:tomcat:

 

 

上传到linux系统中:

 

 

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

 

1、部署监控中心:

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

 

  1. 启动tomcat

   cd bin 目录,输入:./startup.sh

   查询tomcat是否已经启动:ps -ef|grep tomcat 

 

  1. 访问http://192.168.25.128:8080/dubbo-admin/

用户名:root

密码:root

 

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

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

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

 

如果要运行监控中心,必须先启动zookeeper. 监控中心对于dubbo的服务的调用来说不是必须的,不安装也可以运行。安装的目的是为了更好的统计其调用的次数,方便管理。

 

 

猜你喜欢

转载自blog.csdn.net/lkp1603645756/article/details/82254387