欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

在IIS上部署Go?API項(xiàng)目

 更新時間:2022年08月30日 08:33:21   作者:taadis  
這篇文章介紹了在IIS上部署Go?API項(xiàng)目的方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

問題場景

我這邊原先的技術(shù)棧主要是 .NET(Core), 所以服務(wù)器基本上都是 Windows Server + IIS.

這次有個 API 服務(wù)用 Go 重寫, 但是部署有點(diǎn)不美, 直接執(zhí)行黑框框不好看, 也容易丟, 做成服務(wù)又不方便更新維護(hù), 想著能不能繼續(xù)掛載在 IIS 下.

于是乎...

首先想到的是 IIS 下有個 FastCGI 支持, 以前還在 IIS 下部署過 PHP 項(xiàng)目.

搜到 Go 中有個 net/http/fcgi 庫, 寫個簡單服務(wù)驗(yàn)證一下, 代碼如下:

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)
	}
}

執(zhí)行 go run main.go 命令后, 程序沒有任何異?;蜉敵鲋苯泳徒Y(jié)束了...

資料搜了一圈看到這玩意基本已被遺忘在不知道哪個旮旯里了...

然后搜到 Azure 前些年用 HttpPlatformHandler Module 在 IIS 上支持 Java/Node/... 應(yīng)用程序.

試了下基本也是廢了.

解決方案

最后溜達(dá)了一圈, 發(fā)現(xiàn) HttpPlatformHandler 已被 ASPNETCore Module 宿主模塊取代.

那么就跟我們在 IIS 上部署 ASP.NET Core 應(yīng)用程序一樣, 首先下載并安裝 ASP.NET Core Hosting Bundle, 了解更多可參閱 ASP.NET Core Module

然后新建對應(yīng)的站點(diǎn), 應(yīng)用程序池調(diào)整成 無托管代碼

IIS 這邊已經(jīng)準(zhǔn)備就緒.

來看看我們代碼和配置

// 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 設(shè)置的環(huán)境變量
	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)
	}
}

關(guān)鍵點(diǎn)就是代碼中要通過獲取 ACNM 提供的端口環(huán)境變量, 也就是 ASPNETCORE_PORT, 熟悉 ASP.NET Core 的小伙伴對這個應(yīng)該不陌生了.

然后構(gòu)建我們的可執(zhí)行文件 xxx.exe

go build

然后配置 web.config 內(nèi)容如下:

<!-- 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 扔到前面新建的站點(diǎn)中即可.

后續(xù)更新升級直接替換 exe 即可.

Go 寫的程序體積比較小, 構(gòu)建后也只有單個執(zhí)行文件, 清爽多了.

最后來個效果圖

注意事項(xiàng)

如出現(xiàn)以下錯誤信息, 可能是端口號已被占用, 換個端口號試試

[ERROR] listen tcp :8080: bind: An attempt was made to access a socket in a way forbidden by its access permissions.

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接

相關(guān)文章

最新評論