SSM整合Dubbo

SSM整合Dubbo

一、 新增maven项目,命名为Days61DubboSSM

该项目为最高父项目

在父项目中添加依赖管理以及jdk插件

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

    <groupId>com.qfedu</groupId>
    <artifactId>Days61DubboSSM</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>DubboPojo</module>
        <module>DubboDao</module>
        <module>DubboServiceContent</module>
        <module>DubboPortal</module>
    </modules>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>4.3.6.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>3.4.4</version>
            </dependency>
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>
                    com.springsource.org.aspectj.weaver
                </artifactId>
                <version>1.6.8.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>1.0.28</version>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.44</version>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>2.8.6</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis-spring</artifactId>
                <version>1.3.1</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-tx</artifactId>
                <version>4.3.6.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
                <version>4.3.6.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>dubbo</artifactId>
                <version>2.5.3</version>
                <!-- 剔除dubbo中比较低版本的spring依赖 -->
                <exclusions>
                    <exclusion>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>org.apache.zookeeper</groupId>
                <artifactId>zookeeper</artifactId>
                <version>3.4.6</version>
                <type>pom</type>
            </dependency>
            <dependency>
                <groupId>com.101tec</groupId>
                <artifactId>zkclient</artifactId>
                <version>0.7</version>
            </dependency>
            <dependency>
                <groupId>commons-io</groupId>
                <artifactId>commons-io</artifactId>
                <version>2.4</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <!-- define the project compile level -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

二、 在父项目中添加子模块DubboPojo

  1. 修改pom文件,添加lombok依赖

    <?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>Days61DubboSSM</artifactId>
            <groupId>com.qfedu</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>DubboPojo</artifactId>
    
        <dependencies>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.16.18</version>
                <scope>provided</scope>
            </dependency>
        </dependencies>
    </project>
    
  2. 新增pojo类,并且让该类实现序列化接口

    package com.qfedu.pojo;
    
    import lombok.Data;
    
    import java.io.Serializable;
    
    @Data
    public class User implements Serializable {
        private String userId;
        private String username;
        private String password;
        private String email;
        private String tel;
        private int level;
        private int integral;
        private int ifNew;
    }
    

三、 在父项目中添加子模块DubboDao

  1. 修改pom文件,Dao模块依赖于Pojo模块

    <?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>Days61DubboSSM</artifactId>
            <groupId>com.qfedu</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>DubboDao</artifactId>
    
        <dependencies>
            <dependency>
                <groupId>com.qfedu</groupId>
                <artifactId>DubboPojo</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
            </dependency>
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>
                    com.springsource.org.aspectj.weaver
                </artifactId>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
            </dependency>
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis-spring</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-tx</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
            </dependency>
        </dependencies>
    </project>
    
  2. 在src中添加Dao接口的java代码

    package com.qfedu.dao;
    
    import com.qfedu.pojo.User;
    
    import java.util.List;
    
    public interface IUserDao {
    
        List<User> getAllUsers();
    }
    
  3. 在resources中添加dao接口对应的mapper的xml映射文件

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper
      PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
      "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <!-- 整个映射文件为mapper节点,里面包含namespace属性 -->
       <mapper namespace="com.qfedu.dao.IUserDao">
       
       <select id="getAllUsers" resultType="user">
          select user_id userId, user_name username, password, email, tel, level, integral, if_new ifNew from users
       </select>
    </mapper>
    

四、 在父项目中添加子模块DubboServiceContent

  1. 在当前模块中新增模块DubboServiceContentInterface

    1. 新增接口

      package com.qfedu.service;
      
      import com.qfedu.pojo.User;
      
      import java.util.List;
      
      public interface IUserService {
      
          List<User> getAllUsers();
      }
      
    2. pom文件

      <?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>DubboServiceContent</artifactId>
              <groupId>com.qfedu</groupId>
              <version>1.0-SNAPSHOT</version>
          </parent>
          <modelVersion>4.0.0</modelVersion>
      
          <artifactId>DubboServiceContentInterface</artifactId>
      </project>
      
  2. 在当前模块中新增模块DubboServiceContentProvider

    1. 新增接口对应的实现类UserServiceImpl

      package com.qfedu.service.impl;
      
      import com.qfedu.dao.IUserDao;
      import com.qfedu.pojo.User;
      import com.qfedu.service.IUserService;
      import org.springframework.stereotype.Service;
      
      import javax.annotation.Resource;
      import java.util.List;
      
      @Service
      public class UserServiceImpl implements IUserService {
      
          @Resource
          private IUserDao userDao;
      
          @Override
          public List<User> getAllUsers() {
              return userDao.getAllUsers();
          }
      }
      
    2. 修改pom文件,该提供者依赖于接口,再添加tomcat的插件来运行该子模块

      <?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>DubboServiceContent</artifactId>
              <groupId>com.qfedu</groupId>
              <version>1.0-SNAPSHOT</version>
          </parent>
          <modelVersion>4.0.0</modelVersion>
          <packaging>war</packaging>
      
          <artifactId>DubboServiceContentProvider</artifactId>
      
          <dependencies>
              <dependency>
                  <groupId>com.qfedu</groupId>
                  <artifactId>DubboServiceContentInterface</artifactId>
                  <version>1.0-SNAPSHOT</version>
              </dependency>
              <dependency>
                  <groupId>org.springframework</groupId>
                  <artifactId>spring-webmvc</artifactId>
              </dependency>
              <dependency>
                  <groupId>org.aspectj</groupId>
                  <artifactId>com.springsource.org.aspectj.weaver</artifactId>
              </dependency>
              <dependency>
                  <groupId>com.alibaba</groupId>
                  <artifactId>dubbo</artifactId>
              </dependency>
              <dependency>
                  <groupId>org.apache.zookeeper</groupId>
                  <artifactId>zookeeper</artifactId>
                  <type>pom</type>
              </dependency>
              <dependency>
                  <groupId>com.101tec</groupId>
                  <artifactId>zkclient</artifactId>
              </dependency>
              <dependency>
                  <groupId>commons-io</groupId>
                  <artifactId>commons-io</artifactId>
              </dependency>
          </dependencies>
      
          <build>
              <!--不过滤java下的xml文件-->
              <resources>
                  <resource>
                      <directory>src/main/java</directory>
                      <includes>
                          <include>**/*.xml</include>
                      </includes>
                      <filtering>false</filtering>
                  </resource>
                  <resource>
                      <directory>src/main/resources</directory>
                      <includes>
                          <include>**/*.properties</include>
                          <include>**/*.xml</include>
                      </includes>
                      <filtering>false</filtering>
                  </resource>
              </resources>
              <plugins>
                  <!-- 添加tomcat插件 -->
                  <plugin>
                      <groupId>org.apache.tomcat.maven</groupId>
                      <artifactId>tomcat7-maven-plugin</artifactId>
                      <version>2.2</version>
                      <configuration>
                          <path>/</path>
                          <port>8081</port>
                      </configuration>
                  </plugin>
              </plugins>
          </build>
      </project>
      
    3. 在resources下新增db.properties文件

      aaa=root
      bbb=123456
      driver=com.mysql.jdbc.Driver
      url=jdbc:mysql://localhost:3306/supermarket?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
      
      maxActive=20
      
    4. 在resources下新增spring-mybatis.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:context="http://www.springframework.org/schema/context"
         xmlns:tx="http://www.springframework.org/schema/tx"
         xmlns:aop="http://www.springframework.org/schema/aop"
         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-4.3.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
         
         <context:property-placeholder location="classpath:db.properties"/>
         
         <bean class="com.alibaba.druid.pool.DruidDataSource" id="ds">
            <property name="url" value="${url}"></property>
            <property name="username" value="${aaa}"></property>
            <property name="password" value="${bbb}"></property>
            <property name="driverClassName" value="${driver}"></property>
            
            <property name="maxActive" value="${maxActive}"></property>
         </bean>
         
         <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sf">
            <property name="typeAliasesPackage" value="com.qfedu.pojo" />
            <property name="mapperLocations" value="classpath:com/qfedu/dao/*.xml" />
            <property name="dataSource" ref="ds"></property>
            <property name="configLocation" value="classpath:mybatis-config.xml" />
         </bean>
         
         <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="com.qfedu.dao" />
            <property name="sqlSessionFactoryBeanName" value="sf"></property>
         </bean>
         
         <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="dtx">
            <property name="dataSource" ref="ds"></property>
         </bean>
         
         <tx:advice id="tx" transaction-manager="dtx">
            <!--定义属性,声明事务规则 -->
            <tx:attributes>
               <tx:method name="create*" propagation="REQUIRED" isolation="DEFAULT"
                  rollback-for="Exception" />
               <tx:method name="insert*" propagation="REQUIRED" isolation="DEFAULT"
                  rollback-for="Exception" />
               <tx:method name="add*" propagation="REQUIRED" isolation="DEFAULT"
                  rollback-for="Exception" />
               <tx:method name="upd*" propagation="REQUIRED" isolation="DEFAULT"
                  rollback-for="Exception" />
               <tx:method name="del*" propagation="REQUIRED" isolation="DEFAULT"
                  rollback-for="Exception" />
               <tx:method name="execute*" propagation="REQUIRED"
                  isolation="DEFAULT" rollback-for="Exception" />
               <tx:method name="do*" propagation="REQUIRED" isolation="DEFAULT"
                  rollback-for="Exception" />
               <tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT"
                  rollback-for="Exception" />
               <tx:method name="set*" propagation="REQUIRED" isolation="DEFAULT"
                  rollback-for="Exception" />
               <tx:method name="get*" propagation="SUPPORTS" isolation="DEFAULT"
                  read-only="true" />
               <tx:method name="show*" propagation="SUPPORTS" isolation="DEFAULT"
                  read-only="true" />
               <tx:method name="list*" propagation="SUPPORTS" isolation="DEFAULT"
                  read-only="true" />
               <tx:method name="select*" propagation="SUPPORTS" isolation="DEFAULT"
                  read-only="true" />
               <tx:method name="query*" propagation="SUPPORTS" isolation="DEFAULT"
                  read-only="true" />
               <tx:method name="has*" propagation="SUPPORTS" isolation="DEFAULT"
                  read-only="true" />
               <tx:method name="ntx*" propagation="NOT_SUPPORTED" />
               <tx:method name="*" propagation="SUPPORTS" isolation="DEFAULT"
                  read-only="true" />
            </tx:attributes>
         </tx:advice>
         
         <aop:config>
            <aop:pointcut expression="execution(* com.qfedu.service.*.*(..))" id="mpc"/>
            <aop:advisor advice-ref="tx" pointcut-ref="mpc" />
         </aop:config>
      </beans>
      
    5. 在resources下新增mybatis-config.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>
        
        <settings>
         <setting name="logImpl" value="STDOUT_LOGGING"/>
        </settings>
      </configuration>
      
    6. 在resources下新增spring-dubbo.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:dubbo="http://code.alibabatech.com/schema/dubbo"
         xmlns:context="http://www.springframework.org/schema/context"
         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-4.3.xsd
            http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
         <context:component-scan base-package="com.qfedu.service" />
         
         <!-- 定义服务提供者在dubbo中的名称 -->
         <dubbo:application name="DubboServiceContentProvider"/>
         
         <!-- 配置dubbo协议, dubbo, RMI, hession -->
         <dubbo:protocol name="dubbo" port="20880" />
         
         <!-- 完成dubbo的注册 -->
         <!-- <dubbo:registry address="zookeeper://192.168.31.13:2181"></dubbo:registry> -->
         <dubbo:registry address="zookeeper://192.168.31.134:2181"></dubbo:registry>
         
         <!-- 将接口暴露给消费方 -->
         <dubbo:service interface="com.qfedu.service.IUserService" ref="userServiceImpl"></dubbo:service>
      </beans>
      
    7. 修改web.xml文件,来加载所有的配置文件

      <?xml version="1.0" encoding="UTF-8"?>
      <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
               version="3.1">
      
          <context-param>
              <param-name>contextConfigLocation</param-name>
              <param-value>classpath:spring-*.xml</param-value>
          </context-param>
      
          <listener>
              <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
          </listener>
      </web-app>
      
  3. 修改当前子模块的pom文件

    <?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>Days61DubboSSM</artifactId>
            <groupId>com.qfedu</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>DubboServiceContent</artifactId>
        <packaging>pom</packaging>
    
        <dependencies>
            <dependency>
                <groupId>com.qfedu</groupId>
                <artifactId>DubboDao</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>
        </dependencies>
    
        <modules>
            <module>DubboServiceContentInterface</module>
            <module>DubboServiceContentProvider</module>
        </modules>
    
    </project>
    

    五、新增DubboPortal子模块

    1. 修改该模块的pom文件,分别添加依赖以及tomcat插件,当前模块依赖于接口(不能直接依赖于提供者)

      <?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>Days61DubboSSM</artifactId>
              <groupId>com.qfedu</groupId>
              <version>1.0-SNAPSHOT</version>
          </parent>
          <modelVersion>4.0.0</modelVersion>
      
          <artifactId>DubboPortal</artifactId>
          <packaging>war</packaging>
      
          <dependencies>
              <dependency>
                  <groupId>com.qfedu</groupId>
                  <artifactId>DubboServiceContentInterface</artifactId>
                  <version>1.0-SNAPSHOT</version>
              </dependency>
              <dependency>
                  <groupId>org.springframework</groupId>
                  <artifactId>spring-webmvc</artifactId>
              </dependency>
              <dependency>
                  <groupId>com.fasterxml.jackson.core</groupId>
                  <artifactId>jackson-databind</artifactId>
              </dependency>
              <dependency>
                  <groupId>org.apache.zookeeper</groupId>
                  <artifactId>zookeeper</artifactId>
                  <type>pom</type>
              </dependency>
              <dependency>
                  <groupId>com.alibaba</groupId>
                  <artifactId>dubbo</artifactId>
              </dependency>
              <dependency>
                  <groupId>com.101tec</groupId>
                  <artifactId>zkclient</artifactId>
              </dependency>
          </dependencies>
      
          <build>
              <!--不过滤java下的xml文件-->
              <resources>
                  <resource>
                      <directory>src/main/java</directory>
                      <includes>
                          <include>**/*.xml</include>
                      </includes>
                      <filtering>false</filtering>
                  </resource>
                  <resource>
                      <directory>src/main/resources</directory>
                      <includes>
                          <include>**/*.properties</include>
                          <include>**/*.xml</include>
                      </includes>
                      <filtering>false</filtering>
                  </resource>
              </resources>
              <plugins>
      
                  <!-- 添加tomcat插件 -->
                  <plugin>
                      <groupId>org.apache.tomcat.maven</groupId>
                      <artifactId>tomcat7-maven-plugin</artifactId>
                      <version>2.2</version>
                      <configuration>
                          <path>/</path>
                          <port>80</port>
                      </configuration>
                  </plugin>
              </plugins>
          </build>
      </project>
      
    2. 新增controller类

      package com.qfedu.controller;
      
      import com.qfedu.pojo.User;
      import com.qfedu.service.IUserService;
      import org.springframework.web.bind.annotation.GetMapping;
      import org.springframework.web.bind.annotation.RestController;
      
      import javax.annotation.Resource;
      import java.util.List;
      
      @RestController
      public class UserController {
      
          @Resource
          private IUserService userService;
      
          @GetMapping("/Users")
          public List<User> getAllUsers(){
              return userService.getAllUsers();
          }
      }
      
    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" 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>Days94SSMDubboPortal</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>
        
        <!-- needed for ContextLoaderListener -->
         <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-*.xml</param-value>
         </context-param>
      
         <!-- Bootstraps the root web application context before servlet initialization -->
         <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
         </listener>
        
        <!-- The front controller of this Spring Web application, responsible for handling all application requests -->
         <servlet>
            <servlet-name>springDispatcherServlet</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
               <param-name>contextConfigLocation</param-name>
               <param-value>classpath:springmvc.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
         </servlet>
      
         <!-- Map all requests to the DispatcherServlet for handling -->
         <servlet-mapping>
            <servlet-name>springDispatcherServlet</servlet-name>
            <url-pattern>/</url-pattern>
         </servlet-mapping>
      </web-app>
      
    4. 在resources下添加配置文件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:context="http://www.springframework.org/schema/context"
         xmlns:mvc="http://www.springframework.org/schema/mvc"
         xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
            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-4.3.xsd">
      
         <context:component-scan base-package="com.qfedu.controller" />
         
         <mvc:annotation-driven />
         
         <mvc:default-servlet-handler/>
      </beans>
      
    5. 在resources下添加配置文件spring-dubbo.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:context="http://www.springframework.org/schema/context"
         xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
         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-4.3.xsd
            http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
      
         <dubbo:application name="DubboPortal"/>
         
         <!-- 完成dubbo的注册 -->
         <dubbo:registry address="zookeeper://192.168.31.134:2181"></dubbo:registry>
         
         <dubbo:reference interface="com.qfedu.service.IUserService" id="userService" />
      </beans>
      

六、启动运行测试

  1. 启动zookeeper

  2. 启动provider对应的tomcat

  3. 启动protal消费者对应的tomcat

  4. 在浏览器中输入http://localhost/Users

  5. 页面上得到数据对应的json

    [{"userId":"20190514112608","username":"张三","password":"zhangsan","email":"zhangsan@163.com","tel":"13679189439","level":1,"integral":0,"ifNew":1},{"userId":"20190516203155","username":"东方不败","password":"222222","email":"gg@163.com","tel":"15536541234","level":1,"integral":0,"ifNew":1},{"userId":"20190516205310","username":"张无忌","password":"222222","email":"hei@163.com","tel":"17755421234","level":1,"integral":0,"ifNew":1},{"userId":"20190516205311","username":"路飞","password":"222222","email":"lf@163.com","tel":"13715546631","level":1,"integral":0,"ifNew":1},{"userId":"20190516205403","username":"吸氧羊","password":"222222","email":"miemie@163.com","tel":"1235468798","level":1,"integral":0,"ifNew":1},{"userId":"20190516205431","username":"路飞","password":"222222","email":"lf@163.com","tel":"13715546631","level":1,"integral":0,"ifNew":0},{"userId":"20190516205457","username":"蓝猫","password":"222222","email":"135445888@qq.com","tel":"15315468536","level":1,"integral":0,"ifNew":0},{"userId":"20190516205513","username":"蔡文姬","password":"222222","email":"cc@163.com","tel":"18312546321","level":1,"integral":0,"ifNew":0},{"userId":"20190516205530","username":"索隆","password":"222222","email":"sl@163.com","tel":"15545876652","level":1,"integral":0,"ifNew":0},{"userId":"20190516205542","username":"乌索普","password":"222222","email":"wspp@163.com","tel":"13865421355","level":1,"integral":0,"ifNew":0},{"userId":"20190516205551","username":"卡普","password":"222222","email":"kp@163.com","tel":"18354568796","level":1,"integral":0,"ifNew":0},{"userId":"20190516205603","username":"黄猿","password":"222222","email":"hy@163.com","tel":"13754632156","level":1,"integral":0,"ifNew":0},{"userId":"20190516205613","username":"乔巴","password":"123","email":"qb@163.com","tel":"13154897845","level":1,"integral":0,"ifNew":0},{"userId":"20190516205624","username":"艾斯","password":"222222","email":"aisi@163.com","tel":"15487965456","level":1,"integral":0,"ifNew":0},{"userId":"20190516205633","username":"赤犬","password":"222222","email":"cq@163.com","tel":"13896421564","level":1,"integral":0,"ifNew":0},{"userId":"20190516205654","username":"多弗朗明哥","password":"222222","email":"duoduo@163.com","tel":"13548976523","level":1,"integral":0,"ifNew":0}]
    
  6. 至此SSM整合dubbo完成

  7. 如果想分别监控提供者和消费者,可以配置dubbo-admin的war包来启动服务监控消费者和提供者

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值