/
CC.java
330 lines (251 loc) · 12.7 KB
/
CC.java
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
* (C) Copyright 2015-2016 the original author or authors.
*
* Licensed 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.
*
* Contributors:
* ohun@live.cn (夜色)
*/
package com.mpush.tools.config;
import com.mpush.api.spi.net.DnsMapping;
import com.mpush.tools.common.Profiler;
import com.mpush.tools.config.data.RedisNode;
import com.typesafe.config.*;
import java.io.File;
import java.time.Duration;
import java.util.*;
import java.util.concurrent.TimeUnit;
import static java.util.stream.Collectors.toCollection;
/**
* mpush 配置中心
* Created by yxx on 2016/5/20.
*
* @author ohun@live.cn
*/
public interface CC {
Config cfg = load();
static Config load() {
Config config = ConfigFactory.load();//扫描加载所有可用的配置文件
String custom_conf = "mp.conf";//加载自定义配置, 值来自jvm启动参数指定-Dmp.conf
if (config.hasPath(custom_conf)) {
File file = new File(config.getString(custom_conf));
if (file.exists()) {
Config custom = ConfigFactory.parseFile(file);
config = custom.withFallback(config);
}
}
return config;
}
interface mp {
Config cfg = CC.cfg.getObject("mp").toConfig();
String log_dir = cfg.getString("log-dir");
String log_level = cfg.getString("log-level");
String log_conf_path = cfg.getString("log-conf-path");
interface core {
Config cfg = mp.cfg.getObject("core").toConfig();
int session_expired_time = (int) cfg.getDuration("session-expired-time").getSeconds();
int max_heartbeat = (int) cfg.getDuration("max-heartbeat", TimeUnit.MILLISECONDS);
int max_packet_size = (int) cfg.getMemorySize("max-packet-size").toBytes();
int min_heartbeat = (int) cfg.getDuration("min-heartbeat", TimeUnit.MILLISECONDS);
long compress_threshold = cfg.getBytes("compress-threshold");
int max_hb_timeout_times = cfg.getInt("max-hb-timeout-times");
String epoll_provider = cfg.getString("epoll-provider");
static boolean useNettyEpoll() {
if (!"netty".equals(CC.mp.core.epoll_provider)) return false;
String name = CC.cfg.getString("os.name").toLowerCase(Locale.UK).trim();
return name.startsWith("linux");//只在linux下使用netty提供的epoll库
}
}
interface net {
Config cfg = mp.cfg.getObject("net").toConfig();
int connect_server_port = cfg.getInt("connect-server-port");
int gateway_server_port = cfg.getInt("gateway-server-port");
int admin_server_port = cfg.getInt("admin-server-port");
int gateway_client_port = cfg.getInt("gateway-client-port");
String gateway_server_net = cfg.getString("gateway-server-net");
String gateway_server_multicast = cfg.getString("gateway-server-multicast");
String gateway_client_multicast = cfg.getString("gateway-client-multicast");
static boolean tcpGateway() {
return "tcp".equals(gateway_server_net);
}
static boolean udpGateway() {
return "udp".equals(gateway_server_net);
}
static boolean udtGateway() {
return "udt".equals(gateway_server_net);
}
static boolean sctpGateway() {
return "sctp".equals(gateway_server_net);
}
interface public_ip_mapping {
Map<String, Object> mappings = net.cfg.getObject("public-host-mapping").unwrapped();
static String getString(String localIp) {
return (String) mappings.get(localIp);
}
}
interface snd_buf {
Config cfg = net.cfg.getObject("snd_buf").toConfig();
int connect_server = (int) cfg.getMemorySize("connect-server").toBytes();
int gateway_server = (int) cfg.getMemorySize("gateway-server").toBytes();
int gateway_client = (int) cfg.getMemorySize("gateway-client").toBytes();
}
interface rcv_buf {
Config cfg = net.cfg.getObject("rcv_buf").toConfig();
int connect_server = (int) cfg.getMemorySize("connect-server").toBytes();
int gateway_server = (int) cfg.getMemorySize("gateway-server").toBytes();
int gateway_client = (int) cfg.getMemorySize("gateway-client").toBytes();
}
interface traffic_shaping {
Config cfg = net.cfg.getObject("traffic-shaping").toConfig();
interface gateway_client {
Config cfg = traffic_shaping.cfg.getObject("gateway-client").toConfig();
boolean enabled = cfg.getBoolean("enabled");
long check_interval = cfg.getDuration("check-interval", TimeUnit.MILLISECONDS);
long write_global_limit = cfg.getBytes("write-global-limit");
long read_global_limit = cfg.getBytes("read-global-limit");
long write_channel_limit = cfg.getBytes("write-channel-limit");
long read_channel_limit = cfg.getBytes("read-channel-limit");
}
interface gateway_server {
Config cfg = traffic_shaping.cfg.getObject("gateway-server").toConfig();
boolean enabled = cfg.getBoolean("enabled");
long check_interval = cfg.getDuration("check-interval", TimeUnit.MILLISECONDS);
long write_global_limit = cfg.getBytes("write-global-limit");
long read_global_limit = cfg.getBytes("read-global-limit");
long write_channel_limit = cfg.getBytes("write-channel-limit");
long read_channel_limit = cfg.getBytes("read-channel-limit");
}
interface connect_server {
Config cfg = traffic_shaping.cfg.getObject("connect-server").toConfig();
boolean enabled = cfg.getBoolean("enabled");
long check_interval = cfg.getDuration("check-interval", TimeUnit.MILLISECONDS);
long write_global_limit = cfg.getBytes("write-global-limit");
long read_global_limit = cfg.getBytes("read-global-limit");
long write_channel_limit = cfg.getBytes("write-channel-limit");
long read_channel_limit = cfg.getBytes("read-channel-limit");
}
}
}
interface security {
Config cfg = mp.cfg.getObject("security").toConfig();
int aes_key_length = cfg.getInt("aes-key-length");
String public_key = cfg.getString("public-key");
String private_key = cfg.getString("private-key");
}
interface thread {
Config cfg = mp.cfg.getObject("thread").toConfig();
interface pool {
Config cfg = thread.cfg.getObject("pool").toConfig();
int conn_work = cfg.getInt("conn-work");
int http_work = cfg.getInt("http-work");
int push_task = cfg.getInt("push-task");
int push_client = cfg.getInt("push-client");
int ack_timer = cfg.getInt("ack-timer");
interface event_bus {
Config cfg = pool.cfg.getObject("event-bus").toConfig();
int min = cfg.getInt("min");
int max = cfg.getInt("max");
int queue_size = cfg.getInt("queue-size");
}
interface mq {
Config cfg = pool.cfg.getObject("mq").toConfig();
int min = cfg.getInt("min");
int max = cfg.getInt("max");
int queue_size = cfg.getInt("queue-size");
}
}
}
interface zk {
Config cfg = mp.cfg.getObject("zk").toConfig();
int sessionTimeoutMs = (int) cfg.getDuration("sessionTimeoutMs", TimeUnit.MILLISECONDS);
String watch_path = cfg.getString("watch-path");
int connectionTimeoutMs = (int) cfg.getDuration("connectionTimeoutMs", TimeUnit.MILLISECONDS);
String namespace = cfg.getString("namespace");
String digest = cfg.getString("digest");
String server_address = cfg.getString("server-address");
interface retry {
Config cfg = zk.cfg.getObject("retry").toConfig();
int maxRetries = cfg.getInt("maxRetries");
int baseSleepTimeMs = (int) cfg.getDuration("baseSleepTimeMs", TimeUnit.MILLISECONDS);
int maxSleepMs = (int) cfg.getDuration("maxSleepMs", TimeUnit.MILLISECONDS);
}
}
interface redis {
Config cfg = mp.cfg.getObject("redis").toConfig();
boolean write_to_zk = cfg.getBoolean("write-to-zk");
String password = cfg.getString("password");
String clusterModel = cfg.getString("cluster-model");
List<RedisNode> nodes = cfg.getList("nodes")
.stream()//第一纬度数组
.map(v -> RedisNode.from(v.unwrapped().toString()))
.collect(toCollection(ArrayList::new));
static boolean isCluster() {
return "cluster".equals(clusterModel);
}
static <T> T getPoolConfig(Class<T> clazz) {
return ConfigBeanImpl.createInternal(cfg.getObject("config").toConfig(), clazz);
}
}
interface http {
Config cfg = mp.cfg.getObject("http").toConfig();
boolean proxy_enabled = cfg.getBoolean("proxy-enabled");
int default_read_timeout = (int) cfg.getDuration("default-read-timeout", TimeUnit.MILLISECONDS);
int max_conn_per_host = cfg.getInt("max-conn-per-host");
long max_content_length = cfg.getBytes("max-content-length");
Map<String, List<DnsMapping>> dns_mapping = loadMapping();
static Map<String, List<DnsMapping>> loadMapping() {
Map<String, List<DnsMapping>> map = new HashMap<>();
cfg.getObject("dns-mapping").forEach((s, v) ->
map.put(s, ConfigList.class.cast(v)
.stream()
.map(cv -> DnsMapping.parse((String) cv.unwrapped()))
.collect(toCollection(ArrayList::new))
)
);
return map;
}
}
interface push {
Config cfg = mp.cfg.getObject("push").toConfig();
interface flow_control {
Config cfg = push.cfg.getObject("flow-control").toConfig();
interface global {
Config cfg = flow_control.cfg.getObject("global").toConfig();
int limit = cfg.getNumber("limit").intValue();
int max = cfg.getInt("max");
int duration = (int) cfg.getDuration("duration").toMillis();
}
interface broadcast {
Config cfg = flow_control.cfg.getObject("broadcast").toConfig();
int limit = cfg.getInt("limit");
int max = cfg.getInt("max");
int duration = (int) cfg.getDuration("duration").toMillis();
}
}
}
interface monitor {
Config cfg = mp.cfg.getObject("monitor").toConfig();
String dump_dir = cfg.getString("dump-dir");
boolean dump_stack = cfg.getBoolean("dump-stack");
boolean print_log = cfg.getBoolean("print-log");
Duration dump_period = cfg.getDuration("dump-period");
boolean profile_enabled = cfg.getBoolean("profile-enabled");
Duration profile_slowly_duration = cfg.getDuration("profile-slowly-duration");
}
interface spi {
Config cfg = mp.cfg.getObject("spi").toConfig();
String thread_pool_factory = cfg.getString("thread-pool-factory");
String dns_mapping_manager = cfg.getString("dns-mapping-manager");
}
}
}