二十四、详解web项目调用dubbo的provider

                    详解web项目调用dubbo的provider

一、前言

     在SSM框架中,我们调用(注入)的接口的实现类(@Service注解),在dubbo微服务调用中我们调用的是dubbo提供的接口类(在配置文件中声明实现类,会自动调用对应的实现类),在web项目中我们调用的是提供的接口类型在将接口类注入到Controlller中的时候,注意使用的类名是,Spring容器中定义接口id。

二、创建父类com_parent(pom包)

<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.yang</groupId>
  <artifactId>com_parent</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>


  <!-- 聚合以下项目 -->
   <modules>
    <module>../com_facade</module>
    <module>../com_service</module>
    <module>../com_web_consumer</module>
   </modules>

  <!-- <properties>用于定义全局变量,在POM中通过${property_name}的形式引用变量的值 -->
  <properties>
      <motan.version>0.3.0</motan.version>
        <!-- 在阿里巴巴内部广泛使用的GA版本为:2.4.9,强烈推荐此版本 -->
        <dubbo.version>2.8.4</dubbo.version>
        <dubbox.version>2.8.4</dubbox.version>
        <spring.version>4.3.6.RELEASE</spring.version>
        <mybatis.version>3.2.6</mybatis.version>
        <log4j.version>1.2.17</log4j.version>
        <slf4j.version>1.7.7</slf4j.version>
  </properties>


   <dependencies>
      <!-- dubbo -->
      <dependency>
          <groupId>com.alibaba</groupId>
          <artifactId>dubbo</artifactId>
          <version>2.8.4</version>
          <exclusions>
              <exclusion>
                  <groupId>org.springframework</groupId>
                  <artifactId>spring</artifactId>
              </exclusion>
          </exclusions>
      </dependency>
      <dependency>
          <groupId>com.github.sgroschupf</groupId>
          <artifactId>zkclient</artifactId>
          <version>0.1</version>
      </dependency>


      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</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-oxm</artifactId>
        <version>${spring.version}</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</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-aop</artifactId>
        <version>${spring.version}</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>${spring.version}</version>
      </dependency>

      <!-- SpringMVC对json数据的转换 -->
      <dependency>
        <groupId>com.fasterxml.jackson.module</groupId>
        <artifactId>jackson-module-jaxb-annotations</artifactId>
        <version>2.9.5</version>
      </dependency>
      <!-- 文件上传的包:但会json时候需要这个包 -->
     <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.3.3</version>
     </dependency>


    <!-- 在jsp页面使用jstl必须引入的两个jar包 -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>taglibs</groupId>
        <artifactId>standard</artifactId>
        <version>1.1.2</version>
    </dependency>


    <!-- MyBatis -->
    <dependency>
       <groupId>org.mybatis</groupId>
       <artifactId>mybatis</artifactId>
       <version>3.4.5</version>
    </dependency>

    <!-- MyBatis集成Spring的包 -->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>1.3.1</version>
    </dependency>


    <!-- ehcache-core -->
    <dependency>
        <groupId>net.sf.ehcache</groupId>
        <artifactId>ehcache-core</artifactId>
        <version>2.6.11</version>
    </dependency>

    <!-- mybatis-ehcache -->
    <dependency>
        <groupId>org.mybatis.caches</groupId>
        <artifactId>mybatis-ehcache</artifactId>
        <version>1.1.0</version>
    </dependency>


    <!-- MySQL数据库驱动 -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>6.0.6</version>
    </dependency>       

    <!-- 数据库连接池 -->
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-dbcp2</artifactId>
        <version>2.2.0</version>
    </dependency>

    <!-- asm -->
    <dependency>
        <groupId>org.ow2.asm</groupId>
        <artifactId>asm</artifactId>
        <version>6.1.1</version>
    </dependency>

    <!-- log4j -->
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.16</version>
    </dependency>

    <!-- slf4j -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.25</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.25</version>
        <scope>test</scope>
    </dependency>


      <!-- SpringAOP -->
      <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.8.13</version>
      </dependency>
  </dependencies>


  <build>
    <plugins>
        <!-- 编译插件 -->
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>

        <!--有时候插件检查不通过,需要人为的忽略对有些插件的检查-->
        <plugin>  
            <groupId>org.eclipse.m2e</groupId>  
            <artifactId>lifecycle-mapping</artifactId>  
            <version>1.0.0</version>  
            <configuration>  
                <lifecycleMappingMetadata>  
                    <pluginExecutions>  
                        <!-- 忽略2.0以上版本的maven-dependency-plugin的检查 -->  
                        <pluginExecution>  
                            <pluginExecutionFilter>  
                                <groupId>org.apache.maven.plugins</groupId>  
                                <artifactId>maven-dependency-plugin</artifactId>  
                                <versionRange>[2.0,)</versionRange>  
                                <goals>  
                                    <goal>copy-dependencies</goal>  
                                </goals>  
                            </pluginExecutionFilter>  
                            <action>  
                                <ignore />  
                            </action>  
                        </pluginExecution>  
                    </pluginExecutions>  
                </lifecycleMappingMetadata>  
            </configuration>  
        </plugin>  

    </plugins>
  </build>


</project>

二、com_web_consumer项目基本配置

1、pom.xml文件

<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>
  <artifactId>com_web_consumer</artifactId>
  <packaging>war</packaging>


  <!-- 继承 -->
  <parent>
    <groupId>com.yang</groupId>
    <artifactId>com_parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <!-- 查找jar顺序:relativePath元素中的地址–本地仓库–远程仓库   
                  空值将始终从仓库中获取,不从本地路径获取-->
    <relativePath>../com_parent/pom.xml</relativePath>
  </parent>

  <dependencies>

    <!-- 只要加入接口所在的就可以 -->
    <dependency>
        <groupId>com.yang</groupId>
        <artifactId>com_facade</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
  </dependencies>


</project>

2、spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://code.alibabatech.com/schema/dubbo
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd"
        default-autowire="byName" default-lazy-init="false">


        <context:annotation-config />
        <dubbo:annotation package="com.yang"/>
        <context:component-scan base-package="com.yang" />
        <dubbo:application name="com_consumer" />
        <dubbo:registry timeout="100000" address="zookeeper://192.168.1.105:2181?backup=192.169.1.106:2181,192.168.1.107:2181" />
        <!--dubbo注册的接口,调用接口会自动使用实现类中的方法-->
        <dubbo:reference id="enterprise" interface="com.yang.repo.EnterpriseService" />


</beans>

3、springmvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://code.alibabatech.com/schema/dubbo
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd"
        default-autowire="byName" default-lazy-init="false">

    <mvc:annotation-driven />
    <context:component-scan base-package="com.yang.controller" />


    <!-- ==========================视图解析器 ================================ -->
    <!-- 配置视图解析器 要求将jstl的包加到classpath -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 配置返回页面的路径 -->
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>

4、EnterpriseController.java

package com.yang.controller;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yang.model.Enterprise;
import com.yang.repo.EnterpriseService;


@Controller
public class EnterpriseController {

    //这个注入的是在dubbo中声明的接口
    @Resource(name="enterprise")
    private EnterpriseService enterprise;

    @RequestMapping("/getName")
    @ResponseBody
    public Enterprise getName() {
        return  enterprise.selectByPrimaryKey(1);
    }
}

猜你喜欢

转载自blog.csdn.net/newbie_907486852/article/details/81108858