Cloud Run की मदद से डाइनैमिक कॉन्टेंट उपलब्ध कराएं और माइक्रोसर्विस होस्ट करें

डाइनैमिक कॉन्टेंट जनरेट करने और उसे दिखाने के लिए, Cloud Run को Firebase Hosting से जोड़ें. इसके अलावा, माइक्रोसेवाओं के तौर पर REST एपीआई बनाएं.

Cloud Run का इस्तेमाल करके, कंटेनर इमेज में पैकेज किए गए ऐप्लिकेशन को डिप्लॉय किया जा सकता है. इसके बाद, Firebase Hosting का इस्तेमाल करके, एचटीटीपीएस अनुरोधों को आपके कंटेनर वाले ऐप्लिकेशन को ट्रिगर करने के लिए डायरेक्ट किया जा सकता है.

के इस्तेमाल के उदाहरण और सैंपल देखने के लिए, Cloud Run के साथ इंटिग्रेट किए गए Firebase Hosting की खास जानकारी वाला लेख पढ़ें.


इस गाइड में, आपको ये काम करने का तरीका बताया गया है:

  1. Hello World का सामान्य ऐप्लिकेशन लिखना
  2. किसी ऐप्लिकेशन को कंटेनर में पैकेज करना और उसे Artifact Registry पर अपलोड करना
  3. कंटेनर इमेज को Cloud Run पर डिप्लॉय करना
  4. होस्टिंग के अनुरोधों को कंटेनर वाले ऐप्लिकेशन पर Hosting डायरेक्ट करना

ध्यान दें कि डाइनैमिक कॉन्टेंट को बेहतर तरीके से दिखाने के लिए, आप कैश मेमोरी की सेटिंग को अपनी ज़रूरत के हिसाब से बदल सकते हैं.

शुरू करने से पहले

Cloud Run का इस्तेमाल करने से पहले, आपको कुछ शुरुआती टास्क पूरे करने होंगे, इनमें Cloud Billing खाता सेट अप करना, Cloud Run API को चालू करना, और gcloud कमांड लाइन टूल इंस्टॉल करना शामिल है.

अपने प्रोजेक्ट के लिए बिलिंग सेट अप करना

Cloud Run के लिए, मुफ़्त में इस्तेमाल करने का कोटा मिलता है. हालांकि, Cloud Run का इस्तेमाल करने या उसे आज़माने के लिए, आपके Firebase प्रोजेक्ट से Cloud Billing खाता जुड़ा होना ज़रूरी है.

एपीआई चालू करना और SDK टूल इंस्टॉल करना

  1. Google APIs console में, Cloud Run API को चालू करें:

    1. Google APIs console में, Cloud Run API वाला पेज खोलें.

    2. प्रॉम्प्ट मिलने पर, अपना Firebase प्रोजेक्ट चुनें.

    3. Cloud Run API वाले पेज पर, चालू करें पर क्लिक करें.

  2. Cloud SDK टूल इंस्टॉल करें और उसे शुरू करें.

  3. पक्का करें कि gcloud टूल, सही प्रोजेक्ट के लिए कॉन्फ़िगर किया गया हो:

    gcloud config list

पहला चरण: सैंपल ऐप्लिकेशन लिखना

ध्यान दें कि Cloud Run यहां दिए गए सैंपल में दिखाई गई भाषाओं के अलावा, कई अन्य भाषाओं के साथ भी काम करता है.

Go

  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 एनवायरमेंट वैरिएबल से तय किए गए पोर्ट पर काम करता है.

आपका ऐप्लिकेशन तैयार है. अब इसे कंटेनर में पैकेज किया जा सकता है और Artifact 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.22.1"
      }
    }
    
  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 एनवायरमेंट वैरिएबल से तय किए गए पोर्ट पर काम करता है.

आपका ऐप्लिकेशन तैयार है. अब इसे कंटेनर में पैकेज किया जा सकता है और Artifact 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 एनवायरमेंट वैरिएबल से तय किए गए पोर्ट पर काम करता है.

आपका ऐप्लिकेशन तैयार है. अब इसे कंटेनर में पैकेज किया जा सकता है और Artifact Registry पर अपलोड किया जा सकता है.

Java

  1. Java SE 8 या इसके बाद वाला JDK और CURL इंस्टॉल करें.

    ध्यान दें कि हमें यह काम सिर्फ़ अगले चरण में नया वेब प्रोजेक्ट बनाने के लिए करना है. Dockerfile, जिसके बारे में बाद में बताया गया है, कंटेनर में सभी डिपेंडेंसी लोड करेगा.

  2. कंसोल से, cURL और unzip कमांड का इस्तेमाल करके, नया खाली वेब प्रोजेक्ट बनाएं:

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

    इससे एक SpringBoot प्रोजेक्ट बनता है.

  3. src/main/java/com/example/helloworld/HelloworldApplication.java में, SpringBootApplication क्लास को अपडेट करें. इसके लिए, / मैपिंग को मैनेज करने के लिए @RestController जोड़ें. साथ ही, TARGET एनवायरमेंट वैरिएबल देने के लिए, @Value फ़ील्ड जोड़ें:

    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 एनवायरमेंट वैरिएबल से तय किए गए पोर्ट पर काम करता है.

आपका ऐप्लिकेशन तैयार है. अब इसे कंटेनर में पैकेज किया जा सकता है और Artifact Registry पर अपलोड किया जा सकता है.

दूसरा चरण: किसी ऐप्लिकेशन को कंटेनर में पैकेज करना और उसे Artifact Registry पर अपलोड करना

  1. सोर्स फ़ाइलों वाली डायरेक्ट्री में, Dockerfile नाम की नई फ़ाइल बनाकर, सैंपल ऐप्लिकेशन को कंटेनर में पैकेज करें. अपनी फ़ाइल में यह कॉन्टेंट कॉपी करें.

    Go

    # 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
    
    # Copy local code to the container image.
    COPY . ./
    
    # Install dependencies and tidy up the go.mod and go.sum files.
    RUN go mod tidy
    
    # 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
    

    Java

    # 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 वाली डायरेक्ट्री से यह कमांड चलाएं:Cloud Build

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

    सक्सेस होने पर, आपको एक SUCCESS मैसेज दिखेगा. इसमें इमेज का नाम
    (gcr.io/PROJECT_ID/helloworld) होगा.

कंटेनर इमेज अब Artifact Registry में सेव हो गई है. अगर चाहें, तो इसे फिर से इस्तेमाल किया जा सकता है.

ध्यान दें कि Cloud Build के बजाय, स्थानीय तौर पर इंस्टॉल किए गए Docker के वर्शन का इस्तेमाल करके, कंटेनर को स्थानीय तौर पर बनाया जा सकता है.

तीसरा चरण: कंटेनर इमेज को Cloud Run पर डिप्लॉय करना

  1. डिप्लॉय करने के लिए, यह कमांड इस्तेमाल करें:

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

  2. प्रॉम्प्ट मिलने पर:

  3. डिप्लॉय होने में कुछ समय लगेगा. सक्सेस होने पर, कमांड लाइन पर सेवा का यूआरएल दिखता है. उदाहरण के लिए: https://helloworld-RANDOM_HASH-us-central1.a.run.app

  4. किसी वेब ब्राउज़र में सेवा का यूआरएल खोलकर, डिप्लॉय किए गए कंटेनर पर जाएं.

अगले चरण में, आपको URL से कंटेनर वाले इस ऐप्लिकेशन को ऐक्सेस करने का तरीका बताया जाएगा, ताकि यह Firebase पर होस्ट की गई आपकी साइट के लिए डाइनैमिक कॉन्टेंट जनरेट कर सके.Firebase Hosting

चौथा चरण: होस्टिंग के अनुरोधों को कंटेनर वाले ऐप्लिकेशन पर डायरेक्ट करना

फिर से लिखने के नियमों की मदद से, खास पैटर्न से मेल खाने वाले अनुरोधों को किसी एक डेस्टिनेशन पर डायरेक्ट किया जा सकता है.

यहां दिए गए उदाहरण में, आपकी Hosting साइट पर पेज /helloworld से आने वाले सभी अनुरोधों को, आपके helloworld कंटेनर इंस्टेंस को शुरू करने और चलाने के लिए डायरेक्ट करने का तरीका बताया गया है.

  1. पक्का करें कि:

    • आपके पास CLI का नया वर्शन हो.Firebase

    • आपने Firebase Hosting को शुरू किया हो.

    CLI इंस्टॉल करने और Hostingशुरू करने के बारे में ज़्यादा जानकारी के लिए, के लिए शुरुआती निर्देश देखेंHosting.

  2. अपनी firebase.json फ़ाइल खोलें.

  3. hosting सेक्शन में, यह rewrite कॉन्फ़िगरेशन जोड़ें:

    "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

अब आपके कंटेनर को इन यूआरएल से ऐक्सेस किया जा सकता है:

  • आपके Firebase सबडोमेन:
    PROJECT_ID.web.app/ और PROJECT_ID.firebaseapp.com/

  • कनेक्ट किए गए कोई भी कस्टम डोमेन:
    CUSTOM_DOMAIN/

फिर से लिखने के नियमों के बारे में ज़्यादा जानकारी के लिए, Hosting कॉन्फ़िगरेशन वाला पेज देखें . इसके अलावा, अलग-अलग Hosting कॉन्फ़िगरेशन के लिए, जवाबों की प्राथमिकता के क्रम के बारे में भी जाना जा सकता है.

स्थानीय तौर पर टेस्ट करना

डेवलपमेंट के दौरान, कंटेनर इमेज को स्थानीय तौर पर चलाया और टेस्ट किया जा सकता है. ज़्यादा जानकारी के लिए, Cloud Run दस्तावेज़ देखें.

अगले चरण