【Tars-go】腾讯微服务框架学习使用02-- http 服务

2 http 服务

官方文档说http这里是在net/http原生包的基础上做了修改。

官方给的案例:

go 复制代码
package main

import (
	"net/http"
	"github.com/TarsCloud/TarsGo/tars"
)

func main() {
	mux := &tars.TarsHttpMux{}
	mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("Hello tafgo"))
	})

    cfg := tars.GetServerConfig()
	tars.AddHttpServant(mux, cfg.App+"."+cfg.Server+".HttpObj") //Register http server
	tars.Run()
}

可以看到 在初始化了TarsHttpMux, 再将TarsHttpMux 注册到tars框架中作为servant就可以启动http服务。

  1. TarsHttpMux是什么:

    go 复制代码
    // TarsHttpMux is http.ServeMux for tars http server.
    type TarsHttpMux struct {
    	http.ServeMux
    	cfg *TarsHttpConf
    }

    可以看出TarsHttpMux确实就只是对http.ServeMux做了个包装加入了Conf 并加入了上报状态信息、计算耗时等基本服务器功能。

  2. TarsHttpMux 注册到tars框架中作为servant:

    1. tars.AddHttpServant的底层实现函数如下:

    go 复制代码
    // AddHttpServantWithExceptionStatusChecker add http servant handler with exceptionStatusChecker for obj.
    func (a *application) AddHttpServantWithExceptionStatusChecker(mux HttpHandler, obj string, exceptionStatusChecker func(int) bool) {
    	cfg, ok := a.tarsConfig[obj]
    	if !ok {
    		msg := fmt.Sprintf("http servant obj name not found: %s", obj)
    		ReportNotifyInfo(NotifyError, msg)
    		TLOG.Debug(msg)
    		panic(errors.New(msg))
    	}
    	TLOG.Debugf("add http protocol server: %+v", cfg)
    	a.objRunList = append(a.objRunList, obj)
    	addrInfo := strings.SplitN(cfg.Address, ":", 2)
    	var port int64
    	if len(addrInfo) == 2 {
    		var err error
    		port, err = strconv.ParseInt(addrInfo[1], 10, 32)
    		if err != nil {
    			panic(fmt.Errorf("http server listen port: %s parse err: %v", addrInfo[1], err))
    		}
    	}
    	svrCfg := a.ServerConfig()
    	httpConf := &TarsHttpConf{
    		Container:              svrCfg.Container,
    		AppName:                fmt.Sprintf("%s.%s", svrCfg.App, svrCfg.Server),
    		Version:                svrCfg.Version,
    		IP:                     addrInfo[0],
    		Port:                   int32(port),
    		SetId:                  svrCfg.Setdivision,
    		ExceptionStatusChecker: exceptionStatusChecker,
    	}
    	mux.SetConfig(httpConf)
    	s := &http.Server{Addr: cfg.Address, Handler: mux, TLSConfig: cfg.TlsConfig}
    	a.httpSvrs[obj] = s
    }
    1. 简单来说就是将http根据配置实例化-》再注册Sever-》这代码很好理解
  3. 配置文件配置servant

    xml 复制代码
    <tars>
        <application>
            <server>
                <AiGo.backend.HttpObjAdapter>
                    allow
                    endpoint=http -h 127.0.0.1 -p 18080 -t 60000
                    handlegroup=AiGo.backend.HttpObjAdapter
                    maxconns=200000
                    protocol=http
                    queuecap=10000
                    queuetimeout=60000
                    servant=AiGo.backend.HttpObj
                    shmcap=0
                    shmkey=0
                    threads=1
                </AiGo.backend.HttpObjAdapter>
            </server>
        </application>
    </tars>

    可以看出就protocol=http 这里改了,其他的和tars服务没啥区别。

相关推荐
hengzhepa23 分钟前
ElasticSearch备考 -- Async search
大数据·学习·elasticsearch·搜索引擎·es
小小洋洋2 小时前
BLE MESH学习1-基于沁恒CH582学习
学习
怪我冷i2 小时前
使用vscode调试wails项目(golang桌面GUI)
vscode·golang
Ace'3 小时前
每日一题&&学习笔记
笔记·学习
IM_DALLA3 小时前
【Verilog学习日常】—牛客网刷题—Verilog进阶挑战—VL25
学习·fpga开发·verilog学习
丶Darling.3 小时前
LeetCode Hot100 | Day1 | 二叉树:二叉树的直径
数据结构·c++·学习·算法·leetcode·二叉树
z樾5 小时前
Github界面学习
学习
小吴同学(wlx)6 小时前
Golang 进阶3—— 协程&管道
golang
技术卷6 小时前
GO网络编程(三):海量用户通信系统1:登录功能初步
golang·网络编程
道爷我悟了6 小时前
Vue入门-指令学习-v-html
vue.js·学习·html