| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 | 
							- /*
 
-  * @description: rpc services 实例
 
-  * @Author: CP
 
-  * @Date: 2020-11-12 22:53:14
 
-  * @FilePath: \construction_management\services\rpc_service.go
 
-  */
 
- package services
 
- import (
 
- 	"context"
 
- 	"log"
 
- 	"time"
 
- 	safe "go.mod/proto"
 
- 	"google.golang.org/grpc"
 
- )
 
- type RpcService interface {
 
- 	Test(RpcConnect *grpc.ClientConn)
 
- }
 
- //返回service操作类
 
- type rpcService struct {
 
- 	// address     string
 
- 	// defaultName string
 
- 	//Ctx iris.Context
 
- 	//rpcClient *grpc.ClientConn
 
- 	// 定义proto 接口文件- /proto/rpc.proto
 
- 	// pb.UnimplementedGreeterServer
 
- }
 
- //创建项目用户service
 
- func NewRpcService() RpcService {
 
- 	return &rpcService{
 
- 		// address:     "192.168.1.26:5001",
 
- 		//rpcClient: GetGrpcClient(ctx),
 
- 	}
 
- }
 
- // func GetGrpcClient(ctx iris.Context) *grpc.ClientConn {
 
- // 	// // 启动grpc客户端,连接grpc服务端
 
- // 	// conn, err := grpc.Dial(address, grpc.WithInsecure())
 
- // 	// if err != nil {
 
- // 	// 	log.Fatalf("did not connect: %v", err)
 
- // 	// }
 
- // 	// // defer conn.Close()
 
- // 	// return conn
 
- // }
 
- // 具体的业务逻辑
 
- func (s *rpcService) Test(RpcConnect *grpc.ClientConn) {
 
- 	// 1.结束后关闭
 
- 	//defer s.rpcClient.Close()
 
- 	rpcClient := safe.NewGreeterClient(RpcConnect)
 
- 	// c := pb.NewGreeterClient(conn)
 
- 	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
 
- 	defer cancel()
 
- 	r, err := rpcClient.SayHello(ctx, &safe.HelloRequest{Name: "caipin"})
 
- 	if err != nil {
 
- 		log.Fatalf("could not greet: %v", err)
 
- 	}
 
- 	log.Printf("Greeting: %s", r.GetMessage())
 
- }
 
 
  |