第二天

一世执手 提交于 2020-04-06 09:29:47

独立工程就是打成一个war包,war包可以部署到不同的服务器中;

聚合工程至少有一个是war包,去除web后manager聚合工程剩余dao、service、pojo、interface,把service改成war包

main下加入webapp/WEB-INF/web.xml

导入移动的web文件,创建另一个maven工程

e3-mall-manager-web的pom.xml需要依赖service的接口了

controller中出出错是因为没有了spring的jar包了,因为之前是controller依赖的是service的jar包,因此表表现层需要添加spring的依赖

两个工程之间需要调用服务,要调用服务,首先要知道服务在哪里

dubble介绍:

manager-web相当于客户端去调用service层这个服务端

container相当于spring

当初始化容器时候,服务就开始发布

要使用时候,都需要加入各自的jar包

dubble的用法:

单一工程用法:

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

单一工程中spring的配置


单一工程中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注册中心。

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

Zookeeper

  1. 可以作为集群的管理工具使用。
  2. 可以集中管理配置文件。

dubble发布服务:

1.把dubble的jar包加入工程中;

2.把dubble、zookeeper的xml配置加入的service的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.使用spring发布服务,在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="cn.e3mall.service"></context:component-scan>

<!-- 使用dubbo发布服务 -->

<!-- 提供方应用信息,用于计算依赖关系 -->

<dubbo:application name="e3-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="cn.e3mall.service.ItemService" ref="itemServiceImpl" />

 

</beans>

要单独创建一个接口模块,用于dubble的引用,否则还得把接口拷至工程,在xml配置即可使用。

在manager-web中应引用了interface模块

dubble引用服务:

<?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.25.154:2181,192.168.25.154:2182,192.168.25.154:2183"/>	
	<dubbo:reference interface="cn.e3mall.service.ItemService" id="itemService" />
	
</beans>

manager与web是两个工程,需要启动两个tomcat,因此在web的pom.xml中要配置tomcat插件

报错,网络传输必须实现序列化接口,因此为pojo中的Iterm要实现Serializable接口,逆向工程都可能需要网络传输,因此最好都去实现Serialiazble接口。

重新安装pojo,重启服务器

debug工程

controller层不能debug

debug下找不到源代码

debugconfig->e3-manager-source

在刷新页面

degug默认是1秒,设置超时时间,超时默认重试3次

设置超时时间:在发布服务的的service的applicationContext.xml配置

 

监控中心monitor

一般提供一个war包

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

安装tomcat,并且把监控中心这个war包复制到tomcat中:

商品列表查询功能实现

准备html、css、jsp

html、css放到webapp下

jsp放到webapp/WEB-INF下

一般访问为先访问到controller,然后在跳转到index.jsp页面

写出一个controller:

拦截url可以为“/”

@Controller
public class PageController{
	@requestMapping("/");
	pulbic String showIndex(){
		return "index";
	}
}

访问不显示index.jsp原因

因为web.xml中有个欢迎页,里面有index.jsp

webapp下的WEB-INF下有个index.jsp,因此不会访问jsp里面的index

需要把外面这个index.app删除

还出现错误因为css被拦截了,因此需要配置资源映射

在springmvc.xml中配置资源映射:

location为静资源路径,

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

<mvc: resources location="/jsp/" mapping="/jsp/**">

重启tomcat,刷新页面

商品展示:

请求的url

请求iterm-list就会响应iterm-listjsp

请求谁,就响应以谁为开头的jsp

page被视图解析器解析完后,就会去找WEB-INF/jsp下的jsp文件,就不用挨个去写了

@Controller
public class PageController{
  @RequestMapping("/{page}");
  public String showINdex(){
      return page;
}
}

分页的使用

创建PageHelper工程

在mybatis中的sqlMapconconfig.xml中配置plugins

service层中测试分页共功能:

public class PageHelperService{
     //获取Mapper代理对象要从spring中获取
     //1.初始化spring容器
     ApplicationContext applicationContext = new 
                  ClassPathXmlApplicationContext("classpath:spring/applicatonContext-dao.xml");
     //2.从容器中获取Mapper代理对象
     TbItermMapper itermMapper = applicatonContext.getBean(TbItermMapper.class);
     //3.执行sql语句之前设置分页信息使用的PagrHelper的starPage方法
      PageHelper .startPage(1,10);
     //4.执行查询
      TbItermExample example = new TbItermExample()'查询条件
      List<TbIterm> list  = itermMapper.selectByExample(example);
     //5.获取分页信息,
      PageInfo pageInfo = new PageInfo(list);
      System.out.ptintln(pageInfo.getTotal());总记录数   
      System.out.ptintln(pageINfo.getPages);总页数
      System.out.ptintln(list.size());每页的大小




}

查询商品信息

查询商品信息,返回json数据,一般都是通过一个java对象转换而来,写一个pojo,然后响应pojo会自动转换为json

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

包含total、rows的pojo没有,描述商品列表的pojo有

在conmon中创建包含total、rows的pojo,要实现网路通信,需要实现序列化:

public class EasyUIDataGridResult implements Serializable{
  private long total;
  private list rows;
 
  生成get/set方法


}

dao层不用配置

service层:

接口

public Interface ItermService{
  TbIterm getItermByID(long iterm);
  EasyUIDataResult getItermKist(int page,int rows);

}

实现类:

@override
public EasyUiDataResult gteItermList(int page,int rows){
  //设置分页信息
  PageHelper.startPage(page,rows);
  //执行查询结果
  TbItermExample example = new TbItermExample();
  itermMapper.selectByExample(example);
  //创建一个返回值对象
  EasyUiDataResult result = new EasyUiDataResult();
  result.setRows(list);
  //取分页结果
  PageInfo<TbIterm> pageInfo = new PageInfo<>(list);
   //获取总记录数
  long total = pageInfo.getTotal();
  result.setTotal(total);
  return resultl;
}

表现层:

请求参数:/iterm/List

要相应json数据,需要使用responseBody,返回的对象会自动转换为json数据

controller层中


@RequestMapping("/iterm/List");
@ReponseBody
public EasyUIDataResult getItermList(Integer page,Integer rows){
  //调用服务,查询商品类表
  EasyUIDataResult result = itermService.getItermList(page,rows);
  rerurn result;
}

service中新增方法要重新安装一下

 

 

 

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!