gin实例--提供http服务(2)

794 阅读1分钟

源码

github.com/zsl10/gin-e…

提供http服务

有以下方法提供http服务:

  1. 使用Gin默认服务器
router.Run()
  1. 使用go内置HTTP 服务器
  2. 使用http.Server内置的Shutdown方法来实现实现优雅退出、平滑重启的服务器。当服务被意外终止或中断,使用Shutdown可以优雅的终止服务,其不会断活跃连接,对现有请求连接处理并正常返回才正常退出服务。当服务被意外终止或中断,使用Shutdown可以优雅的终止服务,其不会断活跃连接,对现有请求连接处理并正常返回才正常退出服务。

使用Gin默认服务器

  • 运行:
go run server1.go
[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.

[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
 - using env:	export GIN_MODE=release
 - using code:	gin.SetMode(gin.ReleaseMode)

[GIN-debug] GET    /                         --> demo01/router.WebRoot (3 handlers)
[GIN-debug] POST   /                         --> demo01/router.WebRoot (3 handlers)
[GIN-debug] PUT    /                         --> demo01/router.WebRoot (3 handlers)
[GIN-debug] PATCH  /                         --> demo01/router.WebRoot (3 handlers)
[GIN-debug] HEAD   /                         --> demo01/router.WebRoot (3 handlers)
[GIN-debug] OPTIONS /                         --> demo01/router.WebRoot (3 handlers)
[GIN-debug] DELETE /                         --> demo01/router.WebRoot (3 handlers)
[GIN-debug] CONNECT /                         --> demo01/router.WebRoot (3 handlers)
[GIN-debug] TRACE  /                         --> demo01/router.WebRoot (3 handlers)
[GIN-debug] Listening and serving HTTP on :8080
  • 访问:
curl 127.0.0.1:8080
hello world