The user service is a gRPC service from which we get user data from database.
-
-server-port=9092
To run on port 9092 -
-log-level=debug
To see diffrent level of logs (debug,info,fatal)
To run User Service inside docker follow below stps:
-
Build Dockerfile:
$ docker build -t grpc-server .
-
Run Docker image:
$ docker run -d --name grpc-server -p 9092:9092 grpc-server:latest
To run server on custom port and use different log level there are two flags.
2.1.
-server-port=8081
To run on port 80812.2.
-log-level=debug
For debug logswith flags docker run command
$ docker run -d --name grpc-server -p 8081:8081 grpc-server:latest -server-port=8081 -log-level=debug
To run on local machine we need to first follow below steps:
-
Run command to download dependacy:
$ go mod download
-
To build and run:
$ GOOS=linux go build -o grpc-server main.go
$ ./grpc-server
-
To run on system without building executable:
$ go run main.go
To test the system install grpcurl
which is a command line tool which can interact with gRPC API's
https://github.com/fullstorydev/grpcurl
$ go install github.com/fullstorydev/grpcurl/cmd/grpcurl@latest
[
{
"id": "1",
"fname": "Tony",
"city": "Maliby",
"phone": 1234567890,
"height": 5.67,
"Married": true
},
{
"id": "2",
"fname": "Thor",
"city": "Asgard",
"phone": 1234567890,
"height": 6.67
},
{
"id": "3",
"fname": "Peter",
"city": "New York",
"phone": 1234567890,
"height": 5.3,
"Married": true
}
]
usign --plaintext
for not secure stream like tls.
$ grpcurl --plaintext localhost:9092 list
UserDetail
grpc.reflection.v1alpha.ServerReflection
$ grpcurl --plaintext localhost:9092 list UserDetail
UserDetail.GetUser
UserDetail.GetUsersList
$ grpcurl --plaintext localhost:9092 describe UserDetail.GetUser
UserDetail.GetUser is a method:
rpc GetUser ( .UserDetailRequest ) returns ( .UserDetailResponse );
$ grpcurl --plaintext localhost:9092 describe UserDetail.GetUsersList
UserDetail.GetUsersList is a method:
rpc GetUsersList ( .ListOfUserDetailsRequest ) returns ( .ListOfUserDetailsResponse );
Success Response:
$ grpcurl --plaintext -d '{"id": 1}' localhost:9092 UserDetail/GetUser
{
"users": {
"id": "1",
"fname": "Tony",
"city": "Maliby",
"phone": 1234567890,
"height": 5.67,
"Married": true
}
}
Error Response:
$ grpcurl --plaintext -d '{"id": 4}' localhost:9092 UserDetail/GetUser
ERROR:
Code: NotFound
Message: User with id 4 doesn't exist in Database
Success Response:
$ grpcurl --plaintext -d '{"ids": [1,2,3]}' localhost:9092 UserDetail/GetUsersList
{
"users": [
{
"id": "1",
"fname": "Tony",
"city": "Maliby",
"phone": 1234567890,
"height": 5.67,
"Married": true
},
{
"id": "2",
"fname": "Thor",
"city": "Asgard",
"phone": 1234567890,
"height": 6.67
},
{
"id": "3",
"fname": "Peter",
"city": "New York",
"phone": 1234567890,
"height": 5.3,
"Married": true
}
]
}
With invalid user id:
$ grpcurl --plaintext -d '{"ids": [1,2,3,4]}' localhost:9092 UserDetail/GetUsersList
{
"users": [
{
"id": "1",
"fname": "Tony",
"city": "Maliby",
"phone": 1234567890,
"height": 5.67,
"Married": true
},
{
"id": "2",
"fname": "Thor",
"city": "Asgard",
"phone": 1234567890,
"height": 6.67
},
{
"id": "3",
"fname": "Peter",
"city": "New York",
"phone": 1234567890,
"height": 5.3,
"Married": true
}
],
"error": {
"code": 5,
"message": "Ids doesn't exist [4] in database"
}
}