dubbo.apache.org/dubbo-go/v3@v3.1.1/config_center/mock_dynamic_config.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 config_center
    19  
    20  import (
    21  	"sync"
    22  )
    23  
    24  import (
    25  	gxset "github.com/dubbogo/gost/container/set"
    26  
    27  	"gopkg.in/yaml.v2"
    28  )
    29  
    30  import (
    31  	"dubbo.apache.org/dubbo-go/v3/common"
    32  	"dubbo.apache.org/dubbo-go/v3/common/constant"
    33  	"dubbo.apache.org/dubbo-go/v3/config_center/parser"
    34  	"dubbo.apache.org/dubbo-go/v3/remoting"
    35  )
    36  
    37  // MockDynamicConfigurationFactory defines content
    38  type MockDynamicConfigurationFactory struct {
    39  	Content             string
    40  	ConfiguratorContent string
    41  }
    42  
    43  const (
    44  	mockServiceName = "org.apache.dubbo-go.mockService"
    45  )
    46  
    47  var (
    48  	once                 sync.Once
    49  	dynamicConfiguration *MockDynamicConfiguration
    50  )
    51  
    52  // GetDynamicConfiguration returns a DynamicConfiguration
    53  func (f *MockDynamicConfigurationFactory) GetDynamicConfiguration(_ *common.URL) (DynamicConfiguration, error) {
    54  	var err error
    55  	once.Do(func() {
    56  		dynamicConfiguration = &MockDynamicConfiguration{listener: map[string]ConfigurationListener{}}
    57  		dynamicConfiguration.SetParser(&parser.DefaultConfigurationParser{})
    58  
    59  		dynamicConfiguration.content = `
    60  	dubbo.consumer.request_timeout=5s
    61  	dubbo.consumer.connect_timeout=5s
    62  	dubbo.application.organization=ikurento.com
    63  	dubbo.application.name=BDTService
    64  	dubbo.application.module=dubbogo user-info server
    65  	dubbo.application.version=0.0.1
    66  	dubbo.application.owner=ZX
    67  	dubbo.application.environment=dev
    68  	dubbo.registries.hangzhouzk.protocol=zookeeper
    69  	dubbo.registries.hangzhouzk.timeout=3s
    70  	dubbo.registries.hangzhouzk.address=127.0.0.1:2181
    71  	dubbo.registries.shanghaizk.protocol=zookeeper
    72  	dubbo.registries.shanghaizk.timeout=3s
    73  	dubbo.registries.shanghaizk.address=127.0.0.1:2182
    74  	dubbo.service.com.ikurento.user.UserProvider.protocol=dubbo
    75  	dubbo.service.com.ikurento.user.UserProvider.interface=com.ikurento.user.UserProvider
    76  	dubbo.service.com.ikurento.user.UserProvider.loadbalance=random
    77  	dubbo.service.com.ikurento.user.UserProvider.warmup=100
    78  	dubbo.service.com.ikurento.user.UserProvider.cluster=failover
    79  	dubbo.protocols.jsonrpc1.name=jsonrpc
    80  	dubbo.protocols.jsonrpc1.ip=127.0.0.1
    81  	dubbo.protocols.jsonrpc1.port=20001
    82  `
    83  	})
    84  	if len(f.Content) != 0 {
    85  		dynamicConfiguration.content = f.Content
    86  	} else if len(f.ConfiguratorContent) != 0 {
    87  		dynamicConfiguration.content = f.ConfiguratorContent
    88  	}
    89  	return dynamicConfiguration, err
    90  }
    91  
    92  // PublishConfig will publish the config with the (key, group, value) pair
    93  func (c *MockDynamicConfiguration) PublishConfig(string, string, string) error {
    94  	return nil
    95  }
    96  
    97  // GetConfigKeysByGroup will return all keys with the group
    98  func (c *MockDynamicConfiguration) GetConfigKeysByGroup(group string) (*gxset.HashSet, error) {
    99  	return gxset.NewSet(c.content), nil
   100  }
   101  
   102  // MockDynamicConfiguration uses to parse content and defines listener
   103  type MockDynamicConfiguration struct {
   104  	BaseDynamicConfiguration
   105  	parser   parser.ConfigurationParser
   106  	content  string
   107  	listener map[string]ConfigurationListener
   108  }
   109  
   110  // AddListener adds a listener for MockDynamicConfiguration
   111  func (c *MockDynamicConfiguration) AddListener(key string, listener ConfigurationListener, _ ...Option) {
   112  	c.listener[key] = listener
   113  }
   114  
   115  // RemoveListener removes the listener for MockDynamicConfiguration
   116  func (c *MockDynamicConfiguration) RemoveListener(_ string, _ ConfigurationListener, _ ...Option) {
   117  	// mock remove
   118  }
   119  
   120  // GetConfig returns content of MockDynamicConfiguration
   121  func (c *MockDynamicConfiguration) GetConfig(_ string, _ ...Option) (string, error) {
   122  	return c.content, nil
   123  }
   124  
   125  // GetConfigs For zookeeper, getConfig and getConfigs have the same meaning.
   126  func (c *MockDynamicConfiguration) GetConfigs(key string, opts ...Option) (string, error) {
   127  	return c.GetConfig(key, opts...)
   128  }
   129  
   130  // Parser returns a parser of MockDynamicConfiguration
   131  func (c *MockDynamicConfiguration) Parser() parser.ConfigurationParser {
   132  	return c.parser
   133  }
   134  
   135  // SetParser sets parser of MockDynamicConfiguration
   136  func (c *MockDynamicConfiguration) SetParser(p parser.ConfigurationParser) {
   137  	c.parser = p
   138  }
   139  
   140  // GetProperties gets content of MockDynamicConfiguration
   141  func (c *MockDynamicConfiguration) GetProperties(_ string, _ ...Option) (string, error) {
   142  	return c.content, nil
   143  }
   144  
   145  // GetInternalProperty For zookeeper, getConfig and getConfigs have the same meaning.
   146  func (c *MockDynamicConfiguration) GetInternalProperty(key string, opts ...Option) (string, error) {
   147  	return c.GetProperties(key, opts...)
   148  }
   149  
   150  // GetRule gets properties of MockDynamicConfiguration
   151  func (c *MockDynamicConfiguration) GetRule(key string, opts ...Option) (string, error) {
   152  	return c.GetProperties(key, opts...)
   153  }
   154  
   155  // MockServiceConfigEvent returns ConfiguratorConfig
   156  func (c *MockDynamicConfiguration) MockServiceConfigEvent() {
   157  	config := &parser.ConfiguratorConfig{
   158  		ConfigVersion: "2.7.1",
   159  		Scope:         parser.GeneralType,
   160  		Key:           mockServiceName,
   161  		Enabled:       true,
   162  		Configs: []parser.ConfigItem{
   163  			{
   164  				Type:       parser.GeneralType,
   165  				Enabled:    true,
   166  				Addresses:  []string{"0.0.0.0"},
   167  				Services:   []string{mockServiceName},
   168  				Side:       "provider",
   169  				Parameters: map[string]string{"cluster": "mock1"},
   170  			},
   171  		},
   172  	}
   173  	value, _ := yaml.Marshal(config)
   174  	key := mockServiceName + ":1.0.0:group" + constant.ConfiguratorSuffix
   175  	c.listener[key].Process(&ConfigChangeEvent{Key: key, Value: string(value), ConfigType: remoting.EventTypeAdd})
   176  }
   177  
   178  // MockApplicationConfigEvent returns ConfiguratorConfig
   179  func (c *MockDynamicConfiguration) MockApplicationConfigEvent() {
   180  	config := &parser.ConfiguratorConfig{
   181  		ConfigVersion: "2.7.1",
   182  		Scope:         parser.ScopeApplication,
   183  		Key:           mockServiceName,
   184  		Enabled:       true,
   185  		Configs: []parser.ConfigItem{
   186  			{
   187  				Type:       parser.ScopeApplication,
   188  				Enabled:    true,
   189  				Addresses:  []string{"0.0.0.0"},
   190  				Services:   []string{mockServiceName},
   191  				Side:       "provider",
   192  				Parameters: map[string]string{"cluster": "mock1"},
   193  			},
   194  		},
   195  	}
   196  	value, _ := yaml.Marshal(config)
   197  	key := "test-application" + constant.ConfiguratorSuffix
   198  	c.listener[key].Process(&ConfigChangeEvent{Key: key, Value: string(value), ConfigType: remoting.EventTypeAdd})
   199  }