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

无天有壁纸 2024-05-06 22:34:44

上一篇我们简单介绍了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">然后重新启动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"/> </beans>

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

0 阅读:6

无天有壁纸

简介:感谢大家的关注