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

2 http 服务

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

官方给的案例:

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是什么:

    1. // 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的底层实现函数如下:

    2. // 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
      }
      
    3. 简单来说就是将http根据配置实例化-》再注册Sever-》这代码很好理解

  3. 配置文件配置servant

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

最近更新

  1. TCP协议是安全的吗?

    2024-04-13 22:20:05       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-13 22:20:05       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-13 22:20:05       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-13 22:20:05       20 阅读

热门阅读

  1. rest_framework_mongoengine实现后端的增删改查

    2024-04-13 22:20:05       14 阅读
  2. 说说你对栈、队列的理解?应用场景?

    2024-04-13 22:20:05       17 阅读
  3. WebKit结构简介

    2024-04-13 22:20:05       14 阅读
  4. 乌龟棋(c++实现)

    2024-04-13 22:20:05       17 阅读
  5. uniapp各种常用的提示框

    2024-04-13 22:20:05       15 阅读
  6. C++运算符重载

    2024-04-13 22:20:05       17 阅读
  7. 18. Linux API 编程预备知识

    2024-04-13 22:20:05       14 阅读
  8. 【应用】Spring-Bean注入-xml+注解

    2024-04-13 22:20:05       16 阅读
  9. skynet中newservice和uniqueservice的区别

    2024-04-13 22:20:05       15 阅读
  10. ChatGPT革新学术写作:论文撰写的新思路

    2024-04-13 22:20:05       18 阅读
  11. shell脚本启动jar包

    2024-04-13 22:20:05       14 阅读
  12. C语言隐藏执行其他程序

    2024-04-13 22:20:05       14 阅读
  13. openjudge_2.5基本算法之搜索_1756:八皇后

    2024-04-13 22:20:05       12 阅读