python代码:consul_test.py
import requests
headers = {
"contentType": "application/json"
}
def register(name, id, address, port):
url = "http://192.168.31.144:8500/v1/agent/service/register"
rsp = requests.put(url, headers=headers, json={
"Name": name,
"ID": id,
"Tags": ["mxshop", "bobby", "imooc", "web"],
"Address": address,
"Port": port,
"Check": {
"HTTP": f"http://{address}:{port}/health",
"Timeout": "5s",
"Interval": "5s",
"DeregisterCriticalServiceAfter": "15s",
}
})
if rsp.status_code == 200:
print("注册成功")
else:
print(f"注册失败:{rsp.status_code}")
def deregister(id):
url = f"http://192.168.31.144:8500/v1/agent/service/deregister/{id}"
rsp = requests.put(url, headers=headers)
if rsp.status_code == 200:
print("注销成功")
else:
print(f"注销失败:{rsp.status_code}")
if __name__ == "__main__":
register("mxshop-web", "mxshop-web", "192.168.31.102", 8021)
user-web路由初始化代码:
package initialize
import (
"github.com/gin-gonic/gin"
middlewares "mxshop-api/user-web/middleware"
"mxshop-api/user-web/router"
"net/http"
)
func Routers() *gin.Engine {
Router := gin.Default()
Router.GET("/health", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"code": http.StatusOK,
"success": true,
})
})
Router.Use(middlewares.Cors())
ApiGroup := Router.Group("/u/v1")
router.InitUserRouter(ApiGroup)
router.InitBaseRouter(ApiGroup)
return Router
}
在运行consul_test后怎么都不通过,在docker的consul容器里测试过了192.168.31.102的主机能ping通,在主机浏览器也试过192.168.31.102:8021/health的get请求能返回数据,真的死活都搞不明白为什么consul的健康检查不通过,请问老师您觉得是版本问题吗?可是我看了版本号,consul版本号是v1.11.1,是个旧版本,按说不应该啊,老师,您能帮忙分析一下吗?或者我能加您qq或微信单独沟通一下吗?可以的话要怎么加您qq或微信?