这里简单的记录了 在windows azure上面运行jetty. (--Java,jsp)
Windows azure 提供的Fast CGI支持可以很方便的运行Php程序, Native Code Support的特性,也可以很方便的运行其他您想运行的东西, 当然 还是有一定的限制的。
具体参看: Executing Native Code in Windows Azure and the Development Environment
http://msdn.microsoft.com/en-us/library/dd573362.aspx
效果:http://singtel.cloudapp.net:9527/
1.准备材料
Jre 和Jetty.都可以Google下载到.
这里使用jre6 64bit和jetty7的Zip包.
2.上传jre和jetty.
这2个东西也可以打包到项目里面上传,但是那样文件太大了,这里先上传到 Blob里面.一个简单的上传程序: ---
Upload to blobstatic void Main(string[] args)
{
const string config = @"*************";
CloudStorageAccount acct = CloudStorageAccount.Parse(config);
CloudBlobClient blobclient = acct.CreateCloudBlobClient();
var container = blobclient.GetContainerReference("jre6");
container.CreateIfNotExist();
Upload(container);
}
private static void Upload(CloudBlobContainer container)
{
List<string> failed = new List<string>();
string baseDir = @"C:\Temp\jre6\";
var files = GetFiles(baseDir);
foreach (string file in files)
{
try
{
string fileName = file.Replace(baseDir, string.Empty);
Console.WriteLine(fileName);
var blob = container.GetBlobReference(fileName);
blob.DeleteIfExists();
using (FileStream sr = File.Open(file, FileMode.Open))
{
blob.UploadFromStream(sr);
}
Console.WriteLine(fileName + "-OK");
Thread.Sleep(100);
}
catch
{
failed.Add(file);
}
}
foreach (var item in failed)
{
Console.WriteLine(item + "-Failed");
}
Console.WriteLine("All OK");
Console.ReadKey();
}
---
上传jre和jetty到blob.
使用Azure Storage Explorer看看
3.新建一个带有一个worker role的cloud项目.
ServiceConfiguration.cscfg
---
ServiceConfiguration<ServiceConfiguration serviceName="JettyServer" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration">
<Role name="JettyWorker">
<Instances count="1" />
<ConfigurationSettings>
<Setting name="DiagnosticsConnectionString" value="DefaultEndpointsProtocol=https;AccountName=**;AccountKey=**" />
</ConfigurationSettings>
</Role>
</ServiceConfiguration>
---
ServiceDefinition.csdef
---
ServiceDefinition<ServiceDefinition name="JettyServer" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition">
<WorkerRole name="JettyWorker" enableNativeCodeExecution="true">
<ConfigurationSettings>
<Setting name="DiagnosticsConnectionString" />
</ConfigurationSettings>
<Endpoints>
<InputEndpoint name="Jetty7" protocol="tcp" port="9527" />
</Endpoints>
<LocalResources>
<LocalStorage name="Jetty7Home" cleanOnRoleRecycle="false" sizeInMB="1024" />
</LocalResources>
</WorkerRole>
</ServiceDefinition>
---
在worker role里面下载blob上的程序到LocalStorage 里面.并配置运行jetty.
JettyWorker.cs
---
JettyWorker WorkerRoleusing System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using Microsoft.WindowsAzure.Diagnostics;
using Microsoft.WindowsAzure.ServiceRuntime;
namespace JettyWorker
{
public class WorkerRole : RoleEntryPoint
{
public override void Run()
{
// This is a sample worker implementation. Replace with your logic.
Trace.WriteLine("JettyWorker entry point called", "Information");
while (true)
{
Trace.WriteLine("Working", "Information");
Thread.Sleep(1000 * 60 * 20);
}
}
public override bool OnStart()
{
// Set the maximum number of concurrent connections
ServicePointManager.DefaultConnectionLimit = 12;
//DiagnosticMonitor.Start("DiagnosticsConnectionString");
DiagnosticMonitorConfiguration diagObj = DiagnosticMonitor.GetDefaultInitialConfiguration();
diagObj.Logs.ScheduledTransferPeriod = TimeSpan.FromMinutes(1);
DiagnosticMonitor.Start("DiagnosticsConnectionString", diagObj);
RunJetty runJetty = new RunJetty();
string resourceDir = RoleEnvironment.GetLocalResource("Jetty7Home").RootPath;
string port = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["Jetty7"].IPEndpoint.Port.ToString();
runJetty.StartJetty(resourceDir, port);
// For information on handling configuration changes
// see the MSDN topic at http://go.microsoft.com/fwlink/?LinkId=166357.
RoleEnvironment.Changing += RoleEnvironmentChanging;
return base.OnStart();
}
private void RoleEnvironmentChanging(object sender, RoleEnvironmentChangingEventArgs e)
{
// If a configuration setting is changing
if (e.Changes.Any(change => change is RoleEnvironmentConfigurationSettingChange))
{
// Set e.Cancel to true to restart this role instance
e.Cancel = true;
}
}
}
}
---
RunJetty.cs
---
RunJetty.csusing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
using System.IO;
using System.Threading;
namespace JettyWorker
{
public class RunJetty
{
const string JreName = "jre6";
const string JettyName = "jetty7";
const string stAcct = @"DefaultEndpointsProtocol=https;AccountName=;AccountKey=";
public void StartJetty(string jettyLocation, string jettyPort)
{
Trace.WriteLine("StartJetty...Begin");
Trace.WriteLine("jettyLocation:" + jettyLocation + "||jettyPort:" + jettyPort);
Trace.WriteLine("InitJettyFiles...Begin");
InitJettyFiles(jettyLocation);;
Trace.WriteLine("InitJettyFiles...End");
Trace.WriteLine("StartJettyProcess...Begin");
StartJettyProcess(jettyLocation,jettyPort);
Trace.WriteLine("StartJettyProcess...End");
Trace.WriteLine("StartJetty...End");
}
private void InitJettyFiles(string jettyLocation)
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(stAcct);
CloudBlobClient blobclient = storageAccount.CreateCloudBlobClient();
Trace.WriteLine("DownLoad jetty begin");
var container = blobclient.GetContainerReference(JettyName);
container.CreateIfNotExist();
DownLoad(container, jettyLocation);
Trace.WriteLine("DownLoad jetty end");
Trace.WriteLine("DownLoad jre begin");
var containerJDK = blobclient.GetContainerReference(JreName);
containerJDK.CreateIfNotExist();
DownLoad(containerJDK, jettyLocation);
Trace.WriteLine("DownLoad jre end");
}
private static void DownLoad(CloudBlobContainer container, string baseDir)
{
BlobRequestOptions option = new BlobRequestOptions();
option.UseFlatBlobListing = true;
var blobs = container.ListBlobs(option);
if (!Directory.Exists(baseDir)) Directory.CreateDirectory(baseDir);
List<IListBlobItem> failedFiles = new List<IListBlobItem>();
foreach (var file in blobs)
{
try
{
DownloadSingalFile(container, baseDir, file);
}
catch
{
failedFiles.Add(file);
Trace.WriteLine("Failed:" + file.Uri.ToString());
}
}
int tryCount = 0;
while (failedFiles.Count > 0 || tryCount >5 )
{
Trace.WriteLine("Try failed files.");
tryCount++;
Trace.WriteLine("Try failed files." + tryCount);
for (int i = failedFiles.Count - 1; i > -1; i--)
{
try
{
DownloadSingalFile(container, baseDir, failedFiles);
failedFiles.RemoveAt(i);
}
catch
{
Trace.WriteLine("Failed:" + failedFiles.Uri.ToString());
}
}
}
}
private static void DownloadSingalFile(CloudBlobContainer container, string baseDir, IListBlobItem file)
{
string filePath = baseDir + file.Uri.PathAndQuery;
string temp = filePath.Substring(filePath.LastIndexOf('/'), filePath.Length - filePath.LastIndexOf('/'));
temp = filePath.Replace(temp, string.Empty);
if (temp.Trim().Length == 0) return;
if (!Directory.Exists(temp)) Directory.CreateDirectory(temp);
if (File.Exists(filePath)) File.Delete(filePath);
var blob = container.GetBlobReference(file.Uri.ToString());
blob.DownloadToFile(filePath);
Thread.Sleep(50);
}
private void StartJettyProcess(string jettyLocation,string jettyPort)
{
Process newProc = new Process();
StreamReader sr;
string returnDetails;
try
{
string jettyHome = jettyLocation + @"\" + JettyName;
string jreHome = jettyLocation + @"\" + JreName;
newProc.StartInfo.UseShellExecute = true;
newProc.StartInfo.RedirectStandardOutput = false;
newProc.StartInfo.FileName = jreHome + "\\bin\\java.exe";
newProc.StartInfo.Arguments = string.Format(" -Djetty.port={0} -Djetty.home=\"{1}\" -jar \"{1}\\start.jar\"",
jettyPort, jettyHome);
newProc.EnableRaisingEvents = false;
newProc.Start();
Trace.WriteLine("Service started...");
//sr = newProc.StandardOutput;
//returnDetails = sr.ReadToEnd();
//Trace.WriteLine("Information:" + returnDetails);
}
catch (Exception ex)
{
returnDetails = ex.Message;
Trace.WriteLine(ex.Message);
}
}
}
}
---
4.发布程序,查看Trace信息和执行效果.
PS:这里仅仅是记录了一下在Windows azure平台上面运行jetty. demo的代码很乱,还有很多东西也没考虑,比如对jetty运行状况的监控等等.不过,有了Native Code Support ,LocalStorage 的支持,你可以在该平台上运行一些你想的程序了,比如mysql (http://microsoftpdc.com/Sessions/SVC51)
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com