前面省略,直奔主题
3.Dubbox框架
3.1 Dubbox简介
Dubbox 是一个分布式服务框架,其前身是阿里巴巴开源项目Dubbo ,被国内电商及互联网项目中使用,后期阿里巴巴停止了该项目的维护,当当网便在Dubbo基础上进行优化,并继续维护,为了与原有的Dubbo区分,故将其命名为Dubbox。
Dubbox 致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案。简单的说,dubbox就是个服务框架,如果没有分布式的需求,其实是不需要用的,只有在分布式的时候,才有dubbox这样的分布式服务框架的需求,并且本质上是个服务调用的东东,说白了就是个远程服务调用的分布式框架。
节点角色说明:
· Provider: 暴露服务的服务提供方。
· Consumer: 调用远程服务的服务消费方。
· Registry: 服务注册与发现的注册中心。
· Monitor: 统计服务的调用次数和调用时间的监控中心。
· Container: 服务运行容器。
调用关系说明:
· 0. 服务容器负责启动,加载,运行服务提供者。
· 1. 服务提供者在启动时,向注册中心注册自己提供的服务。
· 2. 服务消费者在启动时,向注册中心订阅自己所需的服务。
· 3. 注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推
送变更数据给消费者。
· 4. 服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,
如果调用失败,再选另一台调用。
· 5. 服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计
数据到监控中心。
配置linux镜像:安装VM后双击此文件挂起品优购服务器
导入的虚拟机必须设置 ip 为 192.168.25.168 才可以使用 FastDFS(后面的课程会
用);如果虚拟机不是 25 网段,需要手动设置网段,
打开虚拟机
帐号:root,密码:itcast
3.2注册中心Zookeeper
3.2.1 Zookeeper 介绍
官方推荐使用 zookeeper 注册中心。注册中心负责服务地址的注册与查找,相当于目录服务,服务提供者和消费者只在启动时与注册中心交互,注册中心不转发请求,压力较小。
Zookeeper 是 Apacahe Hadoop 的子项目,是一个树型的目录服务,支持变更推送,适合作为Dubbox 服务的注册中心,工业强度较高,可用于生产环境。
3.2.2 Zookeeper 在Linux系统的安装
安装步骤:
第一步:安装 jdk(此步省略,我给大家提供的镜像已经安装好JDK)
第二步:把 zookeeper 的压缩包(资源\配套软件\dubbox\zookeeper-3.4.6.tar.gz)上传到 linux 系统。
第三步:解压缩压缩包
tar -zxvf zookeeper-3.4.6.tar.gz |
第四步:进入 zookeeper-3.4.6 目录,创建 data 文件夹,放zookeeper注册数据。
第五步:进入conf目录 ,把 zoo_sample.cfg 改名为 zoo.cfg
cd conf mv zoo_sample.cfg zoo.cfg |
第六步:打开zoo.cfg , 修改 data 属性:dataDir=/root/zookeeper-3.4.6/data
vim zoo.cfg
点击i 进入插入模式
点击ESC 然后 :wq保存并退出
3.2.3 Zookeeper 服务启动
进入bin目录,启动服务输入命令
cd ..
cd bin
输出以下内容表示启动成功
关闭服务输入命令
输出以下提示信息
查看状态:
如果启动状态,提示
如果未启动状态,提示:
3.5入门小Demo
3.5.1服务提供者开发
开发步骤:
(1)创建Maven工程(WAR)dubboxdemo-service ,在pom.xml中引入依赖
Pom配置文件
<!-- 集中定义依赖版本号 --> <properties> <junit.version>4.12</junit.version> <spring.version>4.3.10.RELEASE</spring.version> <spring.security.version>4.2.3.RELEASE</spring.security.version> <mybatis.version>3.4.5</mybatis.version> <mybatis.spring.version>1.3.1</mybatis.spring.version> <mysql.version>5.1.32</mysql.version> <druid.version>1.1.5</druid.version> <pagehelper.version>4.1.4</pagehelper.version> <mapper.version>3.5.0</mapper.version> <dubbo.version>2.6.0</dubbo.version> <slf4j.version>1.7.12</slf4j.version> <javax.persistence.version>1.0</javax.persistence.version> <fastjson.version>1.2.44</fastjson.version> <zookeeper.version>3.4.9</zookeeper.version> <zkclient.version>0.1</zkclient.version> <jedis.version>2.9.0</jedis.version> <spring.data.redis.version>1.8.9.RELEASE</spring.data.redis.version> <servlet.api.version>2.5</servlet.api.version> <commons.fileupload.version>1.3.1</commons.fileupload.version> <fastdfs.client.version>1.25</fastdfs.client.version> <freemarker.version>2.3.23</freemarker.version> <activemq.version>5.14.3</activemq.version> <cas.client.core.version>3.4.1</cas.client.core.version> <spring.data.solr.version>1.5.5.RELEASE</spring.data.solr.version> <httpclient.version>4.5.3</httpclient.version> <wxpay.sdk.version>0.0.3</wxpay.sdk.version> <httpcore.version>4.4.8</httpcore.version> <javassist.version>3.12.1.GA</javassist.version> <commons.codec.version>1.11</commons.codec.version> <dom4j.version>1.6.1</dom4j.version> <xml.apis.version>2.0.2</xml.apis.version> <kaptcha.version>2.3.2</kaptcha.version> </properties>
<dependencyManagement> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> </dependency>
<!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency>
<!-- Mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>${mybatis.spring.version}</version> </dependency>
<!-- 日志 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> </dependency>
<!-- dubbo相关 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>${dubbo.version}</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>${zookeeper.version}</version> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>${zkclient.version}</version> </dependency>
<!-- json的解析 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>${fastjson.version}</version> </dependency>
<dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>${servlet.api.version}</version> <scope>provided</scope> </dependency>
<!-- mybaits分页助手 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>${pagehelper.version}</version> </dependency>
<!-- 通用 Mapper --> <!-- https://mvnrepository.com/artifact/tk.mybatis/mapper --> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper</artifactId> <version>3.2.1</version> </dependency>
<!-- MySql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <!-- 连接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>${druid.version}</version> </dependency>
<!-- https://mvnrepository.com/artifact/javax.persistence/persistence-api --> <dependency> <groupId>javax.persistence</groupId> <artifactId>persistence-api</artifactId> <version>1.0</version> </dependency>
<!-- redis缓存 --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>${spring.data.redis.version}</version> </dependency>
<!-- 文件上传组件 --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>${commons.fileupload.version}</version> </dependency>
<!--fastDFS客户端工具类包 --> <dependency> <groupId>com.github.tobato</groupId> <artifactId>fastdfs-client</artifactId> <version>1.26.6</version> </dependency>
<!--freemarker--> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>${freemarker.version}</version> </dependency>
<!-- ActiveMQ --> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-client</artifactId> <version>${activemq.version}</version> </dependency> <!-- 身份验证 --> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-web</artifactId> <version>${spring.security.version}</version> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-config</artifactId> <version>${spring.security.version}</version> </dependency>
<!-- cas --> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-cas</artifactId> <version>${spring.security.version}</version> </dependency> <dependency> <groupId>org.jasig.cas.client</groupId> <artifactId>cas-client-core</artifactId> <version>${cas.client.core.version}</version> <!-- 排除 log4j包冲突 --> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>log4j-over-slf4j</artifactId> </exclusion> </exclusions> </dependency>
<!-- solr相关 --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-solr</artifactId> <version>${spring.data.solr.version}</version> </dependency>
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>${httpclient.version}</version> </dependency>
<!-- 微信支付 sdk --> <dependency> <groupId>com.github.wxpay</groupId> <artifactId>wxpay-sdk</artifactId> <version>${wxpay.sdk.version}</version> </dependency>
<!-- 验证码工具类包 --> <dependency> <groupId>com.github.penggle</groupId> <artifactId>kaptcha</artifactId> <version>${kaptcha.version}</version> <exclusions> <exclusion> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> </exclusion> </exclusions> </dependency>
<!-- 加密解密 --> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>${commons.codec.version}</version> </dependency>
<dependency> <groupId>javassist</groupId> <artifactId>javassist</artifactId> <version>${javassist.version}</version> </dependency>
<dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> <version>${dom4j.version}</version> </dependency>
<dependency> <groupId>xml-apis</groupId> <artifactId>xml-apis</artifactId> <version>${xml.apis.version}</version> </dependency> </dependencies>
</dependencyManagement>
<build> <plugins> <!-- java编译插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.6.0</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins>
<pluginManagement> <plugins> <!-- tomcat插件 --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> </plugin> </plugins> </pluginManagement> </build> |
(2)在工程的webapps下创建WEB-INF文件夹,创建web.xml
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0" metadata-complete="false">
<!-- 加载spring容器 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
</web-app> |
添加: |
(3)创建业务接口
创建包cn.gdut.dubbodemo.service,用于存放业务接口,创建接口
package cn.gdut.dubbodemo.service;
public interface UserService { public String getName(); } |
(4)创建业务实现类
创建包cn.gdut.dubbodemo.service.impl,用于存放业务实现类。创建业务实现类:
package cn.gdut.dubbodemo.service.impl;
import cn.gdut.dubbodemo.service.UserService; import com.alibaba.dubbo.config.annotation.Service;
@Service public class UserServiceImpl implements UserService { public String getName() { return "itcast"; } } |
注意:Service注解与原来不同,需要引入com.alibaba包下的,不然服务不能注册到注册中心
(5)编写配置文件
在src/main/resources下创建applicationContext-service.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.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.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.xsd">
<dubbo:application name="dubbodemo-service"/> <dubbo:registry address="zookeeper://192.168.25.131:2181"/> <dubbo:annotation package="cn.gdut.dubbodemo.service.impl" /> </beans> |
注意:dubbo:annotation用于扫描@Service注解。
(6)测试运行
3.5.2服务消费者开发
开发步骤:
(1)创建Maven工程(WAR)dubboWeb ,在pom.xml引入依赖 ,同“dubboService”工程。区别就是把tomcat插件的运行端口改为8082 。
(2)在webapps目录下创建WEB-INF 目录,并创建web.xml
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0" metadata-complete="false">
<!-- 解决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> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
<servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 指定加载的配置文件 ,通过参数contextConfigLocation加载--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext-web.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
</web-app> |
(3)拷贝业务接口
将“dubboService”工程的cn.gdut.dubbodemo.service 包以及下面的接口拷贝至此工程。
注意实现类 impl包不要拷贝
(4)编写Controller
package cn.gdut.dubbodemo.controller;
import cn.gdut.dubbodemo.service.UserService; import com.alibaba.dubbo.config.annotation.Reference; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody;
@Controller @RequestMapping("/user") public class UserController {
@Reference private UserService userService;
@RequestMapping("/showName") @ResponseBody public String showName(){ return userService.getName(); } } |
(5)编写spring配置文件
在src/main/resources下创建applicationContext-web.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.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.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.xsd">
<!--SpringMvc的转换器,可以把返回值转换成字符串--> <mvc:annotation-driven > <mvc:message-converters register-defaults="false"> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <constructor-arg value="UTF-8" /> </bean> </mvc:message-converters> </mvc:annotation-driven>
<!-- 引用dubbo 服务 --> <dubbo:application name="dubboxdemo-web" /> <dubbo:registry address="zookeeper://192.168.25.131:2181"/> <dubbo:annotation package="cn.gdut.dubbodemo.controller" /> </beans> |
(6)测试运行
在浏览器输入http://localhost:8082/user/showName.do,查看浏览器输出结果
3.6管理中心的部署
我们在开发时,需要知道注册中心都注册了哪些服务,以便我们开发和测试。我们可以通过部署一个管理中心来实现。其实管理中心就是一个web应用,部署到tomcat即可。
3.6.1管理端安装
(1)编译源码,得到war包
给大家下发的资源中有个dubbox-master.zip ,这个是dubbox的源码,我们可以使用maven命令编译源码得到“管理端”的war包
将此压缩包解压,在命令符下进入dubbo-admin目录 ,输入maven命令
mvn package -Dmaven.skip.test=true |
|
如果你看到如下信息,就说明成功了
(2)进入target文件夹,你会看到一个dubbo-admin-2.8.4.war ,改名为dubbo-admin.war 把它和tomcat传到linux里面:
3.在linux服务器上解压tomcat安装包, 将此dubbo-admin.war 移动到linux服务器的tomcat的webapps下。启动tomcat后会自动解压dubbo-admin.war 。
把dubbo-admin.war移动到webapps下
启动tomcat
(4)如果你部署在zookeeper同一台主机并且端口是默认的2181,则无需修改任何配置。如果不是在一台主机上或端口被修改,需要修改WEB-INF下的dubbo.properties ,修改如下配置:
dubbo.registry.address=zookeeper://127.0.0.1:2181 |
修改后重新启动tomcat
3.6.2管理端使用
(1)打开浏览器,输入http://192.168.25.131:8080/dubbo-admin/ ,登录用户名和密码均为root 进入首页。 (192.168.25.131)是我部署的linux主机地址。
(3)启动服务提供者和消费者工程,可以观察提供者和“消费者”列表
4.品优购-框架搭建
4.1工程结构分析与设计
工程说明:
pinyougou-parent 聚合工程
pinyougou-pojo 通用实体类层
pinyougou-dao 通用数据访问层
pinyougou-xxxxx-interface 某服务层接口
pinyougou-xxxxx-service 某服务层实现
pinyougou-xxxxx-web 某web工程
4.2创建数据库表
执行资源文件夹中pinyougou-db.sql
IDEA 开发环境设置
先创建一个空的工程用来组织其它模块
4.3搭建框架
最终完整的工程结构如下:
4.3.1父工程
品优购聚合父工程;包括后续开发的所有品优购聚合子工程。主要功能是进行依赖、插件统一管理。
创建Maven工程pinyougou-parent (POM) ,groupId 为com.pinyougou ,artifactId 为pinyougou-parent , 在pom.xml中添加锁定版本信息dependencyManagement与pluginManagement,详见“资源/配置文件/第一天搭建/父工程/pom.xml”。
以下模块均继承自此父工程
添加依赖(此处的依赖用于集中的定义版本号,后续的子模块不用在定义版本号。目的是统一所有模块依赖的版本号,管理起来比较方便)下面链接有详细的介绍
https://www.cnblogs.com/feibazhf/p/7886617.html
4.3.2通用实体类模块
实体类工程;统一放置品优购的所有数据库实体类或者其它模型
创建通用实体类模块pinyougou-pojo
加依赖
<!--jar包--> <packaging>jar</packaging>
<dependencies> <!--JPA支持--> <dependency> <groupId>javax.persistence</groupId> <artifactId>persistence-api</artifactId> </dependency>
<!--依赖common--> <dependency> <groupId>com.pinyougou</groupId> <artifactId>pinyougou-common</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <!--spring-data-solr--> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-solr</artifactId> <!--编译期需要--> <scope>provided</scope> </dependency> </dependencies> |
4.3.3通用数据访问模块
创建通用数据访问模块pinyougou-dao.添加Mybatis和pinyougou-pojo依赖 创建步骤和上面一样
<!--jar包--> <packaging>jar</packaging>
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> </properties>
<dependencies> <!--依赖Pojo--> <dependency> <artifactId>pinyougou-model</artifactId> <groupId>com.pinyougou</groupId> <version>1.0-SNAPSHOT</version> </dependency> <!--spring相关--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> </dependency> <!-- MySql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!--数据连接池--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> </dependency> <!-- mybatis-spring 整合 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> </dependency> <!--通用Mapper--> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper</artifactId> </dependency> <!--mybatis--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> </dependency> <!--分页工具包--> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> </dependency> <!-- log start --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </dependency> <!-- log end --> </dependencies> |
将学习资料中的:“配置文件/第一天搭建/数据访问层工程”下的配置文件拷贝到pinyougou-dao工程
4.3.4通用工具类模块
创建通用工具类模块pinyougou-common
Pom
<?xml version="1.0" encoding="UTF-8"?> <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>pinyougou-parent</artifactId> <groupId>com.pinyougou</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion>
<artifactId>pinyougou-common</artifactId> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.6.0</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <verbose>true</verbose> <overwrite>false</overwrite> </configuration> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.21</version> </dependency>
<!--通用Mapper--> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper</artifactId> <version>${mapper.version}</version> </dependency> </dependencies> </plugin> </plugins> </build>
<dependencies> <!--fastDFS--> <dependency> <groupId>org.csource.fastdfs</groupId> <artifactId>fastdfs</artifactId> </dependency> <!-- 缓存 --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> </dependency>
<!--spring-data-redis--> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> </dependency>
<!--servlet--> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <scope>provided</scope> </dependency> <!--微信支付--> <dependency> <groupId>com.github.wxpay</groupId> <artifactId>wxpay-sdk</artifactId> <version>0.0.3</version> </dependency>
<!--httpclient支持--> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.3</version> </dependency> <dependency> <groupId>javax.persistence</groupId> <artifactId>persistence-api</artifactId> </dependency> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper</artifactId> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> </dependency>
</dependencies>
</project> |
逆向工程配置文件generatorConfig.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration> <context id="testTables" targetRuntime="MyBatis3"> <!-- 生成的pojo,将implements Serializable--> <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>
<!--通用Mapper所需参数--> <plugin type="tk.mybatis.mapper.generator.MapperPlugin"> <property name="mappers" value="tk.mybatis.mapper.common.Mapper"/> <property name="forceAnnotation" value="true"/> </plugin>
<commentGenerator> <!-- 是否去除自动生成的注释 true:是 : false:否 --> <property name="suppressAllComments" value="true"/> </commentGenerator> <!--数据库连接的信息:驱动类、连接地址、用户名、密码 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3381/pinyougoudb" userId="root" password="1234"> </jdbcConnection> <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal --> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver>
<!-- targetProject:生成PO类的位置 --> <javaModelGenerator targetPackage="com.pinyougou.model" targetProject=".\src\main\java"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="false"/> <!-- 从数据库返回的值被清理前后的空格 --> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- targetProject:mapper映射文件生成的位置 --> <sqlMapGenerator targetPackage="com.pinyougou.mapper" targetProject=".\src\main\java"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="false"/> </sqlMapGenerator> <!-- targetPackage:mapper接口生成的位置 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.pinyougou.mapper" targetProject=".\src\main\java"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="false"/> </javaClientGenerator>
<!-- 指定数据库表 --> <table domainObjectName="Address" tableName="tb_address"></table> <table domainObjectName="Areas" tableName="tb_areas"></table> <table domainObjectName="Brand" tableName="tb_brand"></table> <table domainObjectName="Cities" tableName="tb_cities"></table> <table domainObjectName="Content" tableName="tb_content"></table> <table domainObjectName="ContentCategory" tableName="tb_content_category"></table> <table domainObjectName="FreightTemplate" tableName="tb_freight_template"></table> <table domainObjectName="Goods" tableName="tb_goods"></table> <table domainObjectName="GoodsDesc" tableName="tb_goods_desc"></table> <table domainObjectName="Item" tableName="tb_item"></table> <table domainObjectName="ItemCat" tableName="tb_item_cat"></table> <table domainObjectName="Order" tableName="tb_order"></table> <table domainObjectName="OrderItem" tableName="tb_order_item"></table> <table domainObjectName="PayLog" tableName="tb_pay_log"></table> <table domainObjectName="Provinces" tableName="tb_provinces"></table> <table domainObjectName="SeckillGoods" tableName="tb_seckill_goods"></table> <table domainObjectName="SeckillOrder" tableName="tb_seckill_order"></table> <table domainObjectName="Seller" tableName="tb_seller"></table> <table domainObjectName="Specification" tableName="tb_specification"></table> <table domainObjectName="SpecificationOption" tableName="tb_specification_option"></table> <table domainObjectName="TypeTemplate" tableName="tb_type_template"></table> <table domainObjectName="User" tableName="tb_user"></table> </context> </generatorConfiguration> |
点击生成pojo和mapper备用
4.3.5商家商品服务接口模块
创建maven(jar)模块pinyougou-sellergoods-interface , pom.xml添加依赖
<?xml version="1.0" encoding="UTF-8"?> <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>pinyougou-parent</artifactId> <groupId>com.pinyougou</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion>
<artifactId>pinyougou-sellergoods-interface</artifactId>
<!--打jar包--> <packaging>jar</packaging>
<dependencies> <!--依赖model--> <dependency> <artifactId>pinyougou-model</artifactId> <groupId>com.pinyougou</groupId> <version>1.0-SNAPSHOT</version> </dependency>
<!--分页工具包--> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> </dependency> </dependencies>
</project> |
4.3.6商家商品服务模块
创建模块pinyougou-sellergoods-service ,
创建成功后将此普通的Java工程转换为web工程,需要安装JBLJavaToWeb插件,见下面链接
https://blog.csdn.net/qq_33442160/article/details/81876257
安装好后进行如下操作
pom.xml引入依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>pinyougou-parent</artifactId> <groupId>com.pinyougou</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>pinyougou-sellergoods-service</artifactId> <!--war包--> <packaging>war</packaging> <dependencies> <!--依赖Mapper--> <dependency> <artifactId>pinyougou-mapper</artifactId> <groupId>com.pinyougou</groupId> <version>1.0-SNAPSHOT</version> </dependency> <!--依赖interface--> <dependency> <artifactId>pinyougou-sellergoods-interface</artifactId> <groupId>com.pinyougou</groupId> <version>1.0-SNAPSHOT</version> </dependency> <!-- spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> </dependency> <!-- Apache工具组件 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> </dependency> <!--dubbo--> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <exclusions> <exclusion> <artifactId>slf4j-api</artifactId> <groupId>org.slf4j</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <exclusions> <exclusion> <artifactId>zookeeper</artifactId> <groupId>org.apache.zookeeper</groupId> </exclusion> <exclusion> <artifactId>slf4j-api</artifactId> <groupId>org.slf4j</groupId> </exclusion> </exclusions> </dependency> <!-- fastjson处理工具包 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> </dependencies> <!--<pluginRepositories>--> <!--<pluginRepository>--> <!--<id>alfresco-public</id>--> <!--<url>https://artifacts.alfresco.com/nexus/content/groups/public</url>--> <!--</pluginRepository>--> <!--<pluginRepository>--> <!--<id>alfresco-public-snapshots</id>--> <!--<url>https://artifacts.alfresco.com/nexus/content/groups/public-snapshots</url>--> <!--<snapshots>--> <!--<enabled>true</enabled>--> <!--<updatePolicy>daily</updatePolicy>--> <!--</snapshots>--> <!--</pluginRepository>--> <!--<pluginRepository>--> <!--<id>beardedgeeks-releases</id>--> <!--<url>http://beardedgeeks.googlecode.com/svn/repository/releases</url>--> <!--</pluginRepository>--> <!--</pluginRepositories>--> <build> <plugins> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <port>18081</port> <path>/</path> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.6.1</version> <configuration> <source>6</source> <target>6</target> </configuration> </plugin> <!--<plugin>--> <!--<groupId>org.apache.tomcat.maven</groupId>--> <!--<artifactId>tomcat8-maven-plugin</artifactId>--> <!--<version>3.0-r1655215</version>--> <!--<configuration>--> <!--<port>18081</port>--> <!--<path>/</path>--> <!--</configuration>--> <!--</plugin>--> </plugins> </build> </project> |
在webapps下创建WEB-INF/web.xml ,加载spring容器
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<!--spring配置文件 改为xml配置--> <!--<context-param>--> <!--<param-name>contextConfigLocation</param-name>--> <!--<param-value>classpath:spring/spring-annoation.xml</param-value>--> <!--</context-param>-->
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring.xml</param-value> </context-param>
<!--配置监听--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
</web-app> |
创建包com.pinyougou.sellergoods.service.impl
在src/main/resources下创建spring.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 事务传播特性配置 --> <tx:advice id="txAdvice" transaction-manager="txManager"> <!-- the transactional semantics... --> <tx:attributes> <tx:method name="add*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="java.lang.Exception"/> <tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="java.lang.Exception"/> <tx:method name="insert*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="java.lang.Exception"/> <tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="java.lang.Exception"/> <tx:method name="modify*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="java.lang.Exception"/> <tx:method name="delete*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="java.lang.Exception"/>
<!-- 查询方法 --> <tx:method name="query*" read-only="true"/> <tx:method name="select*" read-only="true"/> <tx:method name="find*" read-only="true"/> </tx:attributes> </tx:advice>
<!-- 配置事务管理器 --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean>
<!-- 开启AOP注解 --> <aop:aspectj-autoproxy/>
<!-- 声明式事务AOP配置 --> <aop:config> <!--配置AOP切点--> <aop:pointcut expression="execution(* com.pinyougou.sellergoods.service.impl.*.*(..))" id="tranpointcut"/> <!-- 事务控制 --> <aop:advisor advice-ref="txAdvice" pointcut-ref="tranpointcut"/> </aop:config>
<!--引入外部配置--> <import resource="spring-dubbo.xml" /> <import resource="spring-mybatis.xml"/> <import resource="spring-annoation.xml"/>
</beans> |
spring-annoation.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 配置事务管理器 --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean>
<!--开启声明式注解驱动--> <tx:annotation-driven transaction-manager="txManager"></tx:annotation-driven>
<!--引入外部配置--> <import resource="spring-mybatis.xml"/>
</beans> |
spring-dubbo.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!--4、注册应用名字--> <dubbo:application name="pinyougou-sellergoods-service"/>
<!--1、指定注册地址--> <dubbo:registry address="zookeeper://192.168.25.135:2181"/>
<!--2、暴露外部访问接口的端口和协议--> <dubbo:protocol name="dubbo" port="20884"/>
<!--3、Dubbo包扫描--> <dubbo:annotation package="com.pinyougou.sellergoods.service.impl"/>
</beans> |
Dubbo的配置可以参考下面
4.3.7运营商管理后台
创建maven(war)模块pinyougou-manager-web ,
pom.xml引入依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>pinyougou-parent</artifactId> <groupId>com.pinyougou</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>pinyougou-manager-web</artifactId> <!--war包--> <packaging>war</packaging> <dependencies> <!--依赖interface--> <dependency> <artifactId>pinyougou-sellergoods-interface</artifactId> <groupId>com.pinyougou</groupId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>com.pinyougou</groupId> <artifactId>pinyougou-common</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <!--搜索服务--> <!-- SpringMVC --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> </dependency> <!-- spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> </dependency> <!-- Apache工具组件 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> </dependency> <!--FastJSON--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> </dependency> <!--文件上传组件--> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.3.12.RELEASE</version> </dependency> <!--ActiveMQ--> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-client</artifactId> </dependency> <!--dubbo--> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <exclusions> <exclusion> <artifactId>slf4j-api</artifactId> <groupId>org.slf4j</groupId> </exclusion> </exclusions> </dependency> <!-- Test dependencies --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <exclusions> <exclusion> <artifactId>zookeeper</artifactId> <groupId>org.apache.zookeeper</groupId> </exclusion> <exclusion> <artifactId>slf4j-api</artifactId> <groupId>org.slf4j</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-web</artifactId> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-config</artifactId> </dependency>
<dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <port>18082</port> <path>/</path> </configuration> </plugin> </plugins> </build> </project> |
在webapps下创建WEB-INF/web.xml ,加载spring容器
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<!-- 编码过滤器 --> <filter> <filter-name>characterEncoding</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>
<init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter>
<filter-mapping> <filter-name>characterEncoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--//End 编码过滤器 -->
<!-- SpringMVC --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<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>springmvc</servlet-name> <url-pattern>*.shtml</url-pattern> </servlet-mapping> <!-- //End SpringMVC --> <!--指定spring配置文件--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/*.xml</param-value> </context-param>
<!--spring监听--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
<!--委托过滤器:springSecurityFilterChain--> <filter> <filter-name>springSecurityFilterChain</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> </filter>
<filter-mapping> <filter-name>springSecurityFilterChain</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <mime-mapping> <extension>htm</extension> <mime-type>text/html;charset=utf-8</mime-type> </mime-mapping> <mime-mapping> <extension>html</extension> <mime-type>text/html;charset=utf-8</mime-type> </mime-mapping> </web-app> |
创建包com.pinyougou.manager.controller
在src/main/resources下创建spring/springmvc.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!--dubbo配置--> <!--注册应用--> <dubbo:application name="pinyougou-manager-web"/>
<!--设置客户端请求超时时间--> <dubbo:consumer timeout="30000"/>
<!--注册中心--> <dubbo:registry address="zookeeper://192.168.25.135:2181"/>
<!--包扫描--> <dubbo:annotation package="com.pinyougou.manager.controller"/>
<!--注解驱动--> <mvc:annotation-driven> <!--使用FastJSON--> <mvc:message-converters> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <constructor-arg value="UTF-8"/> </bean> <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> <value>application/json;charset=UTF-8</value> </list> </property> <property name="fastJsonConfig"> <bean class="com.alibaba.fastjson.support.config.FastJsonConfig"> <property name="serializerFeatures"> <array> <value>WriteMapNullValue</value> <value>WriteNullStringAsEmpty</value> </array> </property> <property name="charset" value="UTF-8"/> </bean> </property> </bean> </mvc:message-converters> </mvc:annotation-driven>
<!-- 包扫描 --> <context:component-scan base-package="com.pinyougou.manager"/> <!--加载文件资源--> <context:property-placeholder location="classpath:config/sys_config.properties"/>
<!--文件上传解析器--> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="UTF-8"></property> <!-- 设定文件上传的最大值5MB,5*1024*1024 --> <property name="maxUploadSize" value="5242880"></property> </bean> </beans> |
4.3.8商家管理后台
构建web模块pinyougou-shop-web 与运营商管理后台的构建方式类似。Pom也类似,区别:
(1)定义tomcat的启动端口为9102
(2)springmvc.xml
<!-- 引用dubbo 服务 --> <dubbo:application name="pinyougou-shop-web" /> <dubbo:registry address="zookeeper://192.168.25.131:2181"/> <dubbo:annotation package="com.pinyougou.shop.controller" /> |
把生成的代码拷贝到下面两个工程
然后 所有的pojo类,除了Example类,其它的全部实现序列化接口(代码生成器已经帮助生成了,不用我们生成了)
https://blog.csdn.net/weixin_41917449/article/details/81168234
修改每个实体类,让其实现Serializable接口。因为需要不同网络间相互传数据: windows和linux
简单地说,就是可以将一个对象(标志对象的类型)及其状态转换为字节码,保存起来(可以保存在数据库,内存,文件等),然后可以在适当的时候再将其状态恢复(也就是反序列化)。serialization 不但可以在本机做,而且可以经由网络操作。它自动屏蔽了操作系统的差异,字节顺序等。比如,在 Windows 平台生成一个对象并序列化之,然后通过网络传到一台 Unix 机器上,然后可以在这台Unix机器上正确地重构(deserialization)这个对象。 不必关心数据在不同机器上如何表示,也不必关心字节的顺序或者其他任何细节。
注意修改完毕要重新install pojo工程,否则不生效。以后修改完代码,都要重新install一下工程
5.品牌列表-后端代码
5.1需求分析
完成品牌管理的后端代码,在浏览器可查询品牌的数据(json格式)
5.2数据库表
tb_brand 品牌表
字段 |
类型 |
长度 |
含义 |
Id |
Bigint |
|
主键 |
Name |
Varchar |
255 |
品牌名称 |
First_char |
Varchar |
1 |
品牌首字母 |
5.3后端代码
5.2.1 服务层接口
在pinyougou-sellergoods-interface 工程创建BrandService接口
package com.pinyougou.sellergoods.service; import java.util.List; import com.pinyougou.pojo.TbBrand; /** * 品牌服务层接口 * @author Administrator * */ public interface BrandService { /** * 返回全部列表 * @return */ public List<TbBrand> findAll(); } |
|
5.2.2 服务实现类
在pinyougou-sellergoods-service 工程创建BrandServiceImpl类
package com.pinyougou.sellergoods.service.impl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import com.alibaba.dubbo.config.annotation.Service; import com.pinyougou.mapper.TbBrandMapper; import com.pinyougou.pojo.TbBrand; import com.pinyougou.sellergoods.service.BrandService; @Service public class BrandServiceImpl implements BrandService { @Autowired private TbBrandMapper brandMapper; //需要添加数据访问层依赖 @Override public List<TbBrand> findAll() { return brandMapper.selectByExample(null); } } |
5.2.3 控制层代码
在pinyougou-manager-web工程创建com.pinyougou.manager.controller包,包下创建BrandController类
package com.pinyougou.manager.controller; import java.util.List; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.alibaba.dubbo.config.annotation.Reference; import com.pinyougou.pojo.TbBrand; import com.pinyougou.sellergoods.service.BrandService; /** * 品牌controller * @author Administrator */ @RestController @RequestMapping("/brand") public class BrandController { @Reference private BrandService brandService; /** * 返回全部列表 * @return */ @RequestMapping("/findAll") public List<TbBrand> findAll(){ return brandService.findAll(); } } |
5.4测试
启动pinyougou-sellergoods-service
启动pinyougou-manager-web
地址栏输入http://localhost:9101/brand/findAll.do
出错的地方
1 spring的版本和其它jar包的版本有冲突报错java.lang.NoSuchMethodError: org.springframework.beans.factory.annotation.InjectionMetadata.
https://www.iteye.com/problems/77783
2 PageHelper connot be cast to org.apache.ibatis.plugin.Interceptor
PageHelper 的版本和配置文件的配置方式不匹配
https://blog.csdn.net/wgp15732622312/article/details/78640237
3.出现了这个问题耽误了两天!!!!!!!!!!!!!!!!
原因
classpath和classpath*区别: https://www.cnblogs.com/EasonJim/p/6709314.html
classpath:只会到你的class路径中查找找文件。
classpath*:不仅包含class路径,还包括jar文件中(class路径)进行查找。
注意: 用classpath*:需要遍历所有的classpath,所以加载速度是很慢的;因此,在规划的时候,应该尽可能规划好资源文件所在的路径,尽量避免使用classpath*。
classpath*的使用:
当项目中有多个classpath路径,并同时加载多个classpath路径下(此种情况多数不会遇到)的文件,*就发挥了作用,如果不加*,则表示仅仅加载第一个classpath路径。
此外,web的spingmvc配置文件中要添加消息转换器
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter" />
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
</mvc:message-converters>
</mvc:annotation-driven>
否则
不能返回ArrayList
最后
可以看到浏览器输出了json数据。
附录:常见错误
1.在注册中心找不到对应的服务
java.lang.IllegalStateException: Failed to check the status of the service com.pinyougou.sellergoods.service.BrandService. No provider available for the service com.pinyougou.sellergoods.service.BrandService from the url zookeeper://192.168.25.129:2181/com.alibaba.dubbo.registry.RegistryService?application=pinyougou-manager-web&dubbo=2.8.4&interface=com.pinyougou.sellergoods.service.BrandService&methods=update,get,delete,selectOptionList,add,getListByPage&pid=3980&revision=0.0.1-SNAPSHOT&side=consumer×tamp=1501146823396 to the consumer 172.16.17.14 use dubbo version 2.8.4 |
这种错误是服务层代码没有成功注册到注册中心导致,请检查一下你的服务层代码是否添加了@service注解,并且该注解的包一定是com.alibaba.dubbo.config.annotation包,不是org.springframework.stereotype.Service,这个地方极容易出错。另外还有一个原因就是你的服务层工程由于某些原因没有正常启动,也无法注册到注册中心里。
2.无法连接到注册中心
org.I0Itec.zkclient.exception.ZkTimeoutException: Unable to connect to zookeeper server within timeout: 5000 org.I0Itec.zkclient.ZkClient.connect(ZkClient.java:876) org.I0Itec.zkclient.ZkClient.<init>(ZkClient.java:98) org.I0Itec.zkclient.ZkClient.<init>(ZkClient.java:92) org.I0Itec.zkclient.ZkClient.<init>(ZkClient.java:80) com.alibaba.dubbo.remoting.zookeeper.zkclient.ZkclientZookeeperClient.<init>(ZkclientZookeeperClient.java:26) |
请检查IP与端口是否填写正确,检查注册中心是否正常启动