4.3. MicroProfile Fault Tolerance アプリケーションの開発
4.3.1. MicroProfile Fault Tolerance 拡張の追加 リンクのコピーリンクがクリップボードにコピーされました!
MicroProfile Fault Tolerance 拡張は、JBoss EAP XP の一部として提供される standalone-microprofile.xml および standalone-microprofile-ha.xml 設定に含まれています。
エクステンションは標準の standalone.xml 設定に含まれません。エクステンションを使用するには、手動で有効にする必要があります。
前提条件
- EAP XP パックがインストールされている。
手順
以下の管理 CLI コマンドを使用して MicroProfile Fault Tolerance 拡張を追加します。
/extension=org.wildfly.extension.microprofile.fault-tolerance-smallrye:add以下の managenent コマンドを使用して、
microprofile-fault-tolerance-smallryeサブシステムを有効にします。/subsystem=microprofile-fault-tolerance-smallrye:add以下の管理コマンドでサーバーをリロードします。
reload
4.3.2. MicroProfile Fault 容認のための Maven プロジェクトの設定 リンクのコピーリンクがクリップボードにコピーされました!
必要な依存関係で Maven プロジェクトを作成し、MicroProfile Fault Tolerance アプリケーションを作成するためのディレクトリー構造を作成します。
前提条件
- Maven がインストールされている。
手順
Maven プロジェクトを設定します。
mvn archetype:generate \ -DgroupId=com.example.microprofile.faulttolerance \ -DartifactId=microprofile-fault-tolerance \ -DarchetypeGroupId=org.apache.maven.archetypes \ -DarchetypeArtifactId=maven-archetype-webapp \ -DinteractiveMode=false cd microprofile-fault-toleranceこのコマンドは、プロジェクトのディレクトリー構造と
pom.xml設定ファイルを作成します。POM ファイルが
jboss-eap-xp-microprofileBOM の MicroProfile Fault Tolerance アーティファクトのバージョンを自動的に管理できるようにするには、POM ファイルの<dependencyManagement>セクションに BOM をインポートします。<dependencyManagement> <dependencies> <!-- importing the microprofile BOM adds MicroProfile specs --> <dependency> <groupId>org.jboss.bom</groupId> <artifactId>jboss-eap-xp-microprofile</artifactId> <version>${version.microprofile.bom}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>${version.microprofile.bom} を、インストールされた BOM のバージョンに置き換えます。
BOM によって管理される MicroProfile Fault Tolerance アーティファクトをプロジェクト POM ファイルの
<dependency>セクションに追加します。以下の例は、MicroProfile Fault Tolerance 依存関係をファイルに追加する方法を示しています。<!-- Add the MicroProfile Fault Tolerance API. Set provided for the <scope> tag, as the API is included in the server. --> <dependency> <groupId>org.eclipse.microprofile.fault.tolerance</groupId> <artifactId>microprofile-fault-tolerance-api</artifactId> <scope>provided</scope> </dependency>
4.3.3. フォールトトレランスアプリケーションの作成 リンクのコピーリンクがクリップボードにコピーされました!
フォールトトレランスを確保するために再試行、タイムアウト、フォールバックパターンを実装するフォールトトレランスアプリケーションを作成します。
前提条件
- Maven 依存関係が設定されている。
手順
クラスファイルを保存するディレクトリーを作成します。
$ mkdir -p APPLICATION_ROOT/src/main/java/com/example/microprofile/faulttoleranceAPPLICATION_ROOT は、アプリケーションの
pom.xml設定ファイルが含まれるディレクトリーです。新しいディレクトリーに移動します。
$ cd APPLICATION_ROOT/src/main/java/com/example/microprofile/faulttolerance以下の手順では、新しいディレクトリーにすべてのクラスファイルを作成します。
以下の内容で、
Coffee.javaとしてクロージサンプルを表す単純なエンティティーを作成します。package com.example.microprofile.faulttolerance; public class Coffee { public Integer id; public String name; public String countryOfOrigin; public Integer price; public Coffee() { } public Coffee(Integer id, String name, String countryOfOrigin, Integer price) { this.id = id; this.name = name; this.countryOfOrigin = countryOfOrigin; this.price = price; } }以下の内容でクラスファイル
CoffeeApplication.javaを作成します。package com.example.microprofile.faulttolerance; import javax.ws.rs.ApplicationPath; import javax.ws.rs.core.Application; @ApplicationPath("/") public class CoffeeApplication extends Application { }以下の内容で Jakarta Contexts and Dependency Injection Bean を
CoffeeRepositoryService.javaとして作成します。package com.example.microprofile.faulttolerance; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; import javax.enterprise.context.ApplicationScoped; @ApplicationScoped public class CoffeeRepositoryService { private Map<Integer, Coffee> coffeeList = new HashMap<>(); public CoffeeRepositoryService() { coffeeList.put(1, new Coffee(1, "Fernandez Espresso", "Colombia", 23)); coffeeList.put(2, new Coffee(2, "La Scala Whole Beans", "Bolivia", 18)); coffeeList.put(3, new Coffee(3, "Dak Lak Filter", "Vietnam", 25)); } public List<Coffee> getAllCoffees() { return new ArrayList<>(coffeeList.values()); } public Coffee getCoffeeById(Integer id) { return coffeeList.get(id); } public List<Coffee> getRecommendations(Integer id) { if (id == null) { return Collections.emptyList(); } return coffeeList.values().stream() .filter(coffee -> !id.equals(coffee.id)) .limit(2) .collect(Collectors.toList()); } }以下の内容でクラスファイル
CoffeeResource.javaを作成します。package com.example.microprofile.faulttolerance; import java.util.List; import java.util.Random; import java.util.concurrent.atomic.AtomicLong; import javax.inject.Inject; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import java.util.Collections; import javax.ws.rs.PathParam; import org.eclipse.microprofile.faulttolerance.Fallback; import org.eclipse.microprofile.faulttolerance.Timeout; import org.eclipse.microprofile.faulttolerance.Retry; @Path("/coffee") @Produces(MediaType.APPLICATION_JSON) public class CoffeeResource { @Inject private CoffeeRepositoryService coffeeRepository; private AtomicLong counter = new AtomicLong(0); @GET @Retry(maxRetries = 4)1 public List<Coffee> coffees() { final Long invocationNumber = counter.getAndIncrement(); return coffeeRepository.getAllCoffees(); } @GET @Path("/{id}/recommendations") @Timeout(250)2 public List<Coffee> recommendations(@PathParam("id") int id) { return coffeeRepository.getRecommendations(id); } @GET @Path("fallback/{id}/recommendations") @Fallback(fallbackMethod = "fallbackRecommendations")3 public List<Coffee> recommendations2(@PathParam("id") int id) { return coffeeRepository.getRecommendations(id); } public List<Coffee> fallbackRecommendations(int id) { //always return a default coffee return Collections.singletonList(coffeeRepository.getCoffeeById(1)); } }アプリケーションの root ディレクトリーに移動します。
$ cd APPLICATION_ROOT以下の Maven コマンドを使用してアプリケーションをビルドします。
$ mvn clean install wildfly:deployhttp://localhost:8080/microprofile-fault-tolerance/coffeeでアプリケーションにアクセスします。
関連情報
-
アプリケーションの耐障害性をテストするためのエラーを含むフォールトトレランスアプリケーションの詳細は、
microprofile-fault-toleranceクイックスタートを参照してください。