github.com/polarismesh/polaris@v1.17.8/apiserver/grpcserver/config/server.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 config
    19  
    20  import (
    21  	"context"
    22  	"fmt"
    23  	"strings"
    24  
    25  	apiconfig "github.com/polarismesh/specification/source/go/api/v1/config_manage"
    26  	"google.golang.org/grpc"
    27  
    28  	"github.com/polarismesh/polaris/apiserver"
    29  	"github.com/polarismesh/polaris/apiserver/grpcserver"
    30  	commonlog "github.com/polarismesh/polaris/common/log"
    31  	"github.com/polarismesh/polaris/common/model"
    32  	"github.com/polarismesh/polaris/config"
    33  )
    34  
    35  var (
    36  	configLog = commonlog.GetScopeOrDefaultByName(commonlog.ConfigLoggerName)
    37  )
    38  
    39  // ConfigGRPCServer 配置中心 GRPC API 服务器
    40  type ConfigGRPCServer struct {
    41  	grpcserver.BaseGrpcServer
    42  	configServer config.ConfigCenterServer
    43  	openAPI      map[string]apiserver.APIConfig
    44  }
    45  
    46  // GetPort 获取端口
    47  func (g *ConfigGRPCServer) GetPort() uint32 {
    48  	return g.BaseGrpcServer.GetPort()
    49  }
    50  
    51  // GetProtocol 获取Server的协议
    52  func (g *ConfigGRPCServer) GetProtocol() string {
    53  	return "grpc"
    54  }
    55  
    56  // Initialize 初始化GRPC API服务器
    57  func (g *ConfigGRPCServer) Initialize(ctx context.Context, option map[string]interface{},
    58  	apiConf map[string]apiserver.APIConfig) error {
    59  	g.openAPI = apiConf
    60  	return g.BaseGrpcServer.Initialize(ctx, option,
    61  		grpcserver.WithModule(model.ConfigModule),
    62  		grpcserver.WithProtocol(g.GetProtocol()),
    63  		grpcserver.WithLogger(commonlog.FindScope(commonlog.APIServerLoggerName)),
    64  	)
    65  }
    66  
    67  // Run 启动GRPC API服务器
    68  func (g *ConfigGRPCServer) Run(errCh chan error) {
    69  	g.BaseGrpcServer.Run(errCh, g.GetProtocol(), func(server *grpc.Server) error {
    70  		for name, apiConfig := range g.openAPI {
    71  			switch name {
    72  			case "client":
    73  				if apiConfig.Enable {
    74  					apiconfig.RegisterPolarisConfigGRPCServer(server, g)
    75  					openMethod, getErr := GetClientOpenMethod(g.GetProtocol())
    76  					if getErr != nil {
    77  						return getErr
    78  					}
    79  					if g.BaseGrpcServer.OpenMethod == nil {
    80  						g.BaseGrpcServer.OpenMethod = openMethod
    81  					} else {
    82  						for method, opened := range openMethod {
    83  							g.BaseGrpcServer.OpenMethod[method] = opened
    84  						}
    85  					}
    86  				}
    87  			default:
    88  				configLog.Errorf("[Config] api %s does not exist in grpcserver", name)
    89  				return fmt.Errorf("api %s does not exist in grpcserver", name)
    90  			}
    91  		}
    92  		var err error
    93  		if g.configServer, err = config.GetServer(); err != nil {
    94  			configLog.Errorf("[Config] %v", err)
    95  			return err
    96  		}
    97  
    98  		return nil
    99  	})
   100  }
   101  
   102  // Stop 关闭GRPC
   103  func (g *ConfigGRPCServer) Stop() {
   104  	g.BaseGrpcServer.Stop(g.GetProtocol())
   105  }
   106  
   107  // Restart 重启Server
   108  func (g *ConfigGRPCServer) Restart(option map[string]interface{}, apiConf map[string]apiserver.APIConfig,
   109  	errCh chan error) error {
   110  	initFunc := func() error {
   111  		return g.Initialize(context.Background(), option, apiConf)
   112  	}
   113  	runFunc := func() {
   114  		g.Run(errCh)
   115  	}
   116  	return g.BaseGrpcServer.Restart(initFunc, runFunc, g.GetProtocol(), option)
   117  }
   118  
   119  // enterRateLimit 限流
   120  func (g *ConfigGRPCServer) enterRateLimit(ip string, method string) uint32 {
   121  	return g.BaseGrpcServer.EnterRatelimit(ip, method)
   122  }
   123  
   124  // allowAccess 限制访问
   125  func (g *ConfigGRPCServer) allowAccess(method string) bool {
   126  	return g.BaseGrpcServer.AllowAccess(method)
   127  }
   128  
   129  // GetClientOpenMethod .
   130  func GetClientOpenMethod(protocol string) (map[string]bool, error) {
   131  	openMethods := []string{"GetConfigFile", "CreateConfigFile",
   132  		"UpdateConfigFile", "PublishConfigFile", "WatchConfigFiles", "GetConfigFileMetadataList"}
   133  
   134  	openMethod := make(map[string]bool)
   135  
   136  	for _, item := range openMethods {
   137  		method := "/v1.PolarisConfig" + strings.ToUpper(protocol) + "/" + item
   138  		openMethod[method] = true
   139  	}
   140  
   141  	return openMethod, nil
   142  }