1. micro
1.1 获取 go-micro
go get - u github.com/micro/go-micro
1.2 获取 micro 的插件
go get github.com/micro/go-plugins
1.3 入门代码 demo
使用 web.NewService() 参数是我们的l地址加端口(我用的本地就把地址忽略了),返回 web.Service。
使用web.Service 的HandleFunc() 写一个 HTTP 请求。
最后使用web.Service 的Run() 启动。
启动成功后在浏览器访问 http://127.0.0.1:8082/ ,会看到 hello。
package main
import (
"net/http"
"github.com/micro/go-micro/web"
)
func main() {
service := web.NewService(web.Address(":8082"))
service.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hello"))
})
service.Run()
}
2. micro 整合 gin
2.1 代码 demo
使用 gin 写一个请求。
在web.NewService()加一个参数 web.Handler()。
启动成功后在浏览器访问 http://127.0.0.1:8082/hello ,会看到 {"code":200,"message":"hello"}。
package main
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/micro/go-micro/web"
)
func main() {
r := gin.Default()
r.Handle(http.MethodGet, "/hello", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"code": 200,
"message": "hello",
})
})
service := web.NewService(web.Address(":8082"), web.Handler(r))
service.Run()
}
3. micro 将服务注册到 consul
3.1 代码demo
使用 consul.NewRegistry() 参数是 registry.Addrs() 将consul 的地址放进去。
在 web.NewService() 中增加参数web.Registry() 将 生成的 registry 放进去,web.Name() 。
启动后可以在 consul 的 UI 中选择 services 看到 test。
package main
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/micro/go-micro/registry"
"github.com/micro/go-micro/web"
"github.com/micro/go-plugins/registry/consul"
)
func main() {
regi := consul.NewRegistry(registry.Addrs("127.0.0.1:8500"))
r := gin.Default()
r.Handle(http.MethodGet, "/hello", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"code": 200,
"message": "hello",
})
})
service := web.NewService(web.Name("test"), web.Address(":8082"), web.Handler(r), web.Registry(regi))
service.Run()
}
4.从 consul 中获取服务信息
package main
import (
"fmt"
"github.com/micro/go-micro/client/selector"
"github.com/micro/go-micro/registry"
"github.com/micro/go-plugins/registry/consul"
)
func main() {
registry := consul.NewRegistry(registry.Addrs("127.0.0.1:8500"))
service, err := registry.GetService("test")
if err != nil {
fmt.Printf("get service err %v", err)
return
}
//随机方式,返回的 next 是一个没有参数的函数
next := selector.Random(service)
//node 里面有 服务的 地址 ID 元数据
node, err := next()
fmt.Printf("Address:%v , Id:%v ,Metadata:%v\n", node.Address, node.Id, node.Metadata)
}
5. 使用 原生 http 调用服务
package main
import (
"fmt"
"io/ioutil"
"net/http"
"github.com/micro/go-micro/client/selector"
"github.com/micro/go-micro/registry"
"github.com/micro/go-plugins/registry/consul"
)
//callAPI callAPI
func callAPI(url string) []byte {
fmt.Printf("url:%s\n", url)
req, _ := http.NewRequest(http.MethodGet, url, nil)
client := http.DefaultClient
resp, _ := client.Do(req)
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Printf("err:%v\n", err)
}
return b
}
func main() {
registry := consul.NewRegistry(registry.Addrs("127.0.0.1:8500"))
service, err := registry.GetService("test")
if err != nil {
fmt.Printf("get service err %v", err)
return
}
next := selector.Random(service)
node, err := next()
fmt.Printf("Address:%v , Id:%v ,Metadata:%v\n", node.Address, node.Id, node.Metadata)
b := callAPI("http://" + node.Address + "/hello")
fmt.Printf("call /hello get: %s\n", string(b))
}