e3mall项目:项目改造以及dubbo的应用

e3mall项目改造(使用SOA架构)

一、项目拆分

(1)将e3-manager-web从e3-manager中分离,即设置其父工程为:e3-parent,并从e3-manager中拖到e3-parent中。


(2)修改e3-manager-web的pom.xml文件,包括:spring的依赖、e3-manager-interface的依赖、tomcat插件引用以及web.xml修改

e3-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>e3-parent</artifactId>
        <groupId>cn.e3mall</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <groupId>cn.e3mall</groupId>
    <artifactId>e3-manager-web</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>e3-manager-web</name>


    <dependencies>
        <dependency>
            <groupId>cn.e3mall</groupId>
            <artifactId>e3-manager-interface</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>cn.e3mall</groupId>
            <artifactId>e3-manager-interface</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>cn.e3mall</groupId>
            <artifactId>e3-manager-dao</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!-- 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>
        <!-- JSP相关 -->
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jsp-api</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!-- 配置Tomcat插件 -->
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <port>8081</port>
                    <path>/</path>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

e3-manager-web - 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"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5">
  <display-name>e3-manager-web</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

    <!--配置SpringMVC前段控制器-->
    <servlet>
        <servlet-name>e3-manager</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>e3-manager</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!--POST提交乱码解决-->
    <filter>
        <filter-name>encoding</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>encoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

(3)e3-manager-web配置文件(resources),将mybatis以及other复制到e3-manager-service中。再将spring中的applicationContext-*.xml(三个)复制到e3e-manager-service中。e3-manager-web仅保留springmvc.xml即可


(4)修改e3-manager-service的打包方式为war,并手动创建web项目相关目录结构

在pom.xml中加入如下(打包方式):

<packaging>war</packaging>

相关目录结构:包括配置文件以及web.xml配置

e3-manager-service - 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"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5">
  <display-name>e3-manager</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

    <!--配置spring上下文-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/applicationContext-*.xml</param-value>
    </context-param>
    
    <!--配置spring监听器-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>



Dubbo(开源分布式服务框架)应用


一、什么是Dubbo

    Dubbo是 阿里巴巴公司开源的一个高性能优秀的 服务框架 ,使得应用可通过高性能的 RPC 实现服务的输出和输入功能,可以和  Spring 框架无缝集成。

  

架构图

架构节点角色说明:

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

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

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

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

·        Container:服务运行容器。

架构调用关系说明:

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

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

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

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

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

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



二、本项目中的应用

(1)安装zookeeper作为dubbo服务注册中心,上传zookeeper压缩包到Linux,解压缩。     

tar -zxvf zookeeper-3.4.6.tar.gz

在解压后的zookeeper目录下创建data文件夹

mkdir data

修改zookeeper配置文件zoo_sample.cfg为zoo.cfg,并修改其中dataDir=/tmp/zookeeper为上面创建的data目录的路径

mv zoo_sample.cfg zoo.cfg

然后进入zookeeper-3.4.6的bin目录中运行zookeeper即可

./zkServer.sh start   //运行zookeeper
./zkServer.sh status   //查看zookeeper状态
./zkServer.sh stop   //关闭zookeeper

(2)在e3-manager-interface的pom.xml文件中引入dubbo依赖(在interface中引入,由于service和web都依赖interface,所以在service和web中不需要重复引用)

<!-- 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)修改e3-manager-service中resources -> spring -> applicationContext-service.xml,修改后的配置文件如下:

注意:

在书写配置之前,先添加dubbo的文件头信息

xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd

如果address,填写你配置zookeeper的服务器ip以及端口号

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

    <!-- 使用dubbo发布服务 -->
    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="e3-manager" />
    <dubbo:registry protocol="zookeeper"
                    address="192.168.200.130:2181" />
    <!-- dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880" />
    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="cn.e3mall.service.ItemService" ref="itemServiceImpl" />


</beans>

(4)修改e3-manager-web中的springmvc.xml,添加如下配置(注意事项与applicationContext-service.xml一致):

<!-- 引用dubbo服务 -->
<dubbo:application name="e3-manager-web"/>
<dubbo:registry protocol="zookeeper" address="192.168.200.130:2181"/>
<dubbo:reference interface="cn.e3mall.service.ItemService" id="itemService" />

(5)配置maven运行环境,并重新安装e3-manager到maven本地仓库。


运行,并访问:http://localhost:8081/item/691300   (注意这里的端口号是8081,因为8080已被另一个tomcat占用,所以我们在配置第二个tomcat插件时,设置的端口号为8081,否则会运行出错)

运行结果如下:



这个错误信息原因:因为我们这里调用的是远程服务,传输的数据必须能够序列化,而我们的实体类,并没有实现序列化接口,所以会报错。

解决方法:让实体类实现序列化接口:java.io.Serializable,实现序列化接口过后,需要重新把e3-manager安装到maven仓库中。


安装过后运行结果:


扩展(设置service访问超时时间):

在e3-manager-service中的resources -> spring -> applicationContext-service.xml中 <dubbo:service>的末尾加上timeout="300000"  timeout的单位为毫秒,这里的300000代表5分钟


猜你喜欢

转载自blog.csdn.net/qq1031893936/article/details/80289324