【问题标题】:Running multiple applications on the same Azure WebRole (Cloud Services)在同一个 Azure WebRole(云服务)上运行多个应用程序
【发布时间】:2016-02-21 23:18:13
【问题描述】:

我目前有一个带有 Web 角色的 Azure 云服务(所以它是一个安装了 IIS 的虚拟机)。此 Web 角色当前运行一个独特的 Web 应用程序(WebAPI 项目)。

我想做的是创建一个新的 Web 应用程序,但我需要它在同一个 Web 角色上运行(即同一虚拟机上的两个应用程序)以降低成本。我知道这是可能的(然后,两个 Web 应用程序将在同一个 IIS 实例上、在同一台机器上、使用不同的端口上运行)但我找不到有关如何使用最新版本的 Azure SDK 的资源。

我找到了很棒的资源:

但都有些旧(

我目前使用的是 Azure SDK 2.6。我尝试修改我的 ccproj 文件(云服务项目文件),以便两个 WebAPI 项目共享相同的RoleName

<ProjectReference Include="..\MyProject1.csproj">
  <Name>Website</Name>
  <Project>{...}</Project>
  <Private>True</Private>
  <RoleType>Web</RoleType>
  <RoleName>MyRole</RoleName>
</ProjectReference>
<ProjectReference Include="..\MyProject2.csproj">
  <Name>Website</Name>
  <Project>{...}</Project>
  <Private>True</Private>
  <RoleType>Web</RoleType>
  <RoleName>MyRole</RoleName>
</ProjectReference>

这不起作用,我在 Visual Studio 中项目的“角色”节点显示错误 ("no project associated [project 2 name])。

我也尝试修改我的ServiceDefinition.csdef 文件:

<WebRole name="xxxx.Frontend.Azure" vmsize="Small">
    <Sites>
        <Site name="Web">
            <Bindings>
                <Binding name="Endpoint1" endpointName="Endpoint1" />
            </Bindings>
        </Site>
        <Site name="AnotherWeb" physicalDirectory="foo">
            <Bindings>
                <Binding name="Endpoint2" endpointName="Endpoint2" />
            </Bindings>
        </Site>
    </Sites>
    <Endpoints>
        <InputEndpoint name="Endpoint1" protocol="http" port="8080" />
        <InputEndpoint name="Endpoint2" protocol="http" port="8090" />
    </Endpoints>
</WebRole>

也没有运气。

我应该如何进行?


编辑:

这是我当前的 csdef 文件,修改后支持多站点:

<?xml version="1.0" encoding="utf-8"?>
<ServiceDefinition name="AzureCloudService" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition" schemaVersion="2015-04.2.6">
  <WebRole name="XXX.YYY" vmsize="Small">
    <Sites>
      <Site name="Web">
        <Bindings>
          <Binding name="Endpoint1" endpointName="Endpoint1" />
        </Bindings>
      </Site>
    </Sites>
    <Endpoints>
      <InputEndpoint name="Endpoint1" protocol="http" port="80" />
    </Endpoints>
    <Imports>
      <Import moduleName="RemoteAccess" />
    </Imports>
  </WebRole>
  <WebRole name="XXX.ZZZ" vmsize="Small">
    <Sites>
      <Site name="Web">
        <Bindings>
          <Binding name="Endpoint1" endpointName="Endpoint1" />
        </Bindings>
      </Site>
      <Site name="ZZZ" physicalDirectory="..\..\..\XXX.ZZZ\azure.publish">
        <Bindings>
          <Binding name="Endpoint2" endpointName="Endpoint2" />
        </Bindings>
      </Site>
    </Sites>
    <Endpoints>
      <InputEndpoint name="Endpoint1" protocol="http" port="8081" />
      <InputEndpoint name="Endpoint2" protocol="http" port="8090" />
    </Endpoints>
    <Imports>
      <Import moduleName="RemoteAccess" />
    </Imports>
  </WebRole>
</ServiceDefinition>

【问题讨论】:

    标签: .net azure asp.net-web-api azure-web-roles azure-cloud-services


    【解决方案1】:

    我认为您在 csdef 文件更改方面走在了正确的轨道上,而在 ccproj 更改方面走在了错误的轨道上。因此,请摆脱您的第二个 ProjectReference,因为您最终只想要一个 WebRole。

    应该在 ccproj 文件中添加如下一行:

    <MSBuild Projects="..\MyProject2\MyProject2.csproj" ContinueOnError="false" Targets="PublishToFileSystem" Properties="Configuration=$(Configuration);PublishDestination=..\Publish\MyProject2\;AutoParameterizationWebConfigConnectionStrings=False" />
    

    显然有假路径,但重要的是确保 csproj 文件的路径有效,并确保 PublishDestination 路径与您在 csdef 文件中为该站点指定的 PhysicalDirectory 路径匹配,并且请注意,PhysicalDirectory 路径和 PublishDestination 路径相对于同一位置。例如,我们在解决方案根目录中保留一个“Publish”目录,所以对我们来说 PublishDestination 看起来像

    ..\Publish\MyProject2\ 
    

    物理目录看起来像

    ..\..\..\Publish\MyProject2
    

    您的端点配置看起来正确,但如果无法远程访问某个角色,请确保您的 csdef 文件在 Endpoints 中有一行:

        <Endpoints>
            <InputEndpoint name="MyProject1Endpoint" protocol="http" port="80" />
            <InputEndpoint name="MyProject2Endpoint" protocol="http" port="8080" />
        </Endpoints>
    

    ...带有与每个站点的端点名称相对应的“名称”:

        <Sites>
            <Site name="MyProject1Site" physicalDirectory="..\..\..\MyProject1">
                <Bindings>
                    <Binding name="MyProject1Binding" endpointName="MyProject1Endpoint" />
                </Bindings>
            </Site>
            <Site name="MyProject2Site" physicalDirectory="..\..\..\Publish\MyProject2">
                <Bindings>
                    <Binding name="MyProject2Binding" endpointName="MyProject2Endpoint" />
                </Bindings>
            </Site>
        </Sites>
    

    这是假设您希望每个站点在其托管的同一端口上公开访问。如果出于某种原因,MyProject2 在端口 8081 上本地运行,您需要将 localPort="8081" 添加到该 InputEndpoint。

    【讨论】:

    • 谢谢,这让我走上了正轨(我认为)。我仍然需要解决一些问题,但这听起来很有希望
    • 好吧,我成功地将我的两个应用程序部署在同一个 Web 角色上。不幸的是,其中只有一个可以通过 Internet 访问(我在尝试访问另一个时超时)。我已经远程连接到虚拟机(使用 RDP),我可以看到两个应用程序都已正确部署/运行,甚至都可以在本地访问。因此,我假设通过 Internet 访问时没有响应的一个应用程序存在一些网络配置问题。任何想法?谢谢
    • 一个可以在本地访问但不能远程访问的角色听起来一定是端点配置问题。您原始问题中的端点配置看起来是正确的,但是我在答案中添加了一些示例配置行。希望对您有所帮助!
    • 谢谢,但我仔细检查了我的绑定,我没有发现它们有任何问题。我已经用我拥有的当前版本编辑了问题
    • 终于找到了问题:尝试访问 Web 应用程序时出现了一个简单的错字……所以所有这些都可以正常工作。我对 ccproj 所做的确切修改如下所述:michaelcollier.wordpress.com/2013/01/14/… 再次感谢
    【解决方案2】:

    如果您可以将应用程序部署为 Azure Web 应用程序,我强烈推荐它。它旨在很好地处理这种情况。您可以使用所需的任意数量的服务器实例创建应用服务计划,然后多个应用共享该组服务器。

    【讨论】:

    • 不幸的是我不能轻易改变应用程序的构建方式,所以我必须坚持使用云服务。我确信有一个解决方案(因为 Web 角色是一个带有 IIS 的简单虚拟机)
    猜你喜欢
    • 1970-01-01
    • 2021-03-02
    • 2019-01-26
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 2014-05-05
    • 1970-01-01
    相关资源
    最近更新 更多