時間:2022-09-11來源:www.farandoo.com作者:電腦系統城
我這邊原先的技術棧主要是 .NET(Core), 所以服務器基本上都是 Windows Server + IIS.
這次有個 API 服務用 Go 重寫, 但是部署有點不美, 直接執行黑框框不好看, 也容易丟, 做成服務又不方便更新維護, 想著能不能繼續掛載在 IIS 下.
于是乎...
首先想到的是 IIS 下有個 FastCGI 支持, 以前還在 IIS 下部署過 PHP 項目.
搜到 Go 中有個 net/http/fcgi
庫, 寫個簡單服務驗證一下, 代碼如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
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
命令后, 程序沒有任何異?;蜉敵鲋苯泳徒Y束了...
資料搜了一圈看到這玩意基本已被遺忘在不知道哪個旮旯里了...
然后搜到 Azure 前些年用 HttpPlatformHandler Module 在 IIS 上支持 Java/Node/... 應用程序.
試了下基本也是廢了.
最后溜達了一圈, 發現 HttpPlatformHandler 已被 ASPNETCore Module 宿主模塊取代.
那么就跟我們在 IIS 上部署 ASP.NET Core 應用程序一樣, 首先下載并安裝 ASP.NET Core Hosting Bundle, 了解更多可參閱 ASP.NET Core Module
然后新建對應的站點, 應用程序池調整成 無托管代碼
IIS 這邊已經準備就緒.
來看看我們代碼和配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
// 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
1 | go build |
然后配置 web.config 內容如下:
1 2 3 4 5 6 7 8 9 10 11 12 |
<!-- 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.
2022-09-11
瀏覽器安全增強配置的步驟 Windows Server 2019 取消默認IE2022-09-11
mr本地模式 Apache Hive 通用調優featch抓取機制2022-09-11
系統之家推薦Windows Server 2019 DNS服務器配置方法教程圖解大全(入門篇)這篇文章主要介紹了Windows Server 2019 WEB和FTP服務器的配置方法,這都是基本的,需要的朋友可以參考下...
2022-09-11
一、Keepalived介紹 二、Keepalived的應用場景 三、Keepalived的工作原理 1 VRRP協議 2 核心組件 3 分層工作 4 工作狀態 四、Keepalived使用 1 配置介紹 2 使用keepalived對nginx進行高可用搭建 2.1 環境準備 2.2 ng...
2022-09-11