Dubbo が Nacos を統合して登録センターとなる

すぐに始めましょう

Dubbo が Nacos を登録センターに統合するための操作手順は非常に簡単で、大きく分けて「Maven 依存関係の追加」と「登録センターの設定」に分かれます。

Maven 依存関係を追加する

Dubbo クライアントのみに依存する必要があります。推奨バージョンについては、Dubbo 公式ドキュメントを参照するか、Dubbo 開発者に問い合わせてください。

<dependencies>

    ...

    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>dubbo</artifactId>
        <version>3.0.5</version>
    </dependency>

    <!-- Dubbo Nacos registry dependency -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>dubbo-registry-nacos</artifactId>
        <version>3.0.5</version>
    </dependency>

    <!-- Alibaba Spring Context extension -->
    <dependency>
        <groupId>com.alibaba.spring</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>1.0.11</version>
    </dependency>
    ...
    
</dependencies>

レジストリを設定する

Dubbo アプリケーションが Spring Framework を使用してアセンブルされていると仮定すると、 Dubbo Spring 外部構成と Spring XML 構成ファイルの 2 つの構成方法から選択できます。作成者は前者を強く推奨します。

Dubbo Spring の外部構成

Dubbo Spring 外部構成は、  2.5.8 Dubbo によって導入された新機能であり、  Environment Spring プロパティを通じて Dubbo 構成 Bean を自動的に生成およびバインドできるため、構成が簡素化され、マイクロサービス開発の敷居が低くなります。

Dubbo アプリケーションが Nacos を登録センターとして使用し、そのサーバー IP アドレスが であり、登録アドレスが 次のように10.20.153.10Dubbo 外部化構成プロパティとしてファイルに保存されているとします 。dubbo-config.properties

## application
dubbo.application.name = your-dubbo-application

## 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 コンソールに表示されます。

image-20181213103845976-4668726.png |  左 |  747x284

図に示すように、サービス名の前に付加された情報は providers: サービスプロバイダのメタ情報であり、consumers: サービスコンシューマのメタ情報を表している。[詳細] をクリックして、サービス ステータスの詳細を表示します。

image-20181213104145998-4668906.png |  左 |  747x437

Spring XML 構成ファイルを使用して Dubbo レジストリをアセンブルしている場合は、次のセクションを参照してください。

Spring XML 構成ファイル

同様に、Dubbo アプリケーションが Nacos を登録センターとして使用し、そのサーバー IP アドレスが であると仮定し、10.20.153.10次のように XML ファイルで Spring Bean をアセンブルします。

<?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 コンソールに表示されることもわかります。

image-20181213113049185-4671849.png |  左 |  747x274

Nacos レジストリの設定や切り替えは非常に簡単だと思いますか? それでも理解できない場合、または理解できない場合は、次の完全な例を参照してください。

完全な例

上の図のメタデータは、Dubbo Spring アノテーション駆動の例と Dubbo Spring XML 構成駆動の例からのものです。この 2 つは以下で紹介され、好みのプログラミング モデルを選択できます。正式な議論の前に、両方とも Java サービス インターフェイスと実装に依存しているため、両方の準備作業を紹介しましょう。同時に、ローカル( 127.0.0.1)環境でNacosサービスが開始されていることを確認してください。

インターフェイスと実装の例

完全なコード アーカイブの場所:  https://github.com/nacos-group/nacos-examples/tree/master/nacos-dubbo-example

まず、サンプル インターフェイスを次のように定義します。

package com.alibaba.nacos.example.dubbo.service;

public interface DemoService {
    String sayName(String name);
}

上記のインターフェースの実装クラスを提供します。


package com.alibaba.nacos.example.dubbo.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.dubbo.rpc.RpcContext;
import org.springframework.beans.factory.annotation.Value;

/**
 * Default {@link DemoService}
 *  https://nacos.io/zh-cn/docs/use-nacos-with-dubbo.html
 * @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

dubbo.application.qosEnable=false

  • サービスプロバイダーのブートストラップクラスを実装します - DemoServiceProviderBootstrap
package com.alibaba.nacos.example.dubbo.provider;

import com.alibaba.nacos.example.dubbo.service.DemoService;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.PropertySource;
import java.io.IOException;

/**
 * {@link DemoService} provider demo
 * https://nacos.io/zh-cn/docs/use-nacos-with-dubbo.html
 */
@EnableDubbo(scanBasePackages = "com.alibaba.nacos.example.dubbo.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.application.qosEnable=false

同様に、dubbo.registry.address 属性は Nacos 登録センターを指し、他の Dubbo サービスに関連するメタ情報は Nacos 登録センターを通じて取得されます。

  • サービスコンシューマのブートストラップクラスを実装します - DemoServiceConsumerBootstrap
package com.alibaba.nacos.example.dubbo.consumer;


import com.alibaba.nacos.example.dubbo.service.DemoService;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.PropertySource;
import javax.annotation.PostConstruct;
import java.io.IOException;

/**
 * {@link DemoService} consumer demo
 * https://nacos.io/zh-cn/docs/use-nacos-with-dubbo.html
 */
@EnableDubbo
@PropertySource(value = "classpath:/consumer-config.properties")
public class DemoServiceConsumerBootstrap {

    @DubboReference(version = "${demo.service.version}")
    private DemoService demoService;

    @PostConstruct
    public void init() {
        for (int i = 0; i < 10; i++) {
            System.out.println(demoService.sayName("Nacos"));
        }
    }

    public static void main(String[] args) throws IOException {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        context.register(DemoServiceConsumerBootstrap.class);
        context.refresh();
        context.close();
    }
}

同様に、アノテーションは Dubbo アノテーション ドライバーと外部構成をアクティブ化しますが、現在はサービス コンシューマーに属しており、  Java パッケージ名スキャン アノテーションのサービス実装を@EnableDubbo 指定する必要はありません 。@Service

@Reference これは、Dubbo リモート サービスの依存関係注入アノテーションであり、サービス プロバイダーとコンシューマーがインターフェイス、バージョン、およびグループ情報に同意する必要があります。現在のサービス消費の例では、DemoService サービスのバージョンはプロパティ構成ファイルから派生します consumer-config.properties

@PostConstruct コードの一部は、  DemoServiceConsumerBootstrap Bean が初期化されるときに、10 個の Dubbo リモート メソッド呼び出しが実行されることを示しています。

アノテーション主導のサンプルを実行する

ローカルで 2 回起動する DemoServiceProviderBootstrapと、レジストリに 2 つのヘルス サービスが存在します。

image-20181213123909636-4675949.png |  左 |  747x38

再度実行すると DemoServiceConsumerBootstrap、結果は次のようになります。

Service [name :demoService , port : 20880] sayName("Nacos") : Hello,Nacos
Service [name :demoService , port : 20880] sayName("Nacos") : Hello,Nacos
Service [name :demoService , port : 20880] sayName("Nacos") : Hello,Nacos
Service [name :demoService , port : 20880] sayName("Nacos") : Hello,Nacos
Service [name :demoService , port : 20880] sayName("Nacos") : Hello,Nacos
Service [name :demoService , port : 20880] sayName("Nacos") : Hello,Nacos
Service [name :demoService , port : 20880] sayName("Nacos") : Hello,Nacos
Service [name :demoService , port : 20880] sayName("Nacos") : Hello,Nacos
Service [name :demoService , port : 20880] sayName("Nacos") : Hello,Nacos
Service [name :demoService , port : 20880] sayName("Nacos") : Hello,Nacos

操作は正しく、サービス コンシューマーはロード バランシング戦略を使用して、10 件の RPC 呼び出しを 2 つの 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.nacos.example.dubbo.service.DemoService" ref="demoService" version="2.0.0"/>

    <!-- 和本地bean一样实现服务 -->
    <bean id="demoService" class="com.alibaba.nacos.example.dubbo.service.DefaultService"/>
</beans>
  • サービスプロバイダーのブートストラップクラスを実装します - DemoServiceProviderXmlBootstrap

package com.alibaba.nacos.example.dubbo.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.nacos.example.dubbo.service.DemoService" version="2.0.0"/>

</beans>
  • サービスコンシューマのブートストラップクラスを実装します - DemoServiceConsumerXmlBootstrap

package com.alibaba.nacos.example.dubbo.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("Nacos"));
        }
        context.close();
    }
}

XML 構成ドライバーのサンプルを実行する

同様に、 DemoServiceProviderXmlBootstrap 最初に 2 つのブートストラップ クラスを開始し、Nacos レジストリ サービス プロバイダーの変更を観察します。

image-20181213125527201-4676927.png |  左 |  747x33

XML 構成によって駆動されるサービスのバージョンは である 2.0.0ため、サービスの登録にエラーはありません。

次に、サービス コンシューマ ブート クラスを実行し DemoServiceConsumerXmlBootstrap、コンソール出力を観察します。

Service [name :demoService , port : 20880] sayName("Nacos") : Hello,Nacos
Service [name :demoService , port : 20880] sayName("Nacos") : Hello,Nacos
Service [name :demoService , port : 20880] sayName("Nacos") : Hello,Nacos
Service [name :demoService , port : 20880] sayName("Nacos") : Hello,Nacos
Service [name :demoService , port : 20880] sayName("Nacos") : Hello,Nacos
Service [name :demoService , port : 20880] sayName("Nacos") : Hello,Nacos
Service [name :demoService , port : 20880] sayName("Nacos") : Hello,Nacos
Service [name :demoService , port : 20880] sayName("Nacos") : Hello,Nacos
Service [name :demoService , port : 20880] sayName("Nacos") : Hello,Nacos
Service [name :demoService , port : 20880] sayName("Nacos") : Hello,Nacos

結果も実行されており、負荷分散は正常ですが、現在の例では属性が追加されていないため demo.service.name 、情報の「名前」部分の出力は です null

おすすめ

転載: blog.csdn.net/leesinbad/article/details/132381414