[Win服务器] 在IIS上部署GoAPI项目

1702 1
王子 2022-11-5 09:35:08 | 显示全部楼层 |阅读模式
问题场景

我这边原先的技术栈主要是 .NET(Core), 所以服务器基本上都是 Windows Server + IIS.
这次有个 API 服务用 Go 重写, 但是部署有点不美, 直接执行黑框框不好看, 也容易丢, 做成服务又不方便更新维护, 想着能不能继续挂载在 IIS 下.
于是乎...
首先想到的是 IIS 下有个 FastCGI 支持, 以前还在 IIS 下部署过 PHP 项目.
搜到 Go 中有个 net/http/fcgi 库, 写个简单服务验证一下, 代码如下:
  1. package main
  2. import (
  3.         "net"
  4.         "net/http"
  5.         "net/http/fcgi"
  6. )
  7. func handler(resp http.ResponseWriter, req *http.Request) {
  8.         resp.Write([]byte("hello"))
  9. }
  10. func main() {
  11.         mux := http.NewServeMux()
  12.         mux.HandleFunc("/", handler)
  13.         l, err := net.Listen("tcp", ":0")
  14.         if err != nil{
  15.                 panic(err)
  16.         }
  17.         err = fcgi.Serve(l, mux)
  18.         if err != nil{
  19.                 panic(err)
  20.         }
  21. }
复制代码
执行 go run main.go 命令后, 程序没有任何异常或输出直接就结束了...
资料搜了一圈看到这玩意基本已被遗忘在不知道哪个旮旯里了...
然后搜到 Azure 前些年用 HttpPlatformHandler Module 在 IIS 上支持 Java/Node/... 应用程序.
试了下基本也是废了.
解决方案

最后溜达了一圈, 发现 HttpPlatformHandler 已被 ASPNETCore Module 宿主模块取代.
那么就跟我们在 IIS 上部署 ASP.NET Core 应用程序一样, 首先下载并安装 ASP.NET Core Hosting Bundle, 了解更多可参阅 ASP.NET Core Module
然后新建对应的站点, 应用程序池调整成 无托管代码


IIS 这边已经准备就绪.
来看看我们代码和配置
  1. // main.go
  2. package main
  3. import (
  4.         "fmt"
  5.         "net"
  6.         "net/http"
  7.         "os"
  8. )
  9. func handler(w http.ResponseWriter, r *http.Request) {
  10.         w.Write([]byte("Go running on IIS"))
  11. }
  12. func main() {
  13.         mux := http.NewServeMux()
  14.         mux.HandleFunc("/", handler)
  15.         // 获取由 ACNM 设置的环境变量
  16.         port := "0" // default
  17.         envPort := os.Getenv("ASPNETCORE_PORT")
  18.         if envPort != "" {
  19.                 port = envPort
  20.                 fmt.Println("get env ASPNETCORE_PORT", port)
  21.         }
  22.         l, err := net.Listen("tcp", ":" + port)
  23.         if err != nil{
  24.                 panic(err)
  25.         }
  26.         defer l.Close()
  27.         fmt.Println("listening on", l.Addr().String())
  28.         err = http.Serve(l, mux)
  29.         if err != nil{
  30.                 panic(err)
  31.         }
  32. }
复制代码
关键点就是代码中要通过获取 ACNM 提供的端口环境变量, 也就是 ASPNETCORE_PORT, 熟悉 ASP.NET Core 的小伙伴对这个应该不陌生了.
然后构建我们的可执行文件 xxx.exe
  1. go build
复制代码
然后配置 web.config 内容如下:
  1. <!-- web.config -->
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <configuration>
  4.   <location path="." inheritInChildApplications="false">
  5.     <system.webServer>
  6.       <handlers>
  7.         <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
  8.       </handlers>
  9.       <aspNetCore processPath=".\your.exe" arguments="" stdoutLogEnabled="true" stdoutLogFile=".\stdout" />
  10.     </system.webServer>
  11.   </location>
  12. </configuration>
复制代码
xxx.exeweb.config 扔到前面新建的站点中即可.
后续更新升级直接替换 exe 即可.
Go 写的程序体积比较小, 构建后也只有单个执行文件, 清爽多了.


最后来个效果图


注意事项

如出现以下错误信息, 可能是端口号已被占用, 换个端口号试试
[ERROR] listen tcp :8080: bind: An attempt was made to access a socket in a way forbidden by its access permissions.
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对中国红客联盟的支持。如果你想了解更多相关内容请查看下面相关链接

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行