Skip to content

Commit

Permalink
Feature/nacos config (apache#497)
Browse files Browse the repository at this point in the history
* uddapte: nacos config

* Add: nacos config center

* update: nacos config

* add ut

* fmt

* clean code for CI

* update: listen

* Add go sum

* update: fix config_center UT, ignore the cache and log dir

* fix UT

* update: off nacos_config_center UT
  • Loading branch information
PhilYue authored Oct 31, 2022
1 parent d682c6a commit c9614cc
Show file tree
Hide file tree
Showing 14 changed files with 680 additions and 24 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ samples/dubbogo/simple/server/app/app
/logs
/.idea
/.vscode
.cache
.log

.DS_Store
vendor/
Expand Down
26 changes: 26 additions & 0 deletions cmd/pixiu/deployer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package main

type Deployer interface {
initialize() error

start() error

stop() error
}
24 changes: 6 additions & 18 deletions cmd/pixiu/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ import (

import (
"github.com/apache/dubbo-go-pixiu/pixiu/pkg/common/constant"
"github.com/apache/dubbo-go-pixiu/pixiu/pkg/logger"
"github.com/apache/dubbo-go-pixiu/pixiu/pkg/server"
)

var (
Expand All @@ -43,28 +41,18 @@ var (
Version: Version,
PreRun: func(cmd *cobra.Command, args []string) {
initDefaultValue()
},
Run: func(cmd *cobra.Command, args []string) {
err := initLog()
if err != nil {
logger.Warnf("[startGatewayCmd] failed to init logger, %s", err.Error())
}

bootstrap, meta, err := initApiConfig()
err := deploy.initialize()
if err != nil {
if meta {
logger.Warnf("[startGatewayCmd] failed to get api meta config, %s", err.Error())
} else {
logger.Errorf("[startGatewayCmd] failed to get api meta config, %s", err.Error())
}
panic(err)
}
},
Run: func(cmd *cobra.Command, args []string) {

err = initLimitCpus()
err := deploy.start()
if err != nil {
logger.Errorf("[startCmd] failed to get limit cpu number, %s", err.Error())
panic(err)
}

server.Start(bootstrap)
},
}
)
Expand Down
50 changes: 47 additions & 3 deletions cmd/pixiu/pixiu.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package main

import (
"fmt"
"github.com/apache/dubbo-go-pixiu/pixiu/pkg/server"
_ "net/http/pprof"
"runtime"
"strconv"
Expand All @@ -40,7 +41,7 @@ import (

var (
// Version pixiu version
Version = "0.3.0"
Version = "0.5.1"

flagToLogLevel = map[string]string{
"trace": "TRACE",
Expand All @@ -65,6 +66,9 @@ var (
initFromRemote = false
)

// deploy server deployment
var deploy = &DefaultDeployer{}

// main pixiu run method
func main() {
app := getRootCmd()
Expand All @@ -90,6 +94,42 @@ func getRootCmd() *cobra.Command {
return rootCmd
}

type DefaultDeployer struct {
bootstrap *model.Bootstrap
configManger *config.ConfigManager
}

func (d *DefaultDeployer) initialize() error {

err := initLog()
if err != nil {
logger.Warnf("[startGatewayCmd] failed to init logger, %s", err.Error())
}

// load Bootstrap config
d.bootstrap = d.configManger.LoadBootConfig(configPath)
if err != nil {
panic(fmt.Errorf("[startGatewayCmd] failed to get api meta config, %s", err.Error()))
}

err = initLimitCpus()
if err != nil {
logger.Errorf("[startCmd] failed to get limit cpu number, %s", err.Error())
}

return err
}

func (d *DefaultDeployer) start() error {
server.Start(d.bootstrap)
return nil
}

func (d *DefaultDeployer) stop() error {
//TODO implement me
panic("implement me")
}

// initDefaultValue If not set both in args and env, set default values
func initDefaultValue() {
if configPath == "" {
Expand Down Expand Up @@ -135,9 +175,9 @@ func initLog() error {
}

// initApiConfig return value of the bool is for the judgment of whether is a api meta data error, a kind of silly (?)
func initApiConfig() (*model.Bootstrap, bool, error) {
func initApiConfig() (*model.Bootstrap, error) {
bootstrap := config.Load(configPath)
return bootstrap, false, nil
return bootstrap, nil
}

func initLimitCpus() error {
Expand All @@ -153,3 +193,7 @@ func initLimitCpus() error {
logger.Infof("GOMAXPROCS set to %v", limitCpuNumber)
return nil
}

func init() {
deploy.configManger = config.NewConfigManger()
}
28 changes: 28 additions & 0 deletions configcenter/configclient.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package configcenter

type (
ConfigClient interface {
LoadConfig(properties map[string]interface{}) (string, error)

ListenConfig(properties map[string]interface{}) (err error)
}

ListenConfig func(data string)
)
124 changes: 124 additions & 0 deletions configcenter/load.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package configcenter

import (
"strings"
)

import (
"github.com/ghodss/yaml"
)

import (
"github.com/apache/dubbo-go-pixiu/pixiu/pkg/logger"
"github.com/apache/dubbo-go-pixiu/pixiu/pkg/model"
)

const (
KEY_CONFIG_TYPE_NACOS = "nacos"
)

var Parsers = map[string]func(data []byte, v interface{}) error{
".yaml": ParseYamlBytes,
".yml": ParseYamlBytes,
}

type (
Load interface {
LoadConfigs(boot *model.Bootstrap, opts ...Option) (v *model.Bootstrap, err error)
}

Option func(opt *Options)

Options struct {
Remote bool
DataId string
Group string
path string
parser string
}
)

type DefaultConfigLoad struct {
bootConfig *model.Bootstrap
configClient ConfigClient
}

func NewConfigLoad(bootConfig *model.Bootstrap) *DefaultConfigLoad {

var configClient ConfigClient

// config center load
if strings.EqualFold(bootConfig.Config.Type, KEY_CONFIG_TYPE_NACOS) {
configClient, _ = NewNacosConfig(bootConfig)
}

if configClient == nil {
logger.Warnf("no remote config-center")
return nil
}

return &DefaultConfigLoad{
bootConfig: bootConfig,
configClient: configClient,
}
}

func (d *DefaultConfigLoad) LoadConfigs(boot *model.Bootstrap, opts ...Option) (v *model.Bootstrap, err error) {

var opt Options
for _, o := range opts {
o(&opt)
}

if !opt.Remote {
return nil, nil
}

m := map[string]interface{}{}

if strings.EqualFold(boot.Config.Type, KEY_CONFIG_TYPE_NACOS) {
m["dataId"] = getOrDefault(opt.DataId, DataId)
m["group"] = getOrDefault(opt.Group, Group)
}

if len(m) == 0 {
logger.Errorf("no identify properties key when load from remote config center")
return boot, nil
}

data, err := d.configClient.LoadConfig(m)

if err != nil {
return nil, err
}

if len(data) == 0 {
logger.Errorf("the config data load from remote is nil, config center : %s", boot.Config.Type)
return boot, err
}

err = Parsers[".yml"]([]byte(data), boot)

return boot, err
}

func ParseYamlBytes(content []byte, v interface{}) error {
return yaml.Unmarshal(content, v)
}
Loading

0 comments on commit c9614cc

Please sign in to comment.