美文网首页dubbojs css html
Dubbo应用与整体结构

Dubbo应用与整体结构

作者: 右耳菌 | 来源:发表于2022-11-23 23:22 被阅读0次

    1. Dubbo 的架构


    2. Dubbo 调用流程


    3. 架构特点

    • 1.连通性

    • 2. 健壮性


    4. 实际操作 - 例子

    先罗列一下总共有几个包:

    • service-api: 包含了所有定义的接口
    • order-service: 下单的业务
    • sms-service: 发送短信业务
    4.1 创建一个empty project (空项目)

    首先创建一个空的项目,然后添加三个modules与上边三个对应的maven project。





    4.2 service-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">
        <modelVersion>4.0.0</modelVersion>
    
        <!--其实唯一需要留意的就这个而已-->
        <groupId>cn.lazyfennec</groupId>
        <artifactId>service-api</artifactId>
        <version>1.0.0</version>
    
    </project>
    
    • 创建 IOrderService 接口
    package cn.lazyfennec.dubbo.order.api;
    
    /**
     * Order Service
     */
    public interface IOrderService {
    
        /**
         * 创建订单
         * @param orderContent 订单内容
         */
        void create(String orderContent);
    
    }
    
    • 创建ISmsService接口
    package cn.lazyfennec.dubbo.sms.api;
    
    /**
     * Send message interface
     */
    public interface ISmsService {
    
        /**
         * 发送短信方法
         * @param phone 号码
         * @param content 内容
         * @return 发送结果
         */
        Object send(String phone, String content);
    }
    
    4.3 sms-service
    • 修改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>cn.lazyfennec</groupId>
        <artifactId>sms-service</artifactId>
        <version>1.0.0</version>
    
        <dependencies>
            <!--系统模块依赖-->
            <dependency>
                <groupId>cn.lazyfennec</groupId>
                <artifactId>service-api</artifactId>
                <version>1.0.0</version>
            </dependency>
            <!-- 框架依赖 -->
            <!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo -->
            <!-- 新的版本似乎移除了一些东西,这里只是用来作为一个例子,先就这样吧 -->
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo</artifactId>
                <version>2.7.18</version>
            </dependency>
            <dependency>
                <groupId>redis.clients</groupId>
                <artifactId>jedis</artifactId>
                <version>3.8.0</version>
            </dependency>
            <!--暂时用一下老版本,看一下打印的内容-->
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>1.2.17</version>
            </dependency>
        </dependencies>
    </project>
    
    • 在resources文件夹下创建dubbo.properties
    # 名称
    dubbo.application.name=sms-service
    # 注册地址
    dubbo.registry.address=redis://192.168.1.9:6379
    # 过期时间
    dubbo.consumer.timeout=1000
    # Quality of Service 禁用
    dubbo.application.qosEnable=false
    
    • 在resources文件夹下创建log4j.properties
    # Set log levels #
    log4j.rootLogger = info, Console
    
    # Output the log info to the Java Console #
    log4j.appender.Console = org.apache.log4j.ConsoleAppender   
    log4j.appender.Console.Target = System.out   
    log4j.appender.Console.ImmediateFlush = true  
    log4j.appender.Console.Threshold = DEBUG   
    log4j.appender.Console.layout = org.apache.log4j.PatternLayout   
    log4j.appender.Console.layout.ConversionPattern =%d{yyyy-MM-dd HH:mm:ss.SSS} [%C{1}:%L] - %m%n
    
    • 创建SmsServiceImpl,该类实现ISmsService接口,其中类顶上的@DubboService注解表示这是一个Dubbo的服务类,系统发现后会将该服务注册到Redis注册中心中。
    package cn.lazyfennec.dubbo.sms;
    
    import cn.lazyfennec.dubbo.sms.api.ISmsService;
    import org.apache.dubbo.config.annotation.DubboService;
    
    /**
     * @Author: Neco
     * @Description:
     * @Date: create in 2022/11/24 19:38
     */
    @DubboService
    public class SmsServiceImpl implements ISmsService {
        @Override
        public Object send(String phone, String content) {
            System.out.println("发送短信:" + phone + ":" + content);
            return "短信发送成功";
        }
    }
    
    • 创建SmsApplication启动类,即启动SmsService。
    package cn.lazyfennec.dubbo;
    
    import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.PropertySource;
    
    import java.io.IOException;
    
    /**
     * @Author: Neco
     * @Description:
     * @Date: create in 2022/11/24 19:38
     */
    @Configuration // 说明这是一个配置类
    @ComponentScan("cn.lazyfennec.dubbo") // spring注解扫描
    @PropertySource("classpath:/dubbo.properties")  // 指定属性扫描路径
    @EnableDubbo(scanBasePackages = "cn.lazyfennec.dubbo") // 启用Dubbo
    public class SmsApplication {
    
        public static void main(String[] args) throws IOException {
            AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SmsApplication.class);
            context.start();
    
            // 阻塞不退出
            System.in.read();
            context.close();
        }
    }
    
    • 启动sms-service
    2022-11-24 22:28:10.556 [?:?] - using logger: org.apache.dubbo.common.logger.log4j.Log4jLoggerAdapter
    2022-11-24 22:28:12.553 [ServiceClassPostProcessor:219] -  [DUBBO] BeanNameGenerator bean can't be found in BeanFactory with name [org.springframework.context.annotation.internalConfigurationBeanNameGenerator], dubbo version: 2.7.18, current host: 192.168.1.5
    2022-11-24 22:28:12.553 [ServiceClassPostProcessor:221] -  [DUBBO] BeanNameGenerator will be a instance of org.springframework.context.annotation.AnnotationBeanNameGenerator , it maybe a potential problem on bean name generation., dubbo version: 2.7.18, current host: 192.168.1.5
    2022-11-24 22:28:12.589 [ServiceClassPostProcessor:302] -  [DUBBO] The BeanDefinition[Root bean: class [org.apache.dubbo.config.spring.ServiceBean]; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] of ServiceBean has been registered with name : ServiceBean:cn.lazyfennec.dubbo.sms.api.ISmsService, dubbo version: 2.7.18, current host: 192.168.1.5
    2022-11-24 22:28:12.590 [ServiceClassPostProcessor:176] -  [DUBBO] 1 annotated Dubbo's @DubboService Components { [Bean definition with name 'smsServiceImpl': Generic bean: class [cn.lazyfennec.dubbo.sms.SmsServiceImpl]; scope=singleton; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in file [G:\neteasy\subject-4\2.3-手写RPC框架\neco\dubbo\sms-service\target\classes\cn\lazyfennec\dubbo\sms\SmsServiceImpl.class]] } were scanned under package[cn.lazyfennec.dubbo], dubbo version: 2.7.18, current host: 192.168.1.5
    2022-11-24 22:28:13.177 [DubboBootstrap:756] -  [DUBBO] No value is configured in the registry, the DynamicConfigurationFactory extension[name : redis] does not support as the config center, dubbo version: 2.7.18, current host: 192.168.1.5
    2022-11-24 22:28:13.180 [DubboBootstrap:762] -  [DUBBO] The registry[<dubbo:registry address="redis://192.168.1.9:6379" protocol="redis" port="6379" />] will be not used as the config center, dubbo version: 2.7.18, current host: 192.168.1.5
    2022-11-24 22:28:13.231 [ConfigValidationUtils:471] -  [DUBBO] No valid monitor config found, specify monitor info to enable collection of Dubbo statistics, dubbo version: 2.7.18, current host: 192.168.1.5
    2022-11-24 22:28:13.247 [DubboBootstrap:756] -  [DUBBO] No value is configured in the registry, the MetadataReportFactory extension[name : redis] supports as the metadata center, dubbo version: 2.7.18, current host: 192.168.1.5
    2022-11-24 22:28:13.248 [DubboBootstrap:762] -  [DUBBO] The registry[<dubbo:registry address="redis://192.168.1.9:6379" protocol="redis" port="6379" />] will be used as the metadata center, dubbo version: 2.7.18, current host: 192.168.1.5
    2022-11-24 22:28:13.273 [AbstractMetadataReport:198] -  [DUBBO] Load service store file C:\Users\lazyf\.dubbo\dubbo-metadata-sms-service-192.168.1.9-6379.cache, data: {cn.lazyfennec.dubbo.sms.api.ISmsService:::provider:sms-service={"annotations":[],"canonicalName":"cn.lazyfennec.dubbo.sms.api.ISmsService","codeSource":"file:/G:/neteasy/subject-4/2.3-%e6%89%8b%e5%86%99RPC%e6%a1%86%e6%9e%b6/neco/dubbo/service-api/target/classes/","methods":[{"annotations":[],"name":"send","parameterTypes":["java.lang.String","java.lang.String"],"parameters":[],"returnType":"java.lang.Object"}],"parameters":{"side":"provider","service.name":"ServiceBean:/cn.lazyfennec.dubbo.sms.api.ISmsService","release":"2.7.18","methods":"send","deprecated":"false","dubbo":"2.0.2","interface":"cn.lazyfennec.dubbo.sms.api.ISmsService","qos.enable":"false","generic":"false","default":"true","metadata-type":"remote","application":"sms-service","dynamic":"true","anyhost":"true"},"types":[{"enums":[],"items":[],"properties":{},"type":"java.lang.Object","typeBuilderName":"org.apache.dubbo.metadata.definition.builder.DefaultTypeBuilder"},{"enums":[],"items":[],"properties":{},"type":"java.lang.String","typeBuilderName":"org.apache.dubbo.metadata.definition.builder.DefaultTypeBuilder"}],"uniqueId":"cn.lazyfennec.dubbo.sms.api.ISmsService@file:/G:/neteasy/subject-4/2.3-%e6%89%8b%e5%86%99RPC%e6%a1%86%e6%9e%b6/neco/dubbo/service-api/target/classes/"}}, dubbo version: 2.7.18, current host: 192.168.1.5
    2022-11-24 22:28:13.352 [DubboBootstrap:533] -  [DUBBO] DubboBootstrap has been initialized!, dubbo version: 2.7.18, current host: 192.168.1.5
    2022-11-24 22:28:13.353 [DubboBootstrap:884] -  [DUBBO] DubboBootstrap is starting..., dubbo version: 2.7.18, current host: 192.168.1.5
    2022-11-24 22:28:13.396 [ServiceConfig:593] -  [DUBBO] No valid ip found from environment, try to get local host., dubbo version: 2.7.18, current host: 192.168.1.5
    2022-11-24 22:28:13.498 [ServiceConfig:551] -  [DUBBO] Export dubbo service cn.lazyfennec.dubbo.sms.api.ISmsService to local registry url : injvm://127.0.0.1/cn.lazyfennec.dubbo.sms.api.ISmsService?anyhost=true&application=sms-service&bind.ip=192.168.1.5&bind.port=20880&default=true&deprecated=false&dubbo=2.0.2&dynamic=true&generic=false&interface=cn.lazyfennec.dubbo.sms.api.ISmsService&metadata-type=remote&methods=send&pid=30136&qos.enable=false&release=2.7.18&service.name=ServiceBean:/cn.lazyfennec.dubbo.sms.api.ISmsService&side=provider&timestamp=1669300093371, dubbo version: 2.7.18, current host: 192.168.1.5
    2022-11-24 22:28:13.498 [ServiceConfig:501] -  [DUBBO] Register dubbo service cn.lazyfennec.dubbo.sms.api.ISmsService url dubbo://192.168.1.5:20880/cn.lazyfennec.dubbo.sms.api.ISmsService?anyhost=true&application=sms-service&bind.ip=192.168.1.5&bind.port=20880&default=true&deprecated=false&dubbo=2.0.2&dynamic=true&generic=false&interface=cn.lazyfennec.dubbo.sms.api.ISmsService&metadata-type=remote&methods=send&pid=30136&qos.enable=false&release=2.7.18&service.name=ServiceBean:/cn.lazyfennec.dubbo.sms.api.ISmsService&side=provider&timestamp=1669300093371 to registry registry://192.168.1.9:6379/org.apache.dubbo.registry.RegistryService?application=sms-service&dubbo=2.0.2&id=org.apache.dubbo.config.RegistryConfig#0&pid=30136&qos.enable=false&registry=redis&release=2.7.18&timestamp=1669300093366, dubbo version: 2.7.18, current host: 192.168.1.5
    2022-11-24 22:28:13.523 [QosProtocolWrapper:97] -  [DUBBO] qos won't be started because it is disabled. Please check dubbo.application.qos.enable is configured either in system property, dubbo.properties or XML/spring-boot configuration., dubbo version: 2.7.18, current host: 192.168.1.5
    2022-11-24 22:28:15.414 [AbstractServer:69] -  [DUBBO] Start NettyServer bind /0.0.0.0:20880, export /192.168.1.5:20880, dubbo version: 2.7.18, current host: 192.168.1.5
    2022-11-24 22:28:15.434 [AbstractRegistry:288] -  [DUBBO] Register: dubbo://192.168.1.5:20880/cn.lazyfennec.dubbo.sms.api.ISmsService?anyhost=true&application=sms-service&default=true&deprecated=false&dubbo=2.0.2&dynamic=true&generic=false&interface=cn.lazyfennec.dubbo.sms.api.ISmsService&metadata-type=remote&methods=send&pid=30136&release=2.7.18&service.name=ServiceBean:/cn.lazyfennec.dubbo.sms.api.ISmsService&side=provider&timestamp=1669300093371, dubbo version: 2.7.18, current host: 192.168.1.5
    2022-11-24 22:28:15.478 [AbstractRegistry:313] -  [DUBBO] Subscribe: provider://192.168.1.5:20880/cn.lazyfennec.dubbo.sms.api.ISmsService?anyhost=true&application=sms-service&bind.ip=192.168.1.5&bind.port=20880&category=configurators&check=false&default=true&deprecated=false&dubbo=2.0.2&dynamic=true&generic=false&interface=cn.lazyfennec.dubbo.sms.api.ISmsService&metadata-type=remote&methods=send&pid=30136&qos.enable=false&release=2.7.18&service.name=ServiceBean:/cn.lazyfennec.dubbo.sms.api.ISmsService&side=provider&timestamp=1669300093371, dubbo version: 2.7.18, current host: 192.168.1.5
    2022-11-24 22:28:15.487 [MigrationRuleListener:66] -  [DUBBO] config center is not configured!, dubbo version: 2.7.18, current host: 192.168.1.5
    2022-11-24 22:28:15.491 [MigrationRuleListener:83] -  [DUBBO] Using the following migration rule to migrate:, dubbo version: 2.7.18, current host: 192.168.1.5
    2022-11-24 22:28:15.491 [MigrationRuleListener:84] -  [DUBBO] INIT, dubbo version: 2.7.18, current host: 192.168.1.5
    2022-11-24 22:28:15.615 [AbstractMetadataReport:259] -  [DUBBO] store provider metadata. Identifier : org.apache.dubbo.metadata.report.identifier.MetadataIdentifier@2534e317; definition: FullServiceDefinition{parameters={side=provider, service.name=ServiceBean:/cn.lazyfennec.dubbo.sms.api.ISmsService, release=2.7.18, methods=send, deprecated=false, dubbo=2.0.2, interface=cn.lazyfennec.dubbo.sms.api.ISmsService, qos.enable=false, generic=false, default=true, metadata-type=remote, application=sms-service, dynamic=true, anyhost=true}} ServiceDefinition [canonicalName=cn.lazyfennec.dubbo.sms.api.ISmsService, codeSource=file:/G:/neteasy/subject-4/2.3-%e6%89%8b%e5%86%99RPC%e6%a1%86%e6%9e%b6/neco/dubbo/service-api/target/classes/, methods=[MethodDefinition [name=send, parameterTypes=[java.lang.String, java.lang.String], returnType=java.lang.Object]]], dubbo version: 2.7.18, current host: 192.168.1.5
    2022-11-24 22:28:15.628 [DubboBootstrap:915] -  [DUBBO] DubboBootstrap is ready., dubbo version: 2.7.18, current host: 192.168.1.5
    2022-11-24 22:28:15.632 [DubboBootstrap:921] -  [DUBBO] DubboBootstrap has started., dubbo version: 2.7.18, current host: 192.168.1.5
    2022-11-24 22:28:29.721 [RedisRegistry$NotifySub:385] -  [DUBBO] redis event: /dubbo/cn.lazyfennec.dubbo.sms.api.ISmsService/consumers = register, dubbo version: 2.7.18, current host: 192.168.1.5
    2022-11-24 22:28:32.106 [NettyServerHandler:76] -  [DUBBO] The connection of /192.168.1.5:51021 -> /192.168.1.5:20880 is established., dubbo version: 2.7.18, current host: 192.168.1.5
    2022-11-24 22:28:32.543 [RedisRegistry$NotifySub:385] -  [DUBBO] redis event: /dubbo/cn.lazyfennec.dubbo.sms.api.ISmsService/consumers = unregister, dubbo version: 2.7.18, current host: 192.168.1.5
    2022-11-24 22:28:32.603 [AbstractServer:174] -  [DUBBO] All clients has disconnected from /192.168.1.5:20880. You can graceful shutdown now., dubbo version: 2.7.18, current host: 192.168.1.5
    2022-11-24 22:28:32.604 [NettyServerHandler:91] -  [DUBBO] The connection of /192.168.1.5:51021 -> /192.168.1.5:20880 is disconnected., dubbo version: 2.7.18, current host: 192.168.1.5
    2022-11-24 22:32:53.618 [RedisRegistry$NotifySub:385] -  [DUBBO] redis event: /dubbo/cn.lazyfennec.dubbo.sms.api.ISmsService/consumers = register, dubbo version: 2.7.18, current host: 192.168.1.5
    2022-11-24 22:32:55.865 [NettyServerHandler:76] -  [DUBBO] The connection of /192.168.1.5:51327 -> /192.168.1.5:20880 is established., dubbo version: 2.7.18, current host: 192.168.1.5
    
    4.4 order-service
    • 修改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>cn.lazyfennec</groupId>
        <artifactId>order-service</artifactId>
        <version>1.0.0</version>
    
        <dependencies>
            <!--系统模块依赖-->
            <dependency>
                <groupId>cn.lazyfennec</groupId>
                <artifactId>service-api</artifactId>
                <version>1.0.0</version>
            </dependency>
            <!-- 框架依赖 -->
            <!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo -->
            <!-- 新的版本似乎移除了一些东西,这里只是用来作为一个例子,先就这样吧 -->
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo</artifactId>
                <version>2.7.18</version>
            </dependency>
            <dependency>
                <groupId>redis.clients</groupId>
                <artifactId>jedis</artifactId>
                <version>3.8.0</version>
            </dependency>
            <!--暂时用一下老版本,看一下打印的内容-->
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>1.2.17</version>
            </dependency>
        </dependencies>
    </project>
    
    • 在resources文件夹下创建dubbo.properties
    # 名称
    dubbo.application.name=order-service
    # 注册地址
    dubbo.registry.address=redis://192.168.1.9:6379
    # 过期时间
    dubbo.consumer.timeout=1000
    # Quality of Service 禁用
    dubbo.application.qosEnable=false
    
    • 在resources文件夹下创建log4j.properties
    # Set log levels #
    log4j.rootLogger = info, Console
    
    # Output the log info to the Java Console #
    log4j.appender.Console = org.apache.log4j.ConsoleAppender   
    log4j.appender.Console.Target = System.out   
    log4j.appender.Console.ImmediateFlush = true  
    log4j.appender.Console.Threshold = DEBUG   
    log4j.appender.Console.layout = org.apache.log4j.PatternLayout   
    log4j.appender.Console.layout.ConversionPattern =%d{yyyy-MM-dd HH:mm:ss.SSS} [%C{1}:%L] - %m%n
    
    • 创建OrderServiceImpl,该类实现 IOrderService 接口,这里先不用@DubboService 进行注解,具体原因,可以想象一下,但是需要注意里边的 @DubboReference 表示这个是Dubbo服务引用。
    package cn.lazyfennec.dubbo.order;
    
    import cn.lazyfennec.dubbo.order.api.IOrderService;
    import cn.lazyfennec.dubbo.sms.api.ISmsService;
    import org.apache.dubbo.config.annotation.DubboReference;
    import org.springframework.stereotype.Service;
    
    /**
     * @Author: Neco
     * @Description:
     * @Date: create in 2022/11/24 16:50
     */
    @Service
    public class OrderServiceImpl implements IOrderService {
    
        @DubboReference
        ISmsService smsService;
    
        @Override
        public void create(String orderContent) {
            System.out.println("订单创建成功:" + orderContent);
            Object sms = smsService.send("183xxxxxxxx", "您的订单创建成功了: " + orderContent);
            System.out.println("smsService调用结果:" + sms);
        }
    }
    
    • 创建启动类 OrderApplication。
    package cn.lazyfennec.dubbo;
    
    import cn.lazyfennec.dubbo.order.api.IOrderService;
    import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.PropertySource;
    
    import java.io.IOException;
    import java.util.concurrent.CyclicBarrier;
    
    /**
     * @Author: Neco
     * @Description:
     * @Date: create in 2022/11/24 16:51
     */
    @Configuration
    @ComponentScan("cn.lazyfennec.dubbo") // spring注解扫描
    @PropertySource("classpath:/dubbo.properties")
    @EnableDubbo(scanBasePackages = "cn.lazyfennec.dubbo")
    public class OrderApplication {
        public static void main(String[] args) throws IOException {
            AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(OrderApplication.class);
            context.start();
    
            IOrderService orderService = context.getBean(IOrderService.class);
            orderService.create("买一瓶水");
    
            // 阻塞不退出
            System.in.read();
            context.close();
        }
    }
    
    4.5 启动并且查看测试结果
    • OrderApplication


    • SmsApplication



    如果觉得有收获,欢迎点赞和评论,更多知识,请点击关注查看我的主页信息哦~

    相关文章

      网友评论

        本文标题:Dubbo应用与整体结构

        本文链接:https://www.haomeiwen.com/subject/vmfaxdtx.html