Jun 27, 2015
Introducing Cloud Architecture
• The Cloud Architecture
• IaaS
• PaaS
• Next Generation Cloud Service Architecture
• 架構於現有的PaaS/IaaS基礎上
• 將多種服務整合成單一服務
• 提供直覺且簡單的服務管理介面
• 是針對特定型別的應用程式而設計的服務
Windows Azure Mobile Services (WAMS)
• 架構於現有的PaaS上
• 將數種服務整合成單一服務
• 資料儲存: SQL Database Services
• 使用者驗證: Authentication Services
• 訊息推播: Push Notification Services
• 為
• 行動平台的App 開發而設計
• 提供
• 直覺的管理介面
• 具高延展性, 可因應未來使用者增加的情況
連線型行動平台App的架構
Accessing data
authentication
Call web service
如果… 要開發一個雲端待辦事項的應用程式..
• 使用者驗證
• 您需要儲存使用者的資料
• 您需要一個伺服器提供驗證的Web Service
• 儲存待辦事項的資料
• 您需要一個伺服器來儲存這些資料
• 當伺服器移除過期的資料時, 使用者必須收到通知
• 您需要一個在背景執行, 且定時刪除過期資料的程式
• 因此
• 您需要一個Web Server
• 提供使用者驗證
• 執行定期執行移除過期資料的程式
• 提供Web Service供行動裝置呼叫
• 您需要一個資料庫伺服器
• 儲存使用者資料
• 您需要寫網頁程式來提供服務
• ASP.NET
• PHP
• …..
一言以蔽之,要很多伺服器
這還只是網頁或是Web Service程式而已…..
我們還沒開始談App的開發
也沒談到安全性的問題…….
如果使用Windows Azure Mobile Service • 提供SQL Database Service
• 可以儲存資料
• 提供背景排程服務
• 您可以撰寫程式來定期移除過期資料
• 發送推播通知
• 提供整合性的使用者驗證
• 您可以透過Facebook, Microsoft Account, Google Account或 Twitter來驗證使用者
• 您可以由這些驗證服務提供者取得使用者資料
• 因此
• 您不需要準備一個Web Server
• 您不需要準備一個Database Server
• 您不需要撰寫驗證使用者的程式
• 您不需要寫太多的程式就可以完成訊息推播
• 只要
• 申請一個Windows Azure帳戶
• 建立一個Mobile Service
• 設定資料庫及驗證方式
• 下載平台(WP,Android,iOS)需要的函式庫
• 寫很少量的程式碼(Node.js)
• 接著只要專注於您的App開發就好了
讓我們開始吧
建立 Windows Azure Mobile Service
DEMO
建立資料表
關於權限
Can use by any user
Need application key
Authenticated users
Internal use only
設定整合驗證 – Google Account
• 您可以透過 Facebook, Microsoft Account, Google Account 或
Twitter來驗證使用者
關於Google Account
• 您必須設定 Authorized Redirect URIs 與 Authorized JavaScript Origins 兩個欄位
設定透過Google Account進行整合驗證
這就是一個雲端待辦事項App所需要的Server Side 工作
我們可以開始寫App了, 有沒有很歡樂?
建立 Windows Phone 應用程式
• 透過NuGet Package Manager 加入 WAMS
Client Library's
取得存取Azure Mobile Services所需要的Application Key
存取 Mobile Service
public partial class App : Application { internal static MobileServiceClient MobileService = new MobileServiceClient("https://todo64.azure-mobile.net/", "xisXQCpUGwQfwOwUqvxcqwkVhtdiLR72");
• 建立MobileServiceClient物件
資料表與類別的對應
using Microsoft.WindowsAzure.MobileServices; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace TodoApplication { [DataTable("todos")] public class TodoDataModel { public int Id { get; set; } public string Description { get; set; } public bool Complete { get; set; } } }
• Windows Azure Mobile Services 會自動依據您定義的類別來建立資料表結構
添加處理使用者驗證的程式碼
public partial class MainPage : PhoneApplicationPage { private MobileServiceUser user; private async System.Threading.Tasks.Task Authenticate() { while (user == null) { try { user = await App.MobileService.LoginAsync(MobileServiceAuthenticationProvider.Google); } catch (InvalidOperationException) { Dispatcher.BeginInvoke(() => { MessageBox.Show("You must log in. Login Required"); }); } } } private async void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e) { await Authenticate(); }
如何取得使用者資訊?
• 當使用者登入後
• 您可以由驗證提供者端(Facebook, Microsoft Account,
Google Account , Twitter)取得使用者資訊
• 您可以取得一個唯一的使用者編號 (由WAMS提供)
• 但
• 除了由WAMS提供的使用者編號外, 其餘要由驗證提供者端取得的資訊, 無法透過Client Library‘s直接取得
• 如果需要, 您可以
• 撰寫Server-Side Script來取得
• 或是撰寫Custom API回傳給客戶端
Table Triggers
• 開發者可以自訂CRUD 的個別行為
• 以Node.js語法撰寫, 未來會提供.NET Language(像是C#
等等)
Storing User ID
• 我們需要分辨待辦項目是屬於哪個使用者的
• 因此必須自訂Insert的行為, 新增時存入使用者ID
• 順便儲存建立此項目的日期
• createDate, userId 並未定義於先前的對應類別
• WAMS會自動添加這些欄位, 而客戶端可以不知道這些欄位的存在
UI的設計與資料的取得
• 此時並沒有任何資料, 所以先測試增加一筆資料, 方便後面的UI設計
• 取得資料表物件
• 新增資料
Test And Run
Designing UI and Get data
Todo Item record
ListBox
取得資料
• 這行程式會回傳所有資料
• 通常, 我們只希望回傳該使用者的資料
• 透過自訂Read 可以達到這個需求
實作 CUD
• 更新資料
• 刪除資料
• 查詢資料
以Custom API實作查詢
• 雖然直接由客戶端進行查詢很酷
• 但
• 在WAMS中, 有更具效率的做法
以Custom API實作查詢
客戶端呼叫API的程式碼
背景排程執行的工作及Push Notification
• 建立 channels 資料表
• 大多數的 push notification services (WP, iOS,
Android) 都是基於channel的觀念
• Push Notification Services會分配一個ID給裝置
• 因此我們需要一個可以儲存push uri的資料表
建立 新增 channel 資料的API
建立排程
定時執行的Script
Test And Run
Demo
需要用到更多的SQL Server功能嗎?
Android Version
您需要什麼?
• 下載client library's for android
• http://go.microsoft.com/fwlink/p/?linkid=280126&clcid=0x409
• 將所有檔案放到android project中 (libs/)
撰寫程式
• 建立 mobile service client 物件
• 設計UI
_client = new MobileServiceClient("https://todo64.azure-mobile.net/", "WGoPUayxXeaWRsOiHrzGfcotvffFfM31", this);
回傳資料
_table.execute(new TableQueryCallback<Todo>() { @Override public void onCompleted(List<Todo> result, int count, Exception exception, ServiceFilterResponse response) { if(exception == null){ _adapter.clear(); for (Todo item : result) { _adapter.add(item); } } } });
實作CUD
• 新增
• 刪除
• 修改
_table.insert(item, new TableOperationCallback<Todo>());
_table.delete(item, new TableOperationCallback<Todo>());
_table.update(item, new TableOperationCallback<Todo>());
Integrate Google Cloud Message
• 下載 Google Cloud Message Library • 目前已併到Google Play Services API中
呼叫CUSTOM API
ArrayList<Pair<String, String>> parameters = new ArrayList<Pair<String, String>>(); parameters.add(new Pair<String, String>("device", "android")); parameters.add(new Pair<String, String>("uri", regID)); _client.invokeApi("addchannel", "POST", parameters, new ApiJsonOperationCallback() { @Override public void onCompleted(JsonElement jsonObject, Exception exception, ServiceFilterResponse response) { int i = 0; i = i++; } });
Test And Run
Demo
IOS
What’s you need?
• 下載Client Library's
• https://go.microsoft.com/fwLink/p/?LinkID=266533
• 解開後將兩個目錄複製到iOS Project中
撰寫程式
• 建立 mobile service client 物件
• 設計UI
//in .h @property (nonatomic, strong) MSClient *client; // in .m self.client = [MSClient clientWithApplicationURLString:@"https://todo64.azure-mobile.net/" applicationKey:@"<app key><your key="">"];
回傳資料
self.table = [self.client tableWithName:@todos"]; NSPredicate * predicate = [NSPredicate predicateWithFormat:@“Complete == NO"]; [self.table readWithPredicate:predicate completion:^(NSArray *results, NSInteger totalCount, NSError *error) { self.items = [results mutableCopy]; [self.tableView reloadData]; }];
Implement CUD Operations
• 新增、修改、刪除等動作則是透過MSTable的insert、update、delete函式,跟Windows Phone、Android不同的是iOS並沒有所謂的Customers類別,而是用NSDictionary來呈現資料列。
NSDictionary *item = @{ @”Description" : “buy computer” };
參考資料
• Windows Azure Mobile Document
• http://www.windowsazure.com/en-us/develop/mobile/reference/
• My Blog
• http://www.dotblogs.com.tw/code6421
• Windows Azure Mobile Service SDK for Windows Phone 7
• https://github.com/zaxy78/azure-mobile-wp7-sdk
• (unofficial)