---
schema_version: "1"
id: 6510ef087b0da2fa72a1ec5a04a0ba269dbfa64377d57a488383ef5cbc1376e7
title: ローカルモードでの複数のアップストリームサービスを使用した API のパブリッシュ
collection: mulesoft
version: current
locale: ja-jp
portal_url: https://docs.salesforce.com/mulesoft/current/ja-jp/gateway/1.9/flex-local-publish-api-multiple-services.md
canonical_url: https://docs.mulesoft.com/jp/gateway/1.9/flex-local-publish-api-multiple-services
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 0a2726a606786cce81d3cde43adab6ac91c60651394d2390e57d0fe657e939e4
description: MuleSoft Documentation Site
release: "1.9"
latest: false
---
# ローカルモードでの複数のアップストリームサービスを使用した API のパブリッシュ

ローカルモードで実行されている Flex Gateway は、1 つのコンシューマーエンドポイントを通じて複数のアップストリームサービスを公開する API インスタンスをサポートしています。

Flex Gateway は、定義済みのルート順になっている複数のルートと個別のルールセットを使用してトラフィックを異なるセットのアップストリームサービスに転送することで、トラフィックを管理します。各 API インスタンスでは、トラフィックを複数のアップストリームサービスに転送できる複数のルートがサポートされます。

ローカルモードで実行されている Flex Gateway では、複数のアップストリームサービスがある API インスタンスは ​`ApiInstance`​ および ​`PolicyBinding`​ YAML 設定リソースを使用して設定されます。​`ApiInstance`​ リソースでは API インスタンスが定義され、​`route-weighted`​ ポリシーリソースでは 1 つ以上のアップストリームサービスへの 1 つのルートが定義されます。ルートをさらに追加するには、複数の ​`route-weighted`​ リソースを追加します。

次のチュートリアルでは、事前設定済みの ​`route-weighted`​ ポリシーリソースを使用しています。リソースの設定方法については、[複数のアップストリームサービスポリシーのトラフィック管理 (加重)](https://docs.mulesoft.com/jp/gateway/1.9/policies-included-traffic-management)を参照してください。

<table class="tableblock frame-all grid-all stretch">
<colgroup>
<col style="width: 33.3333%;">
<col style="width: 33.3333%;">
<col style="width: 33.3334%;">
</colgroup>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><div class="content"><div class="paragraph">
<p><span class="image"><a class="image" href="#linux"><img src="https://docs.mulesoft.com/jp/gateway/1.9/_images/install-linux-logo.png" alt="20%" width="20%"></a></span></p>
</div></div></td>
<td class="tableblock halign-left valign-top"><div class="content"><div class="paragraph">
<p><span class="image"><a class="image" href="#docker"><img src="https://docs.mulesoft.com/jp/gateway/1.9/_images/install-docker-logo.png" alt="25%" width="25%"></a></span></p>
</div></div></td>
<td class="tableblock halign-left valign-top"><div class="content"><div class="paragraph">
<p><span class="image"><a class="image" href="#kubernetes"><img src="https://docs.mulesoft.com/jp/gateway/1.9/_images/install-kubernetes-logo.png" alt="20%" width="20%"></a></span></p>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><div class="content"><div class="paragraph">
<p><a href="#linux">Linux で Flex Gateway の背後で実行されている API のパブリッシュ</a></p>
</div></div></td>
<td class="tableblock halign-left valign-top"><div class="content"><div class="paragraph">
<p><a href="#docker">Docker コンテナで Flex Gateway の背後で実行されている API のパブリッシュ</a></p>
</div></div></td>
<td class="tableblock halign-left valign-top"><div class="content"><div class="paragraph">
<p><a href="#kubernetes">Kubernetes クラスターで Flex Gateway の背後で実行されている API のパブリッシュ</a></p>
</div></div></td>
</tr>
</tbody>
</table>

## [](#linux)Linux で Flex Gateway の背後で実行されている API のパブリッシュ

### [](#始める前に)始める前に

始める前に、次の準備が整っていることを確認します。

- Flex Gateway がダウンロード済みである。詳細は、[Flex Gateway のダウンロード](https://docs.mulesoft.com/jp/gateway/1.9/flex-install)を参照してください。
- Flex Gateway が登録済みで、ローカルモードで実行されている。詳細は、​[「ローカルモードでの登録と実行」](https://docs.mulesoft.com/jp/gateway/1.9/flex-local-reg-run)​を参照してください。
- アップストリームサービスの URL が用意されている。

### [](#api-のパブリッシュ)API のパブリッシュ

1. `.yaml`​ ファイル拡張子で設定ファイルを作成します。
    
    1. ファイルにカスタム名を付けます。
    2. ファイルを Flex Gateway の設定ディレクトリ ​`/etc/mulesoft/flex-gateway/conf.d/custom`​ に保存します。このディレクトリには複数の設定ファイルを保存できます。
2. 次の YAML スニペットをコピーしてファイルに貼り付け、設定項目値を適切な値に変更します。
    
    次のスニペットでは架空の ​`products-api`​ と ​`products-api-beta`​ というサービスと架空の ​`json-http`​ API インスタンスを使用しています。各自の API 参照名と各自のサービスの詳細を指定してください。
    
    apiVersion: gateway.mulesoft.com/v1alpha1
    kind: ApiInstance
    metadata:
      name: json-http
    spec:
      address: http://0.0.0.0:8080/json
    ​
    ---
    ​
    apiVersion: gateway.mulesoft.com/v1alpha1
    kind: PolicyBinding
    metadata:
      name: weight-routing-test
    spec:
      targetRef:
        name: json-http
      policyRef:
        name: route-weighted
      config:
        routes:
        - weight: 75 # max\_value=100, min\_value=1
          destinationPath: /posts/
          destinationRef:
            name: products-api
        - weight: 25
          destinationPath: /posts/
          destinationRef:
            name: products-api-beta
      rules:
      - path: (.\*)
        methods: POST
        headers:
          x-test: true
        host: test.com
      - methods: GET|POST
        headers:
          x-test: true
          y-test: false
        host: test.com
3. ファイルを保存します。ゲートウェイによって設定が自動的に更新されます。
4. 次のコマンドを実行してログを表示します。
    
    journalctl -u flex-gateway-\*
    
    応答は次のようになります。
    
    \[agent\]\[info\] Generating config
    \[agent\]\[info\] Gateway default/18b4e890fe7d: Adding ApiInstance default/products-users-api http://0.0.0.0:8080
    \[agent\]\[info\] Gateway default/18b4e890fe7d: Adding Route: &{host: path:/api/products(/.\*) methods: headerConditions:\[\] profile:0xc0030529f0} => {Kind:Service Name:products-users-api-products Namespace:default}
    \[agent\]\[info\] Gateway default/18b4e890fe7d: Adding Route: &{host: path:/api/users(/.\*) methods: headerConditions:\[\] profile:0xc0030529f0} => {Kind:Service Name:products-users-api-users Namespace:default}
    \[agent\]\[info\] Gateway default/18b4e890fe7d: Adding Policy default/envoy.filters.http.router
    \[agent\]\[info\] Gateway default/18b4e890fe7d: Adding Service default/monitoring\_metrics http://0.0.0.0:9881
    \[agent\]\[debug\] generating service monitoring\_metrics.default.svc hostname: 0.0.0.0 port: 9881
    \[agent\]\[info\] Gateway default/18b4e890fe7d: Adding Service default/products-users-api-products https://<your products URL>:<your port>/
    \[agent\]\[info\] Gateway default/18b4e890fe7d: Adding Service default/products-users-api-users https://<your users URL>:<your port>/
    \[agent\]\[debug\] generating service products-users-api-products.default.svc hostname: <your products URL> port: <your port>
    \[agent\]\[debug\] generating service products-users-api-users.default.svc hostname: <your users URL> port: <your port>
    \[agent\]\[info\] Writing envoy bootstrap configuration to /tmp/envoy.json
    \[envoy\]\[info\] cds: add 2 cluster(s), remove 2 cluster(s)
    \[envoy\]\[info\] cds: added/updated 1 cluster(s), skipped 1 unmodified cluster(s)

複数のアップストリームサービスを使用した API は Flex Gateway の背後で実行されるようになりました。

## [](#docker)Docker コンテナで Flex Gateway の背後で実行されている API のパブリッシュ

### [](#始める前に-2)始める前に

始める前に、次の準備が整っていることを確認します。

- Flex Gateway がダウンロード済みである。詳細は、[Flex Gateway のダウンロード](https://docs.mulesoft.com/jp/gateway/1.9/flex-install)を参照してください。
- Flex Gateway が登録済みで、ローカルモードで実行されている。詳細は、​[「ローカルモードでの登録と実行」](https://docs.mulesoft.com/jp/gateway/1.9/flex-local-reg-run)​を参照してください。
- アップストリームサービスの URL が用意されている。

### [](#api-のパブリッシュ-2)API のパブリッシュ

1. ターミナルを開いて、Flex Gateway 設定ファイルが保存されているディレクトリに移動します。このディレクトリは、Flex Gateway を起動したときに指定されています。
2. `.yaml`​ ファイル拡張子で設定ファイルを作成します。
    
    1. ファイルにカスタム名を付けます。
    2. ファイルを保存します。
3. 次の YAML スニペットをコピーしてファイルに貼り付け、設定項目を適切な値に変更します。
    
    次のスニペットでは架空の ​`products-api`​ と ​`products-api-beta`​ というサービスと架空の ​`json-http`​ API インスタンスを使用しています。各自の API 参照名と各自のサービスの詳細を指定してください。
    
    apiVersion: gateway.mulesoft.com/v1alpha1
    kind: ApiInstance
    metadata:
      name: json-http
    spec:
      address: http://0.0.0.0:8080/json
    ​
    ---
    ​
    apiVersion: gateway.mulesoft.com/v1alpha1
    kind: PolicyBinding
    metadata:
      name: weight-routing-test
    spec:
      targetRef:
        name: json-http
      policyRef:
        name: route-weighted
      config:
        routes:
        - weight: 75 # max\_value=100, min\_value=1
          destinationPath: /posts/
          destinationRef:
            name: products-api
        - weight: 25
          destinationPath: /posts/
          destinationRef:
            name: products-api-beta
      rules:
      - path: (.\*)
        methods: POST
        headers:
          x-test: true
        host: test.com
      - methods: GET|POST
        headers:
          x-test: true
          y-test: false
        host: test.com
4. ファイルを保存します。ゲートウェイによって設定が自動的に更新されます。
5. Docker コンテナログを表示します。内容は次のようになっています。
    
    \[agent\]\[info\] Generating config
    \[agent\]\[info\] Gateway default/18b4e890fe7d: Adding ApiInstance default/products-users-api http://0.0.0.0:8080
    \[agent\]\[info\] Gateway default/18b4e890fe7d: Adding Route: &{host: path:/api/products(/.\*) methods: headerConditions:\[\] profile:0xc0030529f0} => {Kind:Service Name:products-users-api-products Namespace:default}
    \[agent\]\[info\] Gateway default/18b4e890fe7d: Adding Route: &{host: path:/api/users(/.\*) methods: headerConditions:\[\] profile:0xc0030529f0} => {Kind:Service Name:products-users-api-users Namespace:default}
    \[agent\]\[info\] Gateway default/18b4e890fe7d: Adding Policy default/envoy.filters.http.router
    \[agent\]\[info\] Gateway default/18b4e890fe7d: Adding Service default/monitoring\_metrics http://0.0.0.0:9881
    \[agent\]\[debug\] generating service monitoring\_metrics.default.svc hostname: 0.0.0.0 port: 9881
    \[agent\]\[info\] Gateway default/18b4e890fe7d: Adding Service default/products-users-api-products https://<your products URL>:<your port>/
    \[agent\]\[info\] Gateway default/18b4e890fe7d: Adding Service default/products-users-api-users https://<your users URL>:<your port>/
    \[agent\]\[debug\] generating service products-users-api-products.default.svc hostname: <your products URL> port: <your port>
    \[agent\]\[debug\] generating service products-users-api-users.default.svc hostname: <your users URL> port: <your port>
    \[agent\]\[info\] Writing envoy bootstrap configuration to /tmp/envoy.json
    \[envoy\]\[info\] cds: add 2 cluster(s), remove 2 cluster(s)
    \[envoy\]\[info\] cds: added/updated 1 cluster(s), skipped 1 unmodified cluster(s)

複数のアップストリームサービスを使用した API は Flex Gateway の背後で実行されています。

## [](#kubernetes)Kubernetes クラスターで Flex Gateway の背後で実行されている API のパブリッシュ

### [](#始める前に-3)始める前に

始める前に、次の準備が整っていることを確認します。

- Flex Gateway がダウンロード済みである。詳細は、[Flex Gateway のダウンロード](https://docs.mulesoft.com/jp/gateway/1.9/flex-install)を参照してください。
- Flex Gateway が登録済みで、ローカルモードで実行されている。詳細は、​[「ローカルモードでの登録と実行」](https://docs.mulesoft.com/jp/gateway/1.9/flex-local-reg-run)​を参照してください。
- アップストリームサービスの URL が用意されている。

アップストリームサービスとして [Kubernetes サービス](https://kubernetes.io/docs/concepts/services-networking/service/)​を使用しているか、Kubernetes サービスに対する要求を行うポリシーを適用する場合は、​`service`​ リソースで次のパラメーターを設定します。

- `spec.ports[].protocol`​ を ​`TCP`​ に
- `spec.ports[].name`

`spec.ports[].name`​ サービスは ​`<protocol>[-<suffix>]`​ 構造に準拠している必要があります。 `[-<suffix>]`​ は省略可能であり、​`<protocol>`​ の有効なオプションは次のとおりです。

- HTTP
- H2
- HTTPS

### [](#api-のパブリッシュ-3)API のパブリッシュ

1. `kubectl apply`​ コマンドを使用して次の ​`.yaml`​ 設定ファイルを適用し、設定項目値を適切な値に変更します。
    
    次のスニペットでは架空の ​`products-api`​ と ​`products-api-beta`​ というサービスと架空の ​`json-http`​ API インスタンスを使用しています。各自の API 参照名と各自のサービスの詳細を指定してください。
    
    apiVersion: gateway.mulesoft.com/v1alpha1
    kind: ApiInstance
    metadata:
      name: json-http
    spec:
      address: http://0.0.0.0:8080/json
    ​
    ---
    ​
    apiVersion: gateway.mulesoft.com/v1alpha1
    kind: PolicyBinding
    metadata:
      name: weight-routing-test
    spec:
      targetRef:
        name: json-http
      policyRef:
        name: route-weighted
      config:
        routes:
        - weight: 75 # max\_value=100, min\_value=1
          destinationPath: /posts/
          destinationRef:
            name: products-api
        - weight: 25
          destinationPath: /posts/
          destinationRef:
            name: products-api-beta
      rules:
      - path: (.\*)
        methods: POST
        headers:
          x-test: true
        host: test.com
      - methods: GET|POST
        headers:
          x-test: true
          y-test: false
        host: test.com
2. 次のコマンドを入力してポッドログを表示し、API が正常に作成されたことを確認します。

kubectl logs -f <pod-name>