github.com/polarismesh/polaris@v1.17.8/test/integrate/grpc/client.go (about)

     1  /**
     2   * Tencent is pleased to support the open source community by making Polaris available.
     3   *
     4   * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
     5   *
     6   * Licensed under the BSD 3-Clause License (the "License");
     7   * you may not use this file except in compliance with the License.
     8   * You may obtain a copy of the License at
     9   *
    10   * https://opensource.org/licenses/BSD-3-Clause
    11   *
    12   * Unless required by applicable law or agreed to in writing, software distributed
    13   * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
    14   * CONDITIONS OF ANY KIND, either express or implied. See the License for the
    15   * specific language governing permissions and limitations under the License.
    16   */
    17  
    18  package grpc
    19  
    20  import (
    21  	"fmt"
    22  
    23  	apiconfig "github.com/polarismesh/specification/source/go/api/v1/config_manage"
    24  	apiservice "github.com/polarismesh/specification/source/go/api/v1/service_manage"
    25  	"google.golang.org/grpc"
    26  )
    27  
    28  // NewClient 创建GRPC客户端
    29  func NewClient(address string) (*Client, error) {
    30  	fmt.Printf("\nnew grpc client\n")
    31  
    32  	serviceConn, err := grpc.Dial(fmt.Sprintf("%s:%d", address, 8091), grpc.WithInsecure())
    33  	if err != nil {
    34  		fmt.Printf("%v\n", err)
    35  		return nil, err
    36  	}
    37  
    38  	configConn, err := grpc.Dial(fmt.Sprintf("%s:%d", address, 8093), grpc.WithInsecure())
    39  	if err != nil {
    40  		fmt.Printf("%v\n", err)
    41  		return nil, err
    42  	}
    43  
    44  	client := &Client{
    45  		Conn:         serviceConn,
    46  		ConfigConn:   configConn,
    47  		Worker:       apiservice.NewPolarisGRPCClient(serviceConn),
    48  		ConfigWorker: apiconfig.NewPolarisConfigGRPCClient(configConn),
    49  	}
    50  
    51  	return client, nil
    52  }
    53  
    54  // Client GRPC客户端
    55  type Client struct {
    56  	Conn         *grpc.ClientConn
    57  	ConfigConn   *grpc.ClientConn
    58  	Worker       apiservice.PolarisGRPCClient
    59  	ConfigWorker apiconfig.PolarisConfigGRPCClient
    60  }
    61  
    62  // Close 关闭连接
    63  func (c *Client) Close() {
    64  	c.Conn.Close()
    65  	c.ConfigConn.Close()
    66  }