Skip to content

Commit

Permalink
decouple config package from governance (go-chassis#571)
Browse files Browse the repository at this point in the history
  • Loading branch information
tianxiaoliang authored Mar 27, 2019
1 parent 3603b31 commit 5892c29
Show file tree
Hide file tree
Showing 23 changed files with 249 additions and 175 deletions.
12 changes: 10 additions & 2 deletions chassis.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import (

"github.com/go-chassis/go-chassis/bootstrap"

//load balancing
_ "github.com/go-chassis/go-chassis/pkg/loadbalancing"

//protocols
_ "github.com/go-chassis/go-chassis/client/highway"
_ "github.com/go-chassis/go-chassis/client/rest"
Expand Down Expand Up @@ -147,7 +150,8 @@ func (c *chassis) initialize() error {
if err != nil {
return err
}
if err := loadbalancer.Enable(); err != nil {
strategyName := archaius.GetString("cse.loadbalance.strategy.name", "")
if err := loadbalancer.Enable(strategyName); err != nil {
return err
}
}
Expand All @@ -158,7 +162,11 @@ func (c *chassis) initialize() error {
if err = router.Init(); err != nil {
return err
}
if err := control.Init(); err != nil {
opts := control.Options{
Infra: config.GlobalDefinition.Panel.Infra,
Address: config.GlobalDefinition.Panel.Settings["address"],
}
if err := control.Init(opts); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion control/archaius/panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func newPanel(options control.Options) control.Panel {
//GetCircuitBreaker return command , and circuit breaker settings
func (p *Panel) GetCircuitBreaker(inv invocation.Invocation, serviceType string) (string, hystrix.CommandConfig) {
key := GetCBCacheKey(inv.MicroServiceName, serviceType)
command := control.NewCircuitName(serviceType, inv)
command := control.NewCircuitName(serviceType, config.GetHystrixConfig().CircuitBreakerProperties.Scope, inv)
c, ok := CBConfigCache.Get(key)
if !ok {
c, _ := CBConfigCache.Get(serviceType)
Expand Down
30 changes: 25 additions & 5 deletions control/archaius/panel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ func TestPanel_GetLoadBalancing(t *testing.T) {
err := config.Init()
assert.NoError(t, err)
config.GlobalDefinition.Panel.Infra = "archaius"
err = control.Init()
opts := control.Options{
Infra: config.GlobalDefinition.Panel.Infra,
Address: config.GlobalDefinition.Panel.Settings["address"],
}
err = control.Init(opts)
assert.NoError(t, err)

t.Log("lb")
Expand Down Expand Up @@ -62,7 +66,11 @@ func BenchmarkPanel_GetLoadBalancing(b *testing.B) {
os.Setenv("CHASSIS_HOME", gopath+"/src/github.com/go-chassis/go-chassis/examples/discovery/client/")
config.Init()
config.GlobalDefinition.Panel.Infra = "archaius"
control.Init()
opts := control.Options{
Infra: config.GlobalDefinition.Panel.Infra,
Address: config.GlobalDefinition.Panel.Settings["address"],
}
control.Init(opts)
inv := invocation.Invocation{
SourceMicroService: "",
MicroServiceName: "Server",
Expand All @@ -78,7 +86,11 @@ func BenchmarkPanel_GetLoadBalancing2(b *testing.B) {
os.Setenv("CHASSIS_HOME", gopath+"/src/github.com/go-chassis/go-chassis/examples/discovery/client/")
config.Init()
config.GlobalDefinition.Panel.Infra = "archaius"
control.Init()
opts := control.Options{
Infra: config.GlobalDefinition.Panel.Infra,
Address: config.GlobalDefinition.Panel.Settings["address"],
}
control.Init(opts)
inv := invocation.Invocation{
SourceMicroService: "",
MicroServiceName: "",
Expand All @@ -94,7 +106,11 @@ func BenchmarkPanel_GetCircuitBreaker(b *testing.B) {
os.Setenv("CHASSIS_HOME", gopath+"/src/github.com/go-chassis/go-chassis/examples/discovery/client/")
config.Init()
config.GlobalDefinition.Panel.Infra = "archaius"
control.Init()
opts := control.Options{
Infra: config.GlobalDefinition.Panel.Infra,
Address: config.GlobalDefinition.Panel.Settings["address"],
}
control.Init(opts)
inv := invocation.Invocation{
SourceMicroService: "",
MicroServiceName: "",
Expand All @@ -110,7 +126,11 @@ func BenchmarkPanel_GetRateLimiting(b *testing.B) {
os.Setenv("CHASSIS_HOME", gopath+"/src/github.com/go-chassis/go-chassis/examples/discovery/client/")
config.Init()
config.GlobalDefinition.Panel.Infra = "archaius"
control.Init()
opts := control.Options{
Infra: config.GlobalDefinition.Panel.Infra,
Address: config.GlobalDefinition.Panel.Settings["address"],
}
control.Init(opts)
inv := invocation.Invocation{
SourceMicroService: "",
MicroServiceName: "",
Expand Down
6 changes: 5 additions & 1 deletion control/archaius/transfer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ func TestSaveToCBCache(t *testing.T) {
os.Setenv("CHASSIS_HOME", gopath+"/src/github.com/go-chassis/go-chassis/examples/discovery/client/")
err := config.Init()
assert.NoError(t, err)
err = control.Init()
opts := control.Options{
Infra: config.GlobalDefinition.Panel.Infra,
Address: config.GlobalDefinition.Panel.Settings["address"],
}
err = control.Init(opts)
archaius.SaveToCBCache(config.GetHystrixConfig())
c, _ := archaius.CBConfigCache.Get("Consumer")
assert.Equal(t, 100, c.(hystrix.CommandConfig).MaxConcurrentRequests)
Expand Down
7 changes: 7 additions & 0 deletions control/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package control

//Options is for initiating control panel
type Options struct {
Address string
Infra string
}
27 changes: 11 additions & 16 deletions control/panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package control

import (
"fmt"
"github.com/go-chassis/go-chassis/core/config"
"github.com/go-chassis/go-chassis/core/config/model"
"github.com/go-chassis/go-chassis/core/invocation"
"github.com/go-chassis/go-chassis/third_party/forked/afex/hystrix-go/hystrix"
Expand Down Expand Up @@ -30,19 +29,14 @@ type Panel interface {
GetEgressRule() []EgressConfig
}

//Options is options
type Options struct {
Address string
}

//InstallPlugin install implementation
func InstallPlugin(name string, f func(options Options) Panel) {
panelPlugin[name] = f
}

//Init initialize DefaultPanel
func Init() error {
infra := config.GlobalDefinition.Panel.Infra
func Init(opts Options) error {
infra := opts.Infra
if infra == "" {
infra = "archaius"
}
Expand All @@ -51,22 +45,23 @@ func Init() error {
return fmt.Errorf("do not support [%s] panel", infra)
}

DefaultPanel = f(Options{
Address: config.GlobalDefinition.Panel.Settings["address"],
})
DefaultPanel = f(opts)
return nil
}

// NewCircuitName create circuit command
func NewCircuitName(serviceType string, inv invocation.Invocation) string {
//NewCircuitName create circuit command string
//scope means has two choices, service and api
//if you set it to api, a api level command string will be created. like "Consumer.mall.rest./test"
//set to service, a service level command will be created, like "Consumer.mall"
func NewCircuitName(serviceType, scope string, inv invocation.Invocation) string {
var cmd = serviceType
if inv.MicroServiceName != "" {
cmd = strings.Join([]string{cmd, inv.MicroServiceName}, ".")
}
if config.GetHystrixConfig().CircuitBreakerProperties.Scope == "" {
config.GetHystrixConfig().CircuitBreakerProperties.Scope = ScopeAPI
if scope == "" {
scope = ScopeAPI
}
if config.GetHystrixConfig().CircuitBreakerProperties.Scope == ScopeAPI {
if scope == ScopeAPI {
if inv.SchemaID != "" {
cmd = strings.Join([]string{cmd, inv.SchemaID}, ".")
}
Expand Down
17 changes: 11 additions & 6 deletions control/panel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,15 @@ func TestInit(t *testing.T) {
os.Setenv("CHASSIS_HOME", gopath+"/src/github.com/go-chassis/go-chassis/examples/discovery/client/")
err := config.Init()
assert.NoError(t, err)
err = control.Init()
opts := control.Options{
Infra: config.GlobalDefinition.Panel.Infra,
Address: config.GlobalDefinition.Panel.Settings["address"],
}
err = control.Init(opts)
assert.NoError(t, err)
config.GlobalDefinition.Panel.Infra = "xxx"
err = control.Init()
opts.Infra = "xxx"
err = control.Init(opts)
t.Log(err)
assert.Error(t, err)
}

Expand All @@ -47,14 +52,14 @@ func TestNewCircuitCmd(t *testing.T) {
SchemaID: "rest",
OperationID: "/test",
}
cmd := control.NewCircuitName("Consumer", i)
cmd := control.NewCircuitName("Consumer", config.GetHystrixConfig().CircuitBreakerProperties.Scope, i)
assert.Equal(t, "Consumer.mall.rest./test", cmd)

config.GetHystrixConfig().CircuitBreakerProperties.Scope = "api"
cmd = control.NewCircuitName("Consumer", i)
cmd = control.NewCircuitName("Consumer", config.GetHystrixConfig().CircuitBreakerProperties.Scope, i)
assert.Equal(t, "Consumer.mall.rest./test", cmd)

config.GetHystrixConfig().CircuitBreakerProperties.Scope = "service"
cmd = control.NewCircuitName("Consumer", i)
cmd = control.NewCircuitName("Consumer", config.GetHystrixConfig().CircuitBreakerProperties.Scope, i)
assert.Equal(t, "Consumer.mall", cmd)
}
5 changes: 5 additions & 0 deletions core/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ func GetTransportConf() model.Transport {
return GlobalDefinition.Cse.Transport
}

//GetDataCenter return data center info
func GetDataCenter() *model.DataCenterInfo {
return GlobalDefinition.DataCenter
}

// parse unmarshal configurations on respective structure
func parse() error {
err := readGlobalConfigFile()
Expand Down
12 changes: 10 additions & 2 deletions core/handler/bizkeeper_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ func TestCBInit(t *testing.T) {

lager.Initialize("", "INFO", "", "size", true, 1, 10, 7)
config.Init()
err := control.Init()
opts := control.Options{
Infra: config.GlobalDefinition.Panel.Infra,
Address: config.GlobalDefinition.Panel.Settings["address"],
}
err := control.Init(opts)
assert.NoError(t, err)
}

Expand Down Expand Up @@ -90,7 +94,11 @@ func BenchmarkBizKeepConsumerHandler_Handler(b *testing.B) {

lager.Initialize("", "INFO", "", "size", true, 1, 10, 7)
config.Init()
control.Init()
opts := control.Options{
Infra: config.GlobalDefinition.Panel.Infra,
Address: config.GlobalDefinition.Panel.Settings["address"],
}
control.Init(opts)
inv := &invocation.Invocation{
MicroServiceName: "fakeService",
SchemaID: "schema",
Expand Down
10 changes: 5 additions & 5 deletions core/handler/handler_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/go-chassis/go-chassis/core/common"
"github.com/go-chassis/go-chassis/core/invocation"
"github.com/go-chassis/go-chassis/core/lager"
"github.com/go-mesh/openlogging"
)

var errEmptyChain = errors.New("chain can not be empty")
Expand Down Expand Up @@ -76,7 +76,7 @@ func parseHandlers(handlerStr string) []string {
return s
}

// CreateChains is for to create the chains
//CreateChains create the chains based on type and handler map
func CreateChains(chainType string, handlerNameMap map[string]string) error {
for chainName := range handlerNameMap {
handlerNames := parseHandlers(handlerNameMap[chainName])
Expand All @@ -90,13 +90,13 @@ func CreateChains(chainType string, handlerNameMap map[string]string) error {
return nil
}

//CreateChain create consumer or provider's chain,the final handler is different
//CreateChain create consumer or provider's chain,the handlers is different
func CreateChain(serviceType string, chainName string, handlerNames ...string) (*Chain, error) {
c := &Chain{
ServiceType: serviceType,
Name: chainName,
}
lager.Logger.Debugf("add [%d] handlers for chain [%s]", len(handlerNames), chainName)
openlogging.Debug(fmt.Sprintf("add [%d] handlers for chain [%s]", len(handlerNames), chainName))

for _, name := range handlerNames {
err := addHandler(c, name)
Expand All @@ -106,7 +106,7 @@ func CreateChain(serviceType string, chainName string, handlerNames ...string) (
}

if len(c.Handlers) == 0 {
lager.Logger.Warnf("Chain "+chainName+" is Empty", errEmptyChain)
openlogging.Warn("Chain " + chainName + " is Empty")
return c, nil
}
return c, nil
Expand Down
7 changes: 3 additions & 4 deletions core/handler/loadbalance_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/go-chassis/go-archaius"
"github.com/go-chassis/go-chassis/control"
"github.com/go-chassis/go-chassis/core/invocation"
"github.com/go-chassis/go-chassis/core/lager"
"github.com/go-chassis/go-chassis/core/loadbalancer"
backoffUtil "github.com/go-chassis/go-chassis/pkg/backoff"
"github.com/go-chassis/go-chassis/pkg/util"
Expand All @@ -28,13 +27,13 @@ func (lb *LBHandler) getEndpoint(i *invocation.Invocation, lbConfig control.Load
i.Strategy = lbConfig.Strategy
strategyFun, err = loadbalancer.GetStrategyPlugin(i.Strategy)
if err != nil {
lager.Logger.Errorf("lb error [%s] because of [%s]", loadbalancer.LBError{
openlogging.GetLogger().Errorf("lb error [%s] because of [%s]", loadbalancer.LBError{
Message: "Get strategy [" + i.Strategy + "] failed."}.Error(), err.Error())
}
} else {
strategyFun, err = loadbalancer.GetStrategyPlugin(i.Strategy)
if err != nil {
lager.Logger.Errorf("lb error [%s] because of [%s]", loadbalancer.LBError{
openlogging.GetLogger().Errorf("lb error [%s] because of [%s]", loadbalancer.LBError{
Message: "Get strategy [" + i.Strategy + "] failed."}.Error(), err.Error())
}
}
Expand Down Expand Up @@ -68,7 +67,7 @@ func (lb *LBHandler) getEndpoint(i *invocation.Invocation, lbConfig control.Load
errStr := fmt.Sprintf("No available instance support ["+i.Protocol+"] protocol,"+
" msName: "+i.MicroServiceName+" %v", ins.EndpointsMap)
lbErr := loadbalancer.LBError{Message: errStr}
lager.Logger.Errorf(lbErr.Error())
openlogging.GetLogger().Errorf(lbErr.Error())
return "", lbErr
}
return ep, nil
Expand Down
Loading

0 comments on commit 5892c29

Please sign in to comment.