consul
This commit is contained in:
parent
b57a826749
commit
0ec33fa81b
|
@ -60,6 +60,7 @@ func Init(r *gin.Engine) {
|
||||||
|
|
||||||
// healthcheck
|
// healthcheck
|
||||||
r.GET("/healthcheck", Healthcheck)
|
r.GET("/healthcheck", Healthcheck)
|
||||||
|
r.GET("/timeout", Timeout)
|
||||||
|
|
||||||
// Hello World!
|
// Hello World!
|
||||||
helloWorldGroup := r.Group("/hello_world", Prepare())
|
helloWorldGroup := r.Group("/hello_world", Prepare())
|
||||||
|
|
|
@ -0,0 +1,16 @@
|
||||||
|
package controller
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"service/bizcommon/util"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Timeout(ctx *gin.Context) {
|
||||||
|
time.Sleep(time.Second * 6)
|
||||||
|
data := map[string]interface{}{
|
||||||
|
"time": time.Now().Unix(),
|
||||||
|
"time_str": util.GetTodayZeroTime().Format(time.RFC3339),
|
||||||
|
}
|
||||||
|
ReplyOk(ctx, data)
|
||||||
|
}
|
|
@ -12,6 +12,7 @@ func NewRouter() *gin.Engine {
|
||||||
router := gin.New()
|
router := gin.New()
|
||||||
router.Use(middleware.LoggerMiddleware(gin.LoggerConfig{}))
|
router.Use(middleware.LoggerMiddleware(gin.LoggerConfig{}))
|
||||||
router.Use(gin.RecoveryWithWriter(logger.Global.Out))
|
router.Use(gin.RecoveryWithWriter(logger.Global.Out))
|
||||||
|
router.Use(middleware.TimeoutMiddleware())
|
||||||
|
|
||||||
return router
|
return router
|
||||||
}
|
}
|
||||||
|
|
|
@ -94,8 +94,8 @@ func registerSD(ip string, port int) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func setServerStatusFD(status string) {
|
func setServerStatusFD(status string) {
|
||||||
serverStatusFdPath := "/Users/erwin/SERVER_STATUS_FD"
|
serverStatusFdPath := "/app/SERVER_STATUS_FD"
|
||||||
cmd := exec.Command("echo", fmt.Sprintf("%s > %s", status, serverStatusFdPath))
|
cmd := exec.Command("sh", "-c", fmt.Sprintf("echo %s > %s", status, serverStatusFdPath))
|
||||||
err := cmd.Run()
|
err := cmd.Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Info("run cmd fail, p: %v, err: %v", serverStatusFdPath, err)
|
logger.Info("run cmd fail, p: %v, err: %v", serverStatusFdPath, err)
|
||||||
|
|
|
@ -2,25 +2,27 @@ package middleware
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"service/api/base"
|
||||||
|
"service/api/consts"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gin-contrib/timeout"
|
"github.com/gin-contrib/timeout"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
func testResponse(c *gin.Context) {
|
func timeoutHandler(ctx *gin.Context) {
|
||||||
c.JSON(http.StatusGatewayTimeout, gin.H{
|
ctx.AbortWithStatusJSON(http.StatusGatewayTimeout, base.BaseResponse{
|
||||||
"code": http.StatusGatewayTimeout,
|
Ret: consts.RetCodeFail,
|
||||||
"msg": "timeout",
|
Msg: "出了个小问题,请稍后再试~",
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TimeoutMiddleware() gin.HandlerFunc {
|
func TimeoutMiddleware() gin.HandlerFunc {
|
||||||
return timeout.New(
|
return timeout.New(
|
||||||
timeout.WithTimeout(5000*time.Millisecond),
|
timeout.WithTimeout(3000*time.Millisecond),
|
||||||
timeout.WithHandler(func(c *gin.Context) {
|
timeout.WithHandler(func(c *gin.Context) {
|
||||||
c.Next()
|
c.Next()
|
||||||
}),
|
}),
|
||||||
timeout.WithResponse(testResponse),
|
timeout.WithResponse(timeoutHandler),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue