GO-Grpc微服务开发三 服务调用for golang

栏目: Go · 发布时间: 7年前

内容简介:源码:

微服务调用

rpc

  • golang 版
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


以上所述就是小编给大家介绍的《GO-Grpc微服务开发三 服务调用for golang》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Introduction to Computation and Programming Using Python

Introduction to Computation and Programming Using Python

John V. Guttag / The MIT Press / 2013-7 / USD 25.00

This book introduces students with little or no prior programming experience to the art of computational problem solving using Python and various Python libraries, including PyLab. It provides student......一起来看看 《Introduction to Computation and Programming Using Python》 这本书的介绍吧!

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具