Windows Azure SDK for Node.js に関する備忘録。
Windows Azure SDK for Node.jsは、WebロールとWorkerロールが作成できる。WebロールはIIS上でNode.jsがホストされ、Workerロールでは、ProgramEntryPoint経由でセルフホストされる。通常のWorkerロールのように無限ループってわけではありません。
Webロール
WebロールでのNode.jsの実行は、iisnodeハンドラが役割を担います。以下は、Webロールを作成したときに生成されるWeb.configファイル。system.WebServer セクションの handlers 要素で定義している。
<?xml version="1.0" encoding="utf-8"?> <!-- For more information on how to configure your ASP.NET application, please visit http://go.microsoft.com/fwlink/?LinkId=169433 --> <configuration> <appSettings> <add key="EMULATED" value="true" /> </appSettings> <system.webServer> <modules runAllManagedModulesForAllRequests="false" /> <!-- indicates that the server.js file is a node.js application to be handled by the iisnode module --> <handlers> <add name="iisnode" path="server.js" verb="*" modules="iisnode" /> </handlers> <rewrite> <rules> <clear /> <rule name="app" enabled="true" patternSyntax="ECMAScript" stopProcessing="true"> <match url="server\.js.+" negate="true" /> <conditions logicalGrouping="MatchAll" trackAllCaptures="false" /> <action type="Rewrite" url="server.js" /> </rule> </rules> </rewrite> </system.webServer> </configuration>
IIS経由のメリットなどは、「iisnode」でググればいくつか情報を拾うことができます。
- Tomek on Software: Using URL rewriting with node.js applications hosted in IIS using iisnode
- Home · tjanczuk/iisnode Wiki · GitHub
- iisnode: Internet Information Server用のNode.js
いくつか要約すると以下のようなことをしてくれるっぽい(確認したわけではないので不正確かも)
Workerロール
Workerロールによるセルフホスト環境ならWebSocketを使うことができます。ただし、スケールさせるためには、別の仕組みが必要っぽいですね。
- Node におけるスケールアーキテクチャ考察(Scale 編) - Block Rockin’ Codes
- Redis の Pub/Sub を使って Node.js + WebSocket のスケールアウトを実現する方法FIRN.JP | FIRN.JP
以下サービス定義ファイルの一部。ProgramEntryPoint経由で、直接Node.exeを起動していることが分かります。
<WorkerRole name="WorkerRole1"> <Startup> <Task commandLine="setup_worker.cmd" executionContext="elevated"> <Environment> <Variable name="EMULATED"> <RoleInstanceValue xpath="/RoleEnvironment/Deployment/@emulated" /> </Variable> </Environment> </Task> </Startup> <Endpoints> <InputEndpoint name="HttpIn" protocol="tcp" port="8080" /> </Endpoints> <Runtime> <Environment> <Variable name="PORT"> <RoleInstanceValue xpath="/RoleEnvironment/CurrentInstance/Endpoints/Endpoint[@name='HttpIn']/@port" /> </Variable> <Variable name="EMULATED"> <RoleInstanceValue xpath="/RoleEnvironment/Deployment/@emulated" /> </Variable> </Environment> <EntryPoint> <ProgramEntryPoint commandLine="node.exe .\server.js" setReadyOnProcessStart="true" /> </EntryPoint> </Runtime> </WorkerRole>