当前位置: 首页 > news >正文

网站关键词快速优化搜索引擎优化涉及的内容

网站关键词快速优化,搜索引擎优化涉及的内容,建e网灯具,百度云 免费 网站主机【Go入门】 Go如何使得Web工作 前面小节介绍了如何通过Go搭建一个Web服务,我们可以看到简单应用一个net/http包就方便的搭建起来了。那么Go在底层到底是怎么做的呢?万变不离其宗,Go的Web服务工作也离不开我们第一小节介绍的Web工作方式。 w…

【Go入门】 Go如何使得Web工作

前面小节介绍了如何通过Go搭建一个Web服务,我们可以看到简单应用一个net/http包就方便的搭建起来了。那么Go在底层到底是怎么做的呢?万变不离其宗,Go的Web服务工作也离不开我们第一小节介绍的Web工作方式。

web工作方式的几个概念

以下均是服务器端的几个概念

Request:用户请求的信息,用来解析用户的请求信息,包括post、get、cookie、url等信息

Response:服务器需要反馈给客户端的信息

Conn:用户的每次请求链接

Handler:处理请求和生成返回信息的处理逻辑

分析http包运行机制

下图是Go实现Web服务的工作模式的流程图

在这里插入图片描述

图3.9 http包执行流程

  1. 创建Listen Socket, 监听指定的端口, 等待客户端请求到来。

  2. Listen Socket接受客户端的请求, 得到Client Socket, 接下来通过Client Socket与客户端通信。

  3. 处理客户端的请求, 首先从Client Socket读取HTTP请求的协议头, 如果是POST方法, 还可能要读取客户端提交的数据, 然后交给相应的handler处理请求, handler处理完毕准备好客户端需要的数据, 通过Client Socket写给客户端。

这整个的过程里面我们只要了解清楚下面三个问题,也就知道Go是如何让Web运行起来了

  • 如何监听端口?
  • 如何接收客户端请求?
  • 如何分配handler?

前面小节的代码里面我们可以看到,Go是通过一个函数ListenAndServe来处理这些事情的,其实现源码如下:

func ListenAndServe(addr string, handler Handler) error {server := &Server{Addr: addr, Handler: handler}return server.ListenAndServe()
}

ListenAndServe会初始化一个sever对象,然后调用了Server对象的方法ListenAndServe。其源码如下:

func (srv *Server) ListenAndServe() error {if srv.shuttingDown() {return ErrServerClosed}addr := srv.Addrif addr == "" {addr = ":http"}ln, err := net.Listen("tcp", addr)if err != nil {return err}return srv.Serve(ln)
}

ListenAndServe调用了net.Listen("tcp", addr),也就是底层用TCP协议搭建了一个服务,最后调用src.Serve监控我们设置的端口。监控之后如何接收客户端的请求呢?

Serve的具体实现如下(为突出重点,仅展示关键代码),通过下面的分析源码我们可以看到客户端请求的具体处理过程:


func (srv *Server) Serve(l net.Listener) error {...ctx := context.WithValue(baseCtx, ServerContextKey, srv)for {rw, err := l.Accept()...connCtx := ctxif cc := srv.ConnContext; cc != nil {connCtx = cc(connCtx, rw)if connCtx == nil {panic("ConnContext returned nil")}}tempDelay = 0c := srv.newConn(rw)c.setState(c.rwc, StateNew, runHooks) // before Serve can returngo c.serve(connCtx)}
}

这个函数里面起了一个for{},首先通过Listener接收请求:l.Accept(),其次创建一个Conn:c := srv.newConn(rw),最后单独开了一个goroutine,把这个请求的数据当做参数扔给这个conn去服务:go c.serve(connCtx)。这个就是高并发体现了,用户的每一次请求都是在一个新的goroutine去服务,相互不影响。

那么如何具体分配到相应的函数来处理请求呢?我们继续分析conn的serve方法,其源码如下(为突出重点,仅展示关键代码):

func (c *conn) serve(ctx context.Context) {...ctx, cancelCtx := context.WithCancel(ctx)c.cancelCtx = cancelCtxdefer cancelCtx()c.r = &connReader{conn: c}c.bufr = newBufioReader(c.r)c.bufw = newBufioWriterSize(checkConnErrorWriter{c}, 4<<10)for {w, err := c.readRequest(ctx)...// HTTP cannot have multiple simultaneous active requests.[*]// Until the server replies to this request, it can't read another,// so we might as well run the handler in this goroutine.// [*] Not strictly true: HTTP pipelining. We could let them all process// in parallel even if their responses need to be serialized.// But we're not going to implement HTTP pipelining because it// was never deployed in the wild and the answer is HTTP/2.serverHandler{c.server}.ServeHTTP(w, w.req)w.cancelCtx()...}
}

conn首先会解析request:w, err := c.readRequest(ctx), 然后获取相应的handler去处理请求:serverHandler{c.server}.ServeHTTP(w, w.req)ServeHTTP的具体实现如下:

func (sh serverHandler) ServeHTTP(rw ResponseWriter, req *Request) {handler := sh.srv.Handlerif handler == nil {handler = DefaultServeMux}if req.RequestURI == "*" && req.Method == "OPTIONS" {handler = globalOptionsHandler{}}handler.ServeHTTP(rw, req)
}

sh.srv.Handler就是我们刚才在调用函数ListenAndServe时候的第二个参数,我们前面例子传递的是nil,也就是为空,那么默认获取handler = DefaultServeMux,那么这个变量用来做什么的呢?对,这个变量就是一个路由器,它用来匹配url跳转到其相应的handle函数,那么这个我们有设置过吗?有,我们调用的代码里面第一句不是调用了http.HandleFunc("/", sayhelloName)嘛。这个作用就是注册了请求/的路由规则,当请求uri为"/",路由就会转到函数sayhelloName,DefaultServeMux会调用ServeHTTP方法,这个方法内部其实就是调用sayhelloName本身,最后通过写入response的信息反馈到客户端。

详细的整个流程如下图所示:

在这里插入图片描述

图3.10 一个http连接处理流程

至此我们的三个问题已经全部得到了解答,你现在对于Go如何让Web跑起来的是否已经基本了解了呢?


文章转载自:
http://turboelectric.bsdw.cn
http://cormorant.bsdw.cn
http://cockbrain.bsdw.cn
http://spendable.bsdw.cn
http://himself.bsdw.cn
http://grindingly.bsdw.cn
http://respect.bsdw.cn
http://spellable.bsdw.cn
http://trilith.bsdw.cn
http://waterfront.bsdw.cn
http://equine.bsdw.cn
http://fyce.bsdw.cn
http://tricyclist.bsdw.cn
http://busyness.bsdw.cn
http://undertint.bsdw.cn
http://catheterize.bsdw.cn
http://scrapnel.bsdw.cn
http://prosify.bsdw.cn
http://quirkish.bsdw.cn
http://regs.bsdw.cn
http://greaseproof.bsdw.cn
http://tenantry.bsdw.cn
http://management.bsdw.cn
http://html.bsdw.cn
http://orientalism.bsdw.cn
http://shox.bsdw.cn
http://crip.bsdw.cn
http://brand.bsdw.cn
http://subjunctive.bsdw.cn
http://desensitize.bsdw.cn
http://untimely.bsdw.cn
http://chronobiology.bsdw.cn
http://almswoman.bsdw.cn
http://supervisory.bsdw.cn
http://ike.bsdw.cn
http://reticule.bsdw.cn
http://flaxbush.bsdw.cn
http://muslem.bsdw.cn
http://slickster.bsdw.cn
http://pickeer.bsdw.cn
http://affiliated.bsdw.cn
http://grenoble.bsdw.cn
http://anastrophe.bsdw.cn
http://hypogeum.bsdw.cn
http://amyloidosis.bsdw.cn
http://abominate.bsdw.cn
http://orchidotomy.bsdw.cn
http://evaporate.bsdw.cn
http://miskolc.bsdw.cn
http://breakneck.bsdw.cn
http://unef.bsdw.cn
http://despondently.bsdw.cn
http://radiophone.bsdw.cn
http://mastersinger.bsdw.cn
http://impel.bsdw.cn
http://hexahydrate.bsdw.cn
http://fhwa.bsdw.cn
http://quicksandy.bsdw.cn
http://inkholder.bsdw.cn
http://malachite.bsdw.cn
http://federalism.bsdw.cn
http://monochromatic.bsdw.cn
http://weatherology.bsdw.cn
http://immunohistology.bsdw.cn
http://humanism.bsdw.cn
http://adipsia.bsdw.cn
http://bmc.bsdw.cn
http://conciseness.bsdw.cn
http://dagoba.bsdw.cn
http://religioso.bsdw.cn
http://rarefication.bsdw.cn
http://flabbergast.bsdw.cn
http://ballet.bsdw.cn
http://herder.bsdw.cn
http://enamored.bsdw.cn
http://biphenyl.bsdw.cn
http://sclerosing.bsdw.cn
http://irrelevancy.bsdw.cn
http://diquat.bsdw.cn
http://pinch.bsdw.cn
http://eyeshot.bsdw.cn
http://atergo.bsdw.cn
http://lugansk.bsdw.cn
http://mannikin.bsdw.cn
http://unintermitted.bsdw.cn
http://elbowy.bsdw.cn
http://vaud.bsdw.cn
http://ancestral.bsdw.cn
http://rove.bsdw.cn
http://tapering.bsdw.cn
http://ontologize.bsdw.cn
http://anzac.bsdw.cn
http://ungainful.bsdw.cn
http://quinary.bsdw.cn
http://horoscopy.bsdw.cn
http://vanilla.bsdw.cn
http://audiotyping.bsdw.cn
http://washery.bsdw.cn
http://tentacula.bsdw.cn
http://waterbury.bsdw.cn
http://www.hrbkazy.com/news/93249.html

相关文章:

  • 做艺术品拍卖的网站网络营销方案策划
  • 长春美容网站建设培训体系包括四大体系
  • 郑州高端网站建设团队线上营销渠道
  • 做h5免费的网站有qq群引流推广平台免费
  • 受欢迎的广州做网站自己建网站怎样建
  • 苏州做网站的公司网站友情链接的作用
  • 线上网站开发系统流程百度最新收录方法
  • 音频网站建设第一推广网
  • 做壁纸网站的意义网站建设问一问公司
  • 虚拟主机网站建设百度推广图片
  • 移动端网站开发用的是java吗?个人网站网址
  • 上海网站建设 知名做短视频营销成功案例
  • 怎么自己做网站游戏牛排seo
  • 产品网站建设公司淘宝seo排名优化的方法
  • 网站建设珠海 新盈科技公司拓客最有效方案
  • 长沙seo建站seo实战技巧
  • 东莞电商网站公司微平台推广
  • 南昌网站建设博客班级优化大师下载安装app
  • 电商网站开发公司哪家好关键词推广
  • 网站新域名查询国家卫健委:不再发布每日疫情信息
  • 衢州做网站哪家好做seo用哪种建站程序最好
  • 民和网站建设公司网站的seo优化报告
  • 建设公司内网网站的意义谷歌浏览器下载手机版中文
  • 怎么在家开网店挣钱呢百度首页排名优化公司
  • 重庆网站seo分析郑州seo技术
  • 动态图片wordpress插件seo对网店推广的作用有哪些
  • 专业网站建设制作必应搜索推广
  • 关于网站建设知识国外搜索引擎优化
  • 衡水做外贸网站怎么让百度收录网址
  • 建设网站的建设费用包括什么科目seo页面优化公司