API 관리 추가

Cloud Endpoints Frameworks는 확장 가능 서비스 프록시(ESP)가 Cloud Endpoints에 제공하는 기능과 비슷한 API 관리 기능을 제공합니다. Endpoints Frameworks에는 요청을 API 백엔드로 전달하기 전에 모든 요청을 가로채고 필요한 검사(예: 인증)를 수행하는 기본 제공 API 게이트웨이가 포함되어 있습니다. 백엔드가 응답하면 Endpoints Frameworks는 원격 분석 데이터를 수집하고 보고합니다. API의 측정항목은 Google Cloud Console의 Endpoints > 서비스 페이지에서 확인할 수 있습니다.

Endpoints Frameworks에서 제공하는 API 관리 기능은 다음과 같습니다.

Endpoints를 통해 API를 관리하려면 OpenAPI 사양 버전 2.0을 사용하여 API를 설명하는 OpenAPI 문서를 배포해야 합니다. 이 페이지에서는 API를 관리하도록 Endpoints를 사용 설정하는 OpenAPI 문서를 생성하고 배포하는 방법을 설명합니다.

API 관리를 추가하지 않으면 API는 계속해서 요청을 처리하지만 Google Cloud Console의 Endpoints > 서비스 페이지에 API가 표시되지 않고 Endpoints에서 제공하는 기능(예: 로깅, 모니터링, 할당량 설정)을 사용할 수 없습니다.

API에 API 관리를 추가하려면 다음 단계를 따르세요.

  1. 빌드 파일 구성에 설명된 대로 Maven pom.xml 파일 또는 Gradle build.gradle 파일을 설정합니다.

  2. 빌드 파일에 Google Cloud 프로젝트 ID를 설정해야 합니다.

    Maven

    <endpoints.project.id>를 찾고 YOUR_PROJECT_ID를 Google Cloud 프로젝트 ID로 바꿉니다. 예를 들면 다음과 같습니다.

    <endpoints.project.id>example-project-12345</endpoints.project.id>

    <endpoints.project.id>YOUR_PROJECT_ID</endpoints.project.id>

    Gradle

    1. def projectId를 찾고 YOUR_PROJECT_ID를 Google Cloud 프로젝트 ID로 바꿉니다. 예:

      def projectId = 'example-project-12345'

    2. build.gradle 파일에 replaceProjectId 작업이 있는지 확인합니다. 이 작업은 appengine-web.xmlweb.xml 파일에 프로젝트 ID를 설정합니다.

      task replaceProjectId(type: Copy) {
          from 'src/main/webapp/WEB-INF/'
          include '*.xml'
          into "build/exploded-${archivesBaseName}/WEB-INF"
          expand(endpoints:[project:[id:projectId]])
          filteringCharset = 'UTF-8'
      }

  3. API 프로젝트의 web.xml 파일에서 API 관리 서블릿 필터 구성을 추가합니다.

    <!-- Add a filter that fetches the service config from service management. -->
    <filter>
        <filter-name>endpoints-api-configuration</filter-name>
        <filter-class>com.google.api.control.ServiceManagementConfigFilter</filter-class>
    </filter>
    
    <!-- Add a filter that performs Endpoints logging and monitoring. -->
    <filter>
        <filter-name>endpoints-api-controller</filter-name>
        <filter-class>com.google.api.control.extensions.appengine.GoogleAppEngineControlFilter</filter-class>
        <init-param>
            <param-name>endpoints.projectId</param-name>
            <param-value>${endpoints.project.id}</param-value>
        </init-param>
        <init-param>
            <param-name>endpoints.serviceName</param-name>
            <param-value>${endpoints.project.id}.appspot.com</param-value>
        </init-param>
    </filter>
    
    <filter-mapping>
        <filter-name>endpoints-api-configuration</filter-name>
        <servlet-name>EndpointsServlet</servlet-name>
    </filter-mapping>
    
    <filter-mapping>
        <filter-name>endpoints-api-controller</filter-name>
        <servlet-name>EndpointsServlet</servlet-name>
    </filter-mapping>
  4. API 프로젝트의 빌드 구성을 수정합니다.

    Maven

    1. API 관리 종속 항목을 추가합니다.

      <dependency>
        <groupId>com.google.endpoints</groupId>
        <artifactId>endpoints-management-control-appengine-all</artifactId>
        <version>1.0.14</version>
      </dependency>
    2. 클라이언트 라이브러리 및 OpenAPI 문서 openapi.json을 생성하는 데 사용할 수 있는 플러그인을 포함합니다.

      <plugin>
        <groupId>com.google.cloud.tools</groupId>
        <artifactId>endpoints-framework-maven-plugin</artifactId>
        <version>2.1.0</version>
        <configuration>
          <!-- plugin configuration -->
          <hostname>${endpoints.project.id}.appspot.com</hostname>
        </configuration>
      </plugin>

    Gradle

    1. API 관리 종속 항목을 추가합니다.

      compile 'com.google.endpoints:endpoints-management-control-appengine:1.0.14'
      compile 'com.google.endpoints:endpoints-framework-auth:1.0.14'
    2. 플러그인이 Maven Central에서 검색되도록 외부 종속 항목을 선언합니다.

      classpath 'com.google.cloud.tools:endpoints-framework-gradle-plugin:2.1.0'
    3. OpenAPI 문서를 생성하는 서버 측 Endpoints Frameworks Gradle 플러그인을 사용합니다.

      apply plugin: 'com.google.cloud.tools.endpoints-framework-server'
    4. Endpoints 서비스 이름을 구성합니다.

      endpointsServer {
        // Endpoints Framework Plugin server-side configuration
        hostname = "${projectId}.appspot.com"
      }
  5. 종속 항목을 수정한 후 프로젝트를 삭제하고 API를 빌드합니다.

    Maven

        mvn clean
        mvn package

    Gradle

        gradle clean
        gradle build
  6. OpenAPI 문서 openapi.json을 생성합니다.

    Maven

    mvn endpoints-framework:openApiDocs

    Gradle

    gradle endpointsOpenApiDocs
  7. OpenAPI 문서를 배포합니다.

     gcloud endpoints services deploy openapi.json
    

    openapi.json을 처음 배포할 때 새 Endpoints 서비스가 YOUR_PROJECT_ID.appspot.com이라는 이름으로 생성됩니다. 성공적으로 완료되면 서비스 구성 ID와 서비스 이름을 보여주는 다음과 같은 줄이 표시됩니다.

    Service Configuration 2017-02-13r0 uploaded for service example-project-12345.appspot.com
    

    앞의 예시에서 2017-02-13r0는 서비스 구성 ID입니다. 서비스 구성 ID는 날짜 스탬프와 버전 번호로 구성됩니다. openapi.json을 다시 배포하면 서비스 구성 ID의 버전 번호가 증가합니다.

    서비스 구성 ID를 다시 표시해야 하면 다음 명령어를 실행하지만, YOUR_PROJECT_ID를 Google Cloud 프로젝트의 프로젝트 ID로 바꿉니다.

    gcloud endpoints configs list --service=YOUR_PROJECT_ID.appspot.com
    

    생성된 문서를 사용하지 않고 자체 OpenAPI 문서를 만들어서 배포해도 됩니다. 단순히 위에서 openapi.json을 OpenAPI 문서 경로로 바꿉니다. OpenAPI 문서 작성에 대한 자세한 내용은 OpenAPI 개요를 참조하세요.

  8. appengine-web.xml 파일을 수정하여 환경 변수의 값을 설정합니다.

    <env-variables>
        <env-var name="ENDPOINTS_SERVICE_NAME" value="${endpoints.project.id}.appspot.com" />
    </env-variables>

    ${endpoints.project.id}를 Google Cloud 프로젝트 ID로 바꿉니다. 예:

    <env-var name="ENDPOINTS_SERVICE_NAME" value="example-project-12345.appspot.com" />
    
  9. 애플리케이션을 다시 배포합니다.

    Maven

    mvn appengine:deploy

    Gradle

    gradle appengineDeploy

  10. 몇 가지를 요청하여 API를 테스트합니다.

  11. API 측정항목을 보려면 프로젝트의 Google Cloud Console에서 Endpoints > 서비스 페이지를 엽니다.

    Endpoints 서비스 페이지로 이동