Anda dapat terhubung ke instance Redis dari fungsi Cloud Run menggunakan Akses VPC Serverless.
Penyiapan
Jika Anda telah menginstal Google Cloud CLI dan membuat instance Redis, Anda dapat melewati langkah-langkah ini.
Instal gcloud CLI dan lakukan inisialisasi:
gcloud init
Ikuti Panduan Memulai Cepat untuk membuat instance Redis. Catat zona, alamat IP, dan port instance Redis.
Mengonfigurasi Akses VPC Serverless
Untuk terhubung dari fungsi Cloud Run ke jaringan VPC resmi instance Redis, Anda harus menyiapkan Akses VPC Serverless.
Temukan jaringan resmi instance Redis Anda dengan menjalankan perintah:
gcloud redis instances describe INSTANCE_ID --region REGION
Ikuti petunjuk di Membuat konektor untuk membuat konektor Akses VPC Tanpa Server. Pastikan Anda membuat konektor di region yang sama dengan tempat Anda ingin men-deploy fungsi, dan pastikan konektor terhubung ke jaringan VPC yang diizinkan instance Redis. Ingat nama konektor.
Fungsi contoh
Fungsi contoh ini membuat koneksi ke instance Redis dari fungsi Cloud Run.
Buat clone repositori untuk bahasa pemrograman yang Anda inginkan dan buka folder yang berisi kode contoh:
Go
git clone https://github.com/GoogleCloudPlatform/golang-samples
cd golang-samples/functions/memorystore/redis
Node.js
git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples
cd nodejs-docs-samples/functions/memorystore/redis
Python
git clone https://github.com/GoogleCloudPlatform/python-docs-samples
cd python-docs-samples/functions/memorystore/redis
Kode contoh menambahkan penghitung Redis setiap kali fungsi dipicu:
Go
Fungsi ini menggunakan klien
github.com/gomodule/redigo/redis
.
Node.js
Fungsi ini menggunakan modul redis
.
Python
Fungsi ini menggunakan paket
redis-py
.
Men-deploy sampel ke fungsi Cloud Run
Deploy fungsi menggunakan Google Cloud CLI:
Go
gcloud run deploy visit-count \ --region=REGION \ --source=. \ --base-image=BASE_IMAGE \ --function=VisitCount \ --vpc-connector=projects/PROJECT_ID/locations/REGION/connectors/CONNECTOR_NAME \ --set-env-vars=REDISHOST=REDIS_IP,REDISPORT=REDIS_PORT
Node.js
gcloud run deploy visit-count \ --region=REGION \ --source=. \ --base-image=BASE_IMAGE \ --entry-point=visitCount \ --vpc-connector=projects/PROJECT_ID/locations/REGION/connectors/CONNECTOR_NAME \ --set-env-vars=REDISHOST=REDIS_IP,REDISPORT=REDIS_PORT
Python
gcloud run deploy visit-count \ --region=REGION \ --source=. \ --base-image=BASE_IMAGE \ --function=visit_count \ --vpc-connector=projects/PROJECT_ID/locations/REGION/connectors/CONNECTOR_NAME \ --set-env-vars=REDISHOST=REDIS_IP,REDISPORT=REDIS_PORT
Ganti:
REGION
dengan region tempat Anda ingin men-deploy fungsi.BASE_IMAGE
dengan image dasar untuk fungsi, misalnya,go116
,nodejs16
, ataupython310
. Untuk mengetahui informasi selengkapnya, lihat Runtime bahasa dan image dasar yang didukung untuk mengetahui informasi selengkapnya.PROJECT_ID
dengan ID Google Cloud project Anda.CONNECTOR_NAME
dengan nama konektor Anda.REDIS_IP
danREDIS_PORT
dengan alamat IP dan nomor port instance Redis Anda.
Setelah deployment fungsi selesai, ambil URL fungsi Anda:
gcloud run services describe visit-count \ --region=REGION \
Anda dapat melihat penghitung bertambah setiap kali Anda memicu fungsi dengan mengirim
permintaan GET
ke URL-nya.