python实现微服务器_go-python微服务初尝(go作客户端,python作服务端)

本文介绍了如何使用 gRPC 实现 Go 作为客户端,Python 作为服务端的通信。主要内容包括编写 `hello.proto` 文件,使用 protoc 编译生成 Go 和 Python 代码,以及分别创建 Python 服务端和 Go 客户端进行交互。服务端启动在 50051 端口,客户端通过调用 `SayHello` 方法获取 "hello + name" 的回复。
摘要由CSDN通过智能技术生成

介绍

本文讲述如何使用 grpc,由 go 作为客户端,python 作为服务端进行通信。

(题外:一直迷惑于怎样让他们两个连起来,后来才发现只要对同一个proto文件进行编译就好了。。。😓)

实现功能

python 实现方法 f(name) ,返回 "hello "+name,由 go 调用得到返回值

安装配置

Go

个人配置是 go 1.12 ,使用 go mod 项目管理

因为有些包会被墙,所有要配置GOPROXY,我配置的是阿里的GOPROXY="https://mirrors.aliyun.com/goproxy/"

安装 grpc,protobuf编译器和对应的 go 插件

go get google.golang.org/grpc

go get github.com/golang/protobuf/proto

go get github.com/golang/protobuf/proto-gen-go

注:如果在 goland 编译器里使用命令行也需要配置代理

python3

同样也是安装 grpc,protobuf等

pip3 install grpcio

pip3 install protobuf

pip3 install grpcio-tools

开始

我使用的是 goland 编译器,然后引入了 python 解释器

在红框内选择自己解释器就好

项目结构

本人初尝,可能不规范,敬请指正

-project

-go

-main.go

-micro

-hello.proto

-python

-server.py

这是所需要自己创建的目录和文件,go 包内即 go代码,micro是微服务配置文件,python包是python代码

micro包

首先创建proto文件--hello.proto

syntax = "proto3"; //选择版本

package micro; // 包

service Greeter {

rpc SayHello (HelloRequest) returns (HelloReply) {

}

}

message HelloRequest {

string name = 1;

}

message HelloReply {

string msg = 1;

}

注:这里package 要与当前路径一致

并没有很详细解释 proto 语法,大家又需要可以自行查看

编译 proto文件

首先命令行移动到 micro 包下,然后分别执行 go 和 python 的编译语句

go:hello.proto即为需要编译的文件,编译后会在当前包下生成 hello.pb.go文件

protoc --go_out=plugins=grpc:. hello.proto

python:编译后会生成hello_pb2.py和hello_pb2_grpc.py两个文件

python3 -m grpc_tools.protoc --python_out=. --grpc_python_out=. -I. hello.proto

文件都生成成功的话就可以编写客户端和服务端代码了

python服务端

在 python 包下 server.py 文件内编写如下代码

from concurrent import futures

import time

import grpc

from micro import hello_pb2

from micro import hello_pb2_grpc

class Greeter(hello_pb2_grpc.GreeterServicer):

def SayHello(self, request, context):

return hello_pb2.HelloReply(msg = "hello "+request.name)

def serve():

server = grpc.server(futures.ThreadPoolExecutor(max_workers = 10))

hello_pb2_grpc.add_GreeterServicer_to_server(Greeter(),server)

server.add_insecure_port('[::]:50051')

print("服务启动")

server.start()

try:

while True:

time.sleep(60*60*24)

except KeyboardInterrupt:

server.stop(0)

if __name__=='__main__':

serve()

创建了端口号为50051的服务

可以尝试启动一下服务

go 客户端

在go 包下 main.go 中编写下面代码

package main

import (

"context"

pb "cymdemo/micro"

"fmt"

"google.golang.org/grpc"

)

const address = "localhost:50051"

func main() {

conn,err := grpc.Dial(address,grpc.WithInsecure())

if err != nil {

fmt.Println(err)

}

defer conn.Close()

c := pb.NewGreeterClient(conn)

name := "world"

res,err := c.SayHello(context.Background(),&pb.HelloRequest{Name:name})

if err != nil{

fmt.Println(err)

}

fmt.Println(res.Msg)

}

先启动 python 服务端,然后启动 go 客户端就会拿到调用结果

hello world

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值