首页 > 其他分享 >go 使用grpc和grpcurl

go 使用grpc和grpcurl

时间:2024-09-07 10:14:35浏览次数:1  
标签:err grpc pb grpcurl go StudentService

安装依赖和工具

# ubuntu安装protobuf
apt install libprotobuf-dev protobuf-compiler protoc-gen-go protoc-gen-go-grpc -y
# 查看protobuf版本
protoc --version

# 安装grpcurl
wget https://github.com/fullstorydev/grpcurl/releases/download/v1.8.7/grpcurl_1.8.7_linux_x86_64.tar.gz
tar -zxvf grpcurl_1.8.7_linux_x86_64.tar.gz -C /usr/bin
rm -f /usr/bin/LICENSE
# 查看grpcurl版本
grpcurl --version

运行代码

目录结构

student.proto

syntax = "proto3";

package pb;

option go_package = "./pb";

message GetStudentRequest {
    string name = 1;
}

message GetStudentResponse {
    int32 age = 1;
}

service StudentService {
    rpc getStudent(GetStudentRequest) returns (GetStudentResponse) {};
}

server.go

package main

import (
	"context"
	"fmt"
	"net"

	"test/proto/pb"

	"google.golang.org/grpc"
	"google.golang.org/grpc/reflection"
)

type StudentService struct{}

func (s *StudentService) GetStudent(ctx context.Context, req *pb.GetStudentRequest) (*pb.GetStudentResponse, error) {
	return &pb.GetStudentResponse{Age: 10}, nil
}

func main() {
	listener, err := net.Listen("tcp", "127.0.0.1:1000")
	if err != nil {
		fmt.Println(err)
		return
	}
	defer listener.Close()

	grpcServer := grpc.NewServer()
	reflection.Register(grpcServer)
	pb.RegisterStudentServiceServer(grpcServer, &StudentService{})
	if err = grpcServer.Serve(listener); err != nil {
		fmt.Println(err)
	}
}

client.go

package main

import (
	"context"
	"fmt"

	"test/proto/pb"

	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
)

func main(){
	conn, err := grpc.Dial("127.0.0.1:1000", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		fmt.Println(err)
		return
	}
	defer conn.Close()

	client := pb.NewStudentServiceClient(conn)
	if response, err := client.GetStudent(context.Background(), &pb.GetStudentRequest{
		Name: "1",
	}); err != nil {
		fmt.Println(err)
		return
	} else {
		fmt.Printf("student age is %d", response.Age)
	}
}

生成pb对应的go文件

protoc student.proto --go_out=./ --go-grpc_out=./ --go-grpc_opt=require_unimplemented_servers=false

运行

使用grpcurl

查看服务列表

grpcurl -plaintext 127.0.0.1:1000 list

查看服务方法

grpcurl -plaintext localhost:1000 list pb.StudentService

查看服务方法详情

grpcurl -plaintext localhost:1000 describe pb.StudentService

查看服务方法参数类型

grpcurl -plaintext localhost:1000 describe pb.GetStudentRequest

调用服务方法

grpcurl -plaintext -d '{"name":""}' localhost:1000 pb.StudentService/getStudent

标签:err,grpc,pb,grpcurl,go,StudentService
From: https://www.cnblogs.com/WJQ2017/p/18401390

相关文章

  • Django
    Django1.创建项目1.1终端创建进入终端想将项目放在哪个目录,就进入哪个目录创建django项目(用django-admin.exe工具)#scripts已经配置好环境变量django-adminstartproject项目名1.2Pycharm创建注:项目文件位置不是解释器位置1.2.1说明:终端执行命令行得......
  • Go - Web Application 9
    UsingrequestcontextAtthemomentourlogicforauthenticatingauserconsistsofsimplycheckingwhethera "authenticatedUserID"valueexistsintheirsessiondata,likeso:func(app*application)isAuthenticated(r*http.Request)bool{......
  • 计算机毕业设计必看必学!! 07558 Django校园宿舍管理系统,原创定制程序, java、PHP、py
    摘要本论文主要论述了如何使用django开发一个校园宿舍管理系统,本系统将严格按照软件开发流程进行各个阶段的工作,采用B/S架构,面向对象编程思想进行项目开发。在引言中,作者将论述校园宿舍管理系统的当前背景以及系统开发的目的,后续章节将严格按照软件开发流程,对系统进行各......
  • How to create the Gold gold using RGB color values All In One
    HowtocreatetheGoldgoldusingRGBcolorvaluesAllInOne如何使用RGB颜色值创建金色Gold(Golden)ColorColorName: Gold(Golden)HexColorCode:#FFD700RGBColorCode:RGB(255,215,0)CMYKValues*:0.0%,15.7%,100.0%,0.0%ColorFamily(Hue):Yell......
  • django中间件(认证+权限)【示例】
    fromdjango.utils.deprecationimportMiddlewareMixinfromdjango.shortcutsimportredirect,HttpResponsefromdjango.urlsimportreversefromwwwimportmodelsclassAuthMiddleware(MiddlewareMixin):defprocess_request(self,request):ifreq......
  • Study Plan For Algorithms - Part23
    1.跳跃游戏II题目链接:https://leetcode.cn/problems/jump-game-ii/给定一个长度为n的0索引整数数组nums。初始位置为nums[0]。每个元素nums[i]表示从索引i向前跳转的最大长度:0<=j<=nums[i]i+j<n返回到达nums[n-1]的最小跳跃次数。classSolutio......
  • [Go]-抢购类业务方案
    文章目录要点:1.抢购/秒杀业务的关键挑战2.技术方案3.关键实现点4.性能优化建议5.其他考虑因素细节拆分:1.**高并发处理**2.**限流与防护**3.**库存控制**4.**异步处理**5.**数据一致性**6.**常用架构设计**7.**代码示例**8.进一步优化9.注意事项抢购类业......
  • 利用网站获取Google Play Store中应用安装包,拒绝“所在国家/地区未上线“
    网站获取GooglePlayStore我使用了很长一段时间,GooglePlayStore上有许多应用(如游戏CallofDuty:WarzoneMobile,工具AndFTP等)不允许所在国家/地区下载 注:网站仅能获取免费的应用 网站https://apk.support/apk-downloader(链接添不进去) 使用方法上面的搜索......
  • 计算机毕业设计django+vue车辆理赔系统【开题+论文+程序】
    本系统(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。系统程序文件列表开题报告内容研究背景随着汽车行业的迅猛发展,车辆保险已成为现代社会不可或缺的一部分。然而,传统的车辆理赔流程繁琐、效率低下,且常因信息不对称、处理周期长等......
  • Google Performance Max指南:优化广告的提示
    Google的PerformanceMax广告活动在三年多前结束测试阶段,已成为新手和有经验的营销人员中非常受欢迎的一种广告活动类型。在本文中,我们将详细解读GooglePerformanceMax广告活动是什么,它与其他广告类型的区别,优势和最佳实践,报告功能,广告优化技巧,以及Tinuiti的PMax方法。什么......