IDEA下将dubbo简单项目跑Demo(2019.12版)

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

  • src没用,所以删去
  • 选择maven项目,不用勾选模板骨架,直接main方法,因为不用到服务器
  • 顺序是按照:添加pom依赖-接口实现类-配置文件

项目环境

  • IDE:IntelliJ IDEA15.0.6
  • JDK:1.8.0_161
  • 项目管理工具:Maven项目(没勾选quickstart等模板)
  • Spring: 4.3.21
  • dubbo:2.5.3

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,替换成真实的注册中心地址) -->
    <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注册中心暴露发现服务地址,也可以写成 address="zookeeper://127.0.0.1:2181" ;可相互替换-->
    <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

运行成功界面

服务提供者 dubbo-provider 下 App类

服务消费者 dubbo-consumer 下 TestConsumer 类

若在zkCli.cmd下查看ls dubbo/

参考文章

posted @   Roni_i  阅读(800)  评论(0编辑  收藏  举报
编辑推荐:
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 现代计算机视觉入门之:什么是图片特征编码
· .NET 9 new features-C#13新的锁类型和语义
阅读排行:
· Spring AI + Ollama 实现 deepseek-r1 的API服务和调用
· 《HelloGitHub》第 106 期
· 数据库服务器 SQL Server 版本升级公告
· 深入理解Mybatis分库分表执行原理
· 使用 Dify + LLM 构建精确任务处理应用
历史上的今天:
2018-12-02 后端程序员必备的Linux基础知识
2017-12-02 Codeforces Round #449 (Div. 2) B. Chtholly's request【偶数位回文数】
2017-12-02 Codeforces Round #449 (Div. 2) A. Scarborough Fair【多次区间修改字符串】
2017-12-02 Codeforces Round #299 (Div. 2) B. Tavas and SaDDas【DFS/*进制思维/位运算/一个数为幸运数,当且仅当它的每一位要么是4,要么是7 ,求小于等于n的幸运数个数】
2017-12-02 Codeforces Beta Round #4 (Div. 2 Only) C. Registration system【裸hash/map】
2017-12-02 Codeforces Round #320 (Div. 2) [Bayan Thanks-Round] A. Raising Bacteria【位运算/二进制拆分/细胞繁殖,每天倍增】
2017-12-02 Codeforces Round #277.5 (Div. 2) B. BerSU Ball【贪心/双指针/每两个跳舞的人可以配对,并且他们两个的绝对值只差小于等于1,求最多匹配多少对】
点击右上角即可分享
微信分享提示