forked from apache/dubbo-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmock_dynamic_config.go
178 lines (156 loc) · 5.71 KB
/
mock_dynamic_config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
* 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 config_center
import (
"sync"
)
import (
"gopkg.in/yaml.v2"
)
import (
"github.com/apache/dubbo-go/common"
"github.com/apache/dubbo-go/common/constant"
"github.com/apache/dubbo-go/config_center/parser"
"github.com/apache/dubbo-go/remoting"
)
// MockDynamicConfigurationFactory ...
type MockDynamicConfigurationFactory struct {
Content string
}
var (
once sync.Once
dynamicConfiguration *MockDynamicConfiguration
)
// GetDynamicConfiguration ...
func (f *MockDynamicConfigurationFactory) GetDynamicConfiguration(url *common.URL) (DynamicConfiguration, error) {
var err error
once.Do(func() {
dynamicConfiguration = &MockDynamicConfiguration{listener: map[string]ConfigurationListener{}}
dynamicConfiguration.SetParser(&parser.DefaultConfigurationParser{})
dynamicConfiguration.content = `
dubbo.consumer.request_timeout=5s
dubbo.consumer.connect_timeout=5s
dubbo.application.organization=ikurento.com
dubbo.application.name=BDTService
dubbo.application.module=dubbogo user-info server
dubbo.application.version=0.0.1
dubbo.application.owner=ZX
dubbo.application.environment=dev
dubbo.registries.hangzhouzk.protocol=zookeeper
dubbo.registries.hangzhouzk.timeout=3s
dubbo.registries.hangzhouzk.address=127.0.0.1:2181
dubbo.registries.shanghaizk.protocol=zookeeper
dubbo.registries.shanghaizk.timeout=3s
dubbo.registries.shanghaizk.address=127.0.0.1:2182
dubbo.service.com.ikurento.user.UserProvider.protocol=dubbo
dubbo.service.com.ikurento.user.UserProvider.interface=com.ikurento.user.UserProvider
dubbo.service.com.ikurento.user.UserProvider.loadbalance=random
dubbo.service.com.ikurento.user.UserProvider.warmup=100
dubbo.service.com.ikurento.user.UserProvider.cluster=failover
dubbo.protocols.jsonrpc1.name=jsonrpc
dubbo.protocols.jsonrpc1.ip=127.0.0.1
dubbo.protocols.jsonrpc1.port=20001
`
})
if len(f.Content) != 0 {
dynamicConfiguration.content = f.Content
}
return dynamicConfiguration, err
}
// MockDynamicConfiguration ...
type MockDynamicConfiguration struct {
parser parser.ConfigurationParser
content string
listener map[string]ConfigurationListener
}
// AddListener ...
func (c *MockDynamicConfiguration) AddListener(key string, listener ConfigurationListener, opions ...Option) {
c.listener[key] = listener
}
// RemoveListener ...
func (c *MockDynamicConfiguration) RemoveListener(key string, listener ConfigurationListener, opions ...Option) {
}
// GetConfig ...
func (c *MockDynamicConfiguration) GetConfig(key string, opts ...Option) (string, error) {
return c.content, nil
}
//For zookeeper, getConfig and getConfigs have the same meaning.
func (c *MockDynamicConfiguration) GetConfigs(key string, opts ...Option) (string, error) {
return c.GetConfig(key, opts...)
}
// Parser ...
func (c *MockDynamicConfiguration) Parser() parser.ConfigurationParser {
return c.parser
}
// SetParser ...
func (c *MockDynamicConfiguration) SetParser(p parser.ConfigurationParser) {
c.parser = p
}
// GetProperties ...
func (c *MockDynamicConfiguration) GetProperties(key string, opts ...Option) (string, error) {
return c.content, nil
}
//For zookeeper, getConfig and getConfigs have the same meaning.
func (c *MockDynamicConfiguration) GetInternalProperty(key string, opts ...Option) (string, error) {
return c.GetProperties(key, opts...)
}
// GetRule ...
func (c *MockDynamicConfiguration) GetRule(key string, opts ...Option) (string, error) {
return c.GetProperties(key, opts...)
}
// MockServiceConfigEvent ...
func (c *MockDynamicConfiguration) MockServiceConfigEvent() {
config := &parser.ConfiguratorConfig{
ConfigVersion: "2.7.1",
Scope: parser.GeneralType,
Key: "org.apache.dubbo-go.mockService",
Enabled: true,
Configs: []parser.ConfigItem{
{Type: parser.GeneralType,
Enabled: true,
Addresses: []string{"0.0.0.0"},
Services: []string{"org.apache.dubbo-go.mockService"},
Side: "provider",
Parameters: map[string]string{"cluster": "mock1"},
},
},
}
value, _ := yaml.Marshal(config)
key := "group*org.apache.dubbo-go.mockService:1.0.0" + constant.CONFIGURATORS_SUFFIX
c.listener[key].Process(&ConfigChangeEvent{Key: key, Value: string(value), ConfigType: remoting.EventTypeAdd})
}
// MockApplicationConfigEvent ...
func (c *MockDynamicConfiguration) MockApplicationConfigEvent() {
config := &parser.ConfiguratorConfig{
ConfigVersion: "2.7.1",
Scope: parser.ScopeApplication,
Key: "org.apache.dubbo-go.mockService",
Enabled: true,
Configs: []parser.ConfigItem{
{Type: parser.ScopeApplication,
Enabled: true,
Addresses: []string{"0.0.0.0"},
Services: []string{"org.apache.dubbo-go.mockService"},
Side: "provider",
Parameters: map[string]string{"cluster": "mock1"},
},
},
}
value, _ := yaml.Marshal(config)
key := "test-application" + constant.CONFIGURATORS_SUFFIX
c.listener[key].Process(&ConfigChangeEvent{Key: key, Value: string(value), ConfigType: remoting.EventTypeAdd})
}