すべてのプロダクト
Search
ドキュメントセンター

Microservices Engine:Spring Cloud と MSE Nacos の接続

最終更新日:Jun 23, 2026

Spring Cloud アプリケーションを MSE Nacos インスタンスのエンドポイントで設定すると、アプリケーションは起動時に MSE Nacos サービスレジストリに自動的に登録されます。この登録により、サービス検出、構成管理、およびサービス間の呼び出しが可能になります。このトピックでは、アプリケーションをレジストリに接続する方法について説明します。

前提条件

重要

パブリックネットワーク経由で MSE Nacos にアクセスするには、サービスプロバイダーとサービスコンシューマーの両方がパブリックネットワークアクセス権を持っている必要があります。また、アプリケーションの IP アドレスを MSE Nacos インスタンスのホワイトリストに追加する必要もあります。詳細については、「パブリック IP アドレスホワイトリストの設定」をご参照ください。

サービスプロバイダーの作成

ローカルでサービスプロバイダーアプリケーションを作成し、必要な依存関係を追加し、サービス登録とサービス検出を有効にして、MSE Nacos インスタンスをサービスレジストリとして指定します。

  1. nacos-service-provider という名前の Maven プロジェクトを作成します。

  2. pom.xml ファイルに、次の依存関係を追加します。

    この例では、Spring Boot 2.1.4.RELEASE と Spring Cloud Greenwich.SR1 を使用します。

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/>
    </parent>
    <dependencies>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>2.1.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>                  

    Spring Cloud Greenwich リリースは、Spring Cloud Alibaba バージョン 2.1.1.RELEASE に対応しています。

    • Spring Cloud Finchley リリースを使用する場合、対応する Spring Cloud Alibaba のバージョンは 2.0.1.RELEASE です。

    • Spring Cloud Edgware リリースを使用する場合、対応する Spring Cloud Alibaba のバージョンは 1.5.1.RELEASE です。

    重要

    Spring Cloud Edgware のライフサイクルは終了しています。新しいアプリケーションにはこのバージョンを使用しないことを推奨します。

  3. src\main\java ディレクトリに、com.aliware.edas という名前のパッケージを作成します。

  4. com.aliware.edas パッケージに、サービスプロバイダーのブートストラップクラス ProviderApplication を作成し、次のコードを追加します。

    package com.aliware.edas;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    @SpringBootApplication
        @EnableDiscoveryClient
        public class ProviderApplication {
            public static void main(String[] args) {
                SpringApplication.run(ProviderApplication.class, args);
            }
        }             
    説明

    @EnableDiscoveryClient アノテーションは、アプリケーションのサービス登録とサービス検出機能を有効にします。

  5. com.aliware.edas パッケージに、EchoController を作成します。このメソッドは URL パスからパラメーターを受け取り、それを応答でエコーします。

    package com.aliware.edas;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    @RestController
    public class EchoController {
       @RequestMapping(value = "/echo/{string}", method = RequestMethod.GET)
        public String echo(@PathVariable String string) {
           return string;
          }
    }              
  6. src\main\resources ディレクトリに、application.properties という名前のファイルを作成します。ファイルに次の構成を追加し、Nacos サーバーのエンドポイントを指定します。

    spring.application.name=service-provider
    server.port=18081
    spring.cloud.nacos.discovery.server-addr=mse.XX.nacos.mse.aliyuncs.com:8848
    # To use a custom namespace, uncomment and configure the following line:
    # カスタム名前空間を使用するには、次の行のコメントを解除して設定します:
    #spring.cloud.nacos.discovery.namespace={namespaceId}               

    [インスタンス] ページで、MSE Nacos インスタンスのパブリックエンドポイントを確認できます。フォーマットは mse.XX.nacos.mse.aliyuncs.com です。

    重要

    サービスレジストリとして MSE ZooKeeper または Eureka インスタンスを使用する場合、このステップの Nacos 固有の構成を、ZooKeeper または Eureka の対応する構成に置き換える必要があります。詳細については、「MSE クラスターの注意事項」をご参照ください。

  7. アプリケーションが正常に登録されたことを検証します。

    1. nacos-service-provider プロジェクトで ProviderApplicationmain メソッドを実行して、アプリケーションを起動します。

    2. MSE コンソールにログインし、上部のナビゲーションバーでリージョンを選択します。

    3. 左側のナビゲーションウィンドウで、マイクロサービスの登録 > インスタンス を選択します。インスタンスの名前をクリックします。

    4. Service ManagementServices

    5. [サービス] ページで、service-provider アプリケーションがリストに表示されていることを確認します。

サービスコンシューマーの作成

このセクションでは、サービスコンシューマーを登録し、2つのクライアント (RestTemplate と FeignClient) を使用して Nacos のサービス検出を利用する方法について説明します。

  1. nacos-service-consumer という名前の Maven プロジェクトを作成します。

  2. pom.xml ファイルに、次の依存関係を追加します。

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/>
    </parent>
    <dependencies>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>2.1.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>        
  3. src\main\java ディレクトリに、com.aliware.edas という名前のパッケージを作成します。

  4. com.aliware.edas パッケージで、RestTemplate と FeignClient を設定します。

    1. com.aliware.edas に、EchoService という名前のインターフェイスクラスを作成し、@FeignClient アノテーションを追加し、対応する HTTP URL と HTTP メソッドを設定します。

      package com.aliware.edas;
      import org.springframework.cloud.openfeign.FeignClient;
      import org.springframework.web.bind.annotation.PathVariable;
      import org.springframework.web.bind.annotation.RequestMapping;
      import org.springframework.web.bind.annotation.RequestMethod;
      @FeignClient(name = "service-provider")
      public interface EchoService {
          @RequestMapping(value = "/echo/{str}", method = RequestMethod.GET)
          String echo(@PathVariable("str") String str);
      }                   
    2. com.aliware.edas に、ブートストラップクラス ConsumerApplication を作成し、関連する構成を追加します。

      • @EnableDiscoveryClient アノテーションを使用して、サービス登録とサービス検出を有効にします。

      • @EnableFeignClients アノテーションを使用して、FeignClient を有効化します。

      • @LoadBalanced アノテーションを使用して、RestTemplate をサービス検出と統合します。

      package com.aliware.edas;
      import org.springframework.boot.SpringApplication;
      import org.springframework.boot.autoconfigure.SpringBootApplication;
      import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
      import org.springframework.cloud.client.loadbalancer.LoadBalanced;
      import org.springframework.cloud.openfeign.EnableFeignClients;
      import org.springframework.context.annotation.Bean;
      import org.springframework.web.client.RestTemplate;
      @SpringBootApplication
      @EnableDiscoveryClient
      @EnableFeignClients
      public class ConsumerApplication {
          @LoadBalanced
          @Bean
          public RestTemplate restTemplate() {
              return new RestTemplate();
          }
          public static void main(String[] args) {
              SpringApplication.run(ConsumerApplication.class, args);
          }
      }
  5. com.aliware.edas パッケージに、TestController という名前のクラスを作成して、サービス検出機能を実証および検証します。

    package com.aliware.edas;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.client.RestTemplate;
    @RestController
    public class TestController {
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private EchoService echoService;
    @RequestMapping(value = "/echo-rest/{str}", method = RequestMethod.GET)
    public String rest(@PathVariable String str) {
          return restTemplate.getForObject("http://service-provider/echo/" + str,
                        String.class);
            }
          @RequestMapping(value = "/echo-feign/{str}", method = RequestMethod.GET)
          public String feign(@PathVariable String str) {
                return echoService.echo(str);
            }
        }           
  6. src\main\resources ディレクトリに、application.properties という名前のファイルを作成します。ファイルに次の構成を追加して、Nacos サービスレジストリのアドレスを指定します。

    spring.application.name=service-consumer
    server.port=18082
    spring.cloud.nacos.discovery.server-addr=mse.XX.nacos.mse.aliyuncs.com:8848
    # To use a custom namespace, uncomment and configure the following line:
    # カスタム名前空間を使用するには、次の行のコメントを解除して設定します:
    #spring.cloud.nacos.discovery.namespace={namespaceId}

    インスタンス ページで、MSE Nacos インスタンスのパブリックエンドポイントを確認できます。フォーマットは mse.XX.nacos.mse.aliyuncs.com です。

    重要

    サービスレジストリとして MSE ZooKeeper または Eureka インスタンスを使用する場合、このステップの Nacos 固有の構成を、ZooKeeper または Eureka の対応する構成に置き換える必要があります。詳細については、「注意事項」をご参照ください。

  7. アプリケーションが正常に登録されたことを検証します。

    1. nacos-service-consumer プロジェクトで ConsumerApplicationmain メソッドを実行して、アプリケーションを起動します。

    2. MSE コンソールにログインし、上部のナビゲーションバーでリージョンを選択します。

    3. 左側のナビゲーションウィンドウで、マイクロサービスの登録 > インスタンス を選択します。インスタンスの名前をクリックします。

    4. Service ManagementServices

    5. [サービス] ページで、service-consumer アプリケーションがリストに表示されていることを確認します。

ローカルテスト

サービスコンシューマーからサービスプロバイダーへのサービス呼び出しをローカルでテストします。

  • Linux、UNIX、または macOS では、次のコマンドを使用してサービス呼び出しを行います。

    curl http://127.0.0.1:18082/echo-rest/rest-rest
    curl http://127.0.0.1:18082/echo-feign/feign-rest
  • Windows では、ブラウザのアドレスバーに http://127.0.0.1:18082/echo-rest/rest-resthttp://127.0.0.1:18082/echo-feign/feign-rest を入力します。

    成功した応答は、URL で提供された文字列をエコーします。最初のコマンドは rest-rest を返し、2番目のコマンドは feign-rest を返し、サービス呼び出しが成功したことを確認します。

    consumer consumes Provider

よくある質問

MSE コンソールにサービスが表示されない

アプリケーションアクセスのためのホワイトリストを設定する必要があります。詳細については、「パブリック IP アドレスホワイトリストの設定」をご参照ください。

説明

デフォルトでは、MSE ホワイトリストは 127.0.0.1/32 に設定されており、すべての外部アクセスをブロックします。

サポートされている Spring Cloud のバージョン

  • Spring Cloud Greenwich の場合、対応する Spring Cloud Alibaba のバージョンは 2.1.1.RELEASE です。

  • Spring Cloud Finchley の場合、対応する Spring Cloud Alibaba のバージョンは 2.0.1.RELEASE です。

  • Spring Cloud Edgware の場合、対応する Spring Cloud Alibaba のバージョンは 1.5.1.RELEASE です。

重要

Spring Cloud Edgware のライフサイクルは終了しています。新しいアプリケーションにはこのバージョンを使用しないことを推奨します。