• (转)淘淘商城系列——引用dubbo服务


    http://blog.csdn.net/yerenyuan_pku/article/details/72758663

    上文我们一起学习了如何发布一个dubbo服务,本文我就来教大家如何在web工程中引用dubbo发布的服务。 
    首先我们在web工程也需要添加dubbo的依赖,我们可以直接把taotao-manager-service工程下的关于dubbo的依赖部分拷贝过来,拷贝的内容如下:

    <!-- 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>
    
    <!-- zookeeper的客户端,你要连接zookeeper,需要把以下两个jar包加进来 -->
    <dependency>
        <groupId>org.apache.zookeeper</groupId>
        <artifactId>zookeeper</artifactId>
    </dependency>
    <dependency>
        <groupId>com.github.sgroschupf</groupId>
        <artifactId>zkclient</artifactId>
    </dependency>
    • 1

    拷贝后的taotao-manager-web工程的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>
        <parent>
            <groupId>com.taotao</groupId>
            <artifactId>taotao-parent</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </parent>
        <groupId>com.taotao</groupId>
        <artifactId>taotao-manager-web</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>war</packaging>
    
        <dependencies>
            <!-- 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>
    
            <!-- 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>
    
            <!-- zookeeper的客户端,你要连接zookeeper,需要把以下两个jar包加进来 -->
            <dependency>
                <groupId>org.apache.zookeeper</groupId>
                <artifactId>zookeeper</artifactId>
            </dependency>
            <dependency>
                <groupId>com.github.sgroschupf</groupId>
                <artifactId>zkclient</artifactId>
            </dependency>
        </dependencies>
        <!-- 配置tomcat插件 -->
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.tomcat.maven</groupId>
                    <artifactId>tomcat7-maven-plugin</artifactId>
                    <configuration>
                        <port>8080</port>
                        <path>/</path>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>
    • 1

    下面我们需要在taotao-manager-web工程下的springmvc.xml文件中引用dubbo服务,添加的内容如下。

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

    其中<dubbo:application name="taotao-manager-web"/>是要在注册中心申报一下名字,名字不能冲突。<dubbo:registry protocol="zookeeper" address="192.168.25.167:2181"/>同服务端一样,在注册中心注册下dubbo服务所在的IP及端口。<dubbo:reference interface="com.taotao.service.ItemService" id="itemService" />便是要引用服务端的接口了。 
    引用dubbo服务之后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:p="http://www.springframework.org/schema/p"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" 
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
            http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
    
        <context:component-scan base-package="com.taotao.controller" />
        <mvc:annotation-driven />
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsp/" />
            <property name="suffix" value=".jsp" />
        </bean>
    
        <!-- 引用dubbo服务 -->
        <dubbo:application name="taotao-manager-web"/>
        <dubbo:registry protocol="zookeeper" address="192.168.25.167:2181"/>    
        <dubbo:reference interface="com.taotao.service.ItemService" id="itemService" />
    
    </beans>
    • 1

    由于taotao-manager-web工程与taotao-mananger-interface是独立的两个工程,因此要在taotao-manager-web工程中要添加对taotao-manager-interface的依赖,添加的内容如下:

    <dependency>  
        <groupId>com.taotao</groupId>  
        <artifactId>taotao-manager-interface</artifactId>  
        <version>0.0.1-SNAPSHOT</version>  
    </dependency>  
    • 1

    添加后当前taotao-manager-web工程的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>
        <parent>
            <groupId>com.taotao</groupId>
            <artifactId>taotao-parent</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </parent>
        <groupId>com.taotao</groupId>
        <artifactId>taotao-manager-web</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>war</packaging>
    
        <dependencies>
            <!-- 依赖taotao-common -->
            <dependency>  
                <groupId>com.taotao</groupId>  
                <artifactId>taotao-common</artifactId>  
                <version>0.0.1-SNAPSHOT</version>  
            </dependency>
            <!-- 依赖taotao-manager-interface -->
            <dependency>  
                <groupId>com.taotao</groupId>  
                <artifactId>taotao-manager-interface</artifactId>  
                <version>0.0.1-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>
    
            <!-- 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>
    
            <!-- zookeeper的客户端,你要连接zookeeper,需要把以下两个jar包加进来 -->
            <dependency>
                <groupId>org.apache.zookeeper</groupId>
                <artifactId>zookeeper</artifactId>
            </dependency>
            <dependency>
                <groupId>com.github.sgroschupf</groupId>
                <artifactId>zkclient</artifactId>
            </dependency>
        </dependencies>
        <!-- 配置tomcat插件 -->
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.tomcat.maven</groupId>
                    <artifactId>tomcat7-maven-plugin</artifactId>
                    <configuration>
                        <port>8080</port>
                        <path>/</path>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>
    • 1

    maven更新完之后,我们可以在taotao-manager-web工程所依赖的包中找到taotao-manager-interface,如下图所示。 
     
    添加完对taotao-manager-interface的依赖后,我们便可以直接使用它了。 

    这里,如果我们使用@ResponseBody注解后访问的时候报406错误(无法把对象转换为json串),90%是可能没有引入jackson包,我们到”Maven Dependencies”下查看是否有如下图所示的三个jar包。 
     
    如果没有,那么我们需要在taotao-manager-web工程中要添加对taotao-common的依赖,由于它依赖了jackson包,因此我们不用做任何处理。

     
     
  • 相关阅读:
    [f]动态判断js加载完成
    [f]添加css3动画的方法
    Meta标签以及viewport
    数据结构与算法-单向链表
    Denormalization 2
    Normalization
    Denormalization
    C# 通过反射获取扩展方法
    从ord()中对Unicode编码的理解
    python访问MS SqlServer(通过pyodbc)
  • 原文地址:https://www.cnblogs.com/telwanggs/p/6934204.html
Copyright © 2020-2023  润新知