1,使用micro注册到consul
1.1 新建config.go
package config
type Config struct {
ConsulUrl string
Host string
ServerName string
}
func InitConf() *Config {
return &Config{
ConsulUrl: "127.0.0.1:8500",
Host: ":8081",
ServerName:"go_micro_01",
}
}
1.2,新建 main.go
package main
import (
"github.com/gin-gonic/gin"
"github.com/micro/go-micro/v2/registry"
"github.com/micro/go-micro/v2/web"
"github.com/micro/go-plugins/registry/consul/v2"
"go_micro/src/config"
)
func main() {
configInfo := config.InitConf()
comsulReg := consul.NewRegistry(
registry.Addrs(configInfo.ConsulUrl))
router := gin.Default();
router.GET("/info", func(context *gin.Context) {
context.String(200,"hello micro")
})
service := web.NewService(
web.Name(configInfo.ServerName),
web.Address(configInfo.Host),
web.Handler(router),
web.Registry(comsulReg),
)
service.Run()
}
1.3,测试
2,微服务之间的调用
2.1,创建服务生产者prod_main_8001.go
package main
import (
"github.com/gin-gonic/gin"
"github.com/micro/go-micro/v2/registry"
"github.com/micro/go-micro/v2/web"
"github.com/micro/go-plugins/registry/consul/v2"
"go_micro/src/entity"
"go_micro/src/model"
)
func main() {
comsulReg := consul.NewRegistry(
registry.Addrs("127.0.0.1:8500"))
router := gin.Default();
v1Router := router.Group("/prod")
{
v1Router.POST("/getPro", func(context *gin.Context) {
context.JSON(200,gin.H{
"data":model.NewProdList(5),
"host":8001,
"status":true,
})
})
v1Router.POST("/getProMap", func(context *gin.Context) {
prod := &entity.ProdRto{}
err := context.ShouldBind(&prod)
if err == nil {
context.JSON(200,gin.H{
"data":model.NewProdList(prod.Size),
"host":8001,
"status":true,
})
} else {
context.JSON(200,gin.H{
"data":"调用失败",
"host":8001,
"status":true,
})
}
})
}
service := web.NewService(
web.Name("prodServer"),
web.Address(":8001"),
web.Handler(router),
web.Registry(comsulReg),
web.Metadata(map[string]string{"protocol" : "http"}),
)
service.Init()
service.Run()
}
2.2,创建服务生产者prod_main_8002.go
package main
import (
"github.com/gin-gonic/gin"
"github.com/micro/go-micro/v2/registry"
"github.com/micro/go-micro/v2/web"
"github.com/micro/go-plugins/registry/consul/v2"
"go_micro/src/entity"
"go_micro/src/model"
)
func main() {
comsulReg := consul.NewRegistry(
registry.Addrs("127.0.0.1:8500"))
router := gin.Default();
v1Router := router.Group("/prod")
{
v1Router.POST("/getPro", func(context *gin.Context) {
context.JSON(200,gin.H{
"data":model.NewProdList(5),
"host":8002,
"status":true,
})
})
v1Router.POST("/getProMap", func(context *gin.Context) {
prod := &entity.ProdRto{}
err := context.ShouldBind(&prod)
if err == nil {
context.JSON(200,gin.H{
"data":model.NewProdList(prod.Size),
"host":8002,
"status":true,
})
} else {
context.JSON(200,gin.H{
"data":"调用失败",
"host":8002,
"status":true,
})
}
})
}
service := web.NewService(
web.Name("prodServer"),
web.Address(":8002"),
web.Handler(router),
web.Registry(comsulReg),
web.Metadata(map[string]string{"protocol" : "http"}),
)
service.Run()
}
2.3,创建服务生产者prod_main_8003.go
package main
import (
"github.com/gin-gonic/gin"
"github.com/micro/go-micro/v2/registry"
"github.com/micro/go-micro/v2/web"
"github.com/micro/go-plugins/registry/consul/v2"
"go_micro/src/entity"
"go_micro/src/model"
)
func main() {
comsulReg := consul.NewRegistry(
registry.Addrs("127.0.0.1:8500"))
router := gin.Default();
v1Router := router.Group("/prod")
{
v1Router.POST("/getPro", func(context *gin.Context) {
context.JSON(200,gin.H{
"data":model.NewProdList(5),
"host":8003,
"status":true,
})
})
v1Router.POST("/getProMap", func(context *gin.Context) {
prod := &entity.ProdRto{}
err := context.ShouldBind(&prod)
if err == nil {
context.JSON(200,gin.H{
"data":model.NewProdList(prod.Size),
"host":8003,
"status":true,
})
} else {
context.JSON(200,gin.H{
"data":"调用失败",
"host":8003,
"status":true,
})
}
})
}
service := web.NewService(
web.Name("prodServer"),
web.Address(":8003"),
web.Handler(router),
web.Registry(comsulReg),
web.Metadata(map[string]string{"protocol" : "http"}),
)
service.Run()
}
2.4,创建服务调用者test_main_8004.go
package main
import (
"github.com/gin-gonic/gin"
"github.com/micro/go-micro/v2/client/selector"
"github.com/micro/go-micro/v2/registry"
"github.com/micro/go-micro/v2/web"
"github.com/micro/go-plugins/registry/consul/v2"
"io/ioutil"
"log"
"net/http"
)
func main() {
comsulReg := consul.NewRegistry(
registry.Addrs("127.0.0.1:8500"))
getServer,err := comsulReg.GetService("prodServer")
if(err != nil){
log.Fatal(err)
}
router := gin.Default();
v1Router := router.Group("/text")
{
v1Router.GET("/random", func(context *gin.Context) {
resMap:= make(map[int]string,10)
for i:=0;i<10 ;i++ {
next := selector.Random(getServer)
node,_ := next()
resMap[i] = node.Address
}
context.JSON(200,gin.H{
"data":resMap,
})
})
v1Router.GET("/roundRobin", func(context *gin.Context) {
resMap:= make(map[int]string,10)
for i:=0;i<10 ;i++ {
next := selector.RoundRobin(getServer)
node,_ := next()
resMap[i] = node.Address
}
context.JSON(200,gin.H{
"data":resMap,
})
})
v1Router.GET("/sendApi", func(context *gin.Context) {
next := selector.RoundRobin(getServer)
node,_ := next()
addr := node.Address
res,err:=sendApi(addr,"/prod/getPro","POST")
if(err != nil){
context.JSON(200,gin.H{
"data":err,
})
} else {
context.JSON(200,gin.H{
"data":res,
})
}
})
}
service := web.NewService(
web.Name("textServer"),
web.Address(":8004"),
web.Handler(router),
web.Registry(comsulReg),
)
service.Run()
}
func sendApi(addr string, path string,method string)(string,error) {
req,_ := http.NewRequest(method,"http://" + addr + path,nil)
client := http.DefaultClient
res,err := client.Do(req);
if(err != nil){
return "",err
}
defer res.Body.Close()
buffer,_ := ioutil.ReadAll(res.Body)
return string(buffer),nil
}
2.4,创建服务调用者test_main_8004.go
package main
import (
"github.com/gin-gonic/gin"
"github.com/micro/go-micro/v2/client/selector"
"github.com/micro/go-micro/v2/registry"
"github.com/micro/go-micro/v2/web"
"github.com/micro/go-plugins/registry/consul/v2"
"go_micro/src/entity"
httpUtils "go_micro/src/utils"
)
func main() {
comsulReg := consul.NewRegistry(registry.Addrs("127.0.0.1:8500"))
selector := selector.NewSelector(
selector.Registry(comsulReg),
selector.SetStrategy(selector.RoundRobin),
)
router := gin.Default();
routerV1 := router.Group("/prod")
{
routerV1.GET("/getPro", func(i *gin.Context) {
resps,_ := httpUtils.SendRequestNotParam(selector,"/prod/getPro")
i.JSON(200,gin.H{
"DATA":resps,
});
})
routerV1.POST("/getProMap", func(context *gin.Context) {
prod := &entity.ProdRto{}
context.ShouldBind(&prod)
resps,_ := httpUtils.SendRequestHaveParamMap(selector,"/prod/getProMap",&prod)
context.JSON(200,gin.H{
"DATA":resps,
});
})
}
server := web.NewService(
web.Address(":8005"),
web.Name("testMain8005"),
web.Handler(router),
web.Registry(comsulReg),
)
server.Run()
}