
Ini adalah serangkaian catatan yang diterjemahkan oleh Priti Desai . Karena catatan itu sendiri cukup pendek, beberapa publikasi akan dikumpulkan sekaligus dalam satu publikasi. Rincian penyebaran aplikasi untuk OpenWhisk dengan contoh-contoh akan dipertimbangkan, dan pada akhir siklus, arsitektur dan kode aplikasi yang menggunakan komputasi tanpa server berbasis OpenWhisk akan disajikan.
Mengotomatiskan penerapan aplikasi untuk OpenWhisk
Sudahkah Anda menginstal OpenWhisk dan menguji fungsi hello.js sederhana dalam pekerjaan?
cat hello.js function main() { return {payload: 'Hello World'}; }
Apakah Anda ingin mengotomatiskan penyebaran fungsi paling sederhana ini? Jika demikian, lanjutkan membaca catatan ini. Saya akan menunjukkan cara menggunakan openwhisk-wskdeploy untuk mengotomatiskan penyebaran fungsi sederhana.
Komponen yang Diperlukan
Langkah pertama
Buat file manifes (manifest.yaml) dengan konten berikut:
packages: helloworld: actions: helloworld: location: src/hello.js runtime: nodejs:6 outputs: payload: type: string description: a simple greeting message, Hello World.
Langkah kedua
Buat file untuk penyebaran (deployment.yaml):
application: name: SampleHelloWorld namespace: _ packages: helloworld: actions: helloworld:
Sebenarnya, file ini tidak diperlukan untuk pengiriman helloworld
, cukup manifes dari langkah pertama sudah cukup.
Langkah ketiga
Buat struktur direktori. Itu terlihat seperti ini:
# ls -1R ~/SampleHelloWorldApp/ deployment.yaml manifest.yaml src/ ./src: hello.js
Langkah keempat
Menyebarkan fungsi HelloWorld:
./wskdeploy -p ~/SampleHelloWorldApp/ ____ ___ _ _ _ _ _ /\ \ / _ \ _ __ ___ _ __ | | | | |__ (_)___| | __ /\ /__\ \ | | | | '_ \ / _ \ '_ \| | | | '_ \| / __| |/ / / \____ \ / | |_| | |_) | __/ | | | |/\| | | | | \__ \ < \ \ / \/ \___/| .__/ \___|_| |_|__/\__|_| |_|_|___/_|\_\ \___\/ |_| Packages: Name: helloworld * action: helloworld bindings: Triggers: Rules Do you really want to deploy this? (y/N): y Deploying pacakge helloworld ... Done! Deploying action helloworld/helloworld ... Done! Deployment completed successfully.
Langkah kelima
Periksa kinerja:
# wsk action invoke --blocking --result helloworld/helloworld { "payload": "Hello World" }
Langkah keenam
Bersukacitalah!
Penempatan: Fungsi, Kondisi, dan Aturan
Mari kita lihat hal-hal tambahan yang diperlukan dalam otomatisasi:
Melewati parameter ke suatu fungsi
Ubah kode:
function main(params) { return {payload: 'Hello, ' + params.name + ' from ' + params.place}; }
Langkah pertama
Buat file manifes:
packages: helloworld: actions: helloworld: location: src/hello.js runtime: nodejs:6 inputs: name: type: string description: name of a person place: type: string description: location of a person outputs: payload: type: string description: a simple greeting message, Hello World!
Anda dapat menentukan nilai parameter dalam file manifes dan melewatkan pembuatan file untuk penerapan:
packages: helloworld: actions: helloworld: location: src/hello.js runtime: nodejs:6 inputs: name: Amy place: Paris outputs: payload: type: string description: a simple greeting message, Hello World!
Langkah kedua
Buat file untuk penyebaran:
Kami menetapkan nilai default dengan menambahkan bagian "input" yang bersarang di fungsi "helloworld":
application: name: SampleHelloWorld namespace: _ packages: helloworld: actions: helloworld: inputs: name: Amy place: Paris
Pastikan struktur direktori tidak berubah dari yang terakhir kali.
Langkah ketiga
Perluas fungsi:
# ./wskdeploy -p ~/SampleHelloWorldApp/ ____ ___ _ _ _ _ _ /\ \ / _ \ _ __ ___ _ __ | | | | |__ (_)___| | __ /\ /__\ \ | | | | '_ \ / _ \ '_ \| | | | '_ \| / __| |/ / / \____ \ / | |_| | |_) | __/ | | | |/\| | | | | \__ \ < \ \ / \/ \___/| .__/ \___|_| |_|__/\__|_| |_|_|___/_|\_\ \___\/ |_| Packages: Name: helloworld * action: helloworld bindings: - name: name value: Amy - name: place value: Paris Triggers: Rules Do you really want to deploy this? (y/N): y Deploying pacakge helloworld ... Done! Deploying action helloworld/helloworld ... Done! Deployment completed successfully.
Langkah keempat
Periksa kinerja:
wsk action invoke --blocking --result helloworld/helloworld { "payload": "Hello, Amy from Paris" }
Ganti nilai default dengan meneruskan nilai menggunakan parameter --param :
wsk action invoke --blocking --result helloworld/helloworld --param name Mark --param place Barcelona { "payload": "Hello, Mark from Barcelona" }
Menciptakan kondisi dan aturan yang mengikat
Langkah pertama
Tambahkan dua bagian ke manifes:
packages: helloworld: actions: helloworld: location: src/helloworld.js runtime: nodejs:6 inputs: name: type: string description: name of a person place: type: string description: location of a person outputs: payload: type: string description: a simple greeting message, Hello World! triggers: locationUpdate: rules: helloworldOnLocationUpdate: action: helloworld trigger: locationUpdate
Langkah kedua
Perluas fungsi, kondisi, dan aturan yang mengikat:
./wskdeploy -p ~/SampleHelloWorldApp/ ____ ___ _ _ _ _ _ /\ \ / _ \ _ __ ___ _ __ | | | | |__ (_)___| | __ /\ /__\ \ | | | | '_ \ / _ \ '_ \| | | | '_ \| / __| |/ / / \____ \ / | |_| | |_) | __/ | | | |/\| | | | | \__ \ < \ \ / \/ \___/| .__/ \___|_| |_|__/\__|_| |_|_|___/_|\_\ \___\/ |_| Packages: Name: helloworld * action: helloworld bindings: - name: name value: Amy - name: place value: Paris Triggers: * trigger: locationUpdate bindings: Rules * rule: helloworldOnLocationUpdate - trigger: locationUpdate - action: helloworld Do you really want to deploy this? (y/N): y Deploying pacakge helloworld ... Done! Deploying action helloworld/helloworld ... Done! Deploying trigger locationUpdate ... Done! Deploying rule helloworldOnLocationUpdate ... Done! Deployment completed successfully.
Langkah ketiga
Periksa kinerja:
- Kami melihat fungsi kerja:
wsk activation poll Enter Ctrl-c to exit. Polling for activation logs
- buka terminal lain dan masukkan perintah untuk mengaktifkan respons bersyarat:
wsk trigger fire locationUpdate ok: triggered locationUpdate with id 4c3a8b1792d546a68ac58538c3f5d637
- lihat hasilnya di terminal pertama:
Activation: helloworld (d545c458f3d34d6fbf5c29173be3d29e) [] Activation: locationUpdate (4c3a8b1792d546a68ac58538c3f5d637) [] Activation: helloworldOnLocationUpdate (c099355c1f1f4d6d8d30f54e8dac2b84) []
- kami menentukan id dari respon bersyarat dan memeriksa hasil fungsi:
wsk activation get d545c458f3d34d6fbf5c29173be3d29e ok: got activation d545c458f3d34d6fbf5c29173be3d29e { ... "activationId": "d545c458f3d34d6fbf5c29173be3d29e", "start": 1489444142544, "end": 1489444142598, "response": { "status": "success", "statusCode": 0, "success": true, "result": { "payload": "Hello, Amy from Paris" } }, ... }
Pengumuman
Komputasi tanpa server berdasarkan OpenWhisk, bagian 1
Komputasi tanpa server berdasarkan OpenWhisk, bagian 2
Komputasi tanpa server berdasarkan OpenWhisk, bagian 3
Komputasi tanpa server berdasarkan OpenWhisk, bagian 4