Implement a simple microservice project using Gin + GO-Micro + Consul to implement service registration and discovery functions
I. Introduction of dependencies:
- Gin Framework: Go get -u github.com/gin-gonic/gin
- go-micro: go get -u -v github.com/micro/micro
go get -u -v github.com/micro/go-micro - Consul: Windows version: www.consul.io/downloads.h… Unzip the download and add system variables
Ii. Write user microservice:
1. Project Catalog:
2. router.go:
import "github.com/gin-gonic/gin"
func InitRouters() *gin.Engine {
ginRouter := gin.Default()
ginRouter.POST("/users/", func(context *gin.Context) {
context.String(200, "get userInfo")
})
return ginRouter
}
Copy the code
3. main.go
import ( "gin_micro/userserver/routers" "github.com/micro/go-micro/registry" "github.com/micro/go-micro/web" "github.com/micro/go-plugins/registry/consul" ) var consulReg registry.Registry func init() { consulReg = Consul. NewRegistry (registry. Addrs (127.0.0.1: "8500"), ) } func main() { ginRouter := routers.InitRouters() microService := web.NewService( web.Name("userServer"), // Web.registerttl (30 * time.second),// Set the expiration time of the registration service // web.registerInterval (20 * time.second),// Set the interval at which to register the service again web.Address(":18001"), web.Handler(ginRouter), web.Registry(consulReg)) microService.Run() }Copy the code
Iii. Write order microservice:
1. Project Catalog:
2. router.go:
import "github.com/gin-gonic/gin" func InitRouters() *gin.Engine { ginRouter := gin.Default() ginRouter.POST("/orders/", func(ctx *gin.Context) { ctx.String(200, "get orderInfo") }) return ginRouter }Copy the code
3. main.go
import ( "bytes" "fmt" "gin_micro/orderserver/routers" "github.com/micro/go-micro/client/selector" "github.com/micro/go-micro/registry" "github.com/micro/go-micro/web" "github.com/micro/go-plugins/registry/consul" "net/http" "time" ) var consulReg registry.Registry func init() { consulReg = consul.NewRegistry( Registry.addrs ("127.0.0.1:8500"))} func main() {// Initialize ginRouter := Phones.initRouters () // Register service microService := web.NewService( web.Name("orderServer"), // Web.registerttl (time.second *30),// Set the expiration time of the registration service // web.registerInterval (time.second *20),// Set the interval at which to register the service again Web.address (":18002"), web.handler (ginRouter), web.registry (consulReg), HostAddress := GetServiceAddr("userServer") if Len (hostAddress) <= 0 {FMT.Println("hostAddress is null")} else { url := "http://" + hostAddress + "/users" response, _ := http.Post(url, "application/json; Charset = UTF-8 ", bytes.newBuffer ([]byte(""))) fmt.printf (" Discover service, response = %v\n", Func GetServiceAddr(serverName String) (address String) {var retryTimes int for { servers, err := consulReg.GetService(serverName) fmt.Println(servers) if err ! = nil { fmt.Println(err.Error()) } var services []*registry.Service for _, value := range servers { fmt.Println(value.Name, ":", value.Version) services = append(services, Next := selector.RoundRobin(services) if node, err := next(); Err == nil {address = node.address} if len(address) > 0 {return} // retryTimes++ retrytimes + time.sleep (1 * time.second) If retryTimes >= 5 {return}}}Copy the code
Iv. Start the project
1. Start consul: consul agent-dev-node HHH
2. Service registration
Start userServer /main.go and orderServer /main.go respectively
Publish userServer and orderServer on Consul
Enter localhost:8500\ UI in the browser to go to the Consul interface and view the published microservices:
3. Service discovery
Call the userServer microservice in orderServer /main.go and print the return information to the console