参考文档:
1.快速搭建
1.1.provider暴露服务
provider.xml:
<?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://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://dubbo.apache.org/schema/dubbo
http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="hello-world-app" />
<!-- 使用multicast广播注册中心暴露服务地址 -->
<dubbo:registry address="127.0.0.1:1234" />
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.mine.dubbo.demo.DemoService" ref="demoService" />
<!-- 和本地bean一样实现服务 -->
<bean id="demoService" class="com.mine.dubbo.demo.provider.DemoServiceImpl" />
</beans>
1.2.consumer使用服务
consumer.xml:
<?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://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://dubbo.apache.org/schema/dubbo
http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
<dubbo:application name="consumer-of-helloworld-app" />
<!-- 使用multicast广播注册中心暴露发现服务地址 -->
<dubbo:registry address="127.0.0.1:1234" />
<!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<dubbo:reference id="demoService" interface="com.mine.dubbo.demo.DemoService" />
</beans>
1.3.Spring Boot项目中引入dubbo配置文件:
在启动类上加注解:
@ImportResource(locations = {"classpath:xxx/dubbo/provider.xml","classpath:xxx/dubbo/consumer.xml"})
2.注意事项:
2.1.缺省协议是dubbo协议,dubbo接口的入参和返回值都需要实现Serializable
接口,还有其它一些约束,参考:官方文档-协议-dubbo://;其它协议及约束请查看:官方文档-协议
2.2直连调试
provider.xml无需特殊配置,仅consumer.xml需要进行配置:
<dubbo:reference id="xxxService"
interface="com.mine.dubbo.demo.DemoService"
url="127.0.0.1:20880" />
若要获取dubbo接口调用的返回值,请勿将<dubbo:reference>的async属性(异步执行开关)设置为true,否则获取到的值为null,更多相关属性请参考:官方文档-reference
3.dubbo接口调试
3.1.通过telnet命令进入dubbo命令行
3.2.ls可列出所有已暴露服务,cd进对应服务后,ls列出该服务下所有方法
3.3.invoke ${服务}.${method}即可调用对应dubbo接口,获取返回值
注:linux下可以复制粘贴,windows环境下无法复制,建议在linux环境下使用该功能