簡介
- 該樣例工程演示瞭如何配置使得MOSN作爲標準Http協議的代理+ MOSN之間的協議是HTTP2
- 爲了演示方便,啓動一個MOSN監聽兩個端口,一個轉發Client的請求,一個收到請求以後轉發給Server
其訪問路徑如下:
相當於一個HTTP代理
準備
需要一個編譯好的MOSN程序cd ${projectpath}/cmd/mosn/maingo build
- 示例代碼目錄
${targetpath} = ${projectpath}/examples/codes/http-sample/
- 將編譯好的程序移動到示例代碼目錄
mv main ${targetpath}/cd ${targetpath}
目錄結構
main // 編譯完成的MOSN程序server.go // 模擬的Http Serverconfig.json
配置文件
{
"servers":[
{
// 默認日誌目錄
"default_log_path":"stdout",
// 配置路由,可以配置多個路由,對應不同的
"routers":[
{
//路由的名稱
"router_config_name":"server_router",
//虛擬 host,一個虛擬的 host 可以對應N個真實的host
"virtual_hosts":[{
// 虛擬host名稱
"name":"serverHost",
//DNS 域名,必須能跟 virtual_host 的 URL 匹配
"domains": ["*"],
//可以配置多個 路由
"routers": [
{
//匹配所有的url請求
"match":{"prefix":"/"},
//對應的 cluster 匹配成功後會從 cluster_name 指定的 cluster 中獲取 地址進行轉發
"route":{"cluster_name":"serverCluster"}
}
]
}]
},
{ //參考 server 端的配置說明
"router_config_name":"client_router",
"virtual_hosts":[{
"name":"clientHost",
"domains": ["*"],
"routers": [
{
"match":{"prefix":"/"},
"route":{"cluster_name":"clientCluster"}
}
]
}]
}
],
"listeners":[
{
// serverListener 用於監聽 Mosn的請求,進行轉發
"name":"serverListener",
"address": "127.0.0.1:2046",
"bind_port": true,
"filter_chains": [{
"filters": [
{
"type": "proxy",
"config": {
"downstream_protocol": "Http1",
"upstream_protocol": "Http1",
"router_config_name":"server_router"
}
}
]
}]
},
{
// clientListener 用於監聽應用的請求進行轉發 (這裏實際上是監聽的瀏覽器的請求)
"name":"clientListener",
"address": "127.0.0.1:2045",
"bind_port": true,
"filter_chains": [{
"filters": [
{
"type": "proxy",
"config": {
// 單個mosn流轉,無需進行協議的轉換,配置成一致就可以
"downstream_protocol": "Http1",
"upstream_protocol": "Http1",
"router_config_name":"client_router"
}
}
]
}]
}
]
}
],
"cluster_manager":{
"clusters":[
{
"name":"serverCluster",
"type": "SIMPLE",
"lb_type": "LB_RANDOM",
"max_request_per_conn": 1024,
"conn_buffer_limit_bytes":32768,
"hosts":[
//地址是對應的 應用的服務地址
{"address":"127.0.0.1:8080"}
]
},
{
"name": "clientCluster",
"type": "SIMPLE",
"lb_type": "LB_RANDOM",
"max_request_per_conn": 1024,
"conn_buffer_limit_bytes":32768,
"hosts":[
//地址是對應的server 監聽的地址
{"address":"127.0.0.1:2046"}
]
}
]
},
"admin": {
"address": {
"socket_address": {
"address": "0.0.0.0",
"port_value": 34901
}
}
}
}
運行說明
HTTP Server 代碼
package main
import (
"fmt"
"net/http"
)
/**
* 一個簡單的HTTP服務
*/
func ServeHTTP(w http.ResponseWriter, r *http.Request) {
fmt.Printf("[UPSTREAM]receive request %s", r.URL)
fmt.Println()
w.Header().Set("Content-Type", "text/plain")
fmt.Fprintf(w, "Method: %s\n", r.Method)
fmt.Fprintf(w, "Protocol: %s\n", r.Proto)
fmt.Fprintf(w, "Host: %s\n", r.Host)
fmt.Fprintf(w, "RemoteAddr: %s\n", r.RemoteAddr)
fmt.Fprintf(w, "RequestURI: %q\n", r.RequestURI)
fmt.Fprintf(w, "URL: %#v\n", r.URL)
fmt.Fprintf(w, "Body.ContentLength: %d (-1 means unknown)\n", r.ContentLength)
fmt.Fprintf(w, "Close: %v (relevant for HTTP/1 only)\n", r.Close)
fmt.Fprintf(w, "TLS: %#v\n", r.TLS)
fmt.Fprintf(w, "\nHeaders:\n")
r.Header.Write(w)
}
func main() {
http.HandleFunc("/", ServeHTTP)
http.ListenAndServe("127.0.0.1:8080", nil)
}
啓動一個HTTP Server
go run server.go
啓動MOSN
- 使用config.json 運行非TLS加密的MOSN
./main start -c config.json
使用CURL進行驗證
curl [http://127.0.0.1:2045/](http://127.0.0.1:2045/)