【问题标题】:Service Fabric with nginx and .NET core services带有 nginx 和 .NET 核心服务的 Service Fabric
【发布时间】:2016-10-10 01:20:38
【问题描述】:

我正在设置一个 Service Fabric 应用程序,其中包含:

  • 一个 Nginx 实例作为前端(单实例,端口 80)
  • 使用 Asp.net 核心编写的一些应用程序(1 个网站,一些 API 服务)(多个实例,动态端口)
  • 用于地址解析的网关服务(单实例,端口 8081)

对于 nginx,我使用的解决方案是 Nuget package
网关以及运行 .NET 核心应用程序的示例通常已采用here

.NET 核心团队本身建议将应用程序托管在 真实 Web 服务器后面,例如 nginx。 因此,我想以 nginx 实例作为入口点部署我的 Service Fabric 应用程序,该应用程序重定向到网关服务,该服务将为复制的无状态服务进行服务解析。

我的问题是关于我需要在 nginx.conf 中使用的地址来指向网关地址。在本地尝试时,我可以使用本地地址 127.0.0.1 并且它按预期工作,但是 如果在真实集群上我的 Nginx 和网关实例部署到不同的机器会发生什么?

这是我的应用程序清单:

<?xml version="1.0" encoding="utf-8"?>
<ApplicationManifest xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ApplicationTypeName="SFApplicationType" ApplicationTypeVersion="1.0.0" xmlns="http://schemas.microsoft.com/2011/01/fabric">
  <Parameters>
    <Parameter Name="NginxPoC_InstanceCount" DefaultValue="1" />
    <Parameter Name="Gateway_InstanceCount" DefaultValue="1" />
  </Parameters>
  <ServiceManifestImport>
    <ServiceManifestRef ServiceManifestName="NginxPoCPkg" ServiceManifestVersion="1.0.0" />
    <Policies>
      <RunAsPolicy CodePackageRef="Code" UserRef="Admin" EntryPointType="All" />
    </Policies>
  </ServiceManifestImport>
  <ServiceManifestImport>
    <ServiceManifestRef ServiceManifestName="Gateway" ServiceManifestVersion="1.0.0" />
  </ServiceManifestImport>
  <DefaultServices>
    <Service Name="NginxPoC">
      <StatelessService ServiceTypeName="NginxPoCType" InstanceCount="[NginxPoC_InstanceCount]">
        <SingletonPartition />
      </StatelessService>
    </Service>
    <Service Name="Gateway">
      <StatelessService ServiceTypeName="GatewayType" InstanceCount="[Gateway_InstanceCount]">
        <SingletonPartition />
      </StatelessService>
    </Service>
  </DefaultServices>
  <Principals>
    <Users>
      <User Name="Admin">
        <MemberOf>
          <SystemGroup Name="Administrators" />
        </MemberOf>
      </User>
    </Users>
  </Principals>
</ApplicationManifest>

这是我当前的 nginx.conf 文件:

server {
        listen       80;
        server_name  localhost;

        location / {
            proxy_pass   http://127.0.0.1:8081;
        }
}

2016 年 10 月 9 日更新

根据讨论中的要求,我创建了一个测试项目here。欢迎对项目做出任何贡献。

【问题讨论】:

  • 创建问题,但是如果您正在为服务解析创建一个网关 - 为什么又需要 nginx 在前面,因为它也只是一个代理?我也经历过同样的想法,但选择不在前面有 nginx,所以我很好奇。
  • .NET 核心团队建议在 Kestrel 之前使用 IIS 或 nginx。此外,我还将获得 gzip、http2 等好处。
  • 让我们知道这些建议是否来自公共链接。这是有道理的,也许从这些建议中还有其他值得一读的东西。
  • docs.asp.net/en/latest/fundamentals/… Quote: Kestrel 设计为在代理后运行(例如 IIS 或 Nginx),不应直接面向 Internet 部署。这里有一些提示:channel9.msdn.com/Series/aspnetmonsters/…
  • 我编写了一个代理网关来避免使用 nginx,但基于此我将正确使用 nginx。但是然后使用 nginx 我不明白为什么你还想要网关项目。你不能告诉 nginx 转发到服务吗?

标签: nginx asp.net-core azure-service-fabric


【解决方案1】:

如果您将 nginx 和网关服务部署到所有节点 (InstanceCount = -1),您应该会很好。如果网关服务在一个节点上关闭,您当然无法将请求从 nginx 转发到另一个节点上的网关服务。为此,您需要 nginx 服务来查找网关服务。

您可以使用 REST 调用获取网关的服务端点地址:https://msdn.microsoft.com/en-us/library/azure/dn707638.aspx

【讨论】:

  • 我已经尝试过了,将 InstanceCount 设置为 -1 并使用 127.0.0.1 作为 nginx 配置中的重定向地址(指定服务的端口),假设该服务在每个节点上都可用。不幸的是,它不起作用。
  • 你看过这个博客吗,描述包的使用:haishibai.blogspot.com/2016/08/…
  • 是的,我正在使用那个包。我发现了问题:在 .net 核心应用程序的启动中,我只配置了端点 url。一旦我添加了本地地址(和端口),一切正常。答案是正确的。
  • 太棒了 - 很高兴知道 :-) 您愿意在 GitHub 或其他地方分享该项目作为示例供其他人查看吗?
  • 我已经更新了问题,并提供了指向 GitHub 上的测试解决方案的链接。
猜你喜欢
  • 2018-06-30
  • 2017-08-31
  • 2017-04-18
  • 1970-01-01
  • 1970-01-01
  • 2019-06-21
  • 2018-07-26
  • 2017-01-14
  • 2020-09-08
相关资源
最近更新 更多