Browse Source

init 增加查询key

master v0.0.1
guzeng 3 years ago
parent
commit
8f8599a96e
4 changed files with 284 additions and 0 deletions
  1. +34
    -0
      hash.go
  2. +192
    -0
      redis.pb.go
  3. +24
    -0
      redis.proto
  4. +34
    -0
      string.go

+ 34
- 0
hash.go View File

@ -0,0 +1,34 @@
package redisrpc
import (
"github.com/golang/protobuf/proto"
)
/**
* 使用用户名查询
*/
func GetByHashKey(key, field string, url ...string) (string, error) {
var user_rpc_url string = "127.0.0.1:7953"
if len(url) > 0 && url[0] != "" {
user_rpc_url = url[0]
}
conn, _, err := DialRedisService("tcp", user_rpc_url)
if err != nil {
return nil, err
}
defer conn.Close()
req := &GetHashKey{proto.String(key), proto.String(field), nil}
res := &GetResponse{}
err = conn.GetByHashKey(req, res)
if err != nil {
return "", err
}
return res.GetString(), nil
}

+ 192
- 0
redis.pb.go View File

@ -0,0 +1,192 @@
// Code generated by protoc-gen-go.
// source: redis.proto
// DO NOT EDIT!
/*
Package redisrpc is a generated protocol buffer package.
It is generated from these files:
redis.proto
It has these top-level messages:
GetKey
GetHashKey
GetResponse
*/
package redisrpc
import proto "github.com/chai2010/protorpc/proto"
import math "math"
import "io"
import "log"
import "net"
import "net/rpc"
import "time"
import protorpc "github.com/chai2010/protorpc"
// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = math.Inf
// 使用key查询
type GetKey struct {
Key *string `protobuf:"bytes,1,opt,name=key" json:"key,omitempty"`
XXX_unrecognized []byte `json:"-"`
}
func (m *GetKey) Reset() { *m = GetKey{} }
func (m *GetKey) String() string { return proto.CompactTextString(m) }
func (*GetKey) ProtoMessage() {}
func (m *GetKey) GetKey() string {
if m != nil && m.Key != nil {
return *m.Key
}
return ""
}
// 使用hash key查询
type GetHashKey struct {
Key *string `protobuf:"bytes,1,opt,name=key" json:"key,omitempty"`
Field *string `protobuf:"bytes,2,opt,name=field" json:"field,omitempty"`
XXX_unrecognized []byte `json:"-"`
}
func (m *GetHashKey) Reset() { *m = GetHashKey{} }
func (m *GetHashKey) String() string { return proto.CompactTextString(m) }
func (*GetHashKey) ProtoMessage() {}
func (m *GetHashKey) GetKey() string {
if m != nil && m.Key != nil {
return *m.Key
}
return ""
}
func (m *GetHashKey) GetField() string {
if m != nil && m.Field != nil {
return *m.Field
}
return ""
}
// 使用key查询响应结构
type GetResponse struct {
String_ *string `protobuf:"bytes,1,opt,name=string" json:"string,omitempty"`
XXX_unrecognized []byte `json:"-"`
}
func (m *GetResponse) Reset() { *m = GetResponse{} }
func (m *GetResponse) String() string { return proto.CompactTextString(m) }
func (*GetResponse) ProtoMessage() {}
func (m *GetResponse) GetString_() string {
if m != nil && m.String_ != nil {
return *m.String_
}
return ""
}
func init() {
}
type RedisService interface {
GetByKey(in *GetKey, out *GetResponse) error
GetByHashKey(in *GetHashKey, out *GetResponse) error
}
// AcceptRedisServiceClient accepts connections on the listener and serves requests
// for each incoming connection. Accept blocks; the caller typically
// invokes it in a go statement.
func AcceptRedisServiceClient(lis net.Listener, x RedisService) {
srv := rpc.NewServer()
if err := srv.RegisterName("RedisService", x); err != nil {
log.Fatal(err)
}
for {
conn, err := lis.Accept()
if err != nil {
log.Fatalf("lis.Accept(): %v\n", err)
}
go srv.ServeCodec(protorpc.NewServerCodec(conn))
}
}
// RegisterRedisService publish the given RedisService implementation on the server.
func RegisterRedisService(srv *rpc.Server, x RedisService) error {
if err := srv.RegisterName("RedisService", x); err != nil {
return err
}
return nil
}
// NewRedisServiceServer returns a new RedisService Server.
func NewRedisServiceServer(x RedisService) *rpc.Server {
srv := rpc.NewServer()
if err := srv.RegisterName("RedisService", x); err != nil {
log.Fatal(err)
}
return srv
}
// ListenAndServeRedisService listen announces on the local network address laddr
// and serves the given RedisService implementation.
func ListenAndServeRedisService(network, addr string, x RedisService) error {
lis, err := net.Listen(network, addr)
if err != nil {
return err
}
defer lis.Close()
srv := rpc.NewServer()
if err := srv.RegisterName("RedisService", x); err != nil {
return err
}
for {
conn, err := lis.Accept()
if err != nil {
log.Fatalf("lis.Accept(): %v\n", err)
}
go srv.ServeCodec(protorpc.NewServerCodec(conn))
}
}
type RedisServiceClient struct {
*rpc.Client
}
// NewRedisServiceClient returns a RedisService rpc.Client and stub to handle
// requests to the set of RedisService at the other end of the connection.
func NewRedisServiceClient(conn io.ReadWriteCloser) (*RedisServiceClient, *rpc.Client) {
c := rpc.NewClientWithCodec(protorpc.NewClientCodec(conn))
return &RedisServiceClient{c}, c
}
func (c *RedisServiceClient) GetByKey(in *GetKey, out *GetResponse) error {
return c.Call("RedisService.GetByKey", in, out)
}
func (c *RedisServiceClient) GetByHashKey(in *GetHashKey, out *GetResponse) error {
return c.Call("RedisService.GetByHashKey", in, out)
}
// DialRedisService connects to an RedisService at the specified network address.
func DialRedisService(network, addr string) (*RedisServiceClient, *rpc.Client, error) {
c, err := protorpc.Dial(network, addr)
if err != nil {
return nil, nil, err
}
return &RedisServiceClient{c}, c, nil
}
// DialRedisServiceTimeout connects to an RedisService at the specified network address.
func DialRedisServiceTimeout(network, addr string,
timeout time.Duration) (*RedisServiceClient, *rpc.Client, error) {
c, err := protorpc.DialTimeout(network, addr, timeout)
if err != nil {
return nil, nil, err
}
return &RedisServiceClient{c}, c, nil
}

+ 24
- 0
redis.proto View File

@ -0,0 +1,24 @@
syntax = "proto3";
package redisrpc;
// 使key查询
message GetKey {
string key = 1;
}
// 使hash key查询
message GetHashKey {
string key = 1;
string field = 2;
}
// 使key查询响应结构
message GetResponse {
string string = 1;
}
// rpc方法
service RedisService {
rpc getByKey (GetKey) returns (GetResponse); // 使key查询
rpc getByHashKey (GetHashKey) returns (GetResponse); // 使hash key查询
}

+ 34
- 0
string.go View File

@ -0,0 +1,34 @@
package redisrpc
import (
"github.com/golang/protobuf/proto"
)
/**
* 使用用户名查询
*/
func GetByKey(key string, url ...string) (string, error) {
var user_rpc_url string = "127.0.0.1:7953"
if len(url) > 0 && url[0] != "" {
user_rpc_url = url[0]
}
conn, _, err := DialRedisService("tcp", user_rpc_url)
if err != nil {
return nil, err
}
defer conn.Close()
req := &GetKey{proto.String(key), nil}
res := &GetResponse{}
err = conn.GetByKey(req, res)
if err != nil {
return "", err
}
return res.GetString(), nil
}

Loading…
Cancel
Save