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