عرض المحتوى الديناميكي واستضافة الخدمات المصغَّرة باستخدام Cloud Run

يمكنك إقران "Cloud Run" مع "Firebase Hosting" لإنشاء وعرض للمحتوى الديناميكي أو إنشاء واجهات برمجة تطبيقات REST كخدمات مصغّرة.

باستخدام Cloud Run، يمكنك نشر تطبيق حزمة في صورة حاوية. بعد ذلك، باستخدام Firebase Hosting، يمكنك توجيه طلبات HTTPS لتشغيل الحاوية.

  • يتيح Cloud Run استخدام عدة لغات (بما في ذلك Go وNode.js وPython وJava)، ما يمنحك المرونة في استخدام لغة البرمجة وإطار العمل المفضّلَين لديك.
  • Cloud Run يتدرج تلقائيًا وأفقيًا صورة الحاوية لمعالجة الطلبات التي تم استلامها، ثم يتم تقليل حجمها عندما انخفاض الطلب.
  • لن تدفع إلا مقابل استخدام وحدة المعالجة المركزية (CPU)، والذاكرة والشبكات أثناء معالجة الطلب.

للاطّلاع على أمثلة لحالات الاستخدام ونماذج لتطبيق Cloud Run المدمج مع Firebase Hosting، يُرجى الانتقال إلى مقالة نظرة عامة حول تكنولوجيا السحابة غير المزوّدة بخادم.


يوضّح لك هذا الدليل كيفية تنفيذ ما يلي:

  1. كتابة تطبيق Hello World بسيط
  2. تضمين تطبيق في حاوية وتحميله إلى Container Registry
  3. نشر صورة الحاوية على Cloud Run
  4. توجيه طلبات "Hosting" إلى تطبيقك المحوَّل

تجدر الإشارة إلى أنّه لتحسين أداء عرض المحتوى الديناميكي، يمكنك اتّباع الخطوات التالية: ضبط إعدادات ذاكرة التخزين المؤقت اختياريًا.

قبل البدء

قبل استخدام "Cloud Run"، عليك إكمال بعض المهام الأولية، بما في ذلك إعداد حساب "Cloud Billing" وتفعيل Cloud Run وتثبيت أداة سطر الأوامر gcloud.

إعداد الفوترة لمشروعك

Cloud Run يقدم حصة استخدام مجاني، ولكن لا يزال يتعين عليك حساب واحد (Cloud Billing) المرتبطة بمشروعك في Firebase لاستخدام Cloud Run أو تجربتها.

تفعيل واجهة برمجة التطبيقات وتثبيت حزمة تطوير البرامج (SDK)

  1. تفعيل واجهة برمجة التطبيقات Cloud Run في وحدة تحكّم Google APIs:

    1. افتح صفحة واجهة برمجة التطبيقات في "Cloud Run" في وحدة تحكم Google APIs.

    2. اختَر مشروعك في Firebase عندما يُطلب منك ذلك.

    3. انقر على تفعيل في صفحة واجهة برمجة التطبيقات Cloud Run.

  2. ثبِّت حزمة تطوير البرامج (SDK) لـ Cloud وفعِّلها.

  3. تحقَّق من ضبط أداة gcloud للمشروع الصحيح:

    gcloud config list

الخطوة 1: كتابة نموذج الطلب

يُرجى العِلم أنّ ميزة "Cloud Run" تتيح استخدام عدّة لغات أخرى بالإضافة إلى اللغات الموضحة في النموذج التالي.

انتقال

  1. أنشئ دليلاً جديدًا باسم helloworld-go، ثم غيِّر الدليل إلى :

    mkdir helloworld-go
    cd helloworld-go
  2. أنشئ ملفًا جديدًا باسم helloworld.go، ثم أضِف الرمز التالي:

    package main
    
    import (
    	"fmt"
    	"log"
    	"net/http"
    	"os"
    )
    
    func handler(w http.ResponseWriter, r *http.Request) {
    	log.Print("helloworld: received a request")
    	target := os.Getenv("TARGET")
    	if target == "" {
    		target = "World"
    	}
    	fmt.Fprintf(w, "Hello %s!\n", target)
    }
    
    func main() {
    	log.Print("helloworld: starting server...")
    
    	http.HandleFunc("/", handler)
    
    	port := os.Getenv("PORT")
    	if port == "" {
    		port = "8080"
    	}
    
    	log.Printf("helloworld: listening on port %s", port)
    	log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
    }
    

    ينشئ هذا الرمز خادم ويب أساسيًا ينتظر استقبال البيانات على المنفذ المحدد من قِبل متغير بيئة PORT.

اكتمل تطبيقك وأصبح جاهزًا لنقله إلى حاويات وتحميله إليه. Container Registry

Node.js

  1. أنشئ دليلاً جديدًا باسم helloworld-nodejs، ثم غيِّر الدليل. فيها:

    mkdir helloworld-nodejs
    cd helloworld-nodejs
  2. أنشئ ملف package.json يتضمّن المحتوى التالي:

    {
      "name": "knative-serving-helloworld",
      "version": "1.0.0",
      "description": "Simple hello world sample in Node",
      "main": "index.js",
      "scripts": {
        "start": "node index.js"
      },
      "author": "",
      "license": "Apache-2.0",
      "dependencies": {
        "express": "^4.20.0"
      }
    }
    
  3. أنشئ ملفًا جديدًا باسم index.js، ثم أضِف الرمز التالي:

    const express = require('express');
    const app = express();
    
    app.get('/', (req, res) => {
      console.log('Hello world received a request.');
    
      const target = process.env.TARGET || 'World';
      res.send(`Hello ${target}!\n`);
    });
    
    const port = process.env.PORT || 8080;
    app.listen(port, () => {
      console.log('Hello world listening on port', port);
    });
    

    ينشئ هذا الرمز خادم ويب أساسيًا ينتظر استقبال البيانات على المنفذ المحدد من قِبل متغير بيئة PORT.

اكتمل تطبيقك وأصبح جاهزًا لنقله إلى حاويات وتحميله إليه. Container Registry

Python

  1. أنشئ دليلاً جديدًا باسم helloworld-python، ثم غيِّر الدليل إلى:

    mkdir helloworld-python
    cd helloworld-python
  2. أنشئ ملفًا جديدًا باسم app.py، ثم أضِف الرمز التالي:

    import os
    
    from flask import Flask
    
    app = Flask(__name__)
    
    @app.route('/')
    def hello_world():
        target = os.environ.get('TARGET', 'World')
        return 'Hello {}!\n'.format(target)
    
    if __name__ == "__main__":
        app.run(debug=True,host='0.0.0.0',port=int(os.environ.get('PORT', 8080)))
    

    تنشئ هذه التعليمات البرمجية خادم ويب أساسيًا يستمع إلى المنفذ المحدَّد من خلال متغيّر البيئة PORT.

اكتمل تطبيقك وأصبح جاهزًا لنقله إلى حاويات وتحميله إليه. Container Registry

جافا

  1. تثبيت Java SE 8 أو إصدار JDK الأحدث وCURL.

    لاحظ أننا نحتاج فقط إلى القيام بذلك لإنشاء مشروع الويب الجديد في الخطوة التالية. ويحمّل الملف الشامل، الذي يرد وصفه لاحقًا، جميع والتبعيات في الحاوية.

  2. من وحدة التحكّم، أنشِئ مشروع ويب جديدًا فارغًا باستخدام مفتاح cURL ثم أوامر فك الضغط:

    curl https://start.spring.io/starter.zip \
        -d dependencies=web \
        -d name=helloworld \
        -d artifactId=helloworld \
        -o helloworld.zip
    unzip helloworld.zip

    يؤدي هذا إلى إنشاء مشروع SpringBoot.

  3. تعديل صف SpringBootApplication في src/main/java/com/example/helloworld/HelloworldApplication.java بواسطة إضافة @RestController لمعالجة عملية ربط / وإضافة @Value لتوفير متغير بيئة TARGET:

    package com.example.helloworld;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @SpringBootApplication
    public class HelloworldApplication {
    
      @Value("${TARGET:World}")
      String target;
    
      @RestController
      class HelloworldController {
        @GetMapping("/")
        String hello() {
          return "Hello " + target + "!";
        }
      }
    
      public static void main(String[] args) {
        SpringApplication.run(HelloworldApplication.class, args);
      }
    }
    

    ينشئ هذا الرمز خادم ويب أساسيًا ينتظر استقبال البيانات على المنفذ المحدد من قِبل متغير بيئة PORT.

اكتمل تطبيقك وأصبح جاهزًا للحاويات وتحميله إلى Container Registry.

الخطوة 2: حجز مساحة تخزين لتطبيق وتحميله إلى Container Registry

  1. يمكنك احتواء نموذج التطبيق من خلال إنشاء ملف جديد باسم "Dockerfile" في الدليل نفسه كملفات المصدر. انسخ المحتوى التالي في ملفك.

    انتقال

    # Use the official Golang image to create a build artifact.
    # This is based on Debian and sets the GOPATH to /go.
    FROM golang:latest as builder
    
    ARG TARGETOS
    ARG TARGETARCH
    
    # Create and change to the app directory.
    WORKDIR /app
    
    # Retrieve application dependencies using go modules.
    # Allows container builds to reuse downloaded dependencies.
    COPY go.* ./
    RUN go mod download
    
    # Copy local code to the container image.
    COPY . ./
    
    # Build the binary.
    # -mod=readonly ensures immutable go.mod and go.sum in container builds.
    RUN CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -mod=readonly -v -o server
    
    # Use the official Alpine image for a lean production container.
    # https://hub.docker.com/_/alpine
    # https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds
    FROM alpine:3
    RUN apk add --no-cache ca-certificates
    
    # Copy the binary to the production image from the builder stage.
    COPY --from=builder /app/server /server
    
    # Run the web service on container startup.
    CMD ["/server"]
    

    Node.js

    # Use the official lightweight Node.js 12 image.
    # https://hub.docker.com/_/node
    FROM node:12-slim
    
    # Create and change to the app directory.
    WORKDIR /usr/src/app
    
    # Copy application dependency manifests to the container image.
    # A wildcard is used to ensure both package.json AND package-lock.json are copied.
    # Copying this separately prevents re-running npm install on every code change.
    COPY package*.json ./
    
    # Install production dependencies.
    RUN npm install --only=production
    
    # Copy local code to the container image.
    COPY . ./
    
    # Run the web service on container startup.
    CMD [ "npm", "start" ]
    

    Python

    # Use the official lightweight Python image.
    # https://hub.docker.com/_/python
    FROM python:3.7-slim
    
    # Allow statements and log messages to immediately appear in the Knative logs
    ENV PYTHONUNBUFFERED True
    
    # Copy local code to the container image.
    ENV APP_HOME /app
    WORKDIR $APP_HOME
    COPY . ./
    
    # Install production dependencies.
    RUN pip install Flask gunicorn
    
    # Run the web service on container startup. Here we use the gunicorn
    # webserver, with one worker process and 8 threads.
    # For environments with multiple CPU cores, increase the number of workers
    # to be equal to the cores available.
    CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 --timeout 0 app:app
    

    جافا

    # Use the official maven/Java 8 image to create a build artifact: https://hub.docker.com/_/maven
    FROM maven:3.5-jdk-8-alpine as builder
    
    # Copy local code to the container image.
    WORKDIR /app
    COPY pom.xml .
    COPY src ./src
    
    # Build a release artifact.
    RUN mvn package -DskipTests
    
    # Use the Official OpenJDK image for a lean production stage of our multi-stage build.
    # https://hub.docker.com/_/openjdk
    # https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds
    FROM openjdk:8-jre-alpine
    
    # Copy the jar to the production image from the builder stage.
    COPY --from=builder /app/target/helloworld-*.jar /helloworld.jar
    
    # Run the web service on container startup.
    CMD ["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/helloworld.jar"]
    

  2. أنشئ صورة الحاوية باستخدام Cloud Build من خلال تنفيذ ما يلي: من الدليل الذي يحتوي على ملف Dockerfile:

    gcloud builds submit --tag gcr.io/PROJECT_ID/helloworld

    بعد نجاح العملية، ستظهر لك رسالة SUCCESS (تم بنجاح) تحتوي على اسم الصورة
    (gcr.io/PROJECT_ID/helloworld).

تم تخزين صورة الحاوية الآن في Container Registry ويمكن إعادة استخدامها إذا ما يريده.

يُرجى العِلم بأنّه بدلاً من Cloud Build، يمكنك استخدام إصدار مثبَّت على الجهاز. من Docker إلى أنشئ حاويتك محليًا.

الخطوة 3: نشر صورة الحاوية على Cloud Run

  1. يمكنك النشر باستخدام الأمر التالي:

    gcloud run deploy --image gcr.io/PROJECT_ID/helloworld

  2. عندما يُطلب منك ذلك:

للحصول على أفضل أداء، يمكنك تجميع خدمة Cloud Run من خلال Hosting باستخدام المناطق التالية:

  • us-west1
  • us-central1
  • us-east1
  • europe-west1
  • asia-east1

تتوفر إعادة الكتابة إلى Cloud Run من Hosting في المناطق التالية:

  • asia-east1
  • asia-east2
  • asia-northeast1
  • asia-northeast2
  • asia-northeast3
  • asia-south1
  • asia-south2
  • asia-southeast1
  • asia-southeast2
  • australia-southeast1
  • australia-southeast2
  • europe-central2
  • europe-north1
  • europe-southwest1
  • europe-west1
  • europe-west12
  • europe-west2
  • europe-west3
  • europe-west4
  • europe-west6
  • europe-west8
  • europe-west9
  • me-central1
  • me-west1
  • northamerica-northeast1
  • northamerica-northeast2
  • southamerica-east1
  • southamerica-west1
  • us-central1
  • us-east1
  • us-east4
  • us-east5
  • us-south1
  • us-west1
  • us-west2
  • us-west3
  • us-west4
  • us-west1
  • us-central1
  • us-east1
  • europe-west1
  • asia-east1
  1. يُرجى الانتظار بضع لحظات حتى تكتمل عملية النشر. عند النجاح، يستخدم سطر الأوامر يعرض عنوان URL للخدمة. مثل: https://helloworld-RANDOM_HASH-us-central1.a.run.app

  2. انتقِل إلى الحاوية المنشورة من خلال فتح عنوان URL للخدمة في متصفّح ويب.

ترشدك الخطوة التالية إلى كيفية الوصول إلى هذا التطبيق المحوَّل من Firebase Hosting عنوان URL لكي تتمكّن من إنشاء محتوى ديناميكي موقعًا إلكترونيًا مستضافًا على Firebase.

الخطوة 4: إرسال طلبات الاستضافة المباشرة إلى تطبيقك المحوَّل

باستخدام قواعد إعادة الكتابة، يمكنك توجيه الطلبات التي تتطابق مع أنماط معيّنة إلى وجهة واحدة.

يوضّح المثال التالي كيفية توجيه جميع الطلبات من الصفحة /helloworld على موقعك الإلكتروني Hosting لبدء تشغيل مثيل حاوية helloworld وتشغيله.

  1. تأكَّد مما يلي:

    للحصول على تعليمات مفصّلة حول تثبيت واجهة برمجة التطبيقات وبدء استخدام Hosting، يُرجى الاطّلاع على دليل بدء استخدام Hosting.

  2. افتح ملف firebase.json.

  3. أضِف إعدادات rewrite التالية ضمن القسم hosting:

    "hosting": {
      // ...
    
      // Add the "rewrites" attribute within "hosting"
      "rewrites": [ {
        "source": "/helloworld",
        "run": {
          "serviceId": "helloworld",  // "service name" (from when you deployed the container image)
          "region": "us-central1",    // optional (if omitted, default is us-central1)
          "pinTag": true              // optional (see note below)
        }
      } ]
    }
  4. انشر إعدادات الاستضافة على موقعك الإلكتروني من خلال تنفيذ ما يلي: الأمر من جذر دليل المشروع:

    firebase deploy --only hosting

يمكن الآن الوصول إلى حاويتك عبر عناوين URL التالية:

  • نطاقاتك الفرعية في Firebase:
    PROJECT_ID.web.app/ و PROJECT_ID.firebaseapp.com/

  • أي نطاقات مخصصة مرتبطة:
    CUSTOM_DOMAIN/

انتقِل إلى صفحة إعداد Hosting لـ مزيد من التفاصيل حول قواعد إعادة الكتابة يمكنك تعرف أيضًا على ترتيب أولوية الردود لتهيئات Hosting المختلفة.

الاختبار محليًا

أثناء التطوير، يمكنك تشغيل صورة الحاوية واختبارها محليًا. بالنسبة التعليمات التفصيلية، يُرجى زيارة مستندات Cloud Run

الخطوات التالية