具有 nginx 和 .NET 核心服务的 Service Fabric
Service Fabric with nginx and .NET core services
我正在设置一个 Service Fabric 应用程序,其中包含:
- 一个 Nginx 实例作为前端(单实例,端口 80)
- 一些使用 Asp.net 内核编写的应用程序(1 个网站,一些 API 服务)(多个实例,动态端口)
- 地址解析网关服务(单实例,8081端口)
对于 nginx,我使用的解决方案是 Nuget package。
运行 .NET 核心应用程序的网关和一般情况下的示例已采用 here
.NET 核心团队本身建议在 真实 网络服务器之后托管应用程序,例如 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-09
根据讨论中的要求,我创建了一个测试项目 here。欢迎对项目做出贡献。
如果您将 nginx 和网关服务部署到所有节点 (InstanceCount = -1),您应该没问题。如果一个节点上的网关服务宕机了,你当然无法将请求从 nginx 转发到另一个节点上的网关服务。为此,您需要 nginx 服务来查找网关服务。
您可以使用 REST 调用获取网关的服务端点地址:https://msdn.microsoft.com/en-us/library/azure/dn707638.aspx
我正在设置一个 Service Fabric 应用程序,其中包含:
- 一个 Nginx 实例作为前端(单实例,端口 80)
- 一些使用 Asp.net 内核编写的应用程序(1 个网站,一些 API 服务)(多个实例,动态端口)
- 地址解析网关服务(单实例,8081端口)
对于 nginx,我使用的解决方案是 Nuget package。
运行 .NET 核心应用程序的网关和一般情况下的示例已采用 here
.NET 核心团队本身建议在 真实 网络服务器之后托管应用程序,例如 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-09
根据讨论中的要求,我创建了一个测试项目 here。欢迎对项目做出贡献。
如果您将 nginx 和网关服务部署到所有节点 (InstanceCount = -1),您应该没问题。如果一个节点上的网关服务宕机了,你当然无法将请求从 nginx 转发到另一个节点上的网关服务。为此,您需要 nginx 服务来查找网关服务。
您可以使用 REST 调用获取网关的服务端点地址:https://msdn.microsoft.com/en-us/library/azure/dn707638.aspx