dubbo入门

本文档介绍了如何使用Dubbo进行分布式服务开发。首先,Dubbo主要用于实现服务端和客户端的分布式部署。接着,展示了如何通过Spring集成Dubbo,并在dubbo.properties中配置。在服务端,定义了Service Interface及其实现类,并在provider.xml中配置。客户端则通过consumer.xml配置文件获取服务。最后,提供了服务端和客户端的测试代码,但内容未完全展开。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

百度百科:Dubbo(开源分布式服务框架)_百度百科

首先我们使用dubbo一般是用来做分布式,也就是说服务端和客户端不在一个服务器。

Spring集成

新增dubbo.properties

##
#
# dubbo config
#
##
 
# 是否启用dubbo
dubbo.enable=true

dubbo.applicationName=XXXX
 
# dubbo的注册中心可以是zookeeper redis
dubbo.registryProtocol=zookeeper
 
# 例如xx.xx.xx.xx:2181,xx.xx.xx.xx::2181,xx.xx.xx.xx::2181
dubbo.registryAddress=xx.xx.xx.xx::2181,xx.xx.xx.xx::2181,xx.xx.xx.xx::2181
 
# zookeeper根节点,如果多套环境用一套zookeeper,这个地方一定要区分
dubbo.registryGroup=dubbo-pro
 
# dubbo的注册中心的用户和密码,如:zookeeper的用户 密码  redis的用户密码
dubbo.registryUsername=
dubbo.registryPassword=
# dubbo的监听端口
dubbo.protocolPort=20880
# 最大线程数
dubbo.protocolThreads=300
 
 
#dubbo2.7.x新增配置
#dubbo 配置中心
dubbo.configCenterAddress=ip1:2181,ip2:2181,ip3:2181
dubbo.configCenterProtocol=zookeeper
#dubbo 元数据中心
dubbo.metaReportAddress=zookeeper://zookeeper-0.zookeeper.gtms-preprod.svc.cluster.local:2181,zookeeper-1.zookeeper.gtms-preprod.svc.cluster.local:2181,zookeeper-2.zookeeper.gtms-preprod.svc.cluster.local:2181
#dubbo 服务统计配置 是否启用
dubbo.enableMetrics=true
#dubbo 生产者服务统计配置 端口和协议,端口默认和dubbo的监听端口一致
dubbo.preMetricsPort=20880
dubbo.preMetricsProtocol=dubbo

开发使用

通过<dubbo.../> 配置统一地址

配置服务端<dubbo:serviceinterface="com.alibaba.hello.api.HelloService"ref="helloService"/>

相应客户端

<dubbo:serviceinterface="com.alibaba.hello.api.HelloService"ref="helloService"/>

再结合spring框架,服务端配置<beanid> 客户端通过contenxt上下文获取到

服务端

定义一个Service Interface:(HelloService.java

1

2

3

4

5

6

7

8

package com.alibaba.hello.api;

public interface HelloService

{

  String sayHello(String name);

}

接口的实现类:(HelloServiceImpl.java

1

2

3

4

5

6

7

package com.alibaba.hello.impl;

import com.alibaba.hello.api.HelloService;

public  class  HelloServiceImpl  implements  HelloService{

    public  String  sayHello(String  name){

        return  "Hello" + name;

    }

}

Spring配置:(provider.xml

1

2

3

4

5

6

7

8

9

10

11

12

13

<?xmlversion="1.0"encoding="UTF-8"?>

<beans......>

    <!--Applicationname-->

    <dubbo:applicationname="hello-world-app"/>

    <!--registryaddress,usedforservicetoregisteritself-->

    <dubbo:registryaddress="multicast://224.5.6.7:1234"/>

    <!--exposethisservicethroughdubboprotocol,throughport20880-->

    <dubbo:protocolname="dubbo"port="20880"/>

    <!--whichserviceinterfacedoweexpose?-->

    <dubbo:serviceinterface="com.alibaba.hello.api.HelloService"ref="helloService"/>

    <!--designateimplementation-->

    <beanid="helloService"class="com.alibaba.hello.impl.HelloServiceImpl"/>

</beans>

测试代码:(Provider.java

1

2

3

4

5

6

7

importorg.springframework.context.support.ClassPathXmlApplicationContext;

public  class  Provider{

    public  static  void  main(String[]  args){

        ClassPathXmlApplicationContext  context = new  ClassPathXmlApplicationContext(newString[]{"provider.xml"});

        //启动成功,监听端口为20880System.in.read();//按任意键退出

    }

}

客户端

Spring配置文件:(consumer.xml)

1

2

3

4

5

6

7

8

9

<?xmlversion="1.0"encoding="UTF-8"?>

<beans xmlns=......>

    <!--consumerapplicationname-->

    <dubbo:applicationname="consumer-of-helloworld-app"/>

    <!--registryaddress,usedforconsumertodiscoverservices-->

    <dubbo:registryaddress="multicast://224.5.6.7:1234"/>

    <!--whichservicetoconsume?-->

    <dubbo:referenceid="helloService"interface="com.alibaba.hello.api.HelloService"/>

</beans>

客户端测试代码:(Consumer.java

1

2

3

4

5

6

7

8

9

10

11

import  org.springframework.context.support.ClassPathXmlApplicationContext;

import  com.alibaba.hello.api.HelloService;

public  class  Consumer{

    public  static  void  main(String[]  args){

        ClassPathXmlApplicationContext  context = new  ClassPathXmlApplicationContext(newString[]{"consumer.xml"});

        HelloService  helloService = (HelloService)context.getBean("helloService");

        //getserviceinvocationproxyStringhello=helloService.sayHello("world");

        //doinvoke!System.out.println(hello);

        //cool,howareyou~

    }

}

未完待续.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值