From 3f57d8ee2f83d36f74e04664260047ebeac2240f Mon Sep 17 00:00:00 2001 From: moriadry Date: Sun, 19 May 2019 19:47:53 +0800 Subject: [PATCH 1/3] fix 404 link --- blog/zh-cn/dubbo-registry-nacos-integration.md | 8 ++++---- zh-cn/blog/dubbo-registry-nacos-integration.html | 8 ++++---- zh-cn/blog/dubbo-registry-nacos-integration.json | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/blog/zh-cn/dubbo-registry-nacos-integration.md b/blog/zh-cn/dubbo-registry-nacos-integration.md index 9eee7d0d5f4e..103adfa5f6f3 100644 --- a/blog/zh-cn/dubbo-registry-nacos-integration.md +++ b/blog/zh-cn/dubbo-registry-nacos-integration.md @@ -1,12 +1,12 @@ # Dubbo 融合 Nacos 成为注册中心 -Nacos 作为 Dubbo 生态系统中重要的注册中心实现,其中 [`dubbo-registry-nacos`](https://github.com/dubbo/dubbo-registry-nacos) 则是 Dubbo 融合 Nacos 注册中心的实现。 +Nacos 作为 Dubbo 生态系统中重要的注册中心实现,其中 [`dubbo-registry-nacos`](https://github.com/apache/incubator-dubbo/tree/master/dubbo-registry/dubbo-registry-nacos) 则是 Dubbo 融合 Nacos 注册中心的实现。 ## 预备工作 -当您将 [`dubbo-registry-nacos`](https://github.com/dubbo/dubbo-registry-nacos) 整合到您的 Dubbo 工程之前,请确保后台已经启动 Nacos 服务。如果您尚且不熟悉 Nacos 的基本使用的话,可先行参考 [Nacos 快速入门](https://nacos.io/en-us/docs/quick-start.html):https://nacos.io/en-us/docs/quick-start.html。建议使用 Nacos `0.6.1` 以上的版本。 +当您将 [`dubbo-registry-nacos`](hhttps://github.com/apache/incubator-dubbo/tree/master/dubbo-registry/dubbo-registry-nacos) 整合到您的 Dubbo 工程之前,请确保后台已经启动 Nacos 服务。如果您尚且不熟悉 Nacos 的基本使用的话,可先行参考 [Nacos 快速入门](https://nacos.io/en-us/docs/quick-start.html):https://nacos.io/en-us/docs/quick-start.html。建议使用 Nacos `0.6.1` 以上的版本。 @@ -544,12 +544,12 @@ Service [name :null , port : 20883] sayName("小马哥(mercyblitz)") : Hello Service [name :null , port : 20883] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz) ``` -结果同样运行和负载均衡正常,不过由于当前示例尚未添加属性 `demo.service.name` 的缘故,因此,“name”部分信息输出为 `null`。更多内容请参考:https://github.com/dubbo/dubbo-registry-nacos。 +结果同样运行和负载均衡正常,不过由于当前示例尚未添加属性 `demo.service.name` 的缘故,因此,“name”部分信息输出为 `null`。更多内容请参考:https://github.com/apache/incubator-dubbo/tree/master/dubbo-registry/dubbo-registry-nacos。 如果您关注或喜爱 Dubbo 以及 Nacos 等开源工程,不妨为它们点 “star”,加油打气链接: - Apache Dubbo:https://github.com/apache/incubator-dubbo -- Dubbo Nacos Registry:https://github.com/dubbo/dubbo-registry-nacos +- Dubbo Nacos Registry:https://github.com/apache/incubator-dubbo/tree/master/dubbo-registry/dubbo-registry-nacos - Alibaba Nacos:https://github.com/alibaba/nacos \ No newline at end of file diff --git a/zh-cn/blog/dubbo-registry-nacos-integration.html b/zh-cn/blog/dubbo-registry-nacos-integration.html index 201a851ad6e1..970e08491320 100644 --- a/zh-cn/blog/dubbo-registry-nacos-integration.html +++ b/zh-cn/blog/dubbo-registry-nacos-integration.html @@ -13,9 +13,9 @@

Dubbo 融合 Nacos 成为注册中心

-

Nacos 作为 Dubbo 生态系统中重要的注册中心实现,其中 dubbo-registry-nacos 则是 Dubbo 融合 Nacos 注册中心的实现。

+

Nacos 作为 Dubbo 生态系统中重要的注册中心实现,其中 dubbo-registry-nacos 则是 Dubbo 融合 Nacos 注册中心的实现。

预备工作

-

当您将 dubbo-registry-nacos 整合到您的 Dubbo 工程之前,请确保后台已经启动 Nacos 服务。如果您尚且不熟悉 Nacos 的基本使用的话,可先行参考 Nacos 快速入门https://nacos.io/en-us/docs/quick-start.html。建议使用 Nacos 0.6.1 以上的版本。

+

当您将 dubbo-registry-nacos 整合到您的 Dubbo 工程之前,请确保后台已经启动 Nacos 服务。如果您尚且不熟悉 Nacos 的基本使用的话,可先行参考 Nacos 快速入门https://nacos.io/en-us/docs/quick-start.html。建议使用 Nacos 0.6.1 以上的版本。

快速上手

Dubbo 融合 Nacos 成为注册中心的操作步骤非常简单,大致步骤可分为“增加 Maven 依赖”以及“配置注册中心“。

增加 Maven 依赖

@@ -408,11 +408,11 @@

运行 XML 配置驱动示例

Service [name :null , port : 20883] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz) Service [name :null , port : 20883] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz) -

结果同样运行和负载均衡正常,不过由于当前示例尚未添加属性 demo.service.name 的缘故,因此,“name”部分信息输出为 null。更多内容请参考:https://github.com/dubbo/dubbo-registry-nacos。

+

结果同样运行和负载均衡正常,不过由于当前示例尚未添加属性 demo.service.name 的缘故,因此,“name”部分信息输出为 null。更多内容请参考:https://github.com/apache/incubator-dubbo/tree/master/dubbo-registry/dubbo-registry-nacos。

如果您关注或喜爱 Dubbo 以及 Nacos 等开源工程,不妨为它们点 “star”,加油打气链接:

diff --git a/zh-cn/blog/dubbo-registry-nacos-integration.json b/zh-cn/blog/dubbo-registry-nacos-integration.json index 82c766195963..2db7c5276c32 100644 --- a/zh-cn/blog/dubbo-registry-nacos-integration.json +++ b/zh-cn/blog/dubbo-registry-nacos-integration.json @@ -1,6 +1,6 @@ { "filename": "dubbo-registry-nacos-integration.md", - "__html": "

Dubbo 融合 Nacos 成为注册中心

\n

Nacos 作为 Dubbo 生态系统中重要的注册中心实现,其中 dubbo-registry-nacos 则是 Dubbo 融合 Nacos 注册中心的实现。

\n

预备工作

\n

当您将 dubbo-registry-nacos 整合到您的 Dubbo 工程之前,请确保后台已经启动 Nacos 服务。如果您尚且不熟悉 Nacos 的基本使用的话,可先行参考 Nacos 快速入门https://nacos.io/en-us/docs/quick-start.html。建议使用 Nacos 0.6.1 以上的版本。

\n

快速上手

\n

Dubbo 融合 Nacos 成为注册中心的操作步骤非常简单,大致步骤可分为“增加 Maven 依赖”以及“配置注册中心“。

\n

增加 Maven 依赖

\n

首先,您需要 dubbo-registry-nacos 的 Maven 依赖添加到您的项目中 pom.xml 文件中,并且强烈地推荐您使用 Dubbo 2.6.5

\n
<dependencies>\n\n    ...\n        \n    <!-- Dubbo Nacos registry dependency -->\n    <dependency>\n        <groupId>com.alibaba</groupId>\n        <artifactId>dubbo-registry-nacos</artifactId>\n        <version>0.0.2</version>\n    </dependency>   \n    \n    <!-- Keep latest Nacos client version -->\n    <dependency>\n        <groupId>com.alibaba.nacos</groupId>\n        <artifactId>nacos-client</artifactId>\n        <version>[0.6.1,)</version>\n    </dependency>\n    \n    <!-- Dubbo dependency -->\n    <dependency>\n        <groupId>com.alibaba</groupId>\n        <artifactId>dubbo</artifactId>\n        <version>2.6.5</version>\n    </dependency>\n    \n    <!-- Alibaba Spring Context extension -->\n    <dependency>\n        <groupId>com.alibaba.spring</groupId>\n        <artifactId>spring-context-support</artifactId>\n        <version>1.0.2</version>\n    </dependency>\n\n    ...\n    \n</dependencies>\n
\n

当项目中添加 dubbo-registry-nacos 后,您无需显示地编程实现服务发现和注册逻辑,实际实现由该三方包提供,接下来配置 Naocs 注册中心。

\n

配置注册中心

\n

假设您 Dubbo 应用使用 Spring Framework 装配,将有两种配置方法可选,分别为:Dubbo Spring 外部化配置以及 Spring XML 配置文件以及 ,笔者强烈推荐前者。

\n

Dubbo Spring 外部化配置

\n

Dubbo Spring 外部化配置是由 Dubbo 2.5.8 引入的新特性,可通过 Spring Environment 属性自动地生成并绑定 Dubbo 配置 Bean,实现配置简化,并且降低微服务开发门槛。

\n

假设您 Dubbo 应用的使用 Zookeeper 作为注册中心,并且其服务器 IP 地址为:10.20.153.10,同时,该注册地址作为 Dubbo 外部化配置属性存储在 dubbo-config.properties 文件,如下所示:

\n
## application\ndubbo.application.name = your-dubbo-application\n\n## Zookeeper registry address\ndubbo.registry.address = zookeeper://10.20.153.10:2181\n...\n
\n

假设您的 Nacos Server 同样运行在服务器 10.20.153.10 上,并使用默认 Nacos 服务端口 8848,您只需将 dubbo.registry.address 属性调整如下:

\n
## 其他属性保持不变\n\n## Nacos registry address\ndubbo.registry.address = nacos://10.20.153.10:8848\n...\n
\n

随后,重启您的 Dubbo 应用,Dubbo 的服务提供和消费信息在 Nacos 控制台中可以显示:

\n

\"image-20181213103845976\"

\n

如图所示,服务名前缀为 providers: 的信息为服务提供者的元信息,consumers: 则代表服务消费者的元信息。点击“详情”可查看服务状态详情:

\n

\"image-20181213104145998\"

\n

如果您正在使用 Spring XML 配置文件装配 Dubbo 注册中心的话,请参考下一节。

\n

Spring XML 配置文件

\n

同样,假设您 Dubbo 应用的使用 Zookeeper 作为注册中心,并且其服务器 IP 地址为:10.20.153.10,并且装配 Spring Bean 在 XML 文件中,如下所示:

\n
<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<beans xmlns=\"http://www.springframework.org/schema/beans\"\n    xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n    xmlns:dubbo=\"http://dubbo.apache.org/schema/dubbo\"\n    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\">\n \n    <!-- 提供方应用信息,用于计算依赖关系 -->\n    <dubbo:application name=\"dubbo-provider-xml-demo\"  />\n \n    <!-- 使用 Zookeeper 注册中心 -->\n    <dubbo:registry address=\"zookeeper://10.20.153.10:2181\" />\n \t...\n</beans>\n
\n

Dubbo Spring 外部化配置 配置类似,只需要调整 address 属性配置即可:

\n
<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<beans xmlns=\"http://www.springframework.org/schema/beans\"\n    xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n    xmlns:dubbo=\"http://dubbo.apache.org/schema/dubbo\"\n    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\">\n \n    <!-- 提供方应用信息,用于计算依赖关系 -->\n    <dubbo:application name=\"dubbo-provider-xml-demo\"  />\n \n    <!-- 使用 Nacos 注册中心 -->\n    <dubbo:registry address=\"nacos://10.20.153.10:8848\" />\n \t...\n</beans>\n
\n

重启 Dubbo 应用后,您同样也能发现服务提供方和消费方的注册元信息呈现在 Nacos 控制台中:

\n

\"image-20181213113049185\"

\n

您是否绝对配置或切换 Nacos 注册中心超级 Easy 呢?如果您仍旧意犹未尽或者不甚明白的话,可参考以下完整的示例。

\n

完整示例

\n

以上图片中的元数据源于 Dubbo Spring 注解驱动示例以及 Dubbo Spring XML 配置驱动示例,下面将分别介绍两者,您可以选择自己偏好的编程模型。在正式讨论之前,先来介绍两者的预备工作,因为它们皆依赖 Java 服务接口和实现。同时,请确保本地(127.0.0.1)环境已启动 Nacos 服务

\n

示例接口与实现

\n

首先定义示例接口,如下所示:

\n
package com.alibaba.dubbo.demo.service;\n\n/**\n * DemoService\n *\n * @since 2.6.5\n */\npublic interface DemoService {\n\n    String sayName(String name);\n\n}\n
\n

提供以上接口的实现类:

\n
package com.alibaba.dubbo.demo.service;\n\nimport com.alibaba.dubbo.config.annotation.Service;\nimport com.alibaba.dubbo.rpc.RpcContext;\n\nimport org.springframework.beans.factory.annotation.Value;\n\n/**\n * Default {@link DemoService}\n *\n * @since 2.6.5\n */\n@Service(version = \"${demo.service.version}\")\npublic class DefaultService implements DemoService {\n\n    @Value(\"${demo.service.name}\")\n    private String serviceName;\n\n    public String sayName(String name) {\n        RpcContext rpcContext = RpcContext.getContext();\n        return String.format(\"Service [name :%s , port : %d] %s(\\\"%s\\\") : Hello,%s\",\n                serviceName,\n                rpcContext.getLocalPort(),\n                rpcContext.getMethodName(),\n                name,\n                name);\n    }\n}\n
\n

接口与实现准备妥当后,下面将采用注解驱动和 XML 配置驱动各自实现。

\n

Spring 注解驱动示例

\n

Dubbo 2.5.7 重构了 Spring 注解驱动的编程模型。

\n

服务提供方注解驱动实现

\n\n
## application\ndubbo.application.name = dubbo-provider-demo\n\n## Nacos registry address\ndubbo.registry.address = nacos://127.0.0.1:8848\n\n## Dubbo Protocol\ndubbo.protocol.name = dubbo\ndubbo.protocol.port = -1\n\n# Provider @Service version\ndemo.service.version=1.0.0\ndemo.service.name = demoService\n
\n\n
package com.alibaba.dubbo.demo.provider;\n\nimport com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;\nimport com.alibaba.dubbo.demo.service.DemoService;\n\nimport org.springframework.context.annotation.AnnotationConfigApplicationContext;\nimport org.springframework.context.annotation.PropertySource;\n\nimport java.io.IOException;\n\n/**\n * {@link DemoService} provider demo\n */\n@EnableDubbo(scanBasePackages = \"com.alibaba.dubbo.demo.service\")\n@PropertySource(value = \"classpath:/provider-config.properties\")\npublic class DemoServiceProviderBootstrap {\n\n    public static void main(String[] args) throws IOException {\n        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();\n        context.register(DemoServiceProviderBootstrap.class);\n        context.refresh();\n        System.out.println(\"DemoService provider is starting...\");\n        System.in.read();\n    }\n}\n\n
\n

其中注解 @EnableDubbo 激活 Dubbo 注解驱动以及外部化配置,其 scanBasePackages 属性扫描指定 Java 包,将所有标注 @Service 的服务接口实现类暴露为 Spring Bean,随即被导出 Dubbo 服务。

\n

@PropertySource 是 Spring Framework 3.1 引入的标准导入属性配置资源注解,它将为 Dubbo 提供外部化配置。

\n

服务消费方注解驱动实现

\n\n
## Dubbo Application info\ndubbo.application.name = dubbo-consumer-demo\n\n## Nacos registry address\ndubbo.registry.address = nacos://127.0.0.1:8848\n\n# @Reference version\ndemo.service.version= 1.0.0\n
\n

同样地,dubbo.registry.address 属性指向 Nacos 注册中心,其他 Dubbo 服务相关的元信息通过 Nacos 注册中心获取。

\n\n
package com.alibaba.dubbo.demo.consumer;\n\nimport com.alibaba.dubbo.config.annotation.Reference;\nimport com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;\nimport com.alibaba.dubbo.demo.service.DemoService;\n\nimport org.springframework.context.annotation.AnnotationConfigApplicationContext;\nimport org.springframework.context.annotation.PropertySource;\n\nimport javax.annotation.PostConstruct;\nimport java.io.IOException;\n\n/**\n * {@link DemoService} consumer demo\n */\n@EnableDubbo\n@PropertySource(value = \"classpath:/consumer-config.properties\")\npublic class DemoServiceConsumerBootstrap {\n\n    @Reference(version = \"${demo.service.version}\")\n    private DemoService demoService;\n\n    @PostConstruct\n    public void init() {\n        for (int i = 0; i < 10; i++) {\n            System.out.println(demoService.sayName(\"小马哥(mercyblitz)\"));\n        }\n    }\n\n    public static void main(String[] args) throws IOException {\n        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();\n        context.register(DemoServiceConsumerBootstrap.class);\n        context.refresh();\n        context.close();\n    }\n}\n\n
\n

同样地,@EnableDubbo 注解激活 Dubbo 注解驱动和外部化配置,不过当前属于服务消费者,无需指定 Java 包名扫描标注 @Service 的服务实现。

\n

@Reference 是 Dubbo 远程服务的依赖注入注解,需要服务提供方和消费端约定接口(interface)、版本(version)以及分组(group)信息。在当前服务消费示例中,DemoService 的服务版本来源于属性配置文件 consumer-config.properties

\n

@PostConstruct 部分代码则说明当 DemoServiceConsumerBootstrap Bean 初始化时,执行十次 Dubbo 远程方法调用。

\n

运行注解驱动示例

\n

在本地启动两次 DemoServiceProviderBootstrap,注册中心将出现两个健康服务:

\n

\"image-20181213123909636\"

\n

再运行 DemoServiceConsumerBootstrap,运行结果如下:

\n
Service [name :demoService , port : 20880] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :demoService , port : 20881] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :demoService , port : 20880] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :demoService , port : 20880] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :demoService , port : 20881] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :demoService , port : 20881] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :demoService , port : 20880] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :demoService , port : 20880] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :demoService , port : 20881] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :demoService , port : 20881] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\n
\n

运行无误,并且服务消费方使用了负载均衡策略,将十次 RPC 调用平均分摊到两个 Dubbo 服务提供方实例中。

\n

Spring XML 配置驱动示例

\n

Spring XML 配置驱动是传统 Spring 装配组件的编程模型。

\n

服务提供方 XML 配置驱动

\n\n
<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<beans xmlns=\"http://www.springframework.org/schema/beans\"\n       xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n       xmlns:dubbo=\"http://dubbo.apache.org/schema/dubbo\"\n       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\">\n\n    <!-- 提供方应用信息,用于计算依赖关系 -->\n    <dubbo:application name=\"dubbo-provider-xml-demo\"/>\n\n    <!-- 使用 Nacos 注册中心 -->\n    <dubbo:registry address=\"nacos://127.0.0.1:8848\"/>\n\n    <!-- 用dubbo协议在随机端口暴露服务 -->\n    <dubbo:protocol name=\"dubbo\" port=\"-1\"/>\n\n    <!-- 声明需要暴露的服务接口 -->\n    <dubbo:service interface=\"com.alibaba.dubbo.demo.service.DemoService\" ref=\"demoService\" version=\"2.0.0\"/>\n\n    <!-- 和本地bean一样实现服务 -->\n    <bean id=\"demoService\" class=\"com.alibaba.dubbo.demo.service.DefaultService\"/>\n</beans>\n
\n\n
package com.alibaba.dubbo.demo.provider;\n\nimport com.alibaba.dubbo.demo.service.DemoService;\n\nimport org.springframework.context.support.ClassPathXmlApplicationContext;\n\nimport java.io.IOException;\n\n/**\n * {@link DemoService} provider demo XML bootstrap\n */\npublic class DemoServiceProviderXmlBootstrap {\n\n    public static void main(String[] args) throws IOException {\n        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext();\n        context.setConfigLocation(\"/META-INF/spring/dubbo-provider-context.xml\");\n        context.refresh();\n        System.out.println(\"DemoService provider (XML) is starting...\");\n        System.in.read();\n    }\n}\n
\n

服务消费方 XML 配置驱动

\n\n
<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<beans xmlns=\"http://www.springframework.org/schema/beans\"\n       xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n       xmlns:dubbo=\"http://dubbo.apache.org/schema/dubbo\"\n       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\">\n\n    <!-- 提供方应用信息,用于计算依赖关系 -->\n    <dubbo:application name=\"dubbo-consumer-xml-demo\"/>\n\n    <!-- 使用 Nacos 注册中心 -->\n    <dubbo:registry address=\"nacos://127.0.0.1:8848\"/>\n\n    <!-- 引用服务接口 -->\n    <dubbo:reference id=\"demoService\" interface=\"com.alibaba.dubbo.demo.service.DemoService\" version=\"2.0.0\"/>\n\n</beans>\n
\n\n
package com.alibaba.dubbo.demo.consumer;\n\nimport com.alibaba.dubbo.demo.service.DemoService;\n\nimport org.springframework.context.support.ClassPathXmlApplicationContext;\n\nimport java.io.IOException;\n\n/**\n * {@link DemoService} consumer demo XML bootstrap\n */\npublic class DemoServiceConsumerXmlBootstrap {\n\n    public static void main(String[] args) throws IOException {\n        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext();\n        context.setConfigLocation(\"/META-INF/spring/dubbo-consumer-context.xml\");\n        context.refresh();\n        System.out.println(\"DemoService consumer (XML) is starting...\");\n        DemoService demoService = context.getBean(\"demoService\", DemoService.class);\n        for (int i = 0; i < 10; i++) {\n            System.out.println(demoService.sayName(\"小马哥(mercyblitz)\"));\n        }\n        context.close();\n    }\n}\n
\n

运行 XML 配置驱动示例

\n

同样地,先启动两个 DemoServiceProviderXmlBootstrap 引导类,观察 Nacos 注册中心服务提供者变化:

\n

\"image-20181213125527201\"

\n

XML 配置驱动的服务版本为 2.0.0,因此注册服务无误。

\n

再运行服务消费者引导类 DemoServiceConsumerXmlBootstrap,观察控制台输出内容:

\n
Service [name :null , port : 20882] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :null , port : 20882] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :null , port : 20883] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :null , port : 20882] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :null , port : 20882] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :null , port : 20883] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :null , port : 20882] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :null , port : 20883] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :null , port : 20883] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :null , port : 20883] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\n
\n

结果同样运行和负载均衡正常,不过由于当前示例尚未添加属性 demo.service.name 的缘故,因此,“name”部分信息输出为 null。更多内容请参考:https://github.com/dubbo/dubbo-registry-nacos。

\n

如果您关注或喜爱 Dubbo 以及 Nacos 等开源工程,不妨为它们点 “star”,加油打气链接:

\n\n", + "__html": "

Dubbo 融合 Nacos 成为注册中心

\n

Nacos 作为 Dubbo 生态系统中重要的注册中心实现,其中 dubbo-registry-nacos 则是 Dubbo 融合 Nacos 注册中心的实现。

\n

预备工作

\n

当您将 dubbo-registry-nacos 整合到您的 Dubbo 工程之前,请确保后台已经启动 Nacos 服务。如果您尚且不熟悉 Nacos 的基本使用的话,可先行参考 Nacos 快速入门https://nacos.io/en-us/docs/quick-start.html。建议使用 Nacos 0.6.1 以上的版本。

\n

快速上手

\n

Dubbo 融合 Nacos 成为注册中心的操作步骤非常简单,大致步骤可分为“增加 Maven 依赖”以及“配置注册中心“。

\n

增加 Maven 依赖

\n

首先,您需要 dubbo-registry-nacos 的 Maven 依赖添加到您的项目中 pom.xml 文件中,并且强烈地推荐您使用 Dubbo 2.6.5

\n
<dependencies>\n\n    ...\n        \n    <!-- Dubbo Nacos registry dependency -->\n    <dependency>\n        <groupId>com.alibaba</groupId>\n        <artifactId>dubbo-registry-nacos</artifactId>\n        <version>0.0.2</version>\n    </dependency>   \n    \n    <!-- Keep latest Nacos client version -->\n    <dependency>\n        <groupId>com.alibaba.nacos</groupId>\n        <artifactId>nacos-client</artifactId>\n        <version>[0.6.1,)</version>\n    </dependency>\n    \n    <!-- Dubbo dependency -->\n    <dependency>\n        <groupId>com.alibaba</groupId>\n        <artifactId>dubbo</artifactId>\n        <version>2.6.5</version>\n    </dependency>\n    \n    <!-- Alibaba Spring Context extension -->\n    <dependency>\n        <groupId>com.alibaba.spring</groupId>\n        <artifactId>spring-context-support</artifactId>\n        <version>1.0.2</version>\n    </dependency>\n\n    ...\n    \n</dependencies>\n
\n

当项目中添加 dubbo-registry-nacos 后,您无需显示地编程实现服务发现和注册逻辑,实际实现由该三方包提供,接下来配置 Naocs 注册中心。

\n

配置注册中心

\n

假设您 Dubbo 应用使用 Spring Framework 装配,将有两种配置方法可选,分别为:Dubbo Spring 外部化配置以及 Spring XML 配置文件以及 ,笔者强烈推荐前者。

\n

Dubbo Spring 外部化配置

\n

Dubbo Spring 外部化配置是由 Dubbo 2.5.8 引入的新特性,可通过 Spring Environment 属性自动地生成并绑定 Dubbo 配置 Bean,实现配置简化,并且降低微服务开发门槛。

\n

假设您 Dubbo 应用的使用 Zookeeper 作为注册中心,并且其服务器 IP 地址为:10.20.153.10,同时,该注册地址作为 Dubbo 外部化配置属性存储在 dubbo-config.properties 文件,如下所示:

\n
## application\ndubbo.application.name = your-dubbo-application\n\n## Zookeeper registry address\ndubbo.registry.address = zookeeper://10.20.153.10:2181\n...\n
\n

假设您的 Nacos Server 同样运行在服务器 10.20.153.10 上,并使用默认 Nacos 服务端口 8848,您只需将 dubbo.registry.address 属性调整如下:

\n
## 其他属性保持不变\n\n## Nacos registry address\ndubbo.registry.address = nacos://10.20.153.10:8848\n...\n
\n

随后,重启您的 Dubbo 应用,Dubbo 的服务提供和消费信息在 Nacos 控制台中可以显示:

\n

\"image-20181213103845976\"

\n

如图所示,服务名前缀为 providers: 的信息为服务提供者的元信息,consumers: 则代表服务消费者的元信息。点击“详情”可查看服务状态详情:

\n

\"image-20181213104145998\"

\n

如果您正在使用 Spring XML 配置文件装配 Dubbo 注册中心的话,请参考下一节。

\n

Spring XML 配置文件

\n

同样,假设您 Dubbo 应用的使用 Zookeeper 作为注册中心,并且其服务器 IP 地址为:10.20.153.10,并且装配 Spring Bean 在 XML 文件中,如下所示:

\n
<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<beans xmlns=\"http://www.springframework.org/schema/beans\"\n    xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n    xmlns:dubbo=\"http://dubbo.apache.org/schema/dubbo\"\n    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\">\n \n    <!-- 提供方应用信息,用于计算依赖关系 -->\n    <dubbo:application name=\"dubbo-provider-xml-demo\"  />\n \n    <!-- 使用 Zookeeper 注册中心 -->\n    <dubbo:registry address=\"zookeeper://10.20.153.10:2181\" />\n \t...\n</beans>\n
\n

Dubbo Spring 外部化配置 配置类似,只需要调整 address 属性配置即可:

\n
<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<beans xmlns=\"http://www.springframework.org/schema/beans\"\n    xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n    xmlns:dubbo=\"http://dubbo.apache.org/schema/dubbo\"\n    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\">\n \n    <!-- 提供方应用信息,用于计算依赖关系 -->\n    <dubbo:application name=\"dubbo-provider-xml-demo\"  />\n \n    <!-- 使用 Nacos 注册中心 -->\n    <dubbo:registry address=\"nacos://10.20.153.10:8848\" />\n \t...\n</beans>\n
\n

重启 Dubbo 应用后,您同样也能发现服务提供方和消费方的注册元信息呈现在 Nacos 控制台中:

\n

\"image-20181213113049185\"

\n

您是否绝对配置或切换 Nacos 注册中心超级 Easy 呢?如果您仍旧意犹未尽或者不甚明白的话,可参考以下完整的示例。

\n

完整示例

\n

以上图片中的元数据源于 Dubbo Spring 注解驱动示例以及 Dubbo Spring XML 配置驱动示例,下面将分别介绍两者,您可以选择自己偏好的编程模型。在正式讨论之前,先来介绍两者的预备工作,因为它们皆依赖 Java 服务接口和实现。同时,请确保本地(127.0.0.1)环境已启动 Nacos 服务

\n

示例接口与实现

\n

首先定义示例接口,如下所示:

\n
package com.alibaba.dubbo.demo.service;\n\n/**\n * DemoService\n *\n * @since 2.6.5\n */\npublic interface DemoService {\n\n    String sayName(String name);\n\n}\n
\n

提供以上接口的实现类:

\n
package com.alibaba.dubbo.demo.service;\n\nimport com.alibaba.dubbo.config.annotation.Service;\nimport com.alibaba.dubbo.rpc.RpcContext;\n\nimport org.springframework.beans.factory.annotation.Value;\n\n/**\n * Default {@link DemoService}\n *\n * @since 2.6.5\n */\n@Service(version = \"${demo.service.version}\")\npublic class DefaultService implements DemoService {\n\n    @Value(\"${demo.service.name}\")\n    private String serviceName;\n\n    public String sayName(String name) {\n        RpcContext rpcContext = RpcContext.getContext();\n        return String.format(\"Service [name :%s , port : %d] %s(\\\"%s\\\") : Hello,%s\",\n                serviceName,\n                rpcContext.getLocalPort(),\n                rpcContext.getMethodName(),\n                name,\n                name);\n    }\n}\n
\n

接口与实现准备妥当后,下面将采用注解驱动和 XML 配置驱动各自实现。

\n

Spring 注解驱动示例

\n

Dubbo 2.5.7 重构了 Spring 注解驱动的编程模型。

\n

服务提供方注解驱动实现

\n\n
## application\ndubbo.application.name = dubbo-provider-demo\n\n## Nacos registry address\ndubbo.registry.address = nacos://127.0.0.1:8848\n\n## Dubbo Protocol\ndubbo.protocol.name = dubbo\ndubbo.protocol.port = -1\n\n# Provider @Service version\ndemo.service.version=1.0.0\ndemo.service.name = demoService\n
\n\n
package com.alibaba.dubbo.demo.provider;\n\nimport com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;\nimport com.alibaba.dubbo.demo.service.DemoService;\n\nimport org.springframework.context.annotation.AnnotationConfigApplicationContext;\nimport org.springframework.context.annotation.PropertySource;\n\nimport java.io.IOException;\n\n/**\n * {@link DemoService} provider demo\n */\n@EnableDubbo(scanBasePackages = \"com.alibaba.dubbo.demo.service\")\n@PropertySource(value = \"classpath:/provider-config.properties\")\npublic class DemoServiceProviderBootstrap {\n\n    public static void main(String[] args) throws IOException {\n        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();\n        context.register(DemoServiceProviderBootstrap.class);\n        context.refresh();\n        System.out.println(\"DemoService provider is starting...\");\n        System.in.read();\n    }\n}\n\n
\n

其中注解 @EnableDubbo 激活 Dubbo 注解驱动以及外部化配置,其 scanBasePackages 属性扫描指定 Java 包,将所有标注 @Service 的服务接口实现类暴露为 Spring Bean,随即被导出 Dubbo 服务。

\n

@PropertySource 是 Spring Framework 3.1 引入的标准导入属性配置资源注解,它将为 Dubbo 提供外部化配置。

\n

服务消费方注解驱动实现

\n\n
## Dubbo Application info\ndubbo.application.name = dubbo-consumer-demo\n\n## Nacos registry address\ndubbo.registry.address = nacos://127.0.0.1:8848\n\n# @Reference version\ndemo.service.version= 1.0.0\n
\n

同样地,dubbo.registry.address 属性指向 Nacos 注册中心,其他 Dubbo 服务相关的元信息通过 Nacos 注册中心获取。

\n\n
package com.alibaba.dubbo.demo.consumer;\n\nimport com.alibaba.dubbo.config.annotation.Reference;\nimport com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;\nimport com.alibaba.dubbo.demo.service.DemoService;\n\nimport org.springframework.context.annotation.AnnotationConfigApplicationContext;\nimport org.springframework.context.annotation.PropertySource;\n\nimport javax.annotation.PostConstruct;\nimport java.io.IOException;\n\n/**\n * {@link DemoService} consumer demo\n */\n@EnableDubbo\n@PropertySource(value = \"classpath:/consumer-config.properties\")\npublic class DemoServiceConsumerBootstrap {\n\n    @Reference(version = \"${demo.service.version}\")\n    private DemoService demoService;\n\n    @PostConstruct\n    public void init() {\n        for (int i = 0; i < 10; i++) {\n            System.out.println(demoService.sayName(\"小马哥(mercyblitz)\"));\n        }\n    }\n\n    public static void main(String[] args) throws IOException {\n        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();\n        context.register(DemoServiceConsumerBootstrap.class);\n        context.refresh();\n        context.close();\n    }\n}\n\n
\n

同样地,@EnableDubbo 注解激活 Dubbo 注解驱动和外部化配置,不过当前属于服务消费者,无需指定 Java 包名扫描标注 @Service 的服务实现。

\n

@Reference 是 Dubbo 远程服务的依赖注入注解,需要服务提供方和消费端约定接口(interface)、版本(version)以及分组(group)信息。在当前服务消费示例中,DemoService 的服务版本来源于属性配置文件 consumer-config.properties

\n

@PostConstruct 部分代码则说明当 DemoServiceConsumerBootstrap Bean 初始化时,执行十次 Dubbo 远程方法调用。

\n

运行注解驱动示例

\n

在本地启动两次 DemoServiceProviderBootstrap,注册中心将出现两个健康服务:

\n

\"image-20181213123909636\"

\n

再运行 DemoServiceConsumerBootstrap,运行结果如下:

\n
Service [name :demoService , port : 20880] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :demoService , port : 20881] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :demoService , port : 20880] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :demoService , port : 20880] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :demoService , port : 20881] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :demoService , port : 20881] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :demoService , port : 20880] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :demoService , port : 20880] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :demoService , port : 20881] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :demoService , port : 20881] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\n
\n

运行无误,并且服务消费方使用了负载均衡策略,将十次 RPC 调用平均分摊到两个 Dubbo 服务提供方实例中。

\n

Spring XML 配置驱动示例

\n

Spring XML 配置驱动是传统 Spring 装配组件的编程模型。

\n

服务提供方 XML 配置驱动

\n\n
<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<beans xmlns=\"http://www.springframework.org/schema/beans\"\n       xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n       xmlns:dubbo=\"http://dubbo.apache.org/schema/dubbo\"\n       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\">\n\n    <!-- 提供方应用信息,用于计算依赖关系 -->\n    <dubbo:application name=\"dubbo-provider-xml-demo\"/>\n\n    <!-- 使用 Nacos 注册中心 -->\n    <dubbo:registry address=\"nacos://127.0.0.1:8848\"/>\n\n    <!-- 用dubbo协议在随机端口暴露服务 -->\n    <dubbo:protocol name=\"dubbo\" port=\"-1\"/>\n\n    <!-- 声明需要暴露的服务接口 -->\n    <dubbo:service interface=\"com.alibaba.dubbo.demo.service.DemoService\" ref=\"demoService\" version=\"2.0.0\"/>\n\n    <!-- 和本地bean一样实现服务 -->\n    <bean id=\"demoService\" class=\"com.alibaba.dubbo.demo.service.DefaultService\"/>\n</beans>\n
\n\n
package com.alibaba.dubbo.demo.provider;\n\nimport com.alibaba.dubbo.demo.service.DemoService;\n\nimport org.springframework.context.support.ClassPathXmlApplicationContext;\n\nimport java.io.IOException;\n\n/**\n * {@link DemoService} provider demo XML bootstrap\n */\npublic class DemoServiceProviderXmlBootstrap {\n\n    public static void main(String[] args) throws IOException {\n        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext();\n        context.setConfigLocation(\"/META-INF/spring/dubbo-provider-context.xml\");\n        context.refresh();\n        System.out.println(\"DemoService provider (XML) is starting...\");\n        System.in.read();\n    }\n}\n
\n

服务消费方 XML 配置驱动

\n\n
<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<beans xmlns=\"http://www.springframework.org/schema/beans\"\n       xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n       xmlns:dubbo=\"http://dubbo.apache.org/schema/dubbo\"\n       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\">\n\n    <!-- 提供方应用信息,用于计算依赖关系 -->\n    <dubbo:application name=\"dubbo-consumer-xml-demo\"/>\n\n    <!-- 使用 Nacos 注册中心 -->\n    <dubbo:registry address=\"nacos://127.0.0.1:8848\"/>\n\n    <!-- 引用服务接口 -->\n    <dubbo:reference id=\"demoService\" interface=\"com.alibaba.dubbo.demo.service.DemoService\" version=\"2.0.0\"/>\n\n</beans>\n
\n\n
package com.alibaba.dubbo.demo.consumer;\n\nimport com.alibaba.dubbo.demo.service.DemoService;\n\nimport org.springframework.context.support.ClassPathXmlApplicationContext;\n\nimport java.io.IOException;\n\n/**\n * {@link DemoService} consumer demo XML bootstrap\n */\npublic class DemoServiceConsumerXmlBootstrap {\n\n    public static void main(String[] args) throws IOException {\n        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext();\n        context.setConfigLocation(\"/META-INF/spring/dubbo-consumer-context.xml\");\n        context.refresh();\n        System.out.println(\"DemoService consumer (XML) is starting...\");\n        DemoService demoService = context.getBean(\"demoService\", DemoService.class);\n        for (int i = 0; i < 10; i++) {\n            System.out.println(demoService.sayName(\"小马哥(mercyblitz)\"));\n        }\n        context.close();\n    }\n}\n
\n

运行 XML 配置驱动示例

\n

同样地,先启动两个 DemoServiceProviderXmlBootstrap 引导类,观察 Nacos 注册中心服务提供者变化:

\n

\"image-20181213125527201\"

\n

XML 配置驱动的服务版本为 2.0.0,因此注册服务无误。

\n

再运行服务消费者引导类 DemoServiceConsumerXmlBootstrap,观察控制台输出内容:

\n
Service [name :null , port : 20882] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :null , port : 20882] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :null , port : 20883] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :null , port : 20882] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :null , port : 20882] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :null , port : 20883] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :null , port : 20882] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :null , port : 20883] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :null , port : 20883] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :null , port : 20883] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\n
\n

结果同样运行和负载均衡正常,不过由于当前示例尚未添加属性 demo.service.name 的缘故,因此,“name”部分信息输出为 null。更多内容请参考:https://github.com/dubbo/dubbo-registry-nacos。

\n

如果您关注或喜爱 Dubbo 以及 Nacos 等开源工程,不妨为它们点 “star”,加油打气链接:

\n\n", "link": "/zh-cn/blog/dubbo-registry-nacos-integration.html", "meta": {} } \ No newline at end of file From 7b1b1b46d1a89799bbc20aa244862666378c4b65 Mon Sep 17 00:00:00 2001 From: moriadry Date: Sun, 19 May 2019 19:49:14 +0800 Subject: [PATCH 2/3] fix --- blog/zh-cn/dubbo-registry-nacos-integration.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/blog/zh-cn/dubbo-registry-nacos-integration.md b/blog/zh-cn/dubbo-registry-nacos-integration.md index 103adfa5f6f3..eee8a43f05ed 100644 --- a/blog/zh-cn/dubbo-registry-nacos-integration.md +++ b/blog/zh-cn/dubbo-registry-nacos-integration.md @@ -6,7 +6,7 @@ Nacos 作为 Dubbo 生态系统中重要的注册中心实现,其中 [`dubbo-r ## 预备工作 -当您将 [`dubbo-registry-nacos`](hhttps://github.com/apache/incubator-dubbo/tree/master/dubbo-registry/dubbo-registry-nacos) 整合到您的 Dubbo 工程之前,请确保后台已经启动 Nacos 服务。如果您尚且不熟悉 Nacos 的基本使用的话,可先行参考 [Nacos 快速入门](https://nacos.io/en-us/docs/quick-start.html):https://nacos.io/en-us/docs/quick-start.html。建议使用 Nacos `0.6.1` 以上的版本。 +当您将 [`dubbo-registry-nacos`](https://github.com/apache/incubator-dubbo/tree/master/dubbo-registry/dubbo-registry-nacos) 整合到您的 Dubbo 工程之前,请确保后台已经启动 Nacos 服务。如果您尚且不熟悉 Nacos 的基本使用的话,可先行参考 [Nacos 快速入门](https://nacos.io/en-us/docs/quick-start.html):https://nacos.io/en-us/docs/quick-start.html。建议使用 Nacos `0.6.1` 以上的版本。 From 8316e71576d9ba20f706aa5e5781b60ad48d7d81 Mon Sep 17 00:00:00 2001 From: moriadry Date: Mon, 20 May 2019 17:31:06 +0800 Subject: [PATCH 3/3] only change md file --- zh-cn/blog/dubbo-registry-nacos-integration.html | 8 ++++---- zh-cn/blog/dubbo-registry-nacos-integration.json | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/zh-cn/blog/dubbo-registry-nacos-integration.html b/zh-cn/blog/dubbo-registry-nacos-integration.html index 970e08491320..201a851ad6e1 100644 --- a/zh-cn/blog/dubbo-registry-nacos-integration.html +++ b/zh-cn/blog/dubbo-registry-nacos-integration.html @@ -13,9 +13,9 @@

Dubbo 融合 Nacos 成为注册中心

-

Nacos 作为 Dubbo 生态系统中重要的注册中心实现,其中 dubbo-registry-nacos 则是 Dubbo 融合 Nacos 注册中心的实现。

+

Nacos 作为 Dubbo 生态系统中重要的注册中心实现,其中 dubbo-registry-nacos 则是 Dubbo 融合 Nacos 注册中心的实现。

预备工作

-

当您将 dubbo-registry-nacos 整合到您的 Dubbo 工程之前,请确保后台已经启动 Nacos 服务。如果您尚且不熟悉 Nacos 的基本使用的话,可先行参考 Nacos 快速入门https://nacos.io/en-us/docs/quick-start.html。建议使用 Nacos 0.6.1 以上的版本。

+

当您将 dubbo-registry-nacos 整合到您的 Dubbo 工程之前,请确保后台已经启动 Nacos 服务。如果您尚且不熟悉 Nacos 的基本使用的话,可先行参考 Nacos 快速入门https://nacos.io/en-us/docs/quick-start.html。建议使用 Nacos 0.6.1 以上的版本。

快速上手

Dubbo 融合 Nacos 成为注册中心的操作步骤非常简单,大致步骤可分为“增加 Maven 依赖”以及“配置注册中心“。

增加 Maven 依赖

@@ -408,11 +408,11 @@

运行 XML 配置驱动示例

Service [name :null , port : 20883] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz) Service [name :null , port : 20883] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz) -

结果同样运行和负载均衡正常,不过由于当前示例尚未添加属性 demo.service.name 的缘故,因此,“name”部分信息输出为 null。更多内容请参考:https://github.com/apache/incubator-dubbo/tree/master/dubbo-registry/dubbo-registry-nacos。

+

结果同样运行和负载均衡正常,不过由于当前示例尚未添加属性 demo.service.name 的缘故,因此,“name”部分信息输出为 null。更多内容请参考:https://github.com/dubbo/dubbo-registry-nacos。

如果您关注或喜爱 Dubbo 以及 Nacos 等开源工程,不妨为它们点 “star”,加油打气链接:

diff --git a/zh-cn/blog/dubbo-registry-nacos-integration.json b/zh-cn/blog/dubbo-registry-nacos-integration.json index 2db7c5276c32..82c766195963 100644 --- a/zh-cn/blog/dubbo-registry-nacos-integration.json +++ b/zh-cn/blog/dubbo-registry-nacos-integration.json @@ -1,6 +1,6 @@ { "filename": "dubbo-registry-nacos-integration.md", - "__html": "

Dubbo 融合 Nacos 成为注册中心

\n

Nacos 作为 Dubbo 生态系统中重要的注册中心实现,其中 dubbo-registry-nacos 则是 Dubbo 融合 Nacos 注册中心的实现。

\n

预备工作

\n

当您将 dubbo-registry-nacos 整合到您的 Dubbo 工程之前,请确保后台已经启动 Nacos 服务。如果您尚且不熟悉 Nacos 的基本使用的话,可先行参考 Nacos 快速入门https://nacos.io/en-us/docs/quick-start.html。建议使用 Nacos 0.6.1 以上的版本。

\n

快速上手

\n

Dubbo 融合 Nacos 成为注册中心的操作步骤非常简单,大致步骤可分为“增加 Maven 依赖”以及“配置注册中心“。

\n

增加 Maven 依赖

\n

首先,您需要 dubbo-registry-nacos 的 Maven 依赖添加到您的项目中 pom.xml 文件中,并且强烈地推荐您使用 Dubbo 2.6.5

\n
<dependencies>\n\n    ...\n        \n    <!-- Dubbo Nacos registry dependency -->\n    <dependency>\n        <groupId>com.alibaba</groupId>\n        <artifactId>dubbo-registry-nacos</artifactId>\n        <version>0.0.2</version>\n    </dependency>   \n    \n    <!-- Keep latest Nacos client version -->\n    <dependency>\n        <groupId>com.alibaba.nacos</groupId>\n        <artifactId>nacos-client</artifactId>\n        <version>[0.6.1,)</version>\n    </dependency>\n    \n    <!-- Dubbo dependency -->\n    <dependency>\n        <groupId>com.alibaba</groupId>\n        <artifactId>dubbo</artifactId>\n        <version>2.6.5</version>\n    </dependency>\n    \n    <!-- Alibaba Spring Context extension -->\n    <dependency>\n        <groupId>com.alibaba.spring</groupId>\n        <artifactId>spring-context-support</artifactId>\n        <version>1.0.2</version>\n    </dependency>\n\n    ...\n    \n</dependencies>\n
\n

当项目中添加 dubbo-registry-nacos 后,您无需显示地编程实现服务发现和注册逻辑,实际实现由该三方包提供,接下来配置 Naocs 注册中心。

\n

配置注册中心

\n

假设您 Dubbo 应用使用 Spring Framework 装配,将有两种配置方法可选,分别为:Dubbo Spring 外部化配置以及 Spring XML 配置文件以及 ,笔者强烈推荐前者。

\n

Dubbo Spring 外部化配置

\n

Dubbo Spring 外部化配置是由 Dubbo 2.5.8 引入的新特性,可通过 Spring Environment 属性自动地生成并绑定 Dubbo 配置 Bean,实现配置简化,并且降低微服务开发门槛。

\n

假设您 Dubbo 应用的使用 Zookeeper 作为注册中心,并且其服务器 IP 地址为:10.20.153.10,同时,该注册地址作为 Dubbo 外部化配置属性存储在 dubbo-config.properties 文件,如下所示:

\n
## application\ndubbo.application.name = your-dubbo-application\n\n## Zookeeper registry address\ndubbo.registry.address = zookeeper://10.20.153.10:2181\n...\n
\n

假设您的 Nacos Server 同样运行在服务器 10.20.153.10 上,并使用默认 Nacos 服务端口 8848,您只需将 dubbo.registry.address 属性调整如下:

\n
## 其他属性保持不变\n\n## Nacos registry address\ndubbo.registry.address = nacos://10.20.153.10:8848\n...\n
\n

随后,重启您的 Dubbo 应用,Dubbo 的服务提供和消费信息在 Nacos 控制台中可以显示:

\n

\"image-20181213103845976\"

\n

如图所示,服务名前缀为 providers: 的信息为服务提供者的元信息,consumers: 则代表服务消费者的元信息。点击“详情”可查看服务状态详情:

\n

\"image-20181213104145998\"

\n

如果您正在使用 Spring XML 配置文件装配 Dubbo 注册中心的话,请参考下一节。

\n

Spring XML 配置文件

\n

同样,假设您 Dubbo 应用的使用 Zookeeper 作为注册中心,并且其服务器 IP 地址为:10.20.153.10,并且装配 Spring Bean 在 XML 文件中,如下所示:

\n
<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<beans xmlns=\"http://www.springframework.org/schema/beans\"\n    xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n    xmlns:dubbo=\"http://dubbo.apache.org/schema/dubbo\"\n    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\">\n \n    <!-- 提供方应用信息,用于计算依赖关系 -->\n    <dubbo:application name=\"dubbo-provider-xml-demo\"  />\n \n    <!-- 使用 Zookeeper 注册中心 -->\n    <dubbo:registry address=\"zookeeper://10.20.153.10:2181\" />\n \t...\n</beans>\n
\n

Dubbo Spring 外部化配置 配置类似,只需要调整 address 属性配置即可:

\n
<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<beans xmlns=\"http://www.springframework.org/schema/beans\"\n    xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n    xmlns:dubbo=\"http://dubbo.apache.org/schema/dubbo\"\n    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\">\n \n    <!-- 提供方应用信息,用于计算依赖关系 -->\n    <dubbo:application name=\"dubbo-provider-xml-demo\"  />\n \n    <!-- 使用 Nacos 注册中心 -->\n    <dubbo:registry address=\"nacos://10.20.153.10:8848\" />\n \t...\n</beans>\n
\n

重启 Dubbo 应用后,您同样也能发现服务提供方和消费方的注册元信息呈现在 Nacos 控制台中:

\n

\"image-20181213113049185\"

\n

您是否绝对配置或切换 Nacos 注册中心超级 Easy 呢?如果您仍旧意犹未尽或者不甚明白的话,可参考以下完整的示例。

\n

完整示例

\n

以上图片中的元数据源于 Dubbo Spring 注解驱动示例以及 Dubbo Spring XML 配置驱动示例,下面将分别介绍两者,您可以选择自己偏好的编程模型。在正式讨论之前,先来介绍两者的预备工作,因为它们皆依赖 Java 服务接口和实现。同时,请确保本地(127.0.0.1)环境已启动 Nacos 服务

\n

示例接口与实现

\n

首先定义示例接口,如下所示:

\n
package com.alibaba.dubbo.demo.service;\n\n/**\n * DemoService\n *\n * @since 2.6.5\n */\npublic interface DemoService {\n\n    String sayName(String name);\n\n}\n
\n

提供以上接口的实现类:

\n
package com.alibaba.dubbo.demo.service;\n\nimport com.alibaba.dubbo.config.annotation.Service;\nimport com.alibaba.dubbo.rpc.RpcContext;\n\nimport org.springframework.beans.factory.annotation.Value;\n\n/**\n * Default {@link DemoService}\n *\n * @since 2.6.5\n */\n@Service(version = \"${demo.service.version}\")\npublic class DefaultService implements DemoService {\n\n    @Value(\"${demo.service.name}\")\n    private String serviceName;\n\n    public String sayName(String name) {\n        RpcContext rpcContext = RpcContext.getContext();\n        return String.format(\"Service [name :%s , port : %d] %s(\\\"%s\\\") : Hello,%s\",\n                serviceName,\n                rpcContext.getLocalPort(),\n                rpcContext.getMethodName(),\n                name,\n                name);\n    }\n}\n
\n

接口与实现准备妥当后,下面将采用注解驱动和 XML 配置驱动各自实现。

\n

Spring 注解驱动示例

\n

Dubbo 2.5.7 重构了 Spring 注解驱动的编程模型。

\n

服务提供方注解驱动实现

\n
    \n
  • 定义 Dubbo 提供方外部化配置属性源 - provider-config.properties
  • \n
\n
## application\ndubbo.application.name = dubbo-provider-demo\n\n## Nacos registry address\ndubbo.registry.address = nacos://127.0.0.1:8848\n\n## Dubbo Protocol\ndubbo.protocol.name = dubbo\ndubbo.protocol.port = -1\n\n# Provider @Service version\ndemo.service.version=1.0.0\ndemo.service.name = demoService\n
\n
    \n
  • 实现服务提供方引导类 - DemoServiceProviderBootstrap
  • \n
\n
package com.alibaba.dubbo.demo.provider;\n\nimport com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;\nimport com.alibaba.dubbo.demo.service.DemoService;\n\nimport org.springframework.context.annotation.AnnotationConfigApplicationContext;\nimport org.springframework.context.annotation.PropertySource;\n\nimport java.io.IOException;\n\n/**\n * {@link DemoService} provider demo\n */\n@EnableDubbo(scanBasePackages = \"com.alibaba.dubbo.demo.service\")\n@PropertySource(value = \"classpath:/provider-config.properties\")\npublic class DemoServiceProviderBootstrap {\n\n    public static void main(String[] args) throws IOException {\n        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();\n        context.register(DemoServiceProviderBootstrap.class);\n        context.refresh();\n        System.out.println(\"DemoService provider is starting...\");\n        System.in.read();\n    }\n}\n\n
\n

其中注解 @EnableDubbo 激活 Dubbo 注解驱动以及外部化配置,其 scanBasePackages 属性扫描指定 Java 包,将所有标注 @Service 的服务接口实现类暴露为 Spring Bean,随即被导出 Dubbo 服务。

\n

@PropertySource 是 Spring Framework 3.1 引入的标准导入属性配置资源注解,它将为 Dubbo 提供外部化配置。

\n

服务消费方注解驱动实现

\n
    \n
  • 定义 Dubbo 消费方外部化配置属性源 - consumer-config.properties
  • \n
\n
## Dubbo Application info\ndubbo.application.name = dubbo-consumer-demo\n\n## Nacos registry address\ndubbo.registry.address = nacos://127.0.0.1:8848\n\n# @Reference version\ndemo.service.version= 1.0.0\n
\n

同样地,dubbo.registry.address 属性指向 Nacos 注册中心,其他 Dubbo 服务相关的元信息通过 Nacos 注册中心获取。

\n
    \n
  • 实现服务消费方引导类 - DemoServiceConsumerBootstrap
  • \n
\n
package com.alibaba.dubbo.demo.consumer;\n\nimport com.alibaba.dubbo.config.annotation.Reference;\nimport com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;\nimport com.alibaba.dubbo.demo.service.DemoService;\n\nimport org.springframework.context.annotation.AnnotationConfigApplicationContext;\nimport org.springframework.context.annotation.PropertySource;\n\nimport javax.annotation.PostConstruct;\nimport java.io.IOException;\n\n/**\n * {@link DemoService} consumer demo\n */\n@EnableDubbo\n@PropertySource(value = \"classpath:/consumer-config.properties\")\npublic class DemoServiceConsumerBootstrap {\n\n    @Reference(version = \"${demo.service.version}\")\n    private DemoService demoService;\n\n    @PostConstruct\n    public void init() {\n        for (int i = 0; i < 10; i++) {\n            System.out.println(demoService.sayName(\"小马哥(mercyblitz)\"));\n        }\n    }\n\n    public static void main(String[] args) throws IOException {\n        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();\n        context.register(DemoServiceConsumerBootstrap.class);\n        context.refresh();\n        context.close();\n    }\n}\n\n
\n

同样地,@EnableDubbo 注解激活 Dubbo 注解驱动和外部化配置,不过当前属于服务消费者,无需指定 Java 包名扫描标注 @Service 的服务实现。

\n

@Reference 是 Dubbo 远程服务的依赖注入注解,需要服务提供方和消费端约定接口(interface)、版本(version)以及分组(group)信息。在当前服务消费示例中,DemoService 的服务版本来源于属性配置文件 consumer-config.properties

\n

@PostConstruct 部分代码则说明当 DemoServiceConsumerBootstrap Bean 初始化时,执行十次 Dubbo 远程方法调用。

\n

运行注解驱动示例

\n

在本地启动两次 DemoServiceProviderBootstrap,注册中心将出现两个健康服务:

\n

\"image-20181213123909636\"

\n

再运行 DemoServiceConsumerBootstrap,运行结果如下:

\n
Service [name :demoService , port : 20880] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :demoService , port : 20881] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :demoService , port : 20880] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :demoService , port : 20880] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :demoService , port : 20881] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :demoService , port : 20881] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :demoService , port : 20880] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :demoService , port : 20880] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :demoService , port : 20881] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :demoService , port : 20881] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\n
\n

运行无误,并且服务消费方使用了负载均衡策略,将十次 RPC 调用平均分摊到两个 Dubbo 服务提供方实例中。

\n

Spring XML 配置驱动示例

\n

Spring XML 配置驱动是传统 Spring 装配组件的编程模型。

\n

服务提供方 XML 配置驱动

\n
    \n
  • 定义服务提供方 XML 上下文配置文件 - /META-INF/spring/dubbo-provider-context.xml
  • \n
\n
<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<beans xmlns=\"http://www.springframework.org/schema/beans\"\n       xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n       xmlns:dubbo=\"http://dubbo.apache.org/schema/dubbo\"\n       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\">\n\n    <!-- 提供方应用信息,用于计算依赖关系 -->\n    <dubbo:application name=\"dubbo-provider-xml-demo\"/>\n\n    <!-- 使用 Nacos 注册中心 -->\n    <dubbo:registry address=\"nacos://127.0.0.1:8848\"/>\n\n    <!-- 用dubbo协议在随机端口暴露服务 -->\n    <dubbo:protocol name=\"dubbo\" port=\"-1\"/>\n\n    <!-- 声明需要暴露的服务接口 -->\n    <dubbo:service interface=\"com.alibaba.dubbo.demo.service.DemoService\" ref=\"demoService\" version=\"2.0.0\"/>\n\n    <!-- 和本地bean一样实现服务 -->\n    <bean id=\"demoService\" class=\"com.alibaba.dubbo.demo.service.DefaultService\"/>\n</beans>\n
\n
    \n
  • 实现服务提供方引导类 - DemoServiceProviderXmlBootstrap
  • \n
\n
package com.alibaba.dubbo.demo.provider;\n\nimport com.alibaba.dubbo.demo.service.DemoService;\n\nimport org.springframework.context.support.ClassPathXmlApplicationContext;\n\nimport java.io.IOException;\n\n/**\n * {@link DemoService} provider demo XML bootstrap\n */\npublic class DemoServiceProviderXmlBootstrap {\n\n    public static void main(String[] args) throws IOException {\n        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext();\n        context.setConfigLocation(\"/META-INF/spring/dubbo-provider-context.xml\");\n        context.refresh();\n        System.out.println(\"DemoService provider (XML) is starting...\");\n        System.in.read();\n    }\n}\n
\n

服务消费方 XML 配置驱动

\n
    \n
  • 定义服务消费方 XML 上下文配置文件 - /META-INF/spring/dubbo-consumer-context.xml
  • \n
\n
<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<beans xmlns=\"http://www.springframework.org/schema/beans\"\n       xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n       xmlns:dubbo=\"http://dubbo.apache.org/schema/dubbo\"\n       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\">\n\n    <!-- 提供方应用信息,用于计算依赖关系 -->\n    <dubbo:application name=\"dubbo-consumer-xml-demo\"/>\n\n    <!-- 使用 Nacos 注册中心 -->\n    <dubbo:registry address=\"nacos://127.0.0.1:8848\"/>\n\n    <!-- 引用服务接口 -->\n    <dubbo:reference id=\"demoService\" interface=\"com.alibaba.dubbo.demo.service.DemoService\" version=\"2.0.0\"/>\n\n</beans>\n
\n
    \n
  • 实现服务消费方引导类 - DemoServiceConsumerXmlBootstrap
  • \n
\n
package com.alibaba.dubbo.demo.consumer;\n\nimport com.alibaba.dubbo.demo.service.DemoService;\n\nimport org.springframework.context.support.ClassPathXmlApplicationContext;\n\nimport java.io.IOException;\n\n/**\n * {@link DemoService} consumer demo XML bootstrap\n */\npublic class DemoServiceConsumerXmlBootstrap {\n\n    public static void main(String[] args) throws IOException {\n        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext();\n        context.setConfigLocation(\"/META-INF/spring/dubbo-consumer-context.xml\");\n        context.refresh();\n        System.out.println(\"DemoService consumer (XML) is starting...\");\n        DemoService demoService = context.getBean(\"demoService\", DemoService.class);\n        for (int i = 0; i < 10; i++) {\n            System.out.println(demoService.sayName(\"小马哥(mercyblitz)\"));\n        }\n        context.close();\n    }\n}\n
\n

运行 XML 配置驱动示例

\n

同样地,先启动两个 DemoServiceProviderXmlBootstrap 引导类,观察 Nacos 注册中心服务提供者变化:

\n

\"image-20181213125527201\"

\n

XML 配置驱动的服务版本为 2.0.0,因此注册服务无误。

\n

再运行服务消费者引导类 DemoServiceConsumerXmlBootstrap,观察控制台输出内容:

\n
Service [name :null , port : 20882] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :null , port : 20882] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :null , port : 20883] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :null , port : 20882] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :null , port : 20882] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :null , port : 20883] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :null , port : 20882] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :null , port : 20883] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :null , port : 20883] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :null , port : 20883] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\n
\n

结果同样运行和负载均衡正常,不过由于当前示例尚未添加属性 demo.service.name 的缘故,因此,“name”部分信息输出为 null。更多内容请参考:https://github.com/dubbo/dubbo-registry-nacos。

\n

如果您关注或喜爱 Dubbo 以及 Nacos 等开源工程,不妨为它们点 “star”,加油打气链接:

\n\n", + "__html": "

Dubbo 融合 Nacos 成为注册中心

\n

Nacos 作为 Dubbo 生态系统中重要的注册中心实现,其中 dubbo-registry-nacos 则是 Dubbo 融合 Nacos 注册中心的实现。

\n

预备工作

\n

当您将 dubbo-registry-nacos 整合到您的 Dubbo 工程之前,请确保后台已经启动 Nacos 服务。如果您尚且不熟悉 Nacos 的基本使用的话,可先行参考 Nacos 快速入门https://nacos.io/en-us/docs/quick-start.html。建议使用 Nacos 0.6.1 以上的版本。

\n

快速上手

\n

Dubbo 融合 Nacos 成为注册中心的操作步骤非常简单,大致步骤可分为“增加 Maven 依赖”以及“配置注册中心“。

\n

增加 Maven 依赖

\n

首先,您需要 dubbo-registry-nacos 的 Maven 依赖添加到您的项目中 pom.xml 文件中,并且强烈地推荐您使用 Dubbo 2.6.5

\n
<dependencies>\n\n    ...\n        \n    <!-- Dubbo Nacos registry dependency -->\n    <dependency>\n        <groupId>com.alibaba</groupId>\n        <artifactId>dubbo-registry-nacos</artifactId>\n        <version>0.0.2</version>\n    </dependency>   \n    \n    <!-- Keep latest Nacos client version -->\n    <dependency>\n        <groupId>com.alibaba.nacos</groupId>\n        <artifactId>nacos-client</artifactId>\n        <version>[0.6.1,)</version>\n    </dependency>\n    \n    <!-- Dubbo dependency -->\n    <dependency>\n        <groupId>com.alibaba</groupId>\n        <artifactId>dubbo</artifactId>\n        <version>2.6.5</version>\n    </dependency>\n    \n    <!-- Alibaba Spring Context extension -->\n    <dependency>\n        <groupId>com.alibaba.spring</groupId>\n        <artifactId>spring-context-support</artifactId>\n        <version>1.0.2</version>\n    </dependency>\n\n    ...\n    \n</dependencies>\n
\n

当项目中添加 dubbo-registry-nacos 后,您无需显示地编程实现服务发现和注册逻辑,实际实现由该三方包提供,接下来配置 Naocs 注册中心。

\n

配置注册中心

\n

假设您 Dubbo 应用使用 Spring Framework 装配,将有两种配置方法可选,分别为:Dubbo Spring 外部化配置以及 Spring XML 配置文件以及 ,笔者强烈推荐前者。

\n

Dubbo Spring 外部化配置

\n

Dubbo Spring 外部化配置是由 Dubbo 2.5.8 引入的新特性,可通过 Spring Environment 属性自动地生成并绑定 Dubbo 配置 Bean,实现配置简化,并且降低微服务开发门槛。

\n

假设您 Dubbo 应用的使用 Zookeeper 作为注册中心,并且其服务器 IP 地址为:10.20.153.10,同时,该注册地址作为 Dubbo 外部化配置属性存储在 dubbo-config.properties 文件,如下所示:

\n
## application\ndubbo.application.name = your-dubbo-application\n\n## Zookeeper registry address\ndubbo.registry.address = zookeeper://10.20.153.10:2181\n...\n
\n

假设您的 Nacos Server 同样运行在服务器 10.20.153.10 上,并使用默认 Nacos 服务端口 8848,您只需将 dubbo.registry.address 属性调整如下:

\n
## 其他属性保持不变\n\n## Nacos registry address\ndubbo.registry.address = nacos://10.20.153.10:8848\n...\n
\n

随后,重启您的 Dubbo 应用,Dubbo 的服务提供和消费信息在 Nacos 控制台中可以显示:

\n

\"image-20181213103845976\"

\n

如图所示,服务名前缀为 providers: 的信息为服务提供者的元信息,consumers: 则代表服务消费者的元信息。点击“详情”可查看服务状态详情:

\n

\"image-20181213104145998\"

\n

如果您正在使用 Spring XML 配置文件装配 Dubbo 注册中心的话,请参考下一节。

\n

Spring XML 配置文件

\n

同样,假设您 Dubbo 应用的使用 Zookeeper 作为注册中心,并且其服务器 IP 地址为:10.20.153.10,并且装配 Spring Bean 在 XML 文件中,如下所示:

\n
<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<beans xmlns=\"http://www.springframework.org/schema/beans\"\n    xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n    xmlns:dubbo=\"http://dubbo.apache.org/schema/dubbo\"\n    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\">\n \n    <!-- 提供方应用信息,用于计算依赖关系 -->\n    <dubbo:application name=\"dubbo-provider-xml-demo\"  />\n \n    <!-- 使用 Zookeeper 注册中心 -->\n    <dubbo:registry address=\"zookeeper://10.20.153.10:2181\" />\n \t...\n</beans>\n
\n

Dubbo Spring 外部化配置 配置类似,只需要调整 address 属性配置即可:

\n
<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<beans xmlns=\"http://www.springframework.org/schema/beans\"\n    xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n    xmlns:dubbo=\"http://dubbo.apache.org/schema/dubbo\"\n    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\">\n \n    <!-- 提供方应用信息,用于计算依赖关系 -->\n    <dubbo:application name=\"dubbo-provider-xml-demo\"  />\n \n    <!-- 使用 Nacos 注册中心 -->\n    <dubbo:registry address=\"nacos://10.20.153.10:8848\" />\n \t...\n</beans>\n
\n

重启 Dubbo 应用后,您同样也能发现服务提供方和消费方的注册元信息呈现在 Nacos 控制台中:

\n

\"image-20181213113049185\"

\n

您是否绝对配置或切换 Nacos 注册中心超级 Easy 呢?如果您仍旧意犹未尽或者不甚明白的话,可参考以下完整的示例。

\n

完整示例

\n

以上图片中的元数据源于 Dubbo Spring 注解驱动示例以及 Dubbo Spring XML 配置驱动示例,下面将分别介绍两者,您可以选择自己偏好的编程模型。在正式讨论之前,先来介绍两者的预备工作,因为它们皆依赖 Java 服务接口和实现。同时,请确保本地(127.0.0.1)环境已启动 Nacos 服务

\n

示例接口与实现

\n

首先定义示例接口,如下所示:

\n
package com.alibaba.dubbo.demo.service;\n\n/**\n * DemoService\n *\n * @since 2.6.5\n */\npublic interface DemoService {\n\n    String sayName(String name);\n\n}\n
\n

提供以上接口的实现类:

\n
package com.alibaba.dubbo.demo.service;\n\nimport com.alibaba.dubbo.config.annotation.Service;\nimport com.alibaba.dubbo.rpc.RpcContext;\n\nimport org.springframework.beans.factory.annotation.Value;\n\n/**\n * Default {@link DemoService}\n *\n * @since 2.6.5\n */\n@Service(version = \"${demo.service.version}\")\npublic class DefaultService implements DemoService {\n\n    @Value(\"${demo.service.name}\")\n    private String serviceName;\n\n    public String sayName(String name) {\n        RpcContext rpcContext = RpcContext.getContext();\n        return String.format(\"Service [name :%s , port : %d] %s(\\\"%s\\\") : Hello,%s\",\n                serviceName,\n                rpcContext.getLocalPort(),\n                rpcContext.getMethodName(),\n                name,\n                name);\n    }\n}\n
\n

接口与实现准备妥当后,下面将采用注解驱动和 XML 配置驱动各自实现。

\n

Spring 注解驱动示例

\n

Dubbo 2.5.7 重构了 Spring 注解驱动的编程模型。

\n

服务提供方注解驱动实现

\n
    \n
  • 定义 Dubbo 提供方外部化配置属性源 - provider-config.properties
  • \n
\n
## application\ndubbo.application.name = dubbo-provider-demo\n\n## Nacos registry address\ndubbo.registry.address = nacos://127.0.0.1:8848\n\n## Dubbo Protocol\ndubbo.protocol.name = dubbo\ndubbo.protocol.port = -1\n\n# Provider @Service version\ndemo.service.version=1.0.0\ndemo.service.name = demoService\n
\n
    \n
  • 实现服务提供方引导类 - DemoServiceProviderBootstrap
  • \n
\n
package com.alibaba.dubbo.demo.provider;\n\nimport com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;\nimport com.alibaba.dubbo.demo.service.DemoService;\n\nimport org.springframework.context.annotation.AnnotationConfigApplicationContext;\nimport org.springframework.context.annotation.PropertySource;\n\nimport java.io.IOException;\n\n/**\n * {@link DemoService} provider demo\n */\n@EnableDubbo(scanBasePackages = \"com.alibaba.dubbo.demo.service\")\n@PropertySource(value = \"classpath:/provider-config.properties\")\npublic class DemoServiceProviderBootstrap {\n\n    public static void main(String[] args) throws IOException {\n        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();\n        context.register(DemoServiceProviderBootstrap.class);\n        context.refresh();\n        System.out.println(\"DemoService provider is starting...\");\n        System.in.read();\n    }\n}\n\n
\n

其中注解 @EnableDubbo 激活 Dubbo 注解驱动以及外部化配置,其 scanBasePackages 属性扫描指定 Java 包,将所有标注 @Service 的服务接口实现类暴露为 Spring Bean,随即被导出 Dubbo 服务。

\n

@PropertySource 是 Spring Framework 3.1 引入的标准导入属性配置资源注解,它将为 Dubbo 提供外部化配置。

\n

服务消费方注解驱动实现

\n
    \n
  • 定义 Dubbo 消费方外部化配置属性源 - consumer-config.properties
  • \n
\n
## Dubbo Application info\ndubbo.application.name = dubbo-consumer-demo\n\n## Nacos registry address\ndubbo.registry.address = nacos://127.0.0.1:8848\n\n# @Reference version\ndemo.service.version= 1.0.0\n
\n

同样地,dubbo.registry.address 属性指向 Nacos 注册中心,其他 Dubbo 服务相关的元信息通过 Nacos 注册中心获取。

\n
    \n
  • 实现服务消费方引导类 - DemoServiceConsumerBootstrap
  • \n
\n
package com.alibaba.dubbo.demo.consumer;\n\nimport com.alibaba.dubbo.config.annotation.Reference;\nimport com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;\nimport com.alibaba.dubbo.demo.service.DemoService;\n\nimport org.springframework.context.annotation.AnnotationConfigApplicationContext;\nimport org.springframework.context.annotation.PropertySource;\n\nimport javax.annotation.PostConstruct;\nimport java.io.IOException;\n\n/**\n * {@link DemoService} consumer demo\n */\n@EnableDubbo\n@PropertySource(value = \"classpath:/consumer-config.properties\")\npublic class DemoServiceConsumerBootstrap {\n\n    @Reference(version = \"${demo.service.version}\")\n    private DemoService demoService;\n\n    @PostConstruct\n    public void init() {\n        for (int i = 0; i < 10; i++) {\n            System.out.println(demoService.sayName(\"小马哥(mercyblitz)\"));\n        }\n    }\n\n    public static void main(String[] args) throws IOException {\n        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();\n        context.register(DemoServiceConsumerBootstrap.class);\n        context.refresh();\n        context.close();\n    }\n}\n\n
\n

同样地,@EnableDubbo 注解激活 Dubbo 注解驱动和外部化配置,不过当前属于服务消费者,无需指定 Java 包名扫描标注 @Service 的服务实现。

\n

@Reference 是 Dubbo 远程服务的依赖注入注解,需要服务提供方和消费端约定接口(interface)、版本(version)以及分组(group)信息。在当前服务消费示例中,DemoService 的服务版本来源于属性配置文件 consumer-config.properties

\n

@PostConstruct 部分代码则说明当 DemoServiceConsumerBootstrap Bean 初始化时,执行十次 Dubbo 远程方法调用。

\n

运行注解驱动示例

\n

在本地启动两次 DemoServiceProviderBootstrap,注册中心将出现两个健康服务:

\n

\"image-20181213123909636\"

\n

再运行 DemoServiceConsumerBootstrap,运行结果如下:

\n
Service [name :demoService , port : 20880] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :demoService , port : 20881] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :demoService , port : 20880] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :demoService , port : 20880] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :demoService , port : 20881] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :demoService , port : 20881] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :demoService , port : 20880] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :demoService , port : 20880] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :demoService , port : 20881] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :demoService , port : 20881] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\n
\n

运行无误,并且服务消费方使用了负载均衡策略,将十次 RPC 调用平均分摊到两个 Dubbo 服务提供方实例中。

\n

Spring XML 配置驱动示例

\n

Spring XML 配置驱动是传统 Spring 装配组件的编程模型。

\n

服务提供方 XML 配置驱动

\n
    \n
  • 定义服务提供方 XML 上下文配置文件 - /META-INF/spring/dubbo-provider-context.xml
  • \n
\n
<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<beans xmlns=\"http://www.springframework.org/schema/beans\"\n       xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n       xmlns:dubbo=\"http://dubbo.apache.org/schema/dubbo\"\n       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\">\n\n    <!-- 提供方应用信息,用于计算依赖关系 -->\n    <dubbo:application name=\"dubbo-provider-xml-demo\"/>\n\n    <!-- 使用 Nacos 注册中心 -->\n    <dubbo:registry address=\"nacos://127.0.0.1:8848\"/>\n\n    <!-- 用dubbo协议在随机端口暴露服务 -->\n    <dubbo:protocol name=\"dubbo\" port=\"-1\"/>\n\n    <!-- 声明需要暴露的服务接口 -->\n    <dubbo:service interface=\"com.alibaba.dubbo.demo.service.DemoService\" ref=\"demoService\" version=\"2.0.0\"/>\n\n    <!-- 和本地bean一样实现服务 -->\n    <bean id=\"demoService\" class=\"com.alibaba.dubbo.demo.service.DefaultService\"/>\n</beans>\n
\n
    \n
  • 实现服务提供方引导类 - DemoServiceProviderXmlBootstrap
  • \n
\n
package com.alibaba.dubbo.demo.provider;\n\nimport com.alibaba.dubbo.demo.service.DemoService;\n\nimport org.springframework.context.support.ClassPathXmlApplicationContext;\n\nimport java.io.IOException;\n\n/**\n * {@link DemoService} provider demo XML bootstrap\n */\npublic class DemoServiceProviderXmlBootstrap {\n\n    public static void main(String[] args) throws IOException {\n        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext();\n        context.setConfigLocation(\"/META-INF/spring/dubbo-provider-context.xml\");\n        context.refresh();\n        System.out.println(\"DemoService provider (XML) is starting...\");\n        System.in.read();\n    }\n}\n
\n

服务消费方 XML 配置驱动

\n
    \n
  • 定义服务消费方 XML 上下文配置文件 - /META-INF/spring/dubbo-consumer-context.xml
  • \n
\n
<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<beans xmlns=\"http://www.springframework.org/schema/beans\"\n       xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n       xmlns:dubbo=\"http://dubbo.apache.org/schema/dubbo\"\n       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\">\n\n    <!-- 提供方应用信息,用于计算依赖关系 -->\n    <dubbo:application name=\"dubbo-consumer-xml-demo\"/>\n\n    <!-- 使用 Nacos 注册中心 -->\n    <dubbo:registry address=\"nacos://127.0.0.1:8848\"/>\n\n    <!-- 引用服务接口 -->\n    <dubbo:reference id=\"demoService\" interface=\"com.alibaba.dubbo.demo.service.DemoService\" version=\"2.0.0\"/>\n\n</beans>\n
\n
    \n
  • 实现服务消费方引导类 - DemoServiceConsumerXmlBootstrap
  • \n
\n
package com.alibaba.dubbo.demo.consumer;\n\nimport com.alibaba.dubbo.demo.service.DemoService;\n\nimport org.springframework.context.support.ClassPathXmlApplicationContext;\n\nimport java.io.IOException;\n\n/**\n * {@link DemoService} consumer demo XML bootstrap\n */\npublic class DemoServiceConsumerXmlBootstrap {\n\n    public static void main(String[] args) throws IOException {\n        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext();\n        context.setConfigLocation(\"/META-INF/spring/dubbo-consumer-context.xml\");\n        context.refresh();\n        System.out.println(\"DemoService consumer (XML) is starting...\");\n        DemoService demoService = context.getBean(\"demoService\", DemoService.class);\n        for (int i = 0; i < 10; i++) {\n            System.out.println(demoService.sayName(\"小马哥(mercyblitz)\"));\n        }\n        context.close();\n    }\n}\n
\n

运行 XML 配置驱动示例

\n

同样地,先启动两个 DemoServiceProviderXmlBootstrap 引导类,观察 Nacos 注册中心服务提供者变化:

\n

\"image-20181213125527201\"

\n

XML 配置驱动的服务版本为 2.0.0,因此注册服务无误。

\n

再运行服务消费者引导类 DemoServiceConsumerXmlBootstrap,观察控制台输出内容:

\n
Service [name :null , port : 20882] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :null , port : 20882] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :null , port : 20883] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :null , port : 20882] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :null , port : 20882] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :null , port : 20883] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :null , port : 20882] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :null , port : 20883] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :null , port : 20883] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\nService [name :null , port : 20883] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)\n
\n

结果同样运行和负载均衡正常,不过由于当前示例尚未添加属性 demo.service.name 的缘故,因此,“name”部分信息输出为 null。更多内容请参考:https://github.com/dubbo/dubbo-registry-nacos。

\n

如果您关注或喜爱 Dubbo 以及 Nacos 等开源工程,不妨为它们点 “star”,加油打气链接:

\n\n", "link": "/zh-cn/blog/dubbo-registry-nacos-integration.html", "meta": {} } \ No newline at end of file