Nacos(九)之Dubbo 融合 Nacos 成为注册中心
转载自 Dubbo 融合 Nacos 成为注册中心
Nacos 作为 Dubbo 生态系统中重要的注册中心实现,本文将会介绍如何进行 Dubbo 对接 Nacos 注册中心的工作。
预备工作
请确保后台已经启动 Nacos 服务,可先行参考 Nacos 快速入门。
快速上手
Dubbo 融合 Nacos 成为注册中心的操作步骤非常简单,大致步骤可分为“增加 Maven 依赖”以及“配置注册中心“。
增加 Maven 依赖
只需要依赖Dubbo客户端即可,关于推荐的使用版本,请参考Dubbo官方文档或者咨询Dubbo开发人员:
<dependencies>...<!-- Dubbo dependency --><dependency><groupId>com.alibaba</groupId><artifactId>dubbo</artifactId><version>[latest version]</version></dependency><!-- 使用Spring装配方式时可选: --><dependency><groupId>com.alibaba.spring</groupId><artifactId>spring-context-support</artifactId><version>[latest version]</version></dependency>...</dependencies>
配置注册中心
假设您 Dubbo 应用使用 Spring Framework 装配,将有两种配置方法可选,分别为:Dubbo Spring 外部化配置以及 Spring XML 配置文件以及,笔者强烈推荐前者。
Dubbo Spring 外部化配置
Dubbo Spring 外部化配置是由 Dubbo 2.5.8
引入的新特性,可通过 Spring Environment
属性自动地生成并绑定 Dubbo 配置 Bean,实现配置简化,并且降低微服务开发门槛。
假设您 Dubbo 应用的使用 Zookeeper 作为注册中心,并且其服务器 IP 地址为:10.20.153.10
,同时,该注册地址作为 Dubbo 外部化配置属性存储在 dubbo-config.properties
文件,如下所示:
## application
dubbo.application.name = your-dubbo-application## Zookeeper registry address
dubbo.registry.address = zookeeper://10.20.153.10:2181
...
假设您的 Nacos Server 同样运行在服务器 10.20.153.10
上,并使用默认 Nacos 服务端口 8848
,您只需将 dubbo.registry.address
属性调整如下:
## 其他属性保持不变## Nacos registry address
dubbo.registry.address = nacos://10.20.153.10:8848
##如果要使用自己创建的命名空间可以使用下面2种方式
#dubbo.registry.address = nacos://10.20.153.10:8848?namespace=5cbb70a5-xxx-xxx-xxx-d43479ae0932
#dubbo.registry.parameters.namespace=5cbb70a5-xxx-xxx-xxx-d43479ae0932
...
随后,重启您的 Dubbo 应用,Dubbo 的服务提供和消费信息在 Nacos 控制台中可以显示:
如图所示,服务名前缀为 providers:
的信息为服务提供者的元信息,consumers:
则代表服务消费者的元信息。点击“详情”可查看服务状态详情:
如果您正在使用 Spring XML 配置文件装配 Dubbo 注册中心的话,请参考下一节。
Spring XML 配置文件
同样,假设您 Dubbo 应用的使用 Zookeeper 作为注册中心,并且其服务器 IP 地址为:10.20.153.10
,并且装配 Spring Bean 在 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="dubbo-provider-xml-demo" /><!-- 使用 Zookeeper 注册中心 --><dubbo:registry address="zookeeper://10.20.153.10:2181" />...
</beans>
与 Dubbo Spring 外部化配置 配置类似,只需要调整 address
属性配置即可:
<?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="dubbo-provider-xml-demo" /><!-- 使用 Nacos 注册中心 --><dubbo:registry address="nacos://10.20.153.10:8848" /><!-- 如果要使用自己创建的命名空间可以使用下面配置 --><!-- <dubbo:registry address="nacos://10.20.153.10:8848?namespace=5cbb70a5-xxx-xxx-xxx-d43479ae0932" /> -->...
</beans>
重启 Dubbo 应用后,您同样也能发现服务提供方和消费方的注册元信息呈现在 Nacos 控制台中:
您是否绝对配置或切换 Nacos 注册中心超级 Easy 呢?如果您仍旧意犹未尽或者不甚明白的话,可参考以下完整的示例。
完整示例
以上图片中的元数据源于 Dubbo Spring 注解驱动示例以及 Dubbo Spring XML 配置驱动示例,下面将分别介绍两者,您可以选择自己偏好的编程模型。在正式讨论之前,先来介绍两者的预备工作,因为它们皆依赖 Java 服务接口和实现。同时,请确保本地(127.0.0.1
)环境已启动 Nacos 服务。
示例接口与实现
首先定义示例接口,如下所示:
package com.alibaba.dubbo.demo.service;/*** DemoService** @since 2.6.5*/
public interface DemoService {String sayName(String name);}
提供以上接口的实现类:
package com.alibaba.dubbo.demo.service;import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.dubbo.rpc.RpcContext;import org.springframework.beans.factory.annotation.Value;/*** Default {@link DemoService}** @since 2.6.5*/
@Service(version = "${demo.service.version}")
public class DefaultService implements DemoService {@Value("${demo.service.name}")private String serviceName;public String sayName(String name) {RpcContext rpcContext = RpcContext.getContext();return String.format("Service [name :%s , port : %d] %s(\"%s\") : Hello,%s",serviceName,rpcContext.getLocalPort(),rpcContext.getMethodName(),name,name);}
}
接口与实现准备妥当后,下面将采用注解驱动和 XML 配置驱动各自实现。
Spring 注解驱动示例
Dubbo 2.5.7
重构了 Spring 注解驱动的编程模型。
服务提供方注解驱动实现
- 定义 Dubbo 提供方外部化配置属性源 -
provider-config.properties
## application
dubbo.application.name = dubbo-provider-demo## Nacos registry address
dubbo.registry.address = nacos://127.0.0.1:8848
##如果要使用自己创建的命名空间可以使用下面2种方式
#dubbo.registry.address = nacos://127.0.0.1:8848?namespace=5cbb70a5-xxx-xxx-xxx-d43479ae0932
#dubbo.registry.parameters.namespace=5cbb70a5-xxx-xxx-xxx-d43479ae0932## Dubbo Protocol
dubbo.protocol.name = dubbo
dubbo.protocol.port = -1# Provider @Service version
demo.service.version=1.0.0
demo.service.name = demoService
- 实现服务提供方引导类 -
DemoServiceProviderBootstrap
package com.alibaba.dubbo.demo.provider;import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import com.alibaba.dubbo.demo.service.DemoService;import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.PropertySource;import java.io.IOException;/*** {@link DemoService} provider demo*/
@EnableDubbo(scanBasePackages = "com.alibaba.dubbo.demo.service")
@PropertySource(value = "classpath:/provider-config.properties")
public class DemoServiceProviderBootstrap {public static void main(String[] args) throws IOException {AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();context.register(DemoServiceProviderBootstrap.class);context.refresh();System.out.println("DemoService provider is starting...");System.in.read();}
}
其中注解 @EnableDubbo
激活 Dubbo 注解驱动以及外部化配置,其 scanBasePackages
属性扫描指定 Java 包,将所有标注 @Service
的服务接口实现类暴露为 Spring Bean,随即被导出 Dubbo 服务。
@PropertySource
是 Spring Framework 3.1 引入的标准导入属性配置资源注解,它将为 Dubbo 提供外部化配置。
服务消费方注解驱动实现
- 定义 Dubbo 消费方外部化配置属性源 -
consumer-config.properties
## Dubbo Application info
dubbo.application.name = dubbo-consumer-demo## Nacos registry address
dubbo.registry.address = nacos://127.0.0.1:8848
##如果要使用自己创建的命名空间可以使用下面2种方式
#dubbo.registry.address = nacos://127.0.0.1:8848?namespace=5cbb70a5-xxx-xxx-xxx-d43479ae0932
#dubbo.registry.parameters.namespace=5cbb70a5-xxx-xxx-xxx-d43479ae0932# @Reference version
demo.service.version= 1.0.0
同样地,dubbo.registry.address
属性指向 Nacos 注册中心,其他 Dubbo 服务相关的元信息通过 Nacos 注册中心获取。
- 实现服务消费方引导类 -
DemoServiceConsumerBootstrap
package com.alibaba.dubbo.demo.consumer;import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import com.alibaba.dubbo.demo.service.DemoService;import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.PropertySource;import javax.annotation.PostConstruct;
import java.io.IOException;/*** {@link DemoService} consumer demo*/
@EnableDubbo
@PropertySource(value = "classpath:/consumer-config.properties")
public class DemoServiceConsumerBootstrap {@Reference(version = "${demo.service.version}")private DemoService demoService;@PostConstructpublic void init() {for (int i = 0; i < 10; i++) {System.out.println(demoService.sayName("小马哥(mercyblitz)"));}}public static void main(String[] args) throws IOException {AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();context.register(DemoServiceConsumerBootstrap.class);context.refresh();context.close();}
}
同样地,@EnableDubbo
注解激活 Dubbo 注解驱动和外部化配置,不过当前属于服务消费者,无需指定 Java 包名扫描标注 @Service
的服务实现。
@Reference
是 Dubbo 远程服务的依赖注入注解,需要服务提供方和消费端约定接口(interface)、版本(version)以及分组(group)信息。在当前服务消费示例中,DemoService
的服务版本来源于属性配置文件 consumer-config.properties
。
@PostConstruct
部分代码则说明当 DemoServiceConsumerBootstrap
Bean 初始化时,执行十次 Dubbo 远程方法调用。
运行注解驱动示例
在本地启动两次 DemoServiceProviderBootstrap
,注册中心将出现两个健康服务:
再运行 DemoServiceConsumerBootstrap
,运行结果如下:
Service [name :demoService , port : 20880] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)
Service [name :demoService , port : 20881] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)
Service [name :demoService , port : 20880] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)
Service [name :demoService , port : 20880] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)
Service [name :demoService , port : 20881] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)
Service [name :demoService , port : 20881] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)
Service [name :demoService , port : 20880] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)
Service [name :demoService , port : 20880] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)
Service [name :demoService , port : 20881] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)
Service [name :demoService , port : 20881] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)
运行无误,并且服务消费方使用了负载均衡策略,将十次 RPC 调用平均分摊到两个 Dubbo 服务提供方实例中。
Spring XML 配置驱动示例
Spring XML 配置驱动是传统 Spring 装配组件的编程模型。
服务提供方 XML 配置驱动
- 定义服务提供方 XML 上下文配置文件 -
/META-INF/spring/dubbo-provider-context.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="dubbo-provider-xml-demo"/><!-- 使用 Nacos 注册中心 --><dubbo:registry address="nacos://127.0.0.1:8848"/><!-- 如果要使用自己创建的命名空间可以使用下面配置 --><!-- <dubbo:registry address="nacos://127.0.0.1:8848?namespace=5cbb70a5-xxx-xxx-xxx-d43479ae0932" /> --><!-- 用dubbo协议在随机端口暴露服务 --><dubbo:protocol name="dubbo" port="-1"/><!-- 声明需要暴露的服务接口 --><dubbo:service interface="com.alibaba.dubbo.demo.service.DemoService" ref="demoService" version="2.0.0"/><!-- 和本地bean一样实现服务 --><bean id="demoService" class="com.alibaba.dubbo.demo.service.DefaultService"/>
</beans>
- 实现服务提供方引导类 -
DemoServiceProviderXmlBootstrap
package com.alibaba.dubbo.demo.provider;import com.alibaba.dubbo.demo.service.DemoService;import org.springframework.context.support.ClassPathXmlApplicationContext;import java.io.IOException;/*** {@link DemoService} provider demo XML bootstrap*/
public class DemoServiceProviderXmlBootstrap {public static void main(String[] args) throws IOException {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext();context.setConfigLocation("/META-INF/spring/dubbo-provider-context.xml");context.refresh();System.out.println("DemoService provider (XML) is starting...");System.in.read();}
}
服务消费方 XML 配置驱动
- 定义服务消费方 XML 上下文配置文件 -
/META-INF/spring/dubbo-consumer-context.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="dubbo-consumer-xml-demo"/><!-- 使用 Nacos 注册中心 --><dubbo:registry address="nacos://127.0.0.1:8848"/><!-- 如果要使用自己创建的命名空间可以使用下面配置 --><!-- <dubbo:registry address="nacos://127.0.0.1:8848?namespace=5cbb70a5-xxx-xxx-xxx-d43479ae0932" /> --><!-- 引用服务接口 --><dubbo:reference id="demoService" interface="com.alibaba.dubbo.demo.service.DemoService" version="2.0.0"/></beans>
- 实现服务消费方引导类 -
DemoServiceConsumerXmlBootstrap
package com.alibaba.dubbo.demo.consumer;import com.alibaba.dubbo.demo.service.DemoService;import org.springframework.context.support.ClassPathXmlApplicationContext;import java.io.IOException;/*** {@link DemoService} consumer demo XML bootstrap*/
public class DemoServiceConsumerXmlBootstrap {public static void main(String[] args) throws IOException {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext();context.setConfigLocation("/META-INF/spring/dubbo-consumer-context.xml");context.refresh();System.out.println("DemoService consumer (XML) is starting...");DemoService demoService = context.getBean("demoService", DemoService.class);for (int i = 0; i < 10; i++) {System.out.println(demoService.sayName("小马哥(mercyblitz)"));}context.close();}
}
运行 XML 配置驱动示例
同样地,先启动两个 DemoServiceProviderXmlBootstrap
引导类,观察 Nacos 注册中心服务提供者变化:
XML 配置驱动的服务版本为 2.0.0
,因此注册服务无误。
再运行服务消费者引导类 DemoServiceConsumerXmlBootstrap
,观察控制台输出内容:
Service [name :null , port : 20882] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)
Service [name :null , port : 20882] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)
Service [name :null , port : 20883] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)
Service [name :null , port : 20882] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)
Service [name :null , port : 20882] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)
Service [name :null , port : 20883] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)
Service [name :null , port : 20882] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)
Service [name :null , port : 20883] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)
Service [name :null , port : 20883] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)
Service [name :null , port : 20883] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)
结果同样运行和负载均衡正常,不过由于当前示例尚未添加属性 demo.service.name
的缘故,因此,“name”部分信息输出为 null
。
如果您关注或喜爱 Dubbo 以及 Nacos 等开源工程,不妨为它们点 “star”,加油打气链接:
- Apache Dubbo:https://github.com/apache/dubbo
- Dubbo Nacos Registry:https://github.com/apache/dubbo/tree/master/dubbo-registry/dubbo-registry-nacos
- Alibaba Nacos:https://github.com/alibaba/nacos
Nacos(九)之Dubbo 融合 Nacos 成为注册中心相关推荐
- Dubbo 融合 Nacos 成为注册中心
Nacos 作为 Dubbo 生态系统中重要的注册中心实现,其中 dubbo-registry-nacos 则是 Dubbo 融合 Nacos 注册中心的实现. 预备工作 当您将 dubbo-regi ...
- 源码分析Dubbo前置篇-寻找注册中心、服务提供者、服务消费者功能入口
本节主要阐述如下两个问题: 1.Dubbo自定义标签实现. 2.dubbo通过Spring加载配置文件后,是如何触发注册中心.服务提供者.服务消费者按照Dubbo的设计执行相关的功能. 所谓的执 ...
- Dubbo使用multicast广播注册中心暴露服务地址时启动报错empty notify
2019独角兽企业重金招聘Python工程师标准>>> 开发机上Dubbo使用multicast广播注册中心暴露服务地址 <dubbo:registry protocol=&q ...
- 记录 - Dubbo使用Zookeeper为注册中心添加认证口令时,连接失败的故障
记录 - Dubbo使用Zookeeper为注册中心添加认证口令时,连接失败的故障 故障现象 配置信息 Dubbo服务配置 报错信息 初始化信息 报错的重点信息 报错信息跟踪 解决思路 设置超时时间后 ...
- 【Dubbo】扩展 etcd 注册中心
主要内容 : etcd 数据结构设计 : 构建可运行的注册中心 : 搭建 etcd 集群并在 Dubbo 中运行 . 着重从扩展 Dubbo 新注册中心方面入手 , 重点说明深入开发 Dubbo 注册 ...
- Dubbo之旅--扩展注册中心
在上篇文章中我们介绍了关于协议的扩展,并了解扩展它所需要的需求.本篇主要是对注册中心的扩展进行着重的探索. 同样的问题,为什么我们需要去扩展注册中心的?主要有以下三个需求. (1) 多注册中心注册 需 ...
- nacos enablediscoveryclient_Spring Cloud(五):注册中心nacos篇
点击蓝字关注我们 大家好,我是杰哥,转眼间,我们的Spring Cloud专辑已到了第五次分享,而我们的注册中心篇章已经分别完成了Eureka.zookeeper篇,今天正式进入nacos篇的学习na ...
- Nacos,一款非常优秀的注册中心(附视频)
Nacos 核心源码精讲 - IT贱男 - 掘金小册全方位源码精讲,深度剖析 Nacos 注册中心和配置中心的核心思想.「Nacos 核心源码精讲」由IT贱男撰写,375人购买https://s.ju ...
- Dubbo环境搭建-ZooKeeper注册中心
场景 Dubbo简介与基本概念: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/103555224 注: 博客: https:// ...
最新文章
- php禁止代理ip访问_php禁止某ip或ip地址段访问的方法
- RequisitePro SQL SERVER数据库的配置
- c语言笔试面试大全,C语言笔试面试题大全.doc
- boost::statechart模块实现无效结果分配测试
- linux loop device
- mongodb msi安装包_跟我快速学数据存储:MongoDB非关系型数据库
- SpringBoot 异常回滚 事务的使用___Springboot @Transactional 事务不回滚
- 以生活例子说明单线程与多线程
- “十亿赌约”,雷军输,董明珠胜?
- 1.5.2 编译java程序
- mysql编写触发器语法_mysql触发器语法
- 专用计算机国产化替代工作方案,倪光南再谈核心技术自主可控 列举国产替代方案...
- 回归的误差服从正态分布吗_计量经济学中为什么误差项u服从正态分布,则系数也服从正态分布...
- java囧囧西游之大闹天宫下载_最新囧囧西游之大闹天宫榜单下载_九游
- 小米34寸带鱼屏显示器的耗电计算
- NCRE教材笔记 第一章操作系统概论
- PAKDD2020 阿里巴巴智能运维算法大赛 总结(初赛33,决赛19)
- 微信小程序评论功能的实现(用的是假数据)
- 如何用计算机做函数图像,用计算机画函数图像 优秀教学实录
- 遥感影像如何导入Photoshop软件进行PS调色之智拼图Photoshop交互工具使用技巧
热门文章
- 非抢占式优先算法例题_非抢占短作业优先算法源代码(C语言)
- java pc计数器_java虚拟机-程序计数器PC Register
- 后端学习 - 计算机网络
- leetcode349. 两个数组的交集(思路+详解)
- html css图标怎么跟文字并排,html - FA图标和文字环绕的HTML / CSS问题 - SO中文参考 - www.soinside.com...
- [Java基础]LinkedList集合的特有功能
- [Java基础]List集合
- [蓝桥杯][算法提高VIP]聪明的美食家-dp
- 高等数学上-赵立军-北京大学出版社-题解-练习3.4
- hash table(开放寻址法-线性探查实现的哈希表)