• Dubbo入门使用


    Dubbo的特性

    • 服务治理框架
    • 服务的监控
    • 服务的注册发现
    • 服务的通信
    • 服务的容错
    • 服务的负载均衡

    dubbo支持的注册中心

    • nacos
    • console
    • zookeeper
    • redis
    • eureka
    • etcd ……

    Dubbo入门案例

    通过两个demo来演示如何使用dubbo进行RPC调用
    在这里插入图片描述
    创建一个dubbo服务提供者和一个服务消费者的普通java工程:
    在这里插入图片描述
    对服务提供者,创建一个dubbo-server-api子模块和dubbo-server模块,其中dubbo-server-api模块用来定义公共的接口
    在这里插入图片描述
    在api模块定义一个接口并打包,给dubbo client端和server端的 dubbo-server模块去依赖:
    在这里插入图片描述
    install api模块:
    在这里插入图片描述
    dubbo-server模块来实现这个定义的接口(dubbo-server首先添加dubbo-server-api的依赖)
    在这里插入图片描述
    问题来了,这里dubbo-client-service模块跟dubbo-server-service模块是两个跨进程的模块,client要调用server的接口,就得借助RPC框架来实现远程调用了,因此两个模块都需要添加dubbo依赖:

      <dependency>
          <groupId>org.apache.dubbogroupId>
          <artifactId>dubboartifactId>
          <version>2.7.8version>
        dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    然后创建resources目录(mark as resources directory),添加dubbo配置文件,发布dubbo服务:

    
    <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-server"/>
        
        <dubbo:protocol port="20880" name="dubbo"/>
        
        <dubbo:registry address="N/A"/>
        
        <dubbo:service interface="com.lchtest.dubbo.server.ILoginService" ref="loginService"/>
        <bean id="loginService" class="com.lchtest.dubbo.server.LoginServiceImpl"/>
    
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    使用dubbo的启动类运行dubbo-server模块:
    在这里插入图片描述
    可以看到启动日志中打印了dubbo对外提供服务的协议url, 以及底层使用了netty通信框架:
    在这里插入图片描述
    服务消费端:
    在这里插入图片描述
    上面是通过普通java项目来实现dubbo服务调用的过程,不涉及服务治理和注册中心,接下来使用zookeeper作为注册中心,启动windows版本的zookeeper, 然后两个java项目都添加 zookeeper依赖:

       <dependency>
          
          <groupId>org.apache.dubbogroupId>
          <artifactId>dubbo-dependencies-zookeeperartifactId>
          <version>2.7.8version>
          <type>pomtype>
        dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    并修改两个application.xml文件中的dubbo注册中心为: ,client去掉 dubbo refrence标签里面的url配置:

    
    <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-client"/>
    
        <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
        
        <dubbo:reference id="loginServiceImpl" interface="com.lchtest.dubbo.server.ILoginService"/>
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    启动dubbo-server和dubbo-client , 即可运行:
    在这里插入图片描述
    但是,这个时候,服务提供这个消费者都没有日志了,需要自己配置下log4j日志:

    ###set log levels###
    log4j.rootLogger=debug, stdout
    ###output to the console###
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.Target=System.out
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=[%d{dd/MM/yy HH:mm:ss:SSS z}] %t %5p %c{2}: %m%n
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    源码: https://download.csdn.net/download/weixin_41300437/87088930?spm=1001.2014.3001.5501

  • 相关阅读:
    C#/Vsto中CustomTaskPanes和Ribbon的使用方法
    GIS数据获取:土地利用与土壤属性、DEM、水体水系数据
    每年大促都要补习JVM调优,今年我都总结到这里了。
    OPPO,被折叠的高端之路
    pdf怎么转图片,可得到高清图
    ABP - 初识 ABP
    Jenkins
    【自学笔记】Python中的逻辑函数:any()、all()及同类函数的用法与示例
    【PAT】数据结构树和图月考复习1
    微信小程序进阶——Flex弹性布局&轮播图&会议OA项目(首页)
  • 原文地址:https://blog.csdn.net/weixin_41300437/article/details/127934535