clientutil

package
v0.0.25 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 12, 2024 License: Apache-2.0 Imports: 24 Imported by: 3

README

服务API

package serviceapi

import (
	"github.com/go-micro-saas/service-kit/cluster_service_api"
	pingservicev1 "github.com/go-micro-saas/service-kit/testdata/ping-service/api/ping-service/v1/services"
)

// 示例:仅供参考
const (
	PingService   clientutil.ServiceName = "ping-service"
	NodeidService clientutil.ServiceName = "nodeid-service"

	FeishuApi      clientutil.ServiceName = "feishu-openapi"
	DingtalkApi    clientutil.ServiceName = "dingtalk-openapi"
	DingtalkApiOld clientutil.ServiceName = "dingtalk-openapi-old"
)

// NewPingGRPCClient ...
func NewPingGRPCClient(serviceAPIManager clientutil.ServiceAPIManager, rewriteServiceName ...clientutil.ServiceName) (pingservicev1.SrvPingClient, error) {
	serviceName := PingService
	for i := range rewriteServiceName {
		serviceName = rewriteServiceName[i]
	}
	conn, err := clientutil.NewSingletonServiceAPIConnection(serviceAPIManager, serviceName)
	//conn, err := NewServiceAPIConnection(serviceAPIManager, serviceName)
	if err != nil {
		return nil, err
	}
	grpcConn, err := conn.GetGRPCConnection()
	if err != nil {
		return nil, err
	}
	return pingservicev1.NewSrvPingClient(grpcConn), nil
}

// NewPingHTTPClient ...
func NewPingHTTPClient(serviceAPIManager clientutil.ServiceAPIManager, rewriteServiceName ...clientutil.ServiceName) (pingservicev1.SrvPingHTTPClient, error) {
	serviceName := PingService
	for i := range rewriteServiceName {
		serviceName = rewriteServiceName[i]
	}
	conn, err := clientutil.NewSingletonServiceAPIConnection(serviceAPIManager, serviceName)
	//conn, err := NewServiceAPIConnection(serviceAPIManager, serviceName)
	if err != nil {
		return nil, err
	}
	httpClient, err := conn.GetHTTPClient()
	if err != nil {
		return nil, err
	}
	return pingservicev1.NewSrvPingHTTPClient(httpClient), nil
}

Documentation

Index

Constants

View Source
const (
	Sep            = "://"
	DefaultTimeout = time.Minute
)

Variables

View Source
var (
	OK       int32 = 0
	StatusOK int32 = stdhttp.StatusOK
)

Functions

func CheckHTTPResponse added in v0.0.21

func CheckHTTPResponse(httpCode int, response Response) *errors.Error

func CheckHTTPStatus added in v0.0.21

func CheckHTTPStatus(statusCode int) *errors.Error

func CheckResponseCode added in v0.0.21

func CheckResponseCode(response Response) *errors.Error

func CheckResponseStatus added in v0.0.21

func CheckResponseStatus(resp Response) *errors.Error

Types

type Config

type Config struct {
	ServiceName   string                                   // 服务名称
	TransportType configpb.TransportTypeEnum_TransportType // 传输协议:http、grpc、...;默认: HTTP
	RegistryType  configpb.RegistryTypeEnum_RegistryType   // 注册类型:注册类型:endpoint、consul、...;配置中心配置:${registry_type};例: Bootstrap.Consul
	ServiceTarget string                                   // 服务目标:endpoint或registry,例:http://127.0.0.1:8899、discovery:///${registry_endpoint}
}

Config ...

func (*Config) IsConsulRegistry

func (s *Config) IsConsulRegistry() bool

func (*Config) IsEtcdRegistry

func (s *Config) IsEtcdRegistry() bool

func (*Config) SetByPbClusterServiceApi

func (s *Config) SetByPbClusterServiceApi(cfg *configpb.ClusterServiceApi)

type GetMetadata added in v0.0.21

type GetMetadata interface {
	GetMetadata() map[string]string
}

type Option

type Option func(*option)

func WithConsulClient

func WithConsulClient(consulClient *consulapi.Client) Option

func WithEtcdClient

func WithEtcdClient(etcdClient *clientv3.Client) Option

func WithLogger

func WithLogger(logger log.Logger) Option

type Response added in v0.0.21

type Response interface {
	GetCode() int32
	GetReason() string
	GetMessage() string
}

type ServiceAPIConnection

type ServiceAPIConnection interface {
	GetTransportType() configpb.TransportTypeEnum_TransportType
	IsTransportType(tt configpb.TransportTypeEnum_TransportType) bool
	GetGRPCConnection() (*stdgrpc.ClientConn, error)
	GetHTTPClient() (*http.Client, error)
}

func NewServiceAPIConnection

func NewServiceAPIConnection(serviceAPIManager ServiceAPIManager, serviceName ServiceName) (ServiceAPIConnection, error)

func NewSingletonServiceAPIConnection

func NewSingletonServiceAPIConnection(serviceAPIManager ServiceAPIManager, serviceName ServiceName) (ServiceAPIConnection, error)

type ServiceAPIManager

type ServiceAPIManager interface {
	// RegisterServiceAPIConfigs 注册服务API,覆盖更新
	RegisterServiceAPIConfigs(apis []*Config, opts ...Option) error
	// GetServiceAPIConfig 获取服务配置
	GetServiceAPIConfig(serviceName ServiceName) (*Config, error)
	// NewAPIConnection 实例化客户端链接
	NewAPIConnection(serviceName ServiceName) (ServiceAPIConnection, error)
}

func NewServiceAPIManager

func NewServiceAPIManager(apiConfigs []*Config, opts ...Option) (ServiceAPIManager, error)

type ServiceName

type ServiceName string

ServiceName ...

func (ServiceName) String

func (s ServiceName) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL