【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服务没啥区别。

相关推荐
源代码•宸13 分钟前
前置准备:定时微服务背景和现状
开发语言·经验分享·后端·微服务·云原生·架构·golang
我命由我1234519 分钟前
Photoshop - Photoshop 把两个 PSD 文件合并
学习·ui·职场和发展·产品运营·产品经理·学习方法·photoshop
小雪崩2 小时前
嵌入式学习 day39:TCP并发服务器模型
linux·服务器·c语言·网络·学习·tcp/ip
知识分享小能手2 小时前
深度学习学习教程,从入门到精通,概率与信息论 — 知识点详解(3)
人工智能·深度学习·学习·数据挖掘·概率论
李燚2 小时前
把规则搬回家:三个 BC 的贫血→充血重构实录(第103篇)
golang·agent·ddd·领域驱动设计·eino·deepflux·eino adk
Wendy不吃榴莲2 小时前
# AI短剧教程 - 《后西游记》开播后,AI影视为什么更考验“连续讲故事”?
人工智能·笔记·学习·ai·视频
Agudamu11613 小时前
B站学习视频怎么变笔记:用 Ai好记 + Obsidian 搭建个人知识库的完整教程
人工智能·笔记·学习·音视频
kkkkkkkkkk_Z3 小时前
学嵌入式和Linux应用编程|学习日记:计算机网络基础
linux·学习·计算机网络
麦田里的粮仓3 小时前
图形设计工具箱(免费开源)
学习
每天题库3 小时前
交安C证报考条件有哪些?公路水运安全员C证报名时间及考试科目
学习·安全·考试·题库·考证