เชื่อมต่อโฮสติ้งแอป Firebase กับเครือข่าย VPC
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
Firebase App Hostingแบ็กเอนด์เชื่อมต่อกับเครือข่าย Virtual Private Cloud (VPC) ได้ ซึ่งจะช่วยให้แบ็กเอนด์Firebase App Hostingเข้าถึงบริการแบ็กเอนด์ที่เข้าถึงไม่ได้โดยใช้
ที่อยู่ IP สาธารณะ เช่น Cloud SQL, Spanner, Cloud Memorystore,
Compute Engine หรือ Kubernetes Internal Microservices
การเข้าถึง VPC จะใช้ได้ในรันไทม์ (จากCloud Run
คอนเทนเนอร์) เท่านั้น ไม่ใช่ในเวลาบิลด์ (Cloud Build)
เลือกวิธีเชื่อมต่อกับเครือข่าย VPC
ใช้vpcAccess
การแมปในไฟล์ apphosting.yaml
เพื่อกำหนดค่าการเข้าถึง
ใช้ชื่อเครือข่าย/ตัวเชื่อมต่อที่สมบูรณ์ในตัวเองหรือใช้รหัส การใช้รหัสช่วยให้สามารถ
ย้ายข้อมูลระหว่างสภาพแวดล้อมการจัดเตรียมและการใช้งานจริงที่มี
ตัวเชื่อมต่อ/เครือข่ายที่แตกต่างกันได้
การกำหนดค่าขาออก VPC โดยตรง (apphosting.yaml
):
runConfig:
vpcAccess:
egress: PRIVATE_RANGES_ONLY # Default value
networkInterfaces:
# Specify at least one of network and/or subnetwork
- network: my-network-id
subnetwork: my-subnetwork-id
การกำหนดค่าเครื่องมือเชื่อมต่อแบบ Serverless (apphosting.yaml
):
runConfig:
vpcAccess:
egress: ALL_TRAFFIC
connector: connector-id
ตัวอย่าง: เชื่อมต่อกับ Memorystore สำหรับ Redis จากแอป Next.js
โดยทั่วไปแล้ว ระบบแคช เช่น Redis หรือ Memcached มักใช้เพื่อสร้างเลเยอร์แคชข้อมูลที่รวดเร็วสำหรับแอป ตัวอย่างนี้แสดงวิธีตั้งค่า Memorystore สำหรับ Redis ในโปรเจ็กต์ Google Cloud เดียวกันกับแบ็กเอนด์ Firebase App Hosting และเชื่อมต่อกับแบ็กเอนด์โดยใช้การส่งออก VPC โดยตรง
ขั้นตอนที่ 0: สร้างอินสแตนซ์ Memorystore for Redis
- ไปที่หน้าMemorystore สำหรับ Redis
ในคอนโซล Google Cloud
- ตรวจสอบว่าได้เลือกโปรเจ็กต์เดียวกันกับที่ใช้สำหรับ Firebase App Hosting
- หากเข้าถึงหน้านี้ไม่ได้ โปรดตรวจสอบว่าได้เปิดใช้การเรียกเก็บเงินสำหรับโปรเจ็กต์และได้เปิดใช้ Memorystore API แล้ว
- เลือกสร้างอินสแตนซ์
- กำหนดค่าอินสแตนซ์ใหม่ด้วยการตั้งค่าที่ต้องการ ตัวอย่างค่าที่คุณใช้ได้มีดังนี้
- ป้อน
my-redis-cache
ในส่วนรหัสอินสแตนซ์
- ป้อน
Redis cache
ในส่วนชื่อที่แสดง
- เลือกพื้นฐานในส่วนตัวเลือกแพ็กเกจ ระดับพื้นฐานจะกำหนด
โหนด Redis แบบสแตนด์อโลน ซึ่งแตกต่างจากระดับมาตรฐานที่ใช้
โหนดจำลองเพื่อสำรองข้อมูล
- เลือกภูมิภาคของแบ็กเอนด์ App Hosting จากตัวเลือกภูมิภาค
อย่าลืมตั้งค่านี้ให้ตรงกับภูมิภาคของแบ็กเอนด์
- เลือกใดก็ได้จากตัวเลือกโซน
- ป้อน
5
ในส่วนความจุ ซึ่งจะตั้งค่าความจุของอินสแตนซ์เป็น 5 GB
- เลือก
5.0
ในส่วนเวอร์ชัน (แนะนำ)
- เลือกค่าเริ่มต้นจากตัวเลือกเครือข่ายที่ได้รับอนุญาต
ขั้นตอนที่ 1: อัปเดต apphosting.yaml
ด้วยรหัสเครือข่าย VPC
- ไปที่หน้าเครือข่าย VPC
ในคอนโซล Google Cloud
- ค้นหารหัสเครือข่าย VPC สำหรับอินสแตนซ์ Memorystore สำหรับ Redis (มักจะเป็น
default
)
ตั้งค่าการกำหนดค่าขาออกของ VPC โดยตรงใน apphosting.yaml
โดยใช้รหัสเครือข่าย VPC
runConfig:
vpcAccess:
egress: PRIVATE_RANGES_ONLY # Default value
networkInterfaces:
- network: my-network-id
ขั้นตอนที่ 2: เพิ่มตัวแปรสภาพแวดล้อมที่นำแอปไปยัง Redis
- ค้นหาข้อมูลการเชื่อมต่อ (โฮสต์และพอร์ต) ในแท็บ "การเชื่อมต่อ" ของอินสแตนซ์ Memorystore for Redis ในGoogle Cloud คอนโซล
เชื่อมต่อกับ Redis ด้วยตัวแปรสภาพแวดล้อม REDISPORT
และ REDISHOST
ตั้งค่า
เหล่านี้ใน apphosting.yaml
โดยใช้ค่าโฮสต์และพอร์ตจากคอนโซล
Google Cloud
env:
# Sample only. Use actual values provided by Memorystore
- variable: REDISPORT
value: 6379
- variable: REDISHOST
value: 10.127.16.3
ขั้นตอนที่ 3: ใช้ Redis จากแอป
ติดตั้งแพ็กเกจ npm ของ redis โดยใช้คำสั่งต่อไปนี้
npm install redis@latest
เข้าถึงแคช Redis จากโค้ด ใช้ตัวแปรสภาพแวดล้อม
ที่กำหนดค่าไว้ในขั้นตอนก่อนหน้า ตัวอย่างเช่น วิธีอ่านจากแคชในตัวแฮนเดิลเส้นทาง Next.js มีดังนี้
src/lib/redis.js
import { createClient } from "redis";
// Set these environment variables in apphosting.yaml
const REDISHOST = process.env.REDISHOST;
const REDISPORT = process.env.REDISPORT;
let redisClient;
export async function getClient(req, res) {
// Only connect if a connection isn't already available
if (!redisClient) {
redisClient = await createClient(REDISPORT, REDISHOST)
.on("error", (err) => console.error("Redis Client Error", err))
.connect();
}
return redisClient;
}
src/app/counter/route.js
import { getClient } from "@/lib/redis.js";
export async function GET(request) {
const redisClient = await getClient();
const count = await redisClient.get("counter");
return Response.json({ count });
}
export async function POST(request) {
const redisClient = await getClient();
const count = await redisClient.incr("counter");
return Response.json({ count });
}
ขั้นตอนที่ 4 (ไม่บังคับ): กำหนดค่าแอปสำหรับการพัฒนาในเครื่อง
Firebase App Hostingโปรแกรมจำลองสามารถลบล้างค่าได้โดยใช้
apphosting.emulator.yaml
ในส่วนนี้ คุณสามารถเปลี่ยนค่าของ REDISHOST
เป็น
ชี้ไปยัง localhost เพื่อให้คุณพัฒนาในเครื่องได้โดยใช้การติดตั้ง Redis ในเครื่อง
- ติดตั้ง Redis ในเครื่องของคุณ
สร้างหรือแก้ไข apphosting.emulators.yaml
เพื่ออ้างอิงอินสแตนซ์ในเครื่อง
env:
- variable: REDISHOST
value: 127.0.0.1
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-08-08 UTC
[null,null,["อัปเดตล่าสุด 2025-08-08 UTC"],[],[],null,["\u003cbr /\u003e\n\nYour Firebase App Hosting backend can connect to a [Virtual Private Cloud\n(VPC)](https://cloud.google.com/vpc/docs) network. This allows your\nFirebase App Hosting backend to access backend services not accessible using\npublic IP addresses, such as Cloud SQL, Spanner, Cloud Memorystore,\nCompute Engine, or Kubernetes internal microservices.\n\nVPC access is only available at runtime (from your Cloud Run\ncontainer), not at build time (Cloud Build).\n\nChoose how to connect to a VPC network\n\n- [Direct VPC\n Egress](https://cloud.google.com/run/docs/configuring/vpc-direct-vpc): Simpler, faster, and less expensive. Uses one IP address per container. Recommended for most use cases.\n- [Serverless\n Connectors](https://cloud.google.com/vpc/docs/serverless-vpc-access): Pools IP addresses for larger applications. Requires payment for the underlying VM. See \"Serverless VPC Access\" in the [VPC pricing page](https://cloud.google.com/vpc/network-pricing) for pricing details.\n\nConfigure in `apphosting.yaml`\n\nUse the `vpcAccess` mapping in your `apphosting.yaml` file to configure access.\nUse either a fully qualified network/connector name or an ID. Using IDs allows\nfor portability between staging and production environments with different\nconnectors/networks.\n\nDirect VPC Egress Configuration (`apphosting.yaml`): \n\n runConfig:\n vpcAccess:\n egress: PRIVATE_RANGES_ONLY # Default value\n networkInterfaces:\n # Specify at least one of network and/or subnetwork\n - network: my-network-id\n subnetwork: my-subnetwork-id\n\nServerless Connector Configuration (`apphosting.yaml`): \n\n runConfig:\n vpcAccess:\n egress: ALL_TRAFFIC\n connector: connector-id\n\nExample: connect to Memorystore for Redis from a Next.js app\n\nCaching systems like Redis or Memcached are commonly used to build a fast data\ncaching layer for an app. This example shows you how to set up\n[Memorystore for Redis](https://cloud.google.com/memorystore/docs/redis/memorystore-for-redis-overview)\nin the same Google Cloud project as your Firebase App Hosting backend and\nconnect to it using\n[Direct VPC egress](https://cloud.google.com/run/docs/configuring/vpc-direct-vpc).\n\nStep 0: Create a Memorystore for Redis instance **Note:** you may also be prompted to create a [service connection policy](https://cloud.google.com/vpc/docs/about-service-connection-policies) as part of this setup.\n\n1. Go to the [*Memorystore for Redis* page](https://console.cloud.google.com/memorystore/redis/instances) in the Google Cloud console.\n - Make sure the same project you're using for Firebase App Hosting is selected.\n - If you can't access this page, make sure billing is enabled for your project and that you've enabled the [Memorystore API](https://console.cloud.google.com/apis/dashboard).\n2. Select **Create Instance**.\n3. Configure the new instance with your preferred settings. Here are some example values you can use:\n - Enter `my-redis-cache` under **Instance ID**.\n - Enter `Redis cache` under **Display name**.\n - Choose **Basic** under the tier selector. Basic tier designates a standalone Redis node, as opposed to standard tier, which uses a replica node to backup your data.\n - Choose your App Hosting backend's region from the **Region** selector. **Be sure to set this value to match the region of your backend.**\n - Choose **any** from the zone selector.\n - Enter `5` under **Capacity**. This sets your instance capacity to 5 GB.\n - Select `5.0` under **Version** (recommended).\n - Choose **default** from the **Authorized network** selector.\n\nStep 1: Update `apphosting.yaml` with your VPC network ID\n\n1. Visit the [VPC networks page](/docs/app-hosting/console.cloud.google.com/networking/networks/list) in the Google Cloud console.\n2. Find the VPC network ID for your Memorystore for Redis instance (it will often be `default`).\n3. Set direct VPC egress configuration in `apphosting.yaml` using the VPC\n network ID:\n\n runConfig:\n vpcAccess:\n egress: PRIVATE_RANGES_ONLY # Default value\n networkInterfaces:\n - network: my-network-id\n\nStep 2: Add environment variables that direct your app to Redis\n\n1. Find connection information (host and port) in the \"Connections\" tab of your Memorystore for Redis instance in the Google Cloud console.\n2. Connect to Redis with `REDISPORT` and `REDISHOST` environment variables. Set\n these in `apphosting.yaml` using the host and port values from the\n Google Cloud console:\n\n env:\n # Sample only. Use actual values provided by Memorystore\n - variable: REDISPORT\n value: 6379\n - variable: REDISHOST\n value: 10.127.16.3\n\nStep 3: Use redis from your app\n\n1. Install the [redis](https://www.npmjs.com/package/redis) npm package:\n\n `npm install redis@latest`\n2. Access your redis cache from your code. Use the environment variables\n configured in the previous step. For example, here's how you might read from\n a cache in a Next.js route handler:\n\n - `src/lib/redis.js`\n\n import { createClient } from \"redis\";\n\n // Set these environment variables in apphosting.yaml\n const REDISHOST = process.env.REDISHOST;\n const REDISPORT = process.env.REDISPORT;\n\n let redisClient;\n\n export async function getClient(req, res) {\n // Only connect if a connection isn't already available\n if (!redisClient) {\n redisClient = await createClient(REDISPORT, REDISHOST)\n .on(\"error\", (err) =\u003e console.error(\"Redis Client Error\", err))\n .connect();\n }\n\n return redisClient;\n }\n\n - `src/app/counter/route.js`\n\n import { getClient } from \"@/lib/redis.js\";\n\n export async function GET(request) {\n const redisClient = await getClient();\n const count = await redisClient.get(\"counter\");\n\n return Response.json({ count });\n }\n\n export async function POST(request) {\n const redisClient = await getClient();\n const count = await redisClient.incr(\"counter\");\n\n return Response.json({ count });\n }\n\nStep 4 (optional): Configure your app for local development\n\nThe Firebase App Hosting emulator can override values using\n`apphosting.emulator.yaml`. Here, you can change the value of `REDISHOST` to\npoint to the localhost so that you can develop locally using a local\ninstallation of Redis.\n\n1. [Install Redis on your local machine](https://redis.io/docs/latest/operate/oss_and_stack/install/install-redis/)\n2. Create or edit `apphosting.emulators.yaml` to reference your local instance:\n\n env:\n - variable: REDISHOST\n value: 127.0.0.1"]]