• Dubbo入门案例


    1.基于以下图实现服务 提供者、消费者

    在这里插入图片描述

    2.前期工作

    父POM

    
    <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.0modelVersion>
    
        <groupId>com.tianxiagroupId>
        <artifactId>dubbo-demoartifactId>
        <packaging>pompackaging>
        <version>1.0-SNAPSHOTversion>
        <modules>
            <module>user-service-providermodule>
        modules>
    
        
        <properties>
            <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
            <maven.compiler.source>1.8maven.compiler.source>
            <maven.compiler.target>1.8maven.compiler.target>
            <junit.version>4.12junit.version>
            <log4j.version>1.2.17log4j.version>
            <lombok.version>1.16.18lombok.version>
            <mysql.version>5.1.47mysql.version>
            <druid.version>1.1.16druid.version>
            <mybatis.spring.boot.version>1.3.0mybatis.spring.boot.version>
        properties>
    
        
        <dependencyManagement>
            <dependencies>
                
                <dependency>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-dependenciesartifactId>
                    <version>2.2.2.RELEASEversion>
                    <type>pomtype>
                    <scope>importscope>
                dependency>
                
                <dependency>
                    <groupId>mysqlgroupId>
                    <artifactId>mysql-connector-javaartifactId>
                    <version>${mysql.version}version>
                dependency>
                
                <dependency>
                    <groupId>com.alibabagroupId>
                    <artifactId>druidartifactId>
                    <version>${druid.version}version>
                dependency>
                
                <dependency>
                    <groupId>com.baomidougroupId>
                    <artifactId>mybatis-plus-boot-starterartifactId>
                    <version>3.4.2version>
                dependency>
                
                <dependency>
                    <groupId>junitgroupId>
                    <artifactId>junitartifactId>
                    <version>${junit.version}version>
                dependency>
                
                <dependency>
                    <groupId>log4jgroupId>
                    <artifactId>log4jartifactId>
                    <version>${log4j.version}version>
                dependency>
                
                <dependency>
                    <groupId>org.projectlombokgroupId>
                    <artifactId>lombokartifactId>
                    <version>${lombok.version}version>
                    <optional>trueoptional>
                dependency>
            dependencies>
        dependencyManagement>
    project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78

    3.创建通用模块 - api-common

    POM

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-devtoolsartifactId>
            <scope>runtimescope>
            
            <optional>falseoptional>
        dependency>
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <optional>falseoptional>
        dependency>
        <dependency>
            <groupId>cn.hutoolgroupId>
            <artifactId>hutool-allartifactId>
            <version>5.1.0version>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    暂无YML

    业务类

    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    
    import java.io.Serializable;
    
    /**
     * 用户地址
     * @author liqb
     * @date 2023-05-28 16:39
     */
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public class UserAddress implements Serializable {
        
        private Integer id;
    
        /**
         * 用户地址
         */
        private String userAddress;
    
        /**
         * 用户id
         */
        private String userId;
    
        /**
         * 收货人
         */
        private String consignee;
    
        /**
         * 电话号码
         */
        private String phoneNum;
    
        /**
         * 是否为默认地址    Y-是     N-否
         */
        private String isDefault;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    import com.tianxia.dubbo.entity.UserAddress;
    
    import java.util.List;
    
    /**
     * 用户Service
     * @author liqb
     * @date 2023-05-28 16:47
     */
    public interface UserService {
    
        /**
         * 按照用户id返回所有的收货地址
         * @author liqb
         * @date 2023-05-28 16:48
         * @param userId 用户id
         * @return
         */
        public List<UserAddress> getUserAddressList(String userId);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    /**
     * 订单服务
     * @author liqb
     * @date 2023-05-28 16:52
     */
    public interface OrderService {
    
        /**
         * 初始化订单
         * @author liqb
         * @date 2023-05-28 16:52
         * @param userID 用户id
         */
        public void initOrder(String userID);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    2.创建用户服务提供者 - user-service-provider

    POM

    <dependencies>
        
        <dependency>
            <groupId>com.tianxiagroupId>
            <artifactId>api-commonartifactId>
            <version>1.0-SNAPSHOTversion>
        dependency>
        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>dubboartifactId>
        dependency>
        
        <dependency>
            <groupId>org.apache.curatorgroupId>
            <artifactId>curator-frameworkartifactId>
        dependency>
      dependencies>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    provider.xml

    
    <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="user-service-provider"/>
    
        
        
        <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"/>
    
        
        <dubbo:protocol name="dubbo" port="20881"/>
    
        
        <dubbo:service interface="com.tianxia.dubbo.api.UserService" ref="userServiceImpl"/>
    
        
        <bean id="userServiceImpl" class="com.tianxia.dubbo.service.impl.UserServiceImpl"/>
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    业务类

    import com.tianxia.dubbo.api.UserService;
    import com.tianxia.dubbo.entity.UserAddress;
    import org.springframework.stereotype.Service;
    
    import java.util.Arrays;
    import java.util.List;
    
    /**
     * 用户服务实现类
     * @author liqb
     * @date 2023-05-28 16:48
     */
    @Service
    public class UserServiceImpl implements UserService {
    
        /**
         * 按照用户id返回所有的收货地址
         * @author liqb
         * @date 2023-05-28 16:48
         * @param userId 用户id
         * @return
         */
        @Override
        public List<UserAddress> getUserAddressList(String userId) {
            UserAddress address1 = new UserAddress(1, "河南省郑州巩义市宋陵大厦2F", "1", "安然", "150360313x", "Y");
            UserAddress address2 = new UserAddress(2, "北京市昌平区沙河镇沙阳路", "1", "情话", "1766666395x", "N");
    
            return Arrays.asList(address1,address2);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    启动类

    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import java.io.IOException;
    
    /**
     * 启动类
     * @author liqb
     * @date 2023-05-28 17:27
     */
    public class UserServiceProviderApplication {
        public static void main(String[] args) throws IOException {
            ClassPathXmlApplicationContext applicationContext= new ClassPathXmlApplicationContext("provider.xml");
            applicationContext.start();
            System.in.read();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    在这里插入图片描述

    zkServer.cmd、和zkCli.cmd服务
    dubbo-admin target中cmd运行 java -jar dubbo-admin-0.0.1-SNAPSHOT.jar
    再次启动项目,我们可以看到在zookeeper中已经发现服务提供者。

    在这里插入图片描述

    服务提供者的配置和测试完成

    4.创建服务消费者(订单服务) - order-service-consumer

    POM

    <dependencies>
        
        <dependency>
            <groupId>com.tianxiagroupId>
            <artifactId>api-commonartifactId>
            <version>1.0-SNAPSHOTversion>
        dependency>
        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>dubboartifactId>
        dependency>
        
        <dependency>
            <groupId>org.apache.curatorgroupId>
            <artifactId>curator-frameworkartifactId>
        dependency>
    dependencies>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    consumer.xml

    
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
           xmlns:context="http://www.springframework.org/schema/context"
           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.3.xsd
    		http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd
    		http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
       
        
        <context:component-scan base-package="com.tianxia.dubbo.service.impl"/>
    
        
        <dubbo:application name="order-service-consumer"/>
        
        
        <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
    
        
        <dubbo:reference interface="com.tianxia.dubbo.api.UserService" id="userService"/>
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    业务类

    import com.tianxia.dubbo.api.OrderService;
    import com.tianxia.dubbo.api.UserService;
    import com.tianxia.dubbo.entity.UserAddress;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import java.util.List;
    
    /**
     * 订单服务实现类
     * @author liqb
     * @date 2023-05-28 16:54
     */
    @Service
    public class OrderServiceImpl implements OrderService {
    
        @Autowired
        public UserService userService;
    
        /**
         * 初始化订单
         * @author liqb
         * @date 2023-05-28 16:52
         * @param userID 用户id
         */
        @Override
        public void initOrder(String userID) {
            // 查询用户的收货地址
            List<UserAddress> userAddressList = userService.getUserAddressList(userID);
            
            // 为了直观的看到得到的数据,以下内容也可不写
            System.out.println("当前接收到的userId=> "+userID);
            System.out.println("**********");
            System.out.println("查询到的所有地址为:");
            for (UserAddress userAddress : userAddressList) {
                // 打印远程服务地址的信息
                System.out.println(userAddress.getUserAddress());
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    启动类

    import com.tianxia.dubbo.api.OrderService;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import java.io.IOException;
    
    /**
     * 启动类
     * @author liqb
     * @date 2023-05-28 17:40
     */
    public class OrderServiceConsumerApplication {
        public static void main(String[] args) throws IOException {
            ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("consumer.xml");
            OrderService orderService = applicationContext.getBean(OrderService.class);
    
            // 调用方法查询出数据
            orderService.initOrder("1");
            System.out.println("调用完成...");
            System.in.read();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    注意:消费者的运行测试需要先启动提供者。
    启动服务提供者、消费者。及zookeeper的和dubbo-admin,查看监控信息。
    localhost:7001

    在这里插入图片描述

    在这里插入图片描述

  • 相关阅读:
    什么是.NET的极限优化数值库?
    Pypora导出文件word格式
    netty快速入门
    mfc入门基础(七)向导对话框的创建与显示
    java毕业生设计大学生学籍管理系统计算机源码+系统+mysql+调试部署+lw
    springboot运行jar包,实现复制jar包resources下文件、文件夹(可支持包含子文件夹)到指定的目录
    【Python基础】史上最全||一篇博客搞懂Python面向对象编程(封装、继承、多态)
    【AIGC】开源声音克隆GPT-SoVITS
    [0xGame 2023 公开赛道] week3
    springMAC的原理以及概述
  • 原文地址:https://blog.csdn.net/weixin_44230693/article/details/130915438