Skip to content

Commit 636cd83

Browse files
committed
feat: add the client for testing rpc client
1 parent dc746ad commit 636cd83

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

app/user/client/client.go

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,50 @@
1-
package client
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"time"
7+
8+
"github.com/hashicorp/consul/api"
9+
10+
v1 "github.com/CoderI421/gframework/api/user/v1"
11+
"github.com/CoderI421/gframework/gmicro/registry/consul"
12+
"github.com/CoderI421/gframework/gmicro/server/rpcserver"
13+
)
14+
15+
func main() {
16+
conf := api.DefaultConfig()
17+
conf.Address = "127.0.0.1:8500"
18+
conf.Scheme = "http"
19+
20+
client, err := api.NewClient(conf)
21+
if err != nil {
22+
panic(err)
23+
}
24+
25+
// 服务注册
26+
r := consul.New(client, consul.WithHealthCheck(true))
27+
28+
conn, err := rpcserver.DialInsecure(
29+
context.Background(), rpcserver.WithDiscovery(r),
30+
/*
31+
第3个/是为了第二个参数是空的
32+
默认格式:direct://<authority>/127.0.0.1:8078
33+
以后使用nacos或者其他的中心 也不用改discovery 只修改conf就可以
34+
服务发现可以直接去kartors里面copy registry下的etcd nacos等使用
35+
*/
36+
rpcserver.WithEndpoint("discovery:///user-srv"),
37+
rpcserver.WithClientTimeout(50*time.Second),
38+
)
39+
//conn, err := grpc.Dial("127.0.0.1:8078", grpc.WithTransportCredentials(insecure.NewCredentials()))
40+
if err != nil {
41+
panic(err)
42+
}
43+
defer conn.Close()
44+
uc := v1.NewUserClient(conn)
45+
re, err := uc.GetUserList(context.Background(), &v1.PageInfo{})
46+
if err != nil {
47+
panic(err)
48+
}
49+
fmt.Println(re)
50+
}

app/user/srv/controller/user/list.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import (
99
)
1010

1111
func DTOToResponse(userdto srvv1.UserDTO) upbv1.UserInfoResponse {
12-
return upbv1.UserInfoResponse{}
12+
return upbv1.UserInfoResponse{
13+
NickName: userdto.Name,
14+
}
1315
}
1416

1517
/*

0 commit comments

Comments
 (0)