Dubbo - 基于注册中心发布Dubbo服务

上一篇我们简单介绍了dubbo,并通过一个简单的例子介绍了如何使用dubbo注册、发布服务,以及客户端如何通过通过dubbo进行远程服务调用。 这一篇介绍一下如何基于注册中心发布Dubbo服务。

基于注册中心的 Dubbo 服务

作为主流的服务治理组件,Dubbo 提供了很多丰富的功能,那么最根本的 就是要解决大规模集群之后的服务注册和发现的问题。而 dubbo 中对于注 册中心这块是使用zookeeper来支撑的。当然在目前最新的版本中。 Dubbo能够支持的注册中心有:consul、etcd、nacos、sofa、zookeeper、 redis、multicast
在这里插入图片描述
在我们的例子中是用zookeeper作为注册中心。首先我们需要添加zookeeper的依赖jar包

		<dependency>
	  		<groupId>org.apache.curator</groupId>
	  		<artifactId>curator-framework</artifactId>
	  		<version>4.0.0</version>
	  	</dependency>
	  	<dependency>
	  		<groupId>org.apache.curator</groupId>
	  		<artifactId>curator-recipes</artifactId>
	  		<version>4.0.0</version>
	  	</dependency>

然后修改application.xml:

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
       
       <!-- 服务提供方应用信息 -->
       <dubbo:application name="practice-service" />
       
       <!-- 使用zookeeper注册中心暴露服务地址 -->
       <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
       <!-- 使用dubbo协议在20880端口暴露服务 -->
       <dubbo:protocol name="dubbo" port="20880"/>
       <!-- 声明需要暴露的服务接口 -->
       <dubbo:service interface="org.practice.service.api.LoginService" ref="loginService"/>
       
       <bean id="loginService" class="org.practice.service.provider.LoginServiceImpl"/>
       
</beans>

然后重新启动practice-service-provider,启动好之后在zookeeper中可以看到我们的服务已经注册到zookeeper中了:
在这里插入图片描述
改造客户端:
application.xml:

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
       
       <!-- 服务提供方应用信息 -->
       <dubbo:application name="practice-client" />
       
       <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
       <!-- 通过reference标签获取服务 -->
       <dubbo:reference id="loginService" interface="org.practice.service.api.LoginService"/>
       
</beans>

然后重新运行客户端,可以看到成功调用远程服务并返回结果。

以上就是基于zookeeper作为注册中心来实现远程调用,其它注册中心的实现也是类似的。

这里有一个问题,dubbo 每次都要连 zookeeper? 是不是每次发起一个请求的时候,都需要访问注册中心 呢?作为一个设计者,你会怎么去解决这个问题。 答案很显然是通过缓存实现 在消费端的配置文件中指定如下路径

<dubbo:registry id="zookeeper"  address="zookeeper://127.0.0.1:2181" file="d:/dubboserver" /> 

多注册中心的支持

有的时候客户端需要调用的远程服务不在同一个注册中心中,那么客户端就要配置多个注册中心来访问。下面通过一个例子来展示如何配置多个注册中心。

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
       
       <!-- 服务提供方应用信息 -->
       <dubbo:application name="practice-service" />
       
       <!-- 使用zookeeper注册中心暴露服务地址 -->
       <dubbo:registry id="registryCenter1" address="zookeeper://127.0.0.1:2181"/>
       <dubbo:registry id="registryCenter2" address="zookeeper://127.0.0.1:2181"/>
       <!-- 使用dubbo协议在20880端口暴露服务 -->
       <dubbo:protocol name="dubbo" port="20880"/>
       <!-- 声明需要暴露的服务接口 -->
       <dubbo:service interface="org.practice.service.api.LoginService" ref="loginService" registry="registryCenter1 registryCenter2"/>
       
       <bean id="loginService" class="org.practice.service.provider.LoginServiceImpl"/>
       
</beans>

如上面代码所示,我们可以配置多个注册中心,然后在service中通过registry属性来指定该服务要注册到哪个注册中心中。客户端的配置和服务端类似。

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

无法无天过路客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值