問題場景
我這邊原先的技術棧主要是 .NET(Core), 所以服務器基本上都是 Windows Server + IIS.
這次有個 API 服務用 Go 重寫, 但是部署有點不美, 直接執行黑框框不好看, 也容易丟, 做成服務又不方便更新維護, 想着能不能繼續掛載在 IIS 下.
於是乎...
首先想到的是 IIS 下有個 FastCGI 支持, 以前還在 IIS 下部署過 PHP 項目.
搜到 Go 中有個 net/http/fcgi
庫, 寫個簡單服務驗證一下, 代碼如下:
package main
import (
"net"
"net/http"
"net/http/fcgi"
)
func handler(resp http.ResponseWriter, req *http.Request) {
resp.Write([]byte("hello"))
}
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", handler)
l, err := net.Listen("tcp", ":0")
if err != nil{
panic(err)
}
err = fcgi.Serve(l, mux)
if err != nil{
panic(err)
}
}
執行 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 這邊已經准備就緒.
來看看我們代碼和配置
// main.go
package main
import (
"fmt"
"net"
"net/http"
"os"
)
func handler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Go running on IIS"))
}
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", handler)
// 獲取由 ACNM 設置的環境變量
port := "0" // default
envPort := os.Getenv("ASPNETCORE_PORT")
if envPort != "" {
port = envPort
fmt.Println("get env ASPNETCORE_PORT", port)
}
l, err := net.Listen("tcp", ":" + port)
if err != nil{
panic(err)
}
defer l.Close()
fmt.Println("listening on", l.Addr().String())
err = http.Serve(l, mux)
if err != nil{
panic(err)
}
}
關鍵點就是代碼中要通過獲取 ACNM 提供的端口環境變量, 也就是 ASPNETCORE_PORT
, 熟悉 ASP.NET Core 的小伙伴對這個應該不陌生了.
然后構建我們的可執行文件 xxx.exe
go build
然后配置 web.config 內容如下:
<!-- web.config -->
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="." inheritInChildApplications="false">
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath=".\your.exe" arguments="" stdoutLogEnabled="true" stdoutLogFile=".\stdout" />
</system.webServer>
</location>
</configuration>
把 xxx.exe 和 web.config 扔到前面新建的站點中即可.
后續更新升級直接替換 exe 即可.
Go 寫的程序體積比較小, 構建后也只有單個執行文件, 清爽多了.
最后來個效果圖
注意事項
如出現以下錯誤信息, 可能是端口號已被占用, 換個端口號試試
[ERROR] listen tcp :8080: bind: An attempt was made to access a socket in a way forbidden by its access permissions.
參考
- 程序是如何接收並啟動監聽 IIS 站點設置的端口號的?
- ASP.NET Core 中的 Web 服務器實現
- 在Windows Server 2008上部署Go Web項目
- 使用 FastCGI 在 IIS 7 上托管 PHP 應用程序
- 在Go + FastCGI中,使用多個處理程序有意義嗎?
- Run go web application on IIS
- Announcing the Release of the HttpPlatformHandler Module for IIS 8+
- Download HttpPlatformHandler v1.2
- running-go-behind-iis