Dubbo快速使用手册

参考文档:

dubbo官方文档

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环境下使用该功能

Telnet命令参考手册

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值