1. 程式人生 > >dubbo入門之springmvc+dubbo

dubbo入門之springmvc+dubbo

1、新建maven專案:

image.png


2、修改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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.ym</groupId>
    <artifactId>testdubbodemo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>service</module>
        <module>serviceimpl</module>
        <module>testweb</module>
    </modules>
    <packaging>pom</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.0.8.RELEASE</version>
        </dependency>
        <!--引入dubbo-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.9</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!--引入zookeeper-->
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.10</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.25</version>
        </dependency>
    </dependencies>

    <build>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
            <plugins>
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.0.0</version>
                </plugin>
                <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.7.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.20.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>2.5.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.8.2</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

3、建立遠端介面模組,這個模組用於客戶端和介面實現呼叫,如controller,serviceImpl;

image.png

建立介面:

com.ym;

TestService {
    String getData(String name);
}

4、建立介面實現模組:

image.png

實現遠端介面:

com.ym.service.impl;

com.ym.TestService;
org.springframework.stereotype.;

()
TestServiceImpl TestService {

    String getData(String name) {
        + name;
    }
}

在spring配置檔案中,配置dubbo相關配置項:

<?
<?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:aop="http://www.springframework.org/schema/aop"
      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/aop http://www.springframework.org/schema/aop/spring-aop-5.0.8.xsd
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
     http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
   <!--掃描註解-->
   <context:component-scan base-package="com.ym"/>

   <!--使用dubbo的另外一個原因,可以和spring無縫整合-->
   <!--
       目的:找到註冊中心,告訴他我是誰,埠是多少
   -->
   <!--1、配置別名,目的是在後臺看到這個服務的別名,好區分到底是誰-->
   <dubbo:application name="test1"/>
   <!--2、配置註冊中心
       address表示註冊中心的地址,protocol表示註冊中心的協議格式
   -->
   <dubbo:registry address="192.168.1.224:2181" protocol="zookeeper"/>

   <!--配置要註冊的服務-->
   <dubbo:service interface="com.ym.TestService" ref="testServiceImpl" timeout="6000"/>
   <!--配置埠,因為消費者要想連線服務提供者,必須知道ip和埠,ip在服務註冊到Zookeeper的時候就已經知道了-->
   <dubbo:protocol name="dubbo" port="12345"/>

</beans>


配置web.xml檔案

    Archetype Created Web Application
    
        contextConfigLocation
        classpath:applicationContext.xml
    
    
        org.springframework.web.context.ContextLoaderListener

5、啟動介面模組:clean tomcat7:run

image.png

6、開啟dubbo控制檯檢視服務資訊

image.png

7、建立客戶端模組

image.png

修改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>testdubbodemo</artifactId>
        <groupId>com.ym</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>testweb</artifactId>
    <packaging>war</packaging>

    <name>testweb Maven Webapp</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.ym</groupId>
            <artifactId>service</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>testweb</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <port>9001</port>
                    <path>/</path>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>


配置springmvc檔案:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
     
xmlns:mvc="http://www.springframework.org/schema/mvc"
     
xmlns:context="http://www.springframework.org/schema/context"
     
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.xsd
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xml
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
     http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"
>

<context:component-scan base-package="com.ym"/>
<!--查詢遠端服務,找到對應的註冊中心,我需要什麼型別的服務-->
   <!--1、配置應用名稱,我我是誰-->
   
<dubbo:application name="yangmi"/>
<!--2、配置註冊中心,我要連線的-->
   
<dubbo:registry address="192.168.1.224:2181" protocol="zookeeper"/>
<!--3、查×××,告訴註冊中心我需要啥-->
   
<dubbo:reference interface="com.ym.TestService" id="testService"/>
</beans>

修改web.xml:
<!DOCTYPE web-app PUBLIC
       "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
       "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
   <display-name>Archetype Created Web Application</display-name>
   <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-mvc.xml</param-value>
       </init-param>
       <load-on-startup>2</load-on-startup>
   </servlet>
   <servlet-mapping>
       <servlet-name>springmvc</servlet-name>
       <url-pattern>/*</url-pattern>
   </servlet-mapping>
</web-app>

8、啟動testweb,並觀察dubbo控制檯;