Compare commits
No commits in common. "a6725de634cf55ce1ce0edd71690ecca37e0ce74" and "078be130d7880d5477194898f8715edd5c364f9d" have entirely different histories.
a6725de634
...
078be130d7
4
go.mod
4
go.mod
@ -19,8 +19,6 @@ require (
|
||||
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/lighthouse v1.0.1134
|
||||
go.mongodb.org/mongo-driver v1.17.3
|
||||
golang.org/x/crypto v0.38.0
|
||||
google.golang.org/grpc v1.72.0
|
||||
google.golang.org/protobuf v1.36.6
|
||||
gopkg.in/yaml.v3 v3.0.1
|
||||
gorm.io/datatypes v1.2.5
|
||||
gorm.io/driver/mysql v1.5.7
|
||||
@ -114,6 +112,8 @@ require (
|
||||
golang.org/x/text v0.25.0 // indirect
|
||||
google.golang.org/genproto/googleapis/api v0.0.0-20250324211829-b45e905df463 // indirect
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20250324211829-b45e905df463 // indirect
|
||||
google.golang.org/grpc v1.72.0 // indirect
|
||||
google.golang.org/protobuf v1.36.6 // indirect
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
|
||||
gorm.io/driver/postgres v1.5.11 // indirect
|
||||
modernc.org/libc v1.22.5 // indirect
|
||||
|
@ -105,6 +105,7 @@ service HelloService {
|
||||
protoc -I=. --go-grpc_out=. --go-grpc_opt=module="122.51.31.227/go-course/go18" skills/rpc/protobuf/hello_service/interface.proto
|
||||
```
|
||||
|
||||
|
||||
interface_grpc.pb.go
|
||||
```go
|
||||
// 服务端接口
|
||||
|
@ -1,40 +1,3 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"net"
|
||||
|
||||
"122.51.31.227/go-course/go18/skills/rpc/protobuf/hello_service"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 首先是通过grpc.NewServer()构造一个gRPC服务对象
|
||||
grpcServer := grpc.NewServer()
|
||||
|
||||
// SDK 提供 服务实现对象的注册
|
||||
hello_service.RegisterHelloServiceServer(grpcServer, &HelloService{})
|
||||
|
||||
lis, err := net.Listen("tcp", ":1234")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// 然后通过grpcServer.Serve(lis)在一个监听端口上提供gRPC服务
|
||||
grpcServer.Serve(lis)
|
||||
}
|
||||
|
||||
var _ hello_service.HelloServiceServer = (*HelloService)(nil)
|
||||
|
||||
// 实现一个GRPC的对象, 并行实现了 HelloServiceServer 接口
|
||||
// 该对象就可以注册给GRPC框架
|
||||
type HelloService struct {
|
||||
hello_service.UnimplementedHelloServiceServer
|
||||
}
|
||||
|
||||
func (h *HelloService) Hello(ctx context.Context, req *hello_service.Request) (*hello_service.Response, error) {
|
||||
return &hello_service.Response{
|
||||
Value: "Hello " + req.Value,
|
||||
}, nil
|
||||
}
|
||||
func main() {}
|
||||
|
@ -1,30 +1,5 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"122.51.31.227/go-course/go18/skills/rpc/protobuf/hello_service"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// grpc.Dial负责和gRPC服务建立链接
|
||||
conn, err := grpc.NewClient("localhost:1234", grpc.WithTransportCredentials(insecure.NewCredentials()))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
// 使用SDK调用远程函数
|
||||
helloServiceClient := hello_service.NewHelloServiceClient(conn)
|
||||
resp, err := helloServiceClient.Hello(context.Background(), &hello_service.Request{
|
||||
Value: "bob",
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println(resp.Value)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user