欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用gRPC實(shí)現(xiàn)獲取數(shù)據(jù)庫(kù)版本

 更新時(shí)間:2023年12月26日 09:08:29   作者:242030  
這篇文章主要為大家詳細(xì)介紹了如何使用gRPC實(shí)現(xiàn)獲取數(shù)據(jù)庫(kù)版本,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

這里我們演示一個(gè)通過(guò) gRPC 獲取數(shù)據(jù)庫(kù)版本的案例。

1、新建proto

syntax = "proto3";
package pb;
import "google/protobuf/empty.proto";

service DataBase {
    rpc GetDataBaseVersion(google.protobuf.Empty) returns(VersionResponse) {}
}

message VersionResponse {
    string version = 1;
}

編譯:

$ protoc --gogo_out=plugins=grpc:./ database.proto

2、新建數(shù)據(jù)庫(kù)連接

package model

import (
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"log"
)

type TpOrm struct {
	*gorm.DB
}

var TpDB TpOrm

func InitTpOrm() {
	dsn := "root:root@tcp(127.0.0.1:3306)/test?charset=utf8mb4&parseTime=True&loc=Local"
	db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		log.Println(err)
		return
	}
	TpDB = TpOrm{db}
}

3、gRPC實(shí)現(xiàn)GetDataBaseVersion方法

package impl

import (
	"context"
	"github.com/golang/protobuf/ptypes/empty"
	"proj/model"
	pb "proj/proto"
)

type DataBaseServerImpl struct {
}

func (dataBaseServerImpl *DataBaseServerImpl) GetDataBaseVersion(ctx context.Context, req *empty.Empty) (rep *pb.VersionResponse, err error) {
	var version string
	rep = &pb.VersionResponse{}
	rows, err := model.TpDB.Raw("select version() as version").Rows()
	if err != nil {
		rep.Version = ""
	}
	defer rows.Close()
	for rows.Next() {
		err := rows.Scan(&version)
		if err != nil {
			rep.Version = ""
		}else{
			rep.Version = version
		}
	}
	return
}

4、Server端實(shí)現(xiàn)

package main

import (
	"google.golang.org/grpc"
	"log"
	"net"
	impl "proj/grpc"
	"proj/model"
	pb "proj/proto"
)

func main() {
	model.InitTpOrm()
	gRpcListen, err := net.Listen("tcp", ":23352")
	if err != nil {
		log.Printf("failed grpc listen: %v", err)
	}
	gRpcServer := grpc.NewServer()
	pb.RegisterDataBaseServer(gRpcServer, &impl.DataBaseServerImpl{})
	err = gRpcServer.Serve(gRpcListen)
	if err != nil {
		log.Println("GrpcServer fail start :%v", err.Error())
	} else {
		log.Println("GrpcServer success start %s", ":8090")
	}
}

啟動(dòng):

$ go run server/server.go

5、客戶端實(shí)現(xiàn)

package main

import (
	"context"
	"google.golang.org/grpc"
	"google.golang.org/protobuf/types/known/emptypb"
	"log"
	pb "proj/proto"
)

func main() {
	ctx := context.Background()
	conn, err := grpc.DialContext(ctx, "127.0.0.1:23352", grpc.WithInsecure(), grpc.WithBlock())
	if err != nil {
		log.Println(err)
	}
	client := pb.NewDataBaseClient(conn)
	in := new(emptypb.Empty)
	rep, err := client.GetDataBaseVersion(ctx, in)
	if err != nil {
		log.Println(err)
	} else {
		log.Println(rep.Version)
	}
}

啟動(dòng):

$ go run client/client.go
2023/06/28 17:30:37 5.5.28

6、項(xiàng)目的結(jié)構(gòu)

$ tree go-grpc/
go-grpc/
├── client
│   └── client.go
├── go.mod
├── go.sum
├── grpc
│   └── impl.go
├── model
│   └── init.go
├── proto
│   ├── database.pb.go
│   └── database.proto
├── readme.md
├── server
│   └── server.go
└── test
    └── main.go

6 directories, 10 files

以上就是使用gRPC實(shí)現(xiàn)獲取數(shù)據(jù)庫(kù)版本的詳細(xì)內(nèi)容,更多關(guān)于gRPC獲取數(shù)據(jù)庫(kù)版本的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 淺談?dòng)肎o構(gòu)建不可變的數(shù)據(jù)結(jié)構(gòu)的方法

    淺談?dòng)肎o構(gòu)建不可變的數(shù)據(jù)結(jié)構(gòu)的方法

    這篇文章主要介紹了用Go構(gòu)建不可變的數(shù)據(jù)結(jié)構(gòu)的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • golang實(shí)現(xiàn)可中斷的流式下載功能

    golang實(shí)現(xiàn)可中斷的流式下載功能

    這篇文章主要給大家介紹了golang實(shí)現(xiàn)可中斷的流式下載,文中通過(guò)代碼示例給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下
    2024-01-01
  • golang panic及處理機(jī)制

    golang panic及處理機(jī)制

    Go語(yǔ)言追求簡(jiǎn)潔優(yōu)雅,所以,Go語(yǔ)言不支持傳統(tǒng)的 try…catch…finally 這種異常,因?yàn)镚o語(yǔ)言的設(shè)計(jì)者們認(rèn)為,將異常與控制結(jié)構(gòu)混在一起會(huì)很容易使得代碼變得混亂,今天給大家介紹golang panic及處理機(jī)制,需要的朋友參考下吧
    2021-08-08
  • Golang中time.After的使用理解與釋放問(wèn)題

    Golang中time.After的使用理解與釋放問(wèn)題

    這篇文章主要給大家介紹了關(guān)于Golang中time.After的使用理解與釋放問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-08-08
  • Go語(yǔ)言實(shí)現(xiàn)IP段范圍校驗(yàn)示例

    Go語(yǔ)言實(shí)現(xiàn)IP段范圍校驗(yàn)示例

    這篇文章主要介紹了Go語(yǔ)言實(shí)現(xiàn)IP段范圍校驗(yàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • 一文詳解go中如何實(shí)現(xiàn)定時(shí)任務(wù)

    一文詳解go中如何實(shí)現(xiàn)定時(shí)任務(wù)

    定時(shí)任務(wù)是指按照預(yù)定的時(shí)間間隔或特定時(shí)間點(diǎn)自動(dòng)執(zhí)行的計(jì)劃任務(wù)或操作,這篇文章主要為大家詳細(xì)介紹了go中是如何實(shí)現(xiàn)定時(shí)任務(wù)的,感興趣的可以了解下
    2023-11-11
  • Golang?channel關(guān)閉后是否可以讀取剩余的數(shù)據(jù)詳解

    Golang?channel關(guān)閉后是否可以讀取剩余的數(shù)據(jù)詳解

    這篇文章主要介紹了Golang?channel關(guān)閉后是否可以讀取剩余的數(shù)據(jù),文章通過(guò)一個(gè)測(cè)試?yán)咏o大家詳細(xì)的介紹了是否可以讀取剩余的數(shù)據(jù),需要的朋友可以參考下
    2023-09-09
  • Go語(yǔ)言中的變量和常量

    Go語(yǔ)言中的變量和常量

    這篇文章介紹了Go語(yǔ)言中的變量和常量,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • Go語(yǔ)言實(shí)現(xiàn)lru淘汰策略和超時(shí)過(guò)期

    Go語(yǔ)言實(shí)現(xiàn)lru淘汰策略和超時(shí)過(guò)期

    緩存的大小是有限的,當(dāng)添加數(shù)據(jù)發(fā)現(xiàn)剩余緩存不夠時(shí),需要淘汰緩存中的部分?jǐn)?shù)據(jù),本文主要介紹了Go語(yǔ)言實(shí)現(xiàn)lru淘汰策略和超時(shí)過(guò)期,感興趣的可以了解一下
    2024-02-02
  • Go中變量命名規(guī)則與實(shí)例

    Go中變量命名規(guī)則與實(shí)例

    命名規(guī)則涉及變量、常量、全局函數(shù)、結(jié)構(gòu)、接口、方法等的命名,下面這篇文章主要給大家介紹了關(guān)于Go中變量命名的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-01-01

最新評(píng)論