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.dubbo</groupId>
      <artifactId>dubbo</artifactId>
      <version>2.7.8</version>
    </dependency>

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

<?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: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标签是必须配置的,N/A表示不需要注册到注册中心 -->
    <dubbo:registry address="N/A"/>
    <!-- 指定需要对外发布的服务的接口,必须指定ref,指定具体的接口实现 -->
    <dubbo:service interface="com.lchtest.dubbo.server.ILoginService" ref="loginService"/>
    <bean id="loginService" class="com.lchtest.dubbo.server.LoginServiceImpl"/>
<!-- dubbo启动后,日志打印 [DUBBO] Export dubbo service com.lchtest.dubbo.server.ILoginService to url dubbo://192.168.61.1:20880/com.lchtest.dubbo.server.ILoginService -->
</beans>

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

   <dependency>
      <!-- 下载不到依赖解决方法:https://blog.csdn.net/manDD_HH/article/details/109445337      需要设置依赖类型为pom!!!-->
      <groupId>org.apache.dubbo</groupId>
      <artifactId>dubbo-dependencies-zookeeper</artifactId>
      <version>2.7.8</version>
      <type>pom</type>
    </dependency>

并修改两个application.xml文件中的dubbo注册中心为: <dubbo:registry address=“zookeeper://127.0.0.1:2181”/> ,client去掉 dubbo refrence标签里面的url配置:

<?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: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="N/A"/>-->
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
    <!-- 指定了注册中心地址,这里可以去掉url-->
    <dubbo:reference id="loginServiceImpl" interface="com.lchtest.dubbo.server.ILoginService"/>
</beans>

启动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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值