[云原生] Go web工作流程

简介: [云原生] Go web工作流程

web工作流程

  • Web服务器的工作原理可以简单地归纳为
  • 客户机通过TCP/IP协议建立到服务器的TCP连接
  • 客户端向服务器发送HTTP协议请求包,请求服务器里的资源文档
  • 服务器向客户机发送HTTP协议应答包,如果请求的资源包含有动态语言的内容,那么服务器会调用动态语言的解释引擎负责处理“动态内容”,并将处理得到的数据返回给客户端
  • 客户机与服务器断开。由客户端解释HTML文档,在客户端屏幕上渲染图形结果

HTTP协议

  • 超文本传输协议(HTTP,HyperText Transfer Protocol)是互联网上应用最为广泛的一种网络协议,它详细规定了浏览器和万维网服务器之间互相通信的规则,通过因特网传送万维网文档的数据传送协议
  • HTTP协议通常承载于TCP协议之上
    talk is cheap , show me the code
package main

import (
  "fmt"
  "io/ioutil"
  "log"
  "net/http"
)

func main() {
  //http://127.0.0.1:8000/go
  // 单独写回调函数
  http.HandleFunc("/go", myHandler)
  // addr:监听的地址
  // handler:回调函数
  http.ListenAndServe("127.0.0.1:8000", nil)
}

// handler函数
func myHandler(w http.ResponseWriter, r *http.Request) {
  fmt.Println(r.RemoteAddr, "连接成功")
  // 请求方式:GET POST DELETE PUT UPDATE
  fmt.Println("method:", r.Method)
  // /go
  fmt.Println("url:", r.URL.Path)
  fmt.Println("header:", r.Header)
  fmt.Println("body:", r.Body)
  // 回复
  switch r.Method {
  case "GET":
    {
      w.Write([]byte("get"))
    }
  case "POST":
    {
      // 读取body内容
      content, err := ioutil.ReadAll(r.Body)
      if err != nil {
        log.Println("read post body occurs error: ", err)
      }
      fmt.Println("post body:", string(content))
      w.Write([]byte("post"))
    }
  default:
    {
      w.Write([]byte(r.Method))
    }
  }
  fmt.Println("end---")
}

package main

import (
  "encoding/json"
  "fmt"
  "io"
  "log"
  "net/http"
  "strings"
)

type Data struct {
  Name string `json:"name"`
  Age  int8   `json:"age"`
}

func main() {
  //resp, _ := http.Get("http://www.baidu.com")
  //fmt.Println(resp)
  resp, _ := http.Get("http://127.0.0.1:8000/go")
  ResponseHandler(resp)
  fmt.Println("----")

  data := &Data{
    Name: "hello",
    Age:  18,
  }

  // 转换成json格式
  data_json, err := json.Marshal(data)
  if err != nil {
    log.Println("json.Marshaler error: ", err)
  }
  fmt.Println(string(data_json))
  resp, _ = http.Post("http://127.0.0.1:8000/go", "application/json", strings.NewReader(string(data_json)))
  ResponseHandler(resp)
}

func ResponseHandler(resp *http.Response) {
  defer resp.Body.Close()
  // 200 OK

  fmt.Println(resp.Status)
  fmt.Println(resp.Header)

  buf := make([]byte, 1024)
  for {
    // 接收服务端信息
    n, err := resp.Body.Read(buf)
    if err != nil && err != io.EOF {
      fmt.Println(err)
      return
    } else {
      fmt.Println("读取完毕")
      res := string(buf[:n])
      fmt.Println(res)
      break
    }
  }
}

结果

> go run .\server.go
127.0.0.1:54856 连接成功
method: GET
url: /go
header: map[Accept-Encoding:[gzip] User-Agent:[Go-http-client/1.1]]
body: {}
end---
127.0.0.1:54856 连接成功
method: POST
url: /go
header: map[Accept-Encoding:[gzip] Content-Length:[25] Content-Type:[application/json] User-Agent:[Go-http-client/1.1]]
body: &{0xc0000b6048 <nil> <nil> false true {0 0} false false false 0x475080}
post body: {"name":"hello","age":18}
end---

> go run .\client.go
200 OK
map[Content-Length:[3] Content-Type:[text/plain; charset=utf-8] Date:[Mon, 25 Apr 2022 17:17:31 GMT]]
读取完毕
get
----
{"name":"hello","age":18}
200 OK
map[Content-Length:[4] Content-Type:[text/plain; charset=utf-8] Date:[Mon, 25 Apr 2022 17:17:31 GMT]]
读取完毕
post

http handler的格式模板

// HandleFunc registers the handler function for the given pattern
// in the DefaultServeMux.
// The documentation for ServeMux explains how patterns are matched.
func HandleFunc(pattern string, handler func(ResponseWriter, *Request)) {
  DefaultServeMux.HandleFunc(pattern, handler)
}
相关文章
|
7天前
|
Go
go创建web服务
go创建web服务
19 4
|
19天前
|
存储 监控 Cloud Native
云原生日志处理流程
【6月更文挑战第14天】云原生平台中的日志处理包括9个步骤:收集、ETL、索引、存储、检索、关联、可视化、分析和报告。
|
22天前
|
存储 前端开发 中间件
Go Web 开发 Demo【用户登录、注册、验证】(3)
Go Web 开发 Demo【用户登录、注册、验证】
|
22天前
|
前端开发 数据库连接 Go
Go Web 开发 Demo【用户登录、注册、验证】(1)
Go Web 开发 Demo【用户登录、注册、验证】
|
2月前
|
数据采集 Web App开发 Go
Go语言与chromedp结合:实现Instagram视频抓取的完整流程
使用Go语言和chromedp库,本文展示了如何抓取Instagram的视频文件,同时通过代理IP保障爬虫稳定和隐私。步骤包括安装chromedp、配置代理(如亿牛云),创建Chrome会话,导航至Instagram,提取视频URL,然后下载视频。关键操作有设置代理服务器、启动Chrome会话、抓取和下载视频。提供的代码示例详细解释了实现过程,有助于开发者学习Instagram数据采集。
Go语言与chromedp结合:实现Instagram视频抓取的完整流程
|
22天前
|
JSON 前端开发 Java
Go Web 开发 Demo【用户登录、注册、验证】(4)
Go Web 开发 Demo【用户登录、注册、验证】
|
22天前
|
Go 数据库
Go Web 开发 Demo【用户登录、注册、验证】(2)
Go Web 开发 Demo【用户登录、注册、验证】
|
2月前
|
Kubernetes Cloud Native Go
Golang深入浅出之-Go语言中的云原生开发:Kubernetes与Docker
【5月更文挑战第5天】本文探讨了Go语言在云原生开发中的应用,特别是在Kubernetes和Docker中的使用。Docker利用Go语言的性能和跨平台能力编写Dockerfile和构建镜像。Kubernetes,主要由Go语言编写,提供了方便的客户端库与集群交互。文章列举了Dockerfile编写、Kubernetes资源定义和服务发现的常见问题及解决方案,并给出了Go语言构建Docker镜像和与Kubernetes交互的代码示例。通过掌握这些技巧,开发者能更高效地进行云原生应用开发。
89 1
|
2月前
|
负载均衡 Go 调度
使用Go语言构建高性能的Web服务器:协程与Channel的深度解析
在追求高性能Web服务的今天,Go语言以其强大的并发性能和简洁的语法赢得了开发者的青睐。本文将深入探讨Go语言在构建高性能Web服务器方面的应用,特别是协程(goroutine)和通道(channel)这两个核心概念。我们将通过示例代码,展示如何利用协程处理并发请求,并通过通道实现协程间的通信和同步,从而构建出高效、稳定的Web服务器。
|
2月前
|
缓存 监控 测试技术
【Go语言专栏】使用Go语言构建高性能Web服务
【4月更文挑战第30天】本文探讨了使用Go语言构建高性能Web服务的策略,包括Go语言在并发处理和内存管理上的优势、基本原则(如保持简单、缓存和并发控制)、标准库与第三方框架的选择、编写高效的HTTP处理器、数据库优化以及性能测试和监控。通过遵循最佳实践,开发者可以充分利用Go语言的特性,构建出高性能的Web服务。