-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
426c929
commit b1b3c9b
Showing
4 changed files
with
88 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |