Service Connection

Overview

This paper describes how to use the gRPC framework for the development of GRPC Client.

Sample

Preparation

We run goctl rpc new greet to generate a rpc server service.

  1. # Create a demo directory, Enter the demo directory
  2. $ mkdir demo && cd demo
  3. # Generate a gret service
  4. $ goctl rpc new greet
  5. # Create a new main. o File to create a client for a greet service
  6. $ touch main.go
Service Connection - 图1tip

The following configuration details are referenced service configuration

goctl rpc usage reference goctl rpc

Direct

There are two modes of continuous connectivity, one for a single service and one for a continual service cluster.

Address resolve mode

In main.go file the following code

  1. func main() {
  2. conn := zrpc.MustNewClient(zrpc.RpcClientConf{
  3. Target: "dns:///127.0.0.1:8080",
  4. })
  5. client := greet.NewGreetClient(conn.Conn())
  6. resp, err := client.Ping(context.Background(), &greet.Request{})
  7. if err != nil {
  8. log.Println(err)
  9. return
  10. }
  11. log.Println(resp)
  12. }

Multi-node direct connection mode

In main.go file the following code

  1. func main() {
  2. conn := zrpc.MustNewClient(zrpc.RpcClientConf{
  3. Endpoints: []string{"127.0.0.1:8080","127.0.0.2:8080"},// 直连集群时,只需要给 Endpoints 配置 rpc server的地址即可
  4. })
  5. client := greet.NewGreetClient(conn.Conn())
  6. resp, err := client.Ping(context.Background(), &greet.Request{})
  7. if err != nil {
  8. log.Println(err)
  9. return
  10. }
  11. log.Println(resp)
  12. }

etcd service discovery

In main.go file the following code

  1. func main() {
  2. conn := zrpc.MustNewClient(zrpc.RpcClientConf{
  3. Etcd: discov.EtcdConf{// When discovering via the etcd service, you only need to configure Etcd
  4. Hosts: []string{"127.0.0.1:2379"},
  5. Key: "greet.rpc",
  6. User: "",// When etcd opens acl, it is filled in, so it is not deleted here for the sake of demonstration, and can be ignored if acl is not opened for actual use.
  7. Pass: "",// When etcd opens acl, it is filled in, so it is not deleted here for the sake of demonstration, and can be ignored if acl is not opened for actual use.
  8. CertFile: "",// When etcd opens acl, it is filled in, so it is not deleted here for the sake of demonstration, and can be ignored if acl is not opened for actual use.
  9. CertKeyFile: "",// When etcd opens acl, it is filled in, so it is not deleted here for the sake of demonstration, and can be ignored if acl is not opened for actual use.
  10. CACertFile: "",// When etcd opens acl, it is filled in, so it is not deleted here for the sake of demonstration, and can be ignored if acl is not opened for actual use.
  11. InsecureSkipVerify: false,// When etcd opens acl, it is filled in, so it is not deleted here for the sake of demonstration, and can be ignored if acl is not opened for actual use.
  12. },
  13. })
  14. client := greet.NewGreetClient(conn.Conn())
  15. resp, err := client.Ping(context.Background(), &greet.Request{})
  16. if err != nil {
  17. log.Println(err)
  18. return
  19. }
  20. log.Println(resp)
  21. }

Native Support

If you do not want to initialize using a go-zero repc client, zrpc also supports grpc.ClientConn, you can use grpc.ClientConn directly.

  1. func main() {
  2. conn,err:=grpc.Dial("127.0.0.1:8080",grpc.WithBlock(), grpc.WithTransportCredentials(insecure.NewCredentials()))
  3. if err!=nil{
  4. log.Println(err)
  5. return
  6. }
  7. client := greet.NewGreetClient(conn)
  8. resp, err := client.Ping(context.Background(), &greet.Request{})
  9. if err != nil {
  10. log.Println(err)
  11. return
  12. }
  13. log.Println(resp)
  14. }

Other Service Discoveries

In addition to a go-zero built-in ecd as a service, the community also provides support for the discovery of services such as nacos, consul, etc. More Services found components for details