使用Go語言寫一個(gè)Http?Server的實(shí)現(xiàn)
Http Server 代碼
go.mod:
module goStudy1 go 1.17
main.go:
package main
import (
"fmt"
"os"
"strconv"
//"github.com/thinkeridea/go-extend/exnet"
"io"
"log"
"net/http"
"strings"
)
/*
編寫一個(gè) HTTP 服務(wù)器,4個(gè)功能:
1,接收客戶端 request,并將 request 中帶的 header 寫入 response header
2,讀取當(dāng)前系統(tǒng)的環(huán)境變量中的 VERSION 配置,并寫入 response header
3,Server 端記錄訪問日志包括客戶端 IP,HTTP 返回碼,輸出到 server 端的標(biāo)準(zhǔn)輸出
4,當(dāng)訪問 localhost/healthz 時(shí),應(yīng)返回 200
*/
// Main方法入口
func main() {
println("環(huán)境正常")
// 功能1
http.HandleFunc("/requestAndResponse", requestAndResponse)
// 功能2
http.HandleFunc("/getVersion", getVersion)
// 功能3
http.HandleFunc("/ipAndStatus", ipAndStatus) //注冊接口句柄
// 功能4
http.HandleFunc("/healthz", healthz) //注冊接口句柄
err := http.ListenAndServe(":81", nil) //監(jiān)聽空句柄,80端口被占用,使用81端口
if nil != err {
log.Fatal(err) //顯示錯(cuò)誤日志
}
}
// 功能1,接收請求及響應(yīng)
func requestAndResponse(response http.ResponseWriter, request *http.Request) {
println("調(diào)用requestAndResponse接口")
headers := request.Header //header是Map類型的數(shù)據(jù)
println("傳入的hander:")
for header := range headers { //value是[]string
//println("header的key:" + header)
values := headers[header]
for index, _ := range values {
values[index] = strings.TrimSpace(values[index])
//println("index=" + strconv.Itoa(index))
//println("header的value:" + values[index])
}
//valueString := strings.Join(values, "")
//println("header的value:" + valueString)
println(header + "=" + strings.Join(values, ",")) //打印request的header的k=v
response.Header().Set(header, strings.Join(values, ",")) // 遍歷寫入response的Header
//println()
}
fmt.Fprintln(response, "Header全部數(shù)據(jù):", headers)
io.WriteString(response, "succeed")
}
// 功能2,獲取環(huán)境變量的version
func getVersion(response http.ResponseWriter, request *http.Request) {
println("調(diào)用getVersion接口")
envStr := os.Getenv("VERSION")
//envStr := os.Getenv("HADOOP_HOME")
//println("系統(tǒng)環(huán)境變量:" + envStr) //可以看到 C:\soft\hadoop-3.3.1 Win10需要重啟電腦才能生效
response.Header().Set("VERSION", envStr)
io.WriteString(response, "succeed")
}
// 功能3,輸出IP與返回碼
func ipAndStatus(response http.ResponseWriter, request *http.Request) {
println("調(diào)用ipAndStatus接口")
form := request.RemoteAddr
println("Client->ip:port=" + form) //虛擬機(jī)是橋接模式。使用postman返回的全部是127.0.0.1 用手機(jī)打開網(wǎng)站192.168.1.139:81/ipAndStatus可以看到新IP
ipStr := strings.Split(form, ":")
println("Client->ip=" + ipStr[0]) //打印ip
// 獲取http響應(yīng)碼
//response.WriteHeader(301) //手動(dòng)設(shè)置響應(yīng)碼,默認(rèn)200
//response.WriteHeader(http.StatusOK)//由于默認(rèn)是調(diào)用這個(gè),∴返回碼都是這個(gè)200【server.go有源碼】
println("Client->response code=" + strconv.Itoa(http.StatusOK))
//println("response code->:" + code)
io.WriteString(response, "succeed")
}
// 功能4,連通性測試接口
func healthz(response http.ResponseWriter, request *http.Request) {
println("調(diào)用healthz接口")
response.WriteHeader(200) //設(shè)置返回碼200
//response.WriteHeader(http.StatusOK)//默認(rèn)會(huì)調(diào)用這個(gè)方法,默認(rèn)就是200【server.go有源碼】
io.WriteString(response, "succeed")
}
由于80端口被占用,使用了81端口。
調(diào)試
由于Linux虛擬機(jī)沒有安裝go環(huán)境,只有windows有g(shù)o環(huán)境,使用goland開發(fā)后,用postman調(diào)試。
功能1
網(wǎng)站:http://127.0.0.1:81/requestAndResponse
![[外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機(jī)制,建議將圖片保存下來直接上傳(img-8ZN18qxd-1650553245870)(E:\study\極客時(shí)間\云原生\模塊2編寫Go程序\作業(yè)\goStudy1\Http Server.assets\image-20220420224510186.jpg)]](http://img.jbzj.com/file_images/article/202204/202204260848361.jpg)
POST的request中額外配置了 k2=v1 。Send后可以看到:
![[外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機(jī)制,建議將圖片保存下來直接上傳(img-sLFwYrR3-1650553245872)(E:\study\極客時(shí)間\云原生\模塊2編寫Go程序\作業(yè)\goStudy1\Http Server.assets\image-20220420224623797.jpg)]](http://img.jbzj.com/file_images/article/202204/202204260848362.jpg)
Response中出現(xiàn)了手動(dòng)新增的請求頭及其它默認(rèn)的請求頭?!驹嫉膔esponse只有3對kv結(jié)果,已經(jīng)遍歷添加成功】。說明成功寫入。
功能2
由于Windows需要重啟才能刷新環(huán)境變量,故:
//envStr := os.Getenv("VERSION")
envStr := os.Getenv("HADOOP_HOME")
測試時(shí),此處讀取已經(jīng)存在的環(huán)境變量,原理是一致的。
網(wǎng)站:http://127.0.0.1:81/getVersion
![[外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機(jī)制,建議將圖片保存下來直接上傳(img-5gEC5Fxw-1650553245873)(E:\study\極客時(shí)間\云原生\模塊2編寫Go程序\作業(yè)\goStudy1\Http Server.assets\image-20220420225251189.jpg)]](http://img.jbzj.com/file_images/article/202204/202204260848363.jpg)
說明Go可以讀取到環(huán)境變量的值,并且寫入response的headers。
功能3
網(wǎng)站:http://127.0.0.1:81/ipAndStatus
分別用postman、手機(jī)請求這個(gè)網(wǎng)站【手機(jī)請求時(shí)需要和PC在同一個(gè)路由,將網(wǎng)站IP更換為PC的IP才可以訪問】,goland中顯示:
環(huán)境正常 調(diào)用getVersion接口 調(diào)用ipAndStatus接口 Client->ip:port=127.0.0.1:59595 Client->ip=127.0.0.1 Client->response code=200 調(diào)用ipAndStatus接口 Client->ip:port=192.168.1.138:37548 Client->ip=192.168.1.138 Client->response code=200
顯然讀取到了client的IP。由于server.go中有寫:
// WriteHeader sends an HTTP response header with the provided // status code. // // If WriteHeader is not called explicitly, the first call to Write // will trigger an implicit WriteHeader(http.StatusOK). // Thus explicit calls to WriteHeader are mainly used to // send error codes. // // The provided code must be a valid HTTP 1xx-5xx status code. // Only one header may be written. Go does not currently // support sending user-defined 1xx informational headers, // with the exception of 100-continue response header that the // Server sends automatically when the Request.Body is read. WriteHeader(statusCode int)
默認(rèn)的響應(yīng)頭就是取返回值為200,不設(shè)置就是按照默認(rèn)的200來返回,故此處的響應(yīng)碼為200。
由于響應(yīng)體引用的請求體并不包含返回碼,如果直接從響應(yīng)體的請求中拿返回碼【request.Response.StatusCode】,會(huì)報(bào)內(nèi)存錯(cuò)誤及空指針的panic。
功能4
網(wǎng)址:http://127.0.0.1:81/healthz
使用postman調(diào)用接口,可以看到:
![[外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機(jī)制,建議將圖片保存下來直接上傳(img-mGaVuF96-1650553245874)(E:\study\極客時(shí)間\云原生\模塊2編寫Go程序\作業(yè)\goStudy1\Http Server.assets\image-20220420230044921.jpg)]](http://img.jbzj.com/file_images/article/202204/202204260848374.jpg)
默認(rèn)的響應(yīng)體的響應(yīng)頭的返回碼就是200。且返回值3個(gè)。
可以看出,Go相比Java還是很簡潔的。
到此這篇關(guān)于使用Go語言寫一個(gè)Http Server的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Go語言Http Server內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Gin與Mysql實(shí)現(xiàn)簡單Restful風(fēng)格API實(shí)戰(zhàn)示例詳解
這篇文章主要為大家介紹了Gin與Mysql實(shí)現(xiàn)簡單Restful風(fēng)格API示例詳解,有需要的朋友可以借鑒參考下希望能夠有所幫助,祝大家多多進(jìn)步2021-11-11
詳解golang 定時(shí)任務(wù)time.Sleep和time.Tick實(shí)現(xiàn)結(jié)果比較
本文主要介紹了golang 定時(shí)任務(wù)time.Sleep和time.Tick實(shí)現(xiàn)結(jié)果比較,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
Go語言fsnotify接口實(shí)現(xiàn)監(jiān)測文件修改
這篇文章主要為大家介紹了Go語言fsnotify接口實(shí)現(xiàn)監(jiān)測文件修改的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
Golang實(shí)現(xiàn)多存儲(chǔ)驅(qū)動(dòng)設(shè)計(jì)SDK案例
這篇文章主要介紹了Golang實(shí)現(xiàn)多存儲(chǔ)驅(qū)動(dòng)設(shè)計(jì)SDK案例,Gocache是一個(gè)基于Go語言編寫的多存儲(chǔ)驅(qū)動(dòng)的緩存擴(kuò)展組件,更多具體內(nèi)容感興趣的小伙伴可以參考一下2022-09-09
Go語言實(shí)現(xiàn)字符串搜索算法Boyer-Moore
Boyer-Moore?算法是一種非常高效的字符串搜索算法,被廣泛的應(yīng)用于多種字符串搜索場景,下面我們就來學(xué)習(xí)一下如何利用Go語言實(shí)現(xiàn)這一字符串搜索算法吧2023-11-11
Golang編程實(shí)現(xiàn)生成n個(gè)從a到b不重復(fù)隨機(jī)數(shù)的方法
這篇文章主要介紹了Golang編程實(shí)現(xiàn)生成n個(gè)從a到b不重復(fù)隨機(jī)數(shù)的方法,結(jié)合實(shí)例形式分析了Go語言字符串操作及隨機(jī)數(shù)生成的相關(guān)操作技巧,需要的朋友可以參考下2017-01-01

