29 最初のCoherence RESTアプリケーションの作成
ディストリビューションに付属のCoherenceの例には、RESTアプリケーションのエンドツーエンドの例も含まれます。『Oracle Coherenceのインストール』のCoherence RESTの例に関する項を参照してください。
この章の内容は次のとおりです。
- 基本的なCoherence RESTの例の概要
Coherence RESTの例では、一連のステップを使用して基本的なCoherence RESTアプリケーションを構成および実行します。 - 前提条件
この例を構築するには、次のものをインストールする必要があります。 - ステップ1: クラスタ側の構成
Coherence RESTには、キャッシュおよびプロキシのスキームの両方が必要です。プロキシ・スキームでは、受信するHTTPリクエストを処理するHTTPアクセプタを定義する必要があります。 - ステップ2: ユーザー・タイプの作成
Personユーザー・タイプを作成します。これはキャッシュに保存され、基本的なREST操作を説明するために使用します。 - ステップ3: RESTサービスの構成
Coherence RESTサービスには、公開されるキャッシュに関するメタデータが必要です。 - ステップ4: キャッシュ・サーバー・プロセスの開始
RESTサービスは、キャッシュ・サービス・プロセス(DefaultCacheServer)の一環として公開されます。 - ステップ5: クライアントからRESTサービスへのアクセス
クライアント・アプリケーションでは、キャッシュの操作を行うためにCoherence RESTサービスを使用します。HTTPベース・クライアントを構築するためのクライアント・ライブラリを提供するアプリケーション・プラットフォームは数多くあります。
親トピック: Coherence RESTの使用
基本的なCoherence RESTの例の概要
この章の例では、アプリケーション・サーバーが不要なスタンドアロンのアプリケーションをデプロイするために、埋込みHTTPサーバーを使用します。追加のデプロイメント・オプションが使用できます。「Coherence RESTのデプロイ」を参照してください。
この章のステップを完了するには、Coherence for Javaをインストールする必要があります。またこの例では、次のユーザー定義の変数を使用します。
-
DEV_ROOT- リストされているすべてのステップを実行する場所となるルート・フォルダのパス。つまり、その後のすべてのフォルダはDEV_ROOTに対する相対フォルダになります -
COHERENCE_HOME- この変数は、Coherenceインストールのcoherenceディレクトリへのフルパスである必要があります。
親トピック: 最初のCoherence RESTアプリケーションの作成
前提条件
この例を構築するには、次のものをインストールする必要があります。
必要なソフトウェア
-
JDK 11または17
-
Maven 3.8.5+
-
Coherence 14.1.1.2206 (インストール手順については、「Oracle Coherenceのインストール」)を参照してください。
依存関係
次のCoherence依存関係をローカルのMavenリポジトリにインストールします。使用しているオペレーティング・システムに応じて、次のコマンドを使用します。
mvn install:install-file -Dfile=%COHERENCE_HOME%\lib\coherence.jar -DpomFile=%COHERENCE_HOME%\plugins\maven\com\oracle\coherence\coherence\14.1.1\coherence.14.1.1.pom mvn install:install-file -Dfile=%COHERENCE_HOME%\lib\coherence-rest.jar -DpomFile=%COHERENCE_HOME%\plugins\maven\com\oracle\coherence\coherence-rest\14.1.1\coherence-rest.14.1.1.pom mvn install:install-file -Dfile=%COHERENCE_HOME%\lib\coherence-http-netty.jar -DpomFile=%COHERENCE_HOME%\plugins\maven\com\oracle\coherence\coherence-http-netty\14.1.1\coherence-http-netty.14.1.1.pom
mvn install:install-file -Dfile=$COHERENCE_HOME/lib/coherence.jar -DpomFile=$COHERENCE_HOME/plugins/maven/com/oracle/coherence/coherence/14.1.1/coherence.14.1.1.pom mvn install:install-file -Dfile=$COHERENCE_HOME/lib/coherence-rest.jar -DpomFile=$COHERENCE_HOME/plugins/maven/com/oracle/coherence/coherence-rest/14.1.1/coherence-rest.14.1.1.pom mvn install:install-file -Dfile=$COHERENCE_HOME/lib/coherence-http-netty.jar -DpomFile=$COHERENCE_HOME/plugins/maven/com/oracle/coherence/coherence-http-netty/14.1.1/coherence-http-netty.14.1.1.pom
クラスパスの生成
ノート:
使用している特定の14.1.1.2206バージョンと一致するように、次のコードのCoherenceバージョンを更新します。DEV_ROOTにpom.xmlというファイルを作成します:<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>com.oracle.coherence.rest.example</artifactId>
<groupId>rest</groupId>
<version>1.0.0</version>
<name>Coherence REST dependencies</name>
<packaging>pom</packaging>
<properties>
<coherence.groupId>com.oracle.coherence</coherence.groupId>
<coherence.version>14.1.1-2206-10</coherence.version>
</properties>
<dependencies>
<dependency>
<groupId>${coherence.groupId}</groupId>
<artifactId>coherence-rest</artifactId>
<version>${coherence.version}</version>
</dependency>
<dependency>
<groupId>${coherence.groupId}</groupId>
<artifactId>coherence</artifactId>
<version>${coherence.version}</version>
</dependency>
<dependency>
<groupId>${coherence.groupId}</groupId>
<artifactId>coherence-http-netty</artifactId>
<version>${coherence.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<version>2.41</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.1</version>
</dependency>
</dependencies>
</project>cp.txtというファイルへのクラスパスを生成します:mvn -q dependency:build-classpath -Dmdep.outputFile=cp.txt親トピック: 最初のCoherence RESTアプリケーションの作成
ステップ1: クラスタ側の構成
クラスタ側のキャッシュ構成デプロイメント・ディスクリプタで、キャッシュおよびプロキシを構成します。この例では、プロキシはlocalhostおよびポート8080上でクライアントHTTPリクエストを受け入れるように構成されます。dist-http-exampleという名前の分散キャッシュを定義し、これを使用してクライアント・データをクラスタに格納します。
クラスタ側を構成するには:
親トピック: 最初のCoherence RESTアプリケーションの作成
ステップ2: ユーザー・タイプの作成
Personユーザー・タイプを作成します。これはキャッシュに保存され、基本的なREST操作を説明するために使用します。
Personオブジェクトを作成するには:
親トピック: 最初のCoherence RESTアプリケーションの作成
ステップ3: RESTサービスの構成
RESTサービスを構成するには:
親トピック: 最初のCoherence RESTアプリケーションの作成
ステップ4: キャッシュ・サーバー・プロセスの開始
DefaultCacheServer)の一環として公開されます。キャッシュ・サーバーのクラスパスは、前のステップで作成したすべての構成ファイルおよびPerson.classを検索できるように構成する必要があります。クラスパスには、必要な依存関係ライブラリも含まれている必要があります。「Coherence RESTの依存性」を参照してください。わかりやすくするために、依存性はすべてDEV_ROOT\libsフォルダに配置され、個別にはリストされません。
DEV_ROOTフォルダは、次のような構成になります。
\ \config \config\example-server-config.xml \config\coherence-rest-config.xml \example \example\Person.class \libs \libs\*
次のコマンド行では、キャッシュ・サーバー・プロセスが開始され、coherence.cacheconfigシステム・プロパティを使用して、ステップ1で作成したキャッシュ構成ファイルに明示的に名前が付けられます。また、「前提条件」で作成されたクラスパスが使用されます。
set DEV_ROOT=%cd% java -Dcoherence.wka=127.0.0.1 -cp %DEV_ROOT%\config;%DEV_ROOT%;"$(type cp.txt)" -Dcoherence.cacheconfig=%DEV_ROOT%\config\example-server-config.xml com.tangosol.net.Coherence
export DEV_ROOT=`pwd`
java -Dcoherence.wka=127.0.0.1 -cp ${DEV_ROOT}/config:${DEV_ROOT}:`cat cp.txt` -Dcoherence.cacheconfig=${DEV_ROOT}/config/example-server-config.xml com.tangosol.net.Coherence
ProxyService{Name=ExtendHttpProxyService, State=(SERVICE_STARTED), Id=…, OldestMemberId=1, Serializer=java}親トピック: 最初のCoherence RESTアプリケーションの作成
ステップ5: クライアントからRESTサービスへのアクセス
クライアントでdist-http-exampleキャッシュにアクセスするために使用するPUT、GETおよびPost操作のセマンティクスを次に示します。Jerseyを使用して構築されたJavaクライアントの例は、Jersey-client-2.12.jarライブラリに従っており、このライブラリが必要です。「RESTによるグリッド操作の実行」を参照してください。
curlコマンドを使用した操作の例を次に示します。
Put操作
PUT http://localhost:8080/api/dist-http-example/1
Content-Type=application/json
Request Body: {"name":"chris","age":30}PUT http://localhost:8080/api/dist-http-example/2
Content-Type=application/json
Request Body: {"name":"adam","age":26}GET操作
GET http://localhost:8080/api/dist-http-example/1.json GET http://localhost:8080/api/dist-http-example/1.xml GET http://localhost:8080/api/dist-http-example/entries?q=name is 'chris' GET http://localhost:8080/api/dist-http-example/1.json;p=name GET http://localhost:8080/api/dist-http-example/count() GET http://localhost:8080/api/dist-http-example/double-average(age)
Post操作
POST http://localhost:8080/api/dist-http-example/increment(age,1)
サンプルcurlコマンド
前述のRESTリクエストを実行するためのcurlコマンドを次に示します。
curl -v -H 'Content-Type: application/json' -X PUT http://localhost:8080/api/dist-http-example/1 -d '{"name":"chris","age":30}'
curl -v -H 'Content-Type: application/json' -X PUT http://localhost:8080/api/dist-http-example/2 -d '{"name":"adam","age":26}'curl -X GET http://localhost:8080/api/dist-http-example/1.json curl -X GET http://localhost:8080/api/dist-http-example/1.xml curl -X GET http://localhost:8080/api/dist-http-example/entries?q=name%20is%20%27adam%27 curl -X GET "http://localhost:8080/api/dist-http-example/1.json;p=name" curl -X GET "http://localhost:8080/api/dist-http-example/count()" curl -X GET "http://localhost:8080/api/dist-http-example/double-average(age)"
POST操作
curl -X POST "http://localhost:8080/api/dist-http-example/increment(age,1)"
サンプルJersey RESTクライアント
RestExampe.javaというサンプル・ディレクトリにファイルを作成し、次の内容を追加します。前にPerson.javaで使用した方法と同じ方法でコンパイルし、次を使用して実行します:
SET CLASSPATH=%CP%;%DEV_ROOT%\config;%DEV_ROOT% java example.RestExample
java -cp `cat cp.txt`:. example.RestExamplepackage example;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
public class RestExample {
public static void PUT(String url, MediaType mediaType, String data) {
process(url, "put", mediaType, data);
}
public static void GET(String url, MediaType mediaType) {
process(url, "get", mediaType, null);
}
public static void POST(String url, MediaType mediaType, String data) {
process(url, "post", mediaType, data);
}
public static void DELETE(String url, MediaType mediaType) {
process(url, "delete", mediaType, null);
}
public static void process(String sUrl, String action,
MediaType mediaType,
String data) {
Client client = ClientBuilder.newClient();
Response response = null;
WebTarget webTarget = client.target(sUrl);
String responseType = MediaType.APPLICATION_XML;
if (mediaType == MediaType.APPLICATION_JSON_TYPE) {
responseType = MediaType.APPLICATION_JSON;
}
if (action.equalsIgnoreCase("get")) {
response = webTarget.request(responseType).get();
} else if (action.equalsIgnoreCase("post")) {
Entity<String> person = Entity.entity(data, responseType);
response = webTarget.request(responseType).post(person);
} else if (action.equalsIgnoreCase("put")) {
Entity<String> person = Entity.entity(data, responseType);
response = webTarget.request(responseType).put(person);
} else if (action.equalsIgnoreCase("delete")) {
Entity<String> person = Entity.entity(data, responseType);
response = webTarget.request(responseType).delete();
}
System.out.println(response.readEntity(String.class));
}
public static void main(String[] args) throws URISyntaxException,
MalformedURLException, IOException {
PUT("http://localhost:8080/api/dist-http-example/1",
MediaType.APPLICATION_JSON_TYPE,
"{\"name\":\"chris\",\"age\":32}");
PUT("http://localhost:8080/api/dist-http-example/2",
MediaType.APPLICATION_JSON_TYPE,
"{\"name\":\"\ufeff\u30b8\u30e7\u30f3A\",\"age\":66}");
PUT("http://localhost:8080/api/dist-http-example/3",
MediaType.APPLICATION_JSON_TYPE,
"{\"name\":\"adm\",\"age\":88}");
POST("http://localhost:8080/api/dist-http-example/increment(age,1)",
MediaType.APPLICATION_XML_TYPE, null);
GET("http://localhost:8080/api/dist-http-example/1",
MediaType.APPLICATION_JSON_TYPE);
GET("http://localhost:8080/api/dist-http-example/1",
MediaType.APPLICATION_XML_TYPE);
GET("http://localhost:8080/api/dist-http-example/count()",
MediaType.APPLICATION_XML_TYPE);
}
}親トピック: 最初のCoherence RESTアプリケーションの作成