package core
import (
"log"
consulapi "github.com/hashicorp/consul/api"
)
func RegService() {
config := consulapi.DefaultConfig()
config.Address = "127.0.0.1:8500" //consul地址
reg := consulapi.AgentServiceRegistration{}
reg.Name = "ilocal-golang-server" //注册service的名字
reg.Address = "192.168.88.76" //注册service的ip
reg.Port = 9123 //注册service的端口
reg.Tags = []string{"primary"}
check := consulapi.AgentServiceCheck{} //创建consul的检查器
check.Interval = "5s" //设置consul心跳检查时间间隔
check.HTTP = "http://192.168.88.76:9123/actuator/health" //设置检查使用的url
reg.Check = &check
client, err := consulapi.NewClient(config) //创建客户端
if err != nil {
log.Fatal(err)
}
err = client.Agent().ServiceRegister(®)
if err != nil {
log.Fatal(err)
}
}
package main
import (
"ilocal-server/src/net/ilocal/controller"
"ilocal-server/src/net/ilocal/core"
"github.com/gin-gonic/gin"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
func main() {
r := gin.Default()
r.Any("/metrics", prometheusGinHandler)
user := r.Group("user")
user.GET("/login", controller.Login)
user.GET("/logout", controller.Logout)
r.GET("/actuator/health", func(c *gin.Context) {
c.JSON(200, gin.H{
"status": "UP",
})
})
core.RegService()
r.Run(":9123") // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}
func prometheusGinHandler(ctx *gin.Context) {
h := promhttp.Handler()
h.ServeHTTP(ctx.Writer, ctx.Request)
}
|