项目架构(聚合项目,父子模块)

因为src没用,所以删去

1、首先创建父模块dubbo-parent

其中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.zrl</groupId>
    <artifactId>dubbo-parent</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>

    <modules>
        <module>dubbo-commons</module>
        <module>dubbo-provider</module>
        <module>dubbo-consumer</module>
    </modules>

</project>

2、公用模块 dubbo-commons

1.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-parent</artifactId>
        <groupId>com.zrl</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <modelVersion>4.0.0</modelVersion>

    <artifactId>dubbo-commons</artifactId>

</project>

2.创建com.zrl.service下UserService.java

package com.zrl.service;

/**
 * Created by Roni on 2019/12/2.
 */
public interface UserService {
    public String sayHello(String msg);
}

3、服务提供者 dubbo-provider

1.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-parent</artifactId>
        <groupId>com.zrl</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>dubbo-provider</artifactId>

    <dependencies>
        <!-- 依赖公共模块 -->
        <dependency>
            <groupId>com.zrl</groupId>
            <artifactId>dubbo-commons</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!-- 引入Spring的依赖 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.21.RELEASE</version>
        </dependency>
        <!-- 引入日志的依赖 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.25</version>
        </dependency>
        <!-- 引入dubbo框架(服务端、客户端通用) -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.3</version>
            <exclusions>
                <exclusion>
                    <artifactId>spring</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- 因为dubbo服务端需要注册服务到zk中,因此依赖zkClient包 -->
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency>
    </dependencies>

</project>

2.com.zrl.service.impl下UserServiceImpl

package com.zrl.service.impl;

import com.zrl.service.UserService;

/**
 * Created by Roni on 2019/12/2.
 */
public class UserServiceImpl implements UserService {

    public String sayHello(String msg) {
        System.out.println("服务端接收:"+msg);
        return "你好啊";
    }
}

3.java下App类

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by Roni on 2019/12/2.
 */
public class App {
    public static void main(String[] args) throws Exception{
        ApplicationContext ac =
                new ClassPathXmlApplicationContext("applicationContext.xml");
        //挂起当前线程,如果没有该行代码,服务提供者进程会消亡,服务消费者就发现不了提供者了
        Thread.currentThread().join();

    }
}

4.resources目录下两个配置文件
4.1:applicationContext.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.0.xsd
    http://code.alibabatech.com/schema/dubbo
    http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="dubboProvider" />
    <!-- 使用zookeeper注册中心暴露服务地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880" />
    <!-- 启用monitor模块 -->
    <dubbo:monitor protocol="registry" />
    <bean id="userService" class="com.zrl.service.impl.UserServiceImpl" />
    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="com.zrl.service.UserService" ref="userService"
                   group="dubbo"  version="1.0.0" timeout="3000"/>
</beans>

4.2:log4j.properties:

log4j.rootLogger=INFO,A1
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout

4、服务消费者 dubbo-consumer

1.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-parent</artifactId>
        <groupId>com.zrl</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>dubbo-consumer</artifactId>
    <dependencies>
        <!-- 依赖公共模块 -->
        <dependency>
            <groupId>com.zrl</groupId>
            <artifactId>dubbo-commons</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!-- 依赖Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.21.RELEASE</version>
        </dependency>
        <!-- log4j的依赖 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.25</version>
        </dependency>
        <!-- 引入dubbo框架(服务端、客户端通用) -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.3</version>
            <exclusions>
                <exclusion>
                    <artifactId>spring</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- 因为dubbo服务端需要注册服务到zk中,因此依赖zkClient包 -->
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency>
    </dependencies>

</project>

2.java下TestConsumer类

import com.zrl.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by Roni on 2019/12/2.
 */
public class TestConsumer {
    public static void main(String[] args) {
        ApplicationContext ac =
                new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService service = ac.getBean(UserService.class);
        System.out.println(service.sayHello("hello provider"));
    }
}

3.resources目录下两个配置文件

同上顺序:

<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.0.xsd
    http://code.alibabatech.com/schema/dubbo
    http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
    <dubbo:application name="dubboConsumer" />
    <!-- 使用zookeeper注册中心暴露发现服务地址 -->
    <dubbo:registry  protocol="zookeeper"
                     address="127.0.0.1:2181" />
    <!-- 启动monitor-->
    <dubbo:monitor protocol="registry" />
    <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
    <dubbo:reference id="userService" interface="com.zrl.service.UserService"
                     group="dubbo" version="1.0.0" timeout="3000"/>
</beans>
log4j.rootLogger=INFO,A1
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout

运行成功界面

参考文章

12-22 23:23