GO-Grpc微服务开发二 服务编写

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

内容简介:面板地址:http://127.0.0.1:8500

服务编写

1.定义proto文件

//示例为elc日志
syntax = "proto3";

//service name is kibana
service Kibana {
    //service method is write
    rpc Write(WriteRequest) returns (WriteResponse) {}
}

//writer request struct
message WriteRequest {
    string tag = 1;
    string info = 2;
    string level = 3;
}

//writer response struct
message WriteResponse {
    int32 code = 1;
    string message = 2;
    map<string, string> data = 3;
}

2.将定义的proto编译为 go 文件

protoc --proto_path=.:. --micro_out=. --go_out=. kibana.proto

3.编写服务

package main

import (
    proto "micro-srv/service/kibana/proto"
    "micro-srv/service/kibana/logics"
    "fmt"
    "micro-srv/common"
    "golang.org/x/net/context"
)

//定义服务
type Kibana struct{}

const (
	//服务监听端口
    SRV_PORT = 50061
    //服务名称
    SRV_NAME = "kibana"
)

//服务中的方法必须要全都实现
func (k *Kibana) Write(ctx context.Context, req *proto.WriteRequest) (rsp *proto.WriteResponse, err error) {
    rsp.Code = 0
    rsp.Message = "success"
    rsp.Data = map[string]string{"tag":req.Tag,"info":req.Info,"level":req.Level}
    err = logics.WriteLog(req.Tag, req.Info, req.Level)
    return rsp, err
}

func main() {
	//将服务注册到consul
    server, listener, err := common.Register(SRV_NAME, SRV_PORT)
    if err != nil {
        fmt.Println(err.Error())
    }
    proto.RegisterKibanaServer(server, &Kibana{})
    err = server.Serve(listener)
    if err != nil {
        fmt.Println(err.Error())
    }
}

微服务运行 (本地环境示例)

启动consul

consul agent -dev &

面板地址:http://127.0.0.1:8500

启动并注册服务

cd kibana && go run main.go & GO-Grpc微服务开发二 服务编写

可以看到我们服务以及运行起来了 接下来我们在consul面板看看 GO-Grpc微服务开发二 服务编写

源码: go-grpc-getway


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

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

微机原理与接口技术

微机原理与接口技术

李文英、李勤、刘星、宋蕴新 / 清华大学出版社 / 2001-9 / 26.00元

《高等院校计算机应用技术规划教材•应用型教材系列•微机原理与接口技术》是“高职高专计算机系列教材”之一。全书包括微机原理、汇编语言、接口技术三部分内容。微机原理部分讲述了80x86的内部结构及工作原理、半导体存储器及其系统、微型机总线结构等。汇编语言部分讲述了指令系统、编程技巧。接口技术部分讲述了中断系统、中断控制器、并行接口、串行接口、DMA控制器、定时器,以及A/D、D/A转换器等常用芯片的硬......一起来看看 《微机原理与接口技术》 这本书的介绍吧!

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具