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

Microservices Engine:MSE Nacos レジストリに Spring Cloud アプリケーションを登録する

最終更新日:Feb 21, 2025

Spring Cloud アプリケーションを作成する際に、アプリケーションのマイクロサービスエンジン(MSE)Nacos レジストリのエンドポイントを構成できます。アプリケーションが起動すると、MSE Nacos レジストリに自動的に登録されます。登録後、MSE Nacos レジストリは、Spring Cloud アプリケーションのサービス検出や構成管理などの機能を実装できます。このトピックでは、サービス呼び出しを実装するために MSE Nacos レジストリに Spring Cloud アプリケーションを登録する方法について説明します。

前提条件

  • Maven がダウンロードされ、環境変数が構成されていること。

  • インスタンスが作成されていること。詳細については、「インスタンスを作成する」をご参照ください。

  • 名前空間が作成されていること。詳細については、「名前空間を作成する」をご参照ください。このトピックでは、デフォルトの名前空間である Public を使用します。

重要

インターネット経由で MSE Nacos レジストリにアクセスする場合は、プロバイダーとコンシューマーの両方がインターネットにアクセスできることを確認する必要があります。また、アプリケーションが登録する MSE Nacos レジストリのパブリック IP アドレスのホワイトリストを構成する必要があります。詳細については、「パブリック 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 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 は廃止されました。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 マッピングでは、パスとして /echo/{string} を、HTTP メソッドとして GET を指定します。このようにして、メソッド パラメーターは 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 という名前のファイルを作成します。application.properties ファイルに次の構成を追加して、Nacos サーバーのエンドポイントを指定します。

    spring.application.name=service-provider
    server.port=18081
    spring.cloud.nacos.discovery.server-addr=mse.XX.nacos.mse.aliyuncs.com:8848
    #Configure a custom namespace
    #spring.cloud.nacos.discovery.namespace={namespaceId}               

    [インスタンス] ページで、MSE Nacos レジストリのパブリックエンドポイントを表示できます。パブリックエンドポイントは mse.XX.nacos.mse.aliyuncs.com 形式です。

    重要

    MSE Zookeeper エンジンまたは Eureka エンジンをサービス レジストリとして使用する場合は、サービス レジストリのコードを ZooKeeper または Eureka レジストリのコードに置き換える必要があります。詳細については、「使用上の注意」をご参照ください。

  7. アプリケーションが正常に登録されているかどうかを確認します。

    1. [nacos-service-provider] プロジェクトで [providerapplication][main] 関数を実行して、プロバイダーを起動します。

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

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

    4. 左側のナビゲーションウィンドウで、Service Management > Services を選択します。

    5. [サービス] ページで、アプリケーションが正常に登録されているかどうかを確認します。

コンシューマーを作成する

このセクションでは、サービス登録機能について説明し、Nacos サーバーと連携するように RestTemplate および FeignClient を構成する方法について説明します。

  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.mse パッケージで、[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] という名前のファイルを作成します。[application.properties] ファイルに次の構成を追加して、Nacos サーバーのエンドポイントを指定します。

    spring.application.name=service-consumer
    server.port=18082
    spring.cloud.nacos.discovery.server-addr=mse.XX.nacos.mse.aliyuncs.com:8848
    #Configure a custom namespace
    #spring.cloud.nacos.discovery.namespace={namespaceId}

    [インスタンス] ページで、MSE Nacos レジストリのパブリックエンドポイントを表示できます。パブリックエンドポイントは mse.XX.nacos.mse.aliyuncs.com 形式です。

    重要

    MSE Zookeeper エンジンまたは Eureka エンジンをサービス レジストリとして使用する場合は、サービス レジストリのコードを ZooKeeper または Eureka レジストリのコードに置き換える必要があります。詳細については、「使用上の注意」をご参照ください。

  7. アプリケーションが正常に登録されているかどうかを確認します。

    1. [nacos-service-consumer] プロジェクトで [consumerapplication][main] 関数を実行して、コンシューマーを起動します。

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

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

    4. 左側のナビゲーションウィンドウで、Service Management > Services を選択します。

    5. [サービス] ページで、アプリケーションが正常に登録されているかどうかを確認します。

オンプレミス環境でサービスをテストする

コンシューマーを使用して、オンプレミス環境でプロバイダーを呼び出します。

  • 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-rest と http://127.0.0.1:18082/echo-feign/feign-rest を入力し、Enter キーを押します。

    次の図は、サービス呼び出しが成功したことを示しています。

    Spring Cloud微服务应用是使用MSE调用成功

FAQ

Spring Cloud マイクロサービス アプリケーションを実行した後、Nacos サービス レジストリのサービス管理ページで関連する Spring Cloud サービスが見つからない場合はどうすればよいですか?Spring Cloud マイクロサービス アプリケーションは、オンプレミス環境で開発され、MSE Nacos レジストリに登録されています。

アプリケーションが MSE Nacos レジストリにアクセスできるように、IP アドレスのホワイトリストを構成します。詳細については、「パブリック IP アドレスのホワイトリストを構成する」をご参照ください。

説明

デフォルトでは、IP アドレスのホワイトリストは 127.0.0.1/32 に設定されています。これは、どの IP アドレスもサービス レジストリにアクセスできないことを示しています。

MSE はどの 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 は廃止されました。Spring Cloud Edgware を使用してアプリケーションを開発しないことをお勧めします。