Flex Gateway新着情報
Governance新着情報
Monitoring API Managerアプリケーションで WebSockets インバウンドエンドポイントを公開する場合、<ws:server-settings>
要素を使用してそれをセットアップする必要があります。
WebSockets 接続は HTTP 要求で開始されるため、WebSockets インバウンドエンドポイントを公開するには、基盤となる HTTP リスナー設定を参照する必要があります。これにより、HTTP リスナーの使用可能なすべての機能および設定 (ホスト、ポート、TLS など) を再利用できるようになるだけでなく、アプリケーションで同じポートを使用して WebSockets エンドポイントと通常の HTTP エンドポイントの両方を公開できるようになります。
HTTP リスナー設定を使用して、ホスト、ポート、ベースパスなどのベース接続設定を指定したり、TLS ベースの接続 (WSS) を設定したりできます。
<http:listener-config name="httpListenerConfig" basePath="/regularHttp">
<http:listener-connection host="0.0.0.0" port="80" />
</http:listener-config>
<websocket:config name="wsServer">
<websocket:connection>
<websocket:server-settings listenerConfig="httpListenerConfig" listenerBasePath="/ws"/>
</websocket:connection>
</websocket:config>
<flow name="chat">
<websocket:inbound-listener path="/chat" config-ref="wsServer"/>
<logger level="INFO" message="A new message has been received! #[payload]"/>
</flow>
xml
WebSockets サーバー設定では、listenerConfig
属性の HTTP リスナー設定を参照します。この例では、WebSockets インバウンドリスナーは、HTTP リスナー設定で設定された localhost
およびポート 80 で受信メッセージをリスンします。また、basePath を /ws
に設定して、WebSockets 設定の HTTP basePath 設定を上書きします。要約すると、WebSockets インバウンドリスナーは、ws://localhost:80/ws/chat
で受信メッセージをリスンします。
HTTP リスナーには、TLS 設定コンポーネントへの参照がないため、デフォルトでは WS プロトコルを使用して接続が開きます。
WebSockets 接続は、永続的でステートフルです。そのため、アイドル状態の接続が自動的に終了するようにタイムアウトを設定することをお勧めします。
次の例は、<websocket:server-settings>
を使用して 1 時間のアイドルソケットタイムアウトを設定する方法を示しています。
<http:listener-config name="httpListenerConfig" basePath="/regularHttp">
<http:listener-connection host="0.0.0.0" port="80" />
</http:listener-config>
<websocket:config name="wsServer">
<websocket:connection>
<websocket:server-settings
listenerConfig="httpListenerConfig"
listenerBasePath="/ws"
idleSocketTimeout="1"
idleSocketTimeoutUnit="HOURS">
</websocket:connection>
</websocket:config>
xml
デフォルトでは、すべてのインバウンドソケットに 15 分のアイドルタイムアウトが設定されています。
アプリケーションが外部 WebSockets サービスに接続する必要がある場合、クライアント設定要素を指定します。
<websocket:config name="wsClient">
<websocket:connection>
<websocket:client-settings
host="localhost"
port="80"
basePath="/ws" />
</websocket:connection>
</websocket:config>
<flow="connectToWebSocketFlow">
<websocket:open-outbound-socket
path="/chat"
socketId="myCustomWebSocketID-123"
config-ref="wsClient" />
</websocket:open-outbound-socket>
<logger info="INFO" message="Opened connection to external service!"/>
</flow>
xml
この例では、config-ref 属性の WebSockets クライアント設定 (wsClient) を参照するアウトバウンドソケットを初期化しています。この場合、HTTP 設定を参照する必要はありません。
上記の設定では、WS 要求 URI を構築するために host
、port
、basePath
が指定されています。host
パラメーターは省略可能です。設定しない場合は、localhost
に設定されます。
port
パラメーターは省略可能です。指定しない場合は、プロトコルスキームに従ってデフォルト値が設定されます。WS が使用される場合、デフォルト値は 80
に設定されます。WSS が使用される場合、デフォルト値は 443
に設定されます。
basePath
パラメーターは省略可能です。URI が設定されていない場合は、指定したホストおよびポートに相対的なすべてのクライアント要求のベースパスを示します。basePath
パラメーターが設定されていない場合、デフォルト値は /
に設定されます。
また、URL を指定することもできます。
<websocket:config name="wsClient">
<websocket:connection>
<websocket:client-settings uri="ws://www.mulesoft.com" />
</websocket:connection>
</websocket:config>
xml
ただし、その場合、URL
パラメーターと path
パラメーターは相互に排他的であるため、パスを指定することはできません。これは、パスがホスト、ポート、ベースパスに相対的なパスを示すのに対して、URL はフルリソースロケーターを示すためです。
クライアント設定で、接続要求メッセージに含めるデフォルトヘッダーとデフォルトクエリパラメーターを指定できます。
<websocket:config name="wsClient">
<websocket:connection>
<websocket:client-settings host="localhost" port="${listenerPort}" basePath="/ws">
<websocket:default-headers>
<websocket:header key="myFirstDefaultHeader" value="defaultHeader1" />
<websocket:header key="mySecondDefaultHeader" value="defaultHeader2" />
</websocket:default-headers>
<websocket:default-query-params>
<websocket:query-param key="myFirstDefaultQueryParam" value="query1" />
<websocket:query-param key="mySecondDefaultQueryParam" value="query2" />
</websocket:default-query-params>
</websocket:client-settings>
</websocket:connection>
</websocket:config>
xml
ヘッダーまたはクエリパラメーターを connect 操作に追加することもできます。これらは、接続を確立するために要求のデフォルトヘッダーおよびパラメーターと共に送信されます。
<flow="connectToWebSocketFlow">
<websocket:open-outbound-socket path="/chat" config-ref="wsClient">
<websocket:headers><![CDATA[
#[
{
chatAlias: 'Captain Marvel'
}
]
]]>
</websocket:headers>
<websocket:query-params><![CDATA[
#[
{
theme: 'USA'
}
]
]]>
</websocket:query-params>
</websocket:open-outbound-socket>
</flow>
xml
インバウンド接続と同様に、アイドル状態のアウトバウンド接続が自動的に終了するようにタイムアウトを設定することをお勧めします。
次の例は、<websocket:client-settings>
を使用して 1 時間のアイドルソケットタイムアウトを設定する方法を示しています。
<websocket:config name="wsServer">
<websocket:connection>
<websocket:client-settings
host="localhost"
port="${listenerPort}"
basePath="/ws"
connectionIdleTimeout="1"
connectionIdleTimeoutUnit="HOURS" />
</websocket:connection>
</websocket:config>
xml
デフォルトでは、アウトバウンドソケットにアイドルタイムアウトは設定されていません。ただし、接続先のリモートサービスには設定されていることが多いです。
アプリケーションで受信接続のリスンと外部サービスへの要求の発行の両方を行う場合、サーバーとクライアントを一緒に設定できます。
<http:listener-config
name="httpListenerConfig"
basePath="/willBeOverriddenByWSConfig">
<http:listener-connection host="0.0.0.0" port="${listenerPort}" />
</http:listener-config>
<websocket:config name="ws">
<websocket:connection>
<websocket:server-settings
listenerConfig="httpListenerConfig"
listenerBasePath="/ws" />
<websocket:client-settings
host="localhost"
port="80"
basePath="/ws" />
</websocket:connection>
</websocket:config>
xml
この設定は、インバウンドソケットとアウトバウンドソケットの両方から参照できます。
<flow="acceptIncomingMessagesFlow">
<websocket:inbound-listener path="/quotes" config-ref="ws" />
<logger info="INFO" message="I listen for incoming messages at '/quotes'!"/>
</flow>
<flow="connectToWebSocketFlow">
<websocket:open-outbound-socket path="/chat" config-ref="ws"/>
<logger
info="INFO"
message="Open a connection to an external service listening at path '/chat'!"/>
</flow>
xml
TLS (トランスポート層セキュリティ) を使用すると、クライアントアプリケーションとサーバーアプリケーションは安全なチャネルを介して通信でき、攻撃者による送信データの傍受や改ざんを防ぐことができます。
WebSockets サーバーの場合、参照される HTTP リスナー設定から TLS 設定が継承されます。たとえば、WebSockets リスナーで TLS を設定するには、TLS が設定された <http:listener-config>
を参照します。
まず、トラストストア (アプリケーションが外部サービスと接続する場合) またはキーストア (アプリケーションが受信接続をリスンする場合)、あるいはその両方をセットアップする必要があります。
次の例では、WebSockets サーバーが HTTP リスナーから TLS 設定を継承し、<tls:context>
を参照してキーストアを設定します。
<tls:context name="listenerTlsContext" >
<tls:key-store
path="tls/muleKeystore"
keyPassword="mulepassword"
password="mulepassword"
alias="muleserver" />
</tls:context>
<http:listener-config name="listenerTlsConfig">
<http:listener-connection
protocol="HTTPS"
host="localhost"
port="${listenerPort}"
tlsContext="listenerTlsContext"/>
</http:listener-config>
<websocket:config name="requestConfigWithCertificate">
<websocket:connection>
<websocket:server-settings
listenerConfig="listenerTlsConfig"
listenerBasePath="/"/>
</websocket:connection>
</websocket:config>
xml
WebSocket クライアントの場合、tlsConfig
プロパティで <tls:context>
を直接参照してトラストストアをセットアップできます。
<tls:context name="requestTlsContextWithCertificate" >
<tls:trust-store path="tls/trustStore" password="mulepassword" />
</tls:context>
<websocket:config name="requestConfigWithCertificate">
<websocket:connection>
<websocket:client-settings
host="localhost"
port="${listenerPort}"
tlsContext="requestTlsContextWithCertificate"
protocol="WSS"/>
</websocket:connection>
</websocket:config>
xml
protocol
パラメーターは省略可能で、デフォルトは WS (安全ではない) です。WSS
に設定すると、接続で SSL が有効になります。<tls:content>
が指定されている場合、SSL セッションの作成にその設定が使用されます。それ以外の場合、デフォルト設定が使用されます。
これで設定が完了したので、WebSockets 『例』を試すことができます。