LoginSignup
2
2

More than 5 years have passed since last update.

APIサービスをCloudEndpointsでGKE上に構築してPostmanで動作確認する

Last updated at Posted at 2017-04-18

概要

APIを公開したいとき、API仕様(ドキュメント)やモニタリング、認証といったものが必要になる。
Cloud Endpointsを使うとこれらのことを自分で0から実装する必要ことなくAPIを公開することができる。
以前のCloud EndpointsはAppEngine Standard Environment上で動くフレームワークでJavaやPythonだけサポートされてたが、今は違う。
今はPHP,Goなど他の言語でも利用可能、AppEngineだけでなく、コンテナでも利用可能になった。

Cloud Endpointsは分散API管理システムで、API を作成、共有、維持、保護するための機能を提供する。
OpenAPI仕様とgRPCサービスで使用可能。

Cloud Endpointsのアーキテクチャは下図のとおりであり、Extensible Service ProxyとService Management and Control APIから成る。

endpoints-architecture.png
引用元:https://cloud.google.com/endpoints/docs/about-cloud-endpoints?hl=ja

サンプルコードを利用して、GKE上にデプロイしたAPIを提供するコンテナに対して、Cloud Endpointsで外部にAPI公開を行う。Cloud Endpointsの動作確認をクイックスタートではcurlで行っていたが、今回はPostmanで行う。PostmanはWeb APIのテストクライアントでChrome Appが提供されている。

git clone https://github.com/GoogleCloudPlatform/java-docs-samples
cd java-docs-samples/endpoints/getting-started

Endpointsの設定ファイル作成

swaggerで公開するAPIに必要な情報を記載する。

endpoints/getting-started/src/main/appengine/openapi.yaml
・・・
host: "echo-api.endpoints.YOUR-PROJECT-ID.cloud.goog" ←この部分のプロジェクト名を変更
・・・

Cloud EndpointsにAPI設定をデプロイする

$ gcloud service-management deploy openapi.yaml

コンテナをGKEにデプロイ

kubectl create -f container-engine.yaml
$ kubectl get service
NAME         CLUSTER-IP     EXTERNAL-IP      PORT(S)        AGE
esp-echo     xxxxx   yyyyy   80:32524/TCP   15m

ここで出力されるEXTERNAL-IPは、Postmanで指定するのでメモしておく。

API Keyの取得

API ManagerでAPI Keyを作成する。

PostmanでAPIの動作確認

PostでAPIサービスのURLとパラメータ(keyにAPIキーの値を指定)を指定して、JSON形式のメッセージを記入。

Screen Shot 2017-04-18 at 13.08.39.png

上記のように記入した後、Sendを押すと以下のようにレスポンスが表示される。

Screen Shot 2017-04-18 at 13.08.56.png

エコーするサービスなので、入力したメッセージがレスポンスとして返ってくることを確認。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2