# 微服务调用
## rpc
- golang 版
```go
package main
import (
consulapi "github.com/hashicorp/consul/api"
"fmt"
"os"
"google.golang.org/grpc"
pb "micro-srv/service/kibana/proto"
"golang.org/x/net/context"
)
func main() {
config := consulapi.Config{
//consul作为服务发现
Address:"http://127.0.0.1:8500",
}
client, err := consulapi.NewClient(&config)//非默认情况下需要设置实际的参数
if err != nil {
fmt.Println(err.Error())
os.Exit(0)
}
//取到日志服务信息
services1, _, err := client.Catalog().Service("kibana", "", nil)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
//调用服务
sendData1(services1)
}
func sendData1(service []*consulapi.CatalogService) {
if len(service) > 0 {
//建立连接,TODO GO-Grpc微服务开发五 服务调用优化
conn, err := grpc.Dial(fmt.Sprintf("%s:%d", service[0].ServiceAddress, service[0].ServicePort), grpc.WithInsecure())
//初始化客户端
c := pb.NewKibanaClient(conn)
//发送的数据
request := &pb.WriteRequest{
Tag: "micro_test",
Info: "hello 5.45",
Level: "info",
}
//发起请求
r, err := c.Write(context.Background(), request)
fmt.Println(r, err)
}
}
```
源码:[go-grpc-getway](https://github.com/juelite/micro-srv)
有疑问加站长微信联系(非本文作者))