This is an automated email from the ASF dual-hosted git repository. liujun pushed a commit to branch refactor-with-go-components-experimental in repository https://gitbox.apache.org/repos/asf/dubbo-admin.git
commit e8c1ea0091f4b84f41c973267019523b27de4bed Author: chickenlj <[email protected]> AuthorDate: Wed Jul 19 16:01:15 2023 +0800 fix configuration --- conf/admin.yml | 32 +++++++++++++++- conf/mock_provider.yml | 28 -------------- pkg/admin/bootstrap.go | 5 +-- pkg/admin/providers/mock/mock_provider.go | 21 ++++++++-- pkg/authority/setup.go | 1 + pkg/config/admin/config.go | 1 + pkg/config/app/dubbo-cp/config.go | 7 +++- pkg/config/app/dubbo-cp/dubbo-cp.default.yaml | 19 +++++++-- pkg/core/bootstrap/bootstrap.go | 2 + pkg/core/election/universal/leaderelection.go | 55 +++++++++++++++++++++++++++ pkg/core/runtime/builder.go | 18 ++++----- 11 files changed, 137 insertions(+), 52 deletions(-) diff --git a/conf/admin.yml b/conf/admin.yml index 682fb3a6..aff5a951 100644 --- a/conf/admin.yml +++ b/conf/admin.yml @@ -12,7 +12,6 @@ # 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. - admin: admin-port: 38080 config-center: zookeeper://127.0.0.1:2181 @@ -24,4 +23,33 @@ admin: ip: 127.0.0.1 port: 9090 monitor-port: 22222 - #mysql-dsn: root:password@tcp(127.0.0.1:3306)/dubbo-admin?charset=utf8&parseTime=true \ No newline at end of file +# mysql-dsn: root:password@tcp(127.0.0.1:3306)/dubbo-admin?charset=utf8&parseTime=true +security: + ca-validity: 2592000000 #30 * 24 * 60 * 60 * 1000ms + cert-validity: 3600000 #1 * 60 * 60 * 1000ms + enable-oidc-check: true + webhook-port: 30080 + webhook-allow-on-err: true +kube-config: + namespace: dubbo-system + service-name: dubbo-ca + in-pod-env: false + rest-config-qps: 50 + rest-config-burst: 100 +grpc-cp-server: + plain-server-port: 30060 + secure-server-port: 30062 + debug-port: 30070 +dubbo: + registries: + demoZK: + protocol: zookeeper + address: 127.0.0.1:2181 + protocols: + triple: + name: tri + port: 20000 + provider: + services: + MockServiceServer: + interface: "" # must be compatible with grpc or dubbo-java \ No newline at end of file diff --git a/conf/mock_provider.yml b/conf/mock_provider.yml deleted file mode 100644 index 8f1700d1..00000000 --- a/conf/mock_provider.yml +++ /dev/null @@ -1,28 +0,0 @@ -# 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. - -dubbo: - registries: - demoZK: - protocol: zookeeper - address: 127.0.0.1:2181 - protocols: - triple: - name: tri - port: 20000 - provider: - services: - MockServiceServer: - interface: "" # must be compatible with grpc or dubbo-java \ No newline at end of file diff --git a/pkg/admin/bootstrap.go b/pkg/admin/bootstrap.go index a9218adf..1bc3d2b2 100644 --- a/pkg/admin/bootstrap.go +++ b/pkg/admin/bootstrap.go @@ -19,7 +19,6 @@ package admin import ( "net/url" - "os" "strings" "github.com/apache/dubbo-admin/pkg/admin/providers/mock" @@ -36,7 +35,6 @@ import ( "github.com/apache/dubbo-admin/pkg/admin/config" "github.com/apache/dubbo-admin/pkg/admin/constant" "github.com/apache/dubbo-admin/pkg/admin/model" - conf "github.com/apache/dubbo-admin/pkg/config" "github.com/apache/dubbo-admin/pkg/config/admin" core_runtime "github.com/apache/dubbo-admin/pkg/core/runtime" ) @@ -119,8 +117,7 @@ func RegisterOther(rt core_runtime.Runtime) error { }() // start mock cp-server - os.Setenv(constant.ConfigFileEnvKey, conf.MockProviderConf) - go mock.RunMockServiceServer() + go mock.RunMockServiceServer(rt.Config().Admin, rt.Config().Dubbo) return nil } diff --git a/pkg/admin/providers/mock/mock_provider.go b/pkg/admin/providers/mock/mock_provider.go index 04143c77..1fc6283e 100644 --- a/pkg/admin/providers/mock/mock_provider.go +++ b/pkg/admin/providers/mock/mock_provider.go @@ -19,10 +19,11 @@ package mock import ( "context" + "github.com/apache/dubbo-admin/pkg/config/admin" "github.com/apache/dubbo-admin/pkg/core/logger" - "dubbo.apache.org/dubbo-go/v3/config" + dubbogo "dubbo.apache.org/dubbo-go/v3/config" _ "dubbo.apache.org/dubbo-go/v3/imports" "github.com/apache/dubbo-admin/pkg/admin/mapper" "github.com/apache/dubbo-admin/pkg/admin/providers/mock/api" @@ -47,16 +48,28 @@ func (s *MockServiceServer) GetMockData(ctx context.Context, req *api.GetMockDat }, nil } -func RunMockServiceServer() { +func RunMockServiceServer(admin admin.Admin, dubboConfig dubbogo.RootConfig) { var mockRuleService services.MockRuleService = &services.MockRuleServiceImpl{ MockRuleMapper: &mapper.MockRuleMapperImpl{}, Logger: logger.Logger(), } - config.SetProviderService(&MockServiceServer{ + dubbogo.SetProviderService(&MockServiceServer{ mockRuleService: mockRuleService, }) - if err := config.Load(); err != nil { + + builder := dubbogo.NewRootConfigBuilder(). + SetProvider(dubbogo.NewProviderConfigBuilder().AddService("GreeterProvider", dubbogo.NewServiceConfigBuilder().SetRPCService(MockServiceServer{mockRuleService: mockRuleService}).Build()).Build()). + AddRegistry("zkRegistryKey", dubbogo.NewRegistryConfigBuilder().SetAddress(admin.Registry.Address).Build()) + + for k, v := range dubboConfig.Protocols { + builder.AddProtocol(k, dubbogo.NewProtocolConfigBuilder().SetName(v.Name).SetPort(v.Port).Build()) + } + + rootConfig := builder.Build() + + if err := dubbogo.Load(dubbogo.WithRootConfig(rootConfig)); err != nil { panic(err) } + select {} } diff --git a/pkg/authority/setup.go b/pkg/authority/setup.go index 8c91fac2..026920dc 100644 --- a/pkg/authority/setup.go +++ b/pkg/authority/setup.go @@ -27,6 +27,7 @@ import ( ) func Setup(rt core_runtime.Runtime) error { + server := server.NewServer(rt.Config()) if rt.Config().KubeConfig.InPodEnv { server.WebhookServer = webhook.NewWebhook( diff --git a/pkg/config/admin/config.go b/pkg/config/admin/config.go index 9ec3b315..31bb4aa1 100644 --- a/pkg/config/admin/config.go +++ b/pkg/config/admin/config.go @@ -29,6 +29,7 @@ type Admin struct { Registry AddressConfig `yaml:"registry"` Prometheus Prometheus `yaml:"prometheus"` MysqlDSN string `yaml:"mysql-dsn"` + AdminOnly bool `yaml:"admin-only"` } type Prometheus struct { diff --git a/pkg/config/app/dubbo-cp/config.go b/pkg/config/app/dubbo-cp/config.go index 651439f4..d3f40e49 100644 --- a/pkg/config/app/dubbo-cp/config.go +++ b/pkg/config/app/dubbo-cp/config.go @@ -18,6 +18,7 @@ package dubbo_cp import ( + dubbogo "dubbo.apache.org/dubbo-go/v3/config" "github.com/apache/dubbo-admin/pkg/config" "github.com/pkg/errors" @@ -31,7 +32,8 @@ type Config struct { Admin admin.Admin `yaml:"admin"` GrpcServer server.ServerConfig `yaml:"grpc-cp-server"` Security security.SecurityConfig `yaml:"security"` - KubeConfig kube.KubeConfig `yaml:"KubeConfig-config"` + KubeConfig kube.KubeConfig `yaml:"kube-config"` + Dubbo dubbogo.RootConfig `yaml:"dubbo"` } func (c *Config) Sanitize() { @@ -77,7 +79,7 @@ var DefaultConfig = func() Config { Port: "9090", MonitorPort: "22222", }, - MysqlDSN: "root:password@tcp(127.0.0.1:3306)/dubbo-admin?charset=utf8&parseTime=true", + //MysqlDSN: "root:password@tcp(127.0.0.1:3306)/dubbo-admin?charset=utf8&parseTime=true", }, GrpcServer: server.ServerConfig{ PlainServerPort: 30060, @@ -101,5 +103,6 @@ var DefaultConfig = func() Config { RestConfigQps: 50, RestConfigBurst: 100, }, + Dubbo: dubbogo.RootConfig{}, } } diff --git a/pkg/config/app/dubbo-cp/dubbo-cp.default.yaml b/pkg/config/app/dubbo-cp/dubbo-cp.default.yaml index c3f92256..f5b31b10 100644 --- a/pkg/config/app/dubbo-cp/dubbo-cp.default.yaml +++ b/pkg/config/app/dubbo-cp/dubbo-cp.default.yaml @@ -23,7 +23,7 @@ admin: ip: 127.0.0.1 port: 9090 monitor-port: 22222 - mysql-dsn: root:password@tcp(127.0.0.1:3306)/dubbo-admin?charset=utf8&parseTime=true +# mysql-dsn: root:password@tcp(127.0.0.1:3306)/dubbo-admin?charset=utf8&parseTime=true security: ca-validity: 30 * 24 * 60 * 60 * 1000 cert-validity: 1 * 60 * 60 * 1000 @@ -36,7 +36,20 @@ kube-config: in-pod-env: false rest-config-qps: 50 rest-config-burst: 100 -grpc-server: +grpc-cp-server: plain-server-port: 30060 secure-server-port: 30062 - debug-port: 30070 \ No newline at end of file + debug-port: 30070 +dubbo: + registries: + demoZK: + protocol: zookeeper + address: 127.0.0.1:2181 + protocols: + triple: + name: tri + port: 20000 + provider: + services: + MockServiceServer: + interface: "" # must be compatible with grpc or dubbo-java \ No newline at end of file diff --git a/pkg/core/bootstrap/bootstrap.go b/pkg/core/bootstrap/bootstrap.go index 4d735beb..10a87ab1 100644 --- a/pkg/core/bootstrap/bootstrap.go +++ b/pkg/core/bootstrap/bootstrap.go @@ -19,6 +19,7 @@ package bootstrap import ( "context" + "github.com/apache/dubbo-admin/pkg/core/election/universal" dubbo_cp "github.com/apache/dubbo-admin/pkg/config/app/dubbo-cp" "github.com/apache/dubbo-admin/pkg/core/cert/provider" @@ -40,6 +41,7 @@ func buildRuntime(appCtx context.Context, cfg *dubbo_cp.Config) (core_runtime.Ru } if cfg.KubeConfig.IsKubernetesConnected == false { + builder.WithComponentManager(component.NewManager(universal.NewLeaderElection())) rt, err := builder.Build() if err != nil { return nil, err diff --git a/pkg/core/election/universal/leaderelection.go b/pkg/core/election/universal/leaderelection.go new file mode 100644 index 00000000..85f6511e --- /dev/null +++ b/pkg/core/election/universal/leaderelection.go @@ -0,0 +1,55 @@ +/* + * 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 universal + +import ( + "github.com/apache/dubbo-admin/pkg/core/logger" + "github.com/apache/dubbo-admin/pkg/core/runtime/component" + syncatomic "sync/atomic" +) + +type LeaderElection struct { + callbacks []component.LeaderCallbacks + leader int32 +} + +// Start will start leader election, calling all runFns when we become the leader. +func (l *LeaderElection) Start(stop <-chan struct{}) { + logger.Sugar().Info("starting Leader Elector") + +} + +func (p *LeaderElection) AddCallbacks(callbacks component.LeaderCallbacks) { + p.callbacks = append(p.callbacks, callbacks) +} + +func (p *LeaderElection) IsLeader() bool { + return syncatomic.LoadInt32(&(p.leader)) == 1 +} + +func (p *LeaderElection) setLeader(leader bool) { + var value int32 = 0 + if leader { + value = 1 + } + syncatomic.StoreInt32(&p.leader, value) +} + +func NewLeaderElection() *LeaderElection { + return &LeaderElection{} +} diff --git a/pkg/core/runtime/builder.go b/pkg/core/runtime/builder.go index 5faabbd9..36dd7686 100644 --- a/pkg/core/runtime/builder.go +++ b/pkg/core/runtime/builder.go @@ -84,15 +84,15 @@ func BuilderFor(appCtx context.Context, cfg *dubbo_cp.Config) (*Builder, error) } func (b *Builder) Build() (Runtime, error) { - if b.grpcServer == nil { - return nil, errors.Errorf("grpcServer has not been configured") - } - if b.certStorage == nil { - return nil, errors.Errorf("certStorage has not been configured") - } - if b.kubuClient == nil { - return nil, errors.Errorf("kubuClient has not been configured") - } + //if b.grpcServer == nil && { + // return nil, errors.Errorf("grpcServer has not been configured. ") + //} + //if b.certStorage == nil { + // return nil, errors.Errorf("certStorage has not been configured") + //} + //if b.kubuClient == nil { + // return nil, errors.Errorf("kubuClient has not been configured") + //} return &runtime{ RuntimeInfo: b.runtimeInfo, RuntimeContext: &runtimeContext{
