Spring Cloud アプリケーションを MSE Nacos インスタンスのエンドポイントで設定すると、アプリケーションは起動時に MSE Nacos サービスレジストリに自動的に登録されます。この登録により、サービス検出、構成管理、およびサービス間の呼び出しが可能になります。このトピックでは、アプリケーションをレジストリに接続する方法について説明します。
前提条件
Maven をダウンロードしてインストールし、環境変数を設定します。
名前空間を作成します。このチュートリアルでは、デフォルトの名前空間
Publicを使用します。
パブリックネットワーク経由で MSE Nacos にアクセスするには、サービスプロバイダーとサービスコンシューマーの両方がパブリックネットワークアクセス権を持っている必要があります。また、アプリケーションの IP アドレスを MSE Nacos インスタンスのホワイトリストに追加する必要もあります。詳細については、「パブリック IP アドレスホワイトリストの設定」をご参照ください。
サービスプロバイダーの作成
ローカルでサービスプロバイダーアプリケーションを作成し、必要な依存関係を追加し、サービス登録とサービス検出を有効にして、MSE Nacos インスタンスをサービスレジストリとして指定します。
nacos-service-provider という名前の Maven プロジェクトを作成します。
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 のライフサイクルは終了しています。新しいアプリケーションにはこのバージョンを使用しないことを推奨します。
src\main\javaディレクトリに、com.aliware.edasという名前のパッケージを作成します。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アノテーションは、アプリケーションのサービス登録とサービス検出機能を有効にします。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; } }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 クラスターの注意事項」をご参照ください。
アプリケーションが正常に登録されたことを検証します。
nacos-service-provider プロジェクトで ProviderApplication の main メソッドを実行して、アプリケーションを起動します。
-
MSE コンソールにログインし、上部のナビゲーションバーでリージョンを選択します。
-
左側のナビゲーションウィンドウで、マイクロサービスの登録 > インスタンス を選択します。インスタンスの名前をクリックします。
-
Service ManagementServices
[サービス] ページで、service-provider アプリケーションがリストに表示されていることを確認します。
サービスコンシューマーの作成
このセクションでは、サービスコンシューマーを登録し、2つのクライアント (RestTemplate と FeignClient) を使用して Nacos のサービス検出を利用する方法について説明します。
nacos-service-consumer という名前の Maven プロジェクトを作成します。
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>src\main\javaディレクトリに、com.aliware.edasという名前のパッケージを作成します。com.aliware.edasパッケージで、RestTemplate と FeignClient を設定します。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); }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); } }
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); } }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 の対応する構成に置き換える必要があります。詳細については、「注意事項」をご参照ください。
アプリケーションが正常に登録されたことを検証します。
nacos-service-consumer プロジェクトで ConsumerApplication の main メソッドを実行して、アプリケーションを起動します。
-
MSE コンソールにログインし、上部のナビゲーションバーでリージョンを選択します。
-
左側のナビゲーションウィンドウで、マイクロサービスの登録 > インスタンス を選択します。インスタンスの名前をクリックします。
-
Service ManagementServices
[サービス] ページで、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-restWindows では、ブラウザのアドレスバーに
http://127.0.0.1:18082/echo-rest/rest-restとhttp://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 のライフサイクルは終了しています。新しいアプリケーションにはこのバージョンを使用しないことを推奨します。