日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

在IIS上部署Go?API項目_win服務器

作者:taadis ? 更新時間: 2022-10-25 編程語言

問題場景

我這邊原先的技術棧主要是 .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.

原文鏈接:https://www.cnblogs.com/taadis/p/how-to-deploy-go-api-on-iis.html

欄目分類
最近更新