dubbo.apache.org/dubbo-go/v3@v3.1.1/remoting/polaris/builder.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   * contributor license agreements.  See the NOTICE file distributed with
     4   * this work for additional information regarding copyright ownership.
     5   * The ASF licenses this file to You under the Apache License, Version 2.0
     6   * (the "License"); you may not use this file except in compliance with
     7   * the License.  You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  
    18  package polaris
    19  
    20  import (
    21  	"errors"
    22  	"fmt"
    23  	"net"
    24  	"strconv"
    25  	"strings"
    26  	"sync"
    27  )
    28  
    29  import (
    30  	perrors "github.com/pkg/errors"
    31  
    32  	"github.com/polarismesh/polaris-go"
    33  	"github.com/polarismesh/polaris-go/api"
    34  	"github.com/polarismesh/polaris-go/pkg/config"
    35  )
    36  
    37  import (
    38  	"dubbo.apache.org/dubbo-go/v3/common"
    39  	"dubbo.apache.org/dubbo-go/v3/common/constant"
    40  )
    41  
    42  var (
    43  	once               sync.Once
    44  	namesapce          string
    45  	sdkCtx             api.SDKContext
    46  	openPolarisAbility bool
    47  )
    48  
    49  var (
    50  	ErrorNoOpenPolarisAbility = errors.New("polaris ability not open")
    51  	ErrorSDKContextNotInit    = errors.New("polaris SDKContext not init")
    52  )
    53  
    54  // GetConsumerAPI creates one polaris ConsumerAPI instance
    55  func GetConsumerAPI() (polaris.ConsumerAPI, error) {
    56  	if err := Check(); err != nil {
    57  		return nil, err
    58  	}
    59  
    60  	return polaris.NewConsumerAPIByContext(sdkCtx), nil
    61  }
    62  
    63  // GetProviderAPI creates one polaris ProviderAPI instance
    64  func GetProviderAPI() (polaris.ProviderAPI, error) {
    65  	if err := Check(); err != nil {
    66  		return nil, err
    67  	}
    68  
    69  	return polaris.NewProviderAPIByContext(sdkCtx), nil
    70  }
    71  
    72  // GetRouterAPI create one polaris RouterAPI instance
    73  func GetRouterAPI() (polaris.RouterAPI, error) {
    74  	if err := Check(); err != nil {
    75  		return nil, err
    76  	}
    77  
    78  	return polaris.NewRouterAPIByContext(sdkCtx), nil
    79  }
    80  
    81  // GetLimiterAPI creates one polaris LimiterAPI instance
    82  func GetLimiterAPI() (polaris.LimitAPI, error) {
    83  	if err := Check(); err != nil {
    84  		return nil, err
    85  	}
    86  
    87  	return polaris.NewLimitAPIByContext(sdkCtx), nil
    88  }
    89  
    90  func Check() error {
    91  	if !openPolarisAbility {
    92  		return ErrorNoOpenPolarisAbility
    93  	}
    94  	if sdkCtx == nil {
    95  		return ErrorSDKContextNotInit
    96  	}
    97  	return nil
    98  }
    99  
   100  // GetNamespace gets user defined namespace info
   101  func GetNamespace() string {
   102  	return namesapce
   103  }
   104  
   105  // InitSDKContext inits polaris SDKContext by URL
   106  func InitSDKContext(url *common.URL) error {
   107  	if url == nil {
   108  		return errors.New("url is empty!")
   109  	}
   110  
   111  	openPolarisAbility = true
   112  
   113  	var rerr error
   114  	once.Do(func() {
   115  		addresses := strings.Split(url.Location, ",")
   116  		serverConfigs := make([]string, 0, len(addresses))
   117  		for _, addr := range addresses {
   118  			ip, portStr, err := net.SplitHostPort(addr)
   119  			if err != nil {
   120  				rerr = perrors.WithMessagef(err, "split [%s] ", addr)
   121  			}
   122  			port, _ := strconv.Atoi(portStr)
   123  			serverConfigs = append(serverConfigs, fmt.Sprintf("%s:%d", ip, uint64(port)))
   124  		}
   125  
   126  		polarisConf := config.NewDefaultConfiguration(serverConfigs)
   127  		_sdkCtx, err := api.InitContextByConfig(polarisConf)
   128  		rerr = err
   129  		sdkCtx = _sdkCtx
   130  		namesapce = url.GetParam(constant.RegistryNamespaceKey, constant.PolarisDefaultNamespace)
   131  	})
   132  
   133  	return rerr
   134  }
   135  
   136  func mergePolarisConfiguration(easy, complexConf config.Configuration) {
   137  
   138  	easySvrList := easy.GetGlobal().GetServerConnector().GetAddresses()
   139  
   140  	complexSvrList := complexConf.GetGlobal().GetServerConnector().GetAddresses()
   141  
   142  	result := make(map[string]bool)
   143  
   144  	for i := range complexSvrList {
   145  		result[complexSvrList[i]] = true
   146  	}
   147  
   148  	for i := range easySvrList {
   149  		if _, exist := result[easySvrList[i]]; !exist {
   150  			result[easySvrList[i]] = true
   151  		}
   152  	}
   153  
   154  	finalSvrList := make([]string, 0)
   155  	for k := range result {
   156  		finalSvrList = append(finalSvrList, k)
   157  	}
   158  
   159  	complexConf.GetGlobal().GetServerConnector().SetAddresses(finalSvrList)
   160  }