1、首先创建一个maven工程:

idea如何加载dubbo项目 idea部署dubbo项目_java


idea如何加载dubbo项目 idea部署dubbo项目_java_02


创建完成后,开始配置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.test</groupId>
    <artifactId>dubbo_demo</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <!--modules可以先不写,后面创建子模块的时候会自动加上,如果先写上,在创建子工程后可能会重复,报错-->
    <modules>
        <module>dubbo_api</module>
        <module>dubbo_provider</module>
        <module>dubbo_consumer</module>
    </modules>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
        <motan.version>0.3.0</motan.version>
        <dubbo.version>2.5.3</dubbo.version>
        <dubbox.version>2.8.4</dubbox.version>
        <spring.version>4.3.6.RELEASE</spring.version>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <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>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency>
        <!-- spring相关 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</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-web</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-tx</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>4.3.12.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jms</artifactId>
            <version>4.2.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.7.4</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.6.11</version>
        </dependency>
    </dependencies>
</project>

如果pom.xml文件报红,可以点击图片中的按钮重新导入jar包

idea如何加载dubbo项目 idea部署dubbo项目_intellij-idea_03


2、创建子模块,dubbo_api:

idea如何加载dubbo项目 idea部署dubbo项目_idea如何加载dubbo项目_04


指定父项目

idea如何加载dubbo项目 idea部署dubbo项目_intellij-idea_05


配置dubbo_api的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>dubbo_demo</artifactId>
        <groupId>com.test</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>dubbo_api</artifactId>

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

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

创建一个接口(此处需要将java目录设置为,否则无法创建包和java类):

idea如何加载dubbo项目 idea部署dubbo项目_dubbo_06


idea如何加载dubbo项目 idea部署dubbo项目_dubbo_07

package com.api.service;

public interface DemoApi {
    public String sayHello();
}

3、创建dubbo_provider,作为dubbo的服务提供者,同样是创建一个maven工程,与第2步一样,此处不再重复。
dubbo_provider的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>dubbo_demo</artifactId>
        <groupId>com.test</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>dubbo_provider</artifactId>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>
    <!--dubbo_provider依赖dubbo_api-->
    <dependencies>
        <dependency>
            <groupId>com.test</groupId>
            <artifactId>dubbo_api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

创建接口是实现类:

idea如何加载dubbo项目 idea部署dubbo项目_dubbo_08

package com.provider.service;

import com.api.service.DemoApi;

public class DemoApiImpl implements DemoApi {
    @Override
    public String sayHello() {
        return "Hello World!";
    }
}

然后在resources目录下创建两个配置文件,一个是dubbo的,一个是spring的:
dubbo-provider.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://code.alibabatech.com/schema/dubbo
    http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="dubbo_provider"/>
    <!-- 使用zookeeper注册中心暴露服务地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880"/>
    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="com.api.service.DemoApi" ref="demoApi"/>
    <!-- 接口实现类-->
    <bean id="demoApi" class="com.provider.service.DemoApiImpl"/>

</beans>

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:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/util
        http://www.springframework.org/schema/util/spring-util-4.0.xsd"
       default-autowire="byName">

    <aop:aspectj-autoproxy />
    <context:component-scan base-package="com" />
    <import resource="classpath:dubbo-provider.xml" />
</beans>

然后在创建一个测试类,用来启动服务消费者,暴露服务:

package com.provider.test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

public class ProviderTest {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:springmvc.xml");
        context.start();
        System.out.println("Dubbo provider is start.....");
        try {
            System.in.read();   // 按任意键退出
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

4、创建dubbo_consumer作为dobbo的服务消费者,同样是创建一个maven项目,与第2步一样,此处不再重复;
dubbo_consumer的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>dubbo_demo</artifactId>
        <groupId>com.test</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>dubbo_consumer</artifactId>

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

    <dependencies>
        <dependency>
            <groupId>com.test</groupId>
            <artifactId>dubbo_api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

pom.xml文件配置完成后,在resources目录下创建两个xml配置文件:

小插曲:项目第一天创建成功后,没有什么问题,第二天重新打开项目,dubbo_consumer.xml配置文件报错(beans标签中的部分链接报红),如果出现类似情况,点击下图按钮,重新刷新jar包即可。

idea如何加载dubbo项目 idea部署dubbo项目_java_09


dubbo_consumer.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://code.alibabatech.com/schema/dubbo
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd ">

    <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
    <dubbo:application name="dubbo_consumer"/>
    <!-- 使用multicast广播注册中心暴露发现服务地址 -->
    <dubbo:registry protocol="zookeeper" address="zookeeper://127.0.0.1:2181"/>
    <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
    <dubbo:reference interface="com.api.service.DemoApi" id="demoApi"/>
</beans>

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:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/util
        http://www.springframework.org/schema/util/spring-util-4.0.xsd"
       default-autowire="byName">

    <aop:aspectj-autoproxy />
    <context:component-scan base-package="com" />
    <import resource="classpath:/dubbo_consumer.xml" />
</beans>

然后在创建一个测试类:

idea如何加载dubbo项目 idea部署dubbo项目_intellij-idea_10

package com.consumer.test;

import com.api.service.DemoApi;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

public class ConsumerTest {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "classpath:springmvc.xml" });
        context.start();

        DemoApi demoApi = (DemoApi) context.getBean("demoApi");
        System.out.println(demoApi.sayHello());
        try {
            System.in.read();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

到此位置,整个项目已经创建成功,项目结构如下图:

idea如何加载dubbo项目 idea部署dubbo项目_java-zookeeper_11


5、开始启动程序,前提条件需要先开启zookeeper服务注册中心

①启动顺序:先启动ProviderTest测试类,也就是启动服务提供者,将服务暴露出去,然后启动ConsumerTest测试类,也就是启动服务消费者。

②为了方便观察,我们可以将dubbo-admin也启动起来,这样方便观察服务。

③启动:

在没有启动之前可以看到,dubbo-admin上没有任何服务

idea如何加载dubbo项目 idea部署dubbo项目_java_12


idea如何加载dubbo项目 idea部署dubbo项目_java_13


现在启动ProviderTest后,可以看到控制台输出如下,则代表启动成功。

idea如何加载dubbo项目 idea部署dubbo项目_intellij-idea_14


现在在查看dubbo-admin上面,可以看到已经有服务提供者的信息了:

idea如何加载dubbo项目 idea部署dubbo项目_java_15


在启动ConsumerTest,可以看到控制台输出如下,则代表启动成功:

idea如何加载dubbo项目 idea部署dubbo项目_intellij-idea_16


在查看dubbo-admin上面,已经存下服务消费者的信息了:

idea如何加载dubbo项目 idea部署dubbo项目_dubbo_17


6、小结

其实,在配置服务提供者dubbo_provider和服务消费者dubbo_consumer的时候,我们在resources目录下分别配置了两个springmvc.xml的配置文件,可以看到,在配置文件中导入dubbo_provider.xml和dubbo_consumer.xml配置文件,其实这两个springmvc的配置文件不写也可以,将测试类中修改成dubbo的配置文件即可

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "classpath:dubbo_consumer.xml" });

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:dubbo-provider.xml");

这个简单的dubbo项目就完成了,写的不好,有错误的地方请多指教。