Skip to content

Commit

Permalink
随便写个接口试试
Browse files Browse the repository at this point in the history
  • Loading branch information
zehonghuang committed Jul 1, 2020
1 parent 426c929 commit b1b3c9b
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 2 deletions.
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ module go-raft

go 1.14

require github.com/golang/protobuf v1.4.2 // indirect
require (
github.com/golang/protobuf v1.4.1
google.golang.org/grpc v1.29.1
google.golang.org/protobuf v1.24.0 // indirect
)
43 changes: 43 additions & 0 deletions main/client/startup_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package main

import (
"context"
"fmt"
"go-raft/rpc"
_ "go-raft/rpc"
"google.golang.org/grpc"
)

func main() {
conn, err := grpc.Dial("localhost:18800", grpc.WithInsecure())
if err != nil {

}
defer conn.Close()

groupId := "defult"
serverId := "localhost:8080"
peerId := "p"
term := int64(100)
prevLogTerm := int64(100)
prevLogIndex := int64(100)
prevote := true

requet := rpc.RequestVoteRequest{
GroupId: &groupId,
ServerId: &serverId,
PeerId: &peerId,
Term: &term,
PrevLogTerm: &prevLogTerm,
PrevLogIndex: &prevLogIndex,
PreVote: &prevote,
}

c := rpc.NewRaftServiceClient(conn)

response, err1 := c.PreVote(context.Background(), &requet)
if err1 != nil {
fmt.Println(err1)
}
fmt.Println(response)
}
10 changes: 10 additions & 0 deletions main/startup_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package main

import (
r "go-raft/rpc"
)

func main() {
port := 18800
r.StartedServer(&port)
}
31 changes: 30 additions & 1 deletion rpc/rpc_server.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
package rpc

func startedServer() {
import (
"context"
"fmt"
"google.golang.org/grpc"
"log"
"net"
)

type server struct {
}

func (*server) PreVote(ctx context.Context, req *RequestVoteRequest) (*RequestVoteResponse, error) {
fmt.Println(req)
granted := true
term := int64(100)
return &RequestVoteResponse{Granted: &granted, Term: &term}, nil
}

func (*server) RequestVote(context.Context, *RequestVoteRequest) (*RequestVoteResponse, error) {
return nil, nil
}

func StartedServer(port *int) {
lis, err := net.Listen("tcp", fmt.Sprintf("localhost:%d", *port))
if err != nil {
log.Fatalf("failed to listen: %v", err)
}

grpcServer := grpc.NewServer()
RegisterRaftServiceServer(grpcServer, &server{})
grpcServer.Serve(lis)
}

0 comments on commit b1b3c9b

Please sign in to comment.