Skip to content

Commit 31a3a05

Browse files
feat:add admin http handler. (#559)
1 parent b62c33f commit 31a3a05

File tree

20 files changed

+616
-811
lines changed

20 files changed

+616
-811
lines changed

polaris-assembly/polaris-assembly-factory/src/test/java/com/tencent/polaris/assembly/factory/PrometheusHttpServerTest.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.tencent.polaris.api.config.Configuration;
2222
import com.tencent.polaris.api.config.global.StatReporterConfig;
2323
import com.tencent.polaris.client.api.SDKContext;
24+
import com.tencent.polaris.factory.config.global.AdminConfigImpl;
2425
import com.tencent.polaris.factory.config.global.StatReporterConfigImpl;
2526
import com.tencent.polaris.plugins.stat.prometheus.handler.PrometheusHandlerConfig;
2627
import com.tencent.polaris.test.common.TestUtils;
@@ -37,10 +38,10 @@ public class PrometheusHttpServerTest {
3738
@Test
3839
public void testHttpServerWithPort() throws IOException {
3940
Configuration configuration = TestUtils.configWithEnvAddress();
40-
StatReporterConfigImpl statReporterConfig = (StatReporterConfigImpl)configuration.getGlobal().getStatReporter();
41+
((AdminConfigImpl) configuration.getGlobal().getAdmin()).setHost("0.0.0.0");
42+
((AdminConfigImpl) configuration.getGlobal().getAdmin()).setPort(18080);
43+
StatReporterConfigImpl statReporterConfig = (StatReporterConfigImpl) configuration.getGlobal().getStatReporter();
4144
PrometheusHandlerConfig prometheusHandlerConfig = new PrometheusHandlerConfig();
42-
prometheusHandlerConfig.setHost("0.0.0.0");
43-
prometheusHandlerConfig.setPort(18080);
4445
prometheusHandlerConfig.setPath("/metric");
4546
prometheusHandlerConfig.setType("pull");
4647
statReporterConfig.setPluginConfig(StatReporterConfig.DEFAULT_REPORTER_PROMETHEUS, prometheusHandlerConfig);
@@ -58,10 +59,10 @@ public void testHttpServerWithPort() throws IOException {
5859
@Test
5960
public void testHttpServerRandomPort() throws IOException {
6061
Configuration configuration = TestUtils.configWithEnvAddress();
61-
StatReporterConfigImpl statReporterConfig = (StatReporterConfigImpl)configuration.getGlobal().getStatReporter();
62+
((AdminConfigImpl) configuration.getGlobal().getAdmin()).setHost("0.0.0.0");
63+
((AdminConfigImpl) configuration.getGlobal().getAdmin()).setPort(0);
64+
StatReporterConfigImpl statReporterConfig = (StatReporterConfigImpl) configuration.getGlobal().getStatReporter();
6265
PrometheusHandlerConfig prometheusHandlerConfig = new PrometheusHandlerConfig();
63-
prometheusHandlerConfig.setHost("0.0.0.0");
64-
prometheusHandlerConfig.setPort(0);
6566
prometheusHandlerConfig.setPath("/metric");
6667
prometheusHandlerConfig.setType("pull");
6768
statReporterConfig.setPluginConfig(StatReporterConfig.DEFAULT_REPORTER_PROMETHEUS, prometheusHandlerConfig);
@@ -85,10 +86,10 @@ public void testHttpServerRandomPort() throws IOException {
8586
@Test
8687
public void testHttpServerWithPath() throws IOException {
8788
Configuration configuration = TestUtils.configWithEnvAddress();
88-
StatReporterConfigImpl statReporterConfig = (StatReporterConfigImpl)configuration.getGlobal().getStatReporter();
89+
((AdminConfigImpl) configuration.getGlobal().getAdmin()).setHost("0.0.0.0");
90+
((AdminConfigImpl) configuration.getGlobal().getAdmin()).setPort(18081);
91+
StatReporterConfigImpl statReporterConfig = (StatReporterConfigImpl) configuration.getGlobal().getStatReporter();
8992
PrometheusHandlerConfig prometheusHandlerConfig = new PrometheusHandlerConfig();
90-
prometheusHandlerConfig.setHost("0.0.0.0");
91-
prometheusHandlerConfig.setPort(18081);
9293
prometheusHandlerConfig.setPath("/customMetrics");
9394
prometheusHandlerConfig.setType("pull");
9495
statReporterConfig.setPluginConfig(StatReporterConfig.DEFAULT_REPORTER_PROMETHEUS, prometheusHandlerConfig);

polaris-common/polaris-client/src/main/java/com/tencent/polaris/client/api/SDKContext.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ public class SDKContext extends Destroyable implements InitContext, AutoCloseabl
9898
* 构造器
9999
*
100100
* @param configuration 配置
101-
* @param plugins 插件工厂
102-
* @param valueContext 上下文
101+
* @param plugins 插件工厂
102+
* @param valueContext 上下文
103103
*/
104104
public SDKContext(Configuration configuration, Manager plugins, ValueContext valueContext) {
105105
this.configuration = configuration;
@@ -311,7 +311,7 @@ public synchronized void init() throws PolarisException {
311311
}
312312
extensions.init(configuration, plugins, valueContext);
313313
plugins.postContextInitPlugins(extensions);
314-
extensions.initHttpServer(plugins);
314+
extensions.initHttpServer(configuration, plugins);
315315
reportClient(extensions);
316316
registerDestroyHook(extensions);
317317
}

polaris-common/polaris-config-default/src/main/resources/conf/default-config.yml

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ global:
8686
#描述: 事件上报插件名列表
8787
reporters:
8888
- logger
89+
# 描述:Admin相关的配置
90+
admin:
91+
# 描述:Admin的监听的IP
92+
host: 0.0.0.0
93+
# 描述:Admin监听的端口
94+
port: 28080
8995
#描述: 监控及日志数据上报相关配置
9096
statReporter:
9197
#描述: 是否启用上报
@@ -97,19 +103,10 @@ global:
97103
#默认值:pull
98104
#范围:pull|push
99105
type: pull
100-
#描述: 设置 prometheus http-server 的监听端口
101-
#类型:int
102-
#默认值: 28080, 默认会自动从 [28080, 28090] 进行逐个探测占用
103-
#如果设置为负数,则不会开启默认的http-server,如果设置为0,则随机选择一个可用端口进行启动 http-server
104-
port: 28080
105106
#描述: 设置 prometheus http-server 的拉取path
106107
#类型:string
107108
#默认值: /metric
108109
path: /metric
109-
#描述: 设置 prometheus http-server 的监听的IP
110-
#类型:string
111-
#默认值: 0.0.0.0
112-
host: 0.0.0.0
113110
# #描述: 设置 pushgateway 的地址, 仅 type == push 时生效
114111
# #类型:string
115112
# #默认 global.serverConnector.addresses[0]:9091
@@ -301,10 +298,6 @@ provider:
301298
lossless:
302299
# 是否启用优雅上下线
303300
enable: false
304-
# 优雅上下线功能需要对外提供接口,接口的监听IP
305-
host: 0.0.0.0
306-
# 优雅上下线功能需要对外提供接口,接口的监听端口
307-
port: 28080
308301
strategy: DELAY_BY_TIME
309302
# 优雅上线时,如果没有启动探测,则兜底延迟多久会进行上线
310303
delayRegisterInterval: 30s
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Tencent is pleased to support the open source community by making Polaris available.
3+
*
4+
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
5+
*
6+
* Licensed under the BSD 3-Clause License (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* https://opensource.org/licenses/BSD-3-Clause
11+
*
12+
* Unless required by applicable law or agreed to in writing, software distributed
13+
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
14+
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
15+
* specific language governing permissions and limitations under the License.
16+
*/
17+
18+
package com.tencent.polaris.api.config.global;
19+
20+
import com.tencent.polaris.api.config.verify.Verifier;
21+
22+
/**
23+
* Admin Config.
24+
*
25+
* @author Haotian Zhang
26+
*/
27+
public interface AdminConfig extends Verifier {
28+
29+
/**
30+
* Admin的监听IP
31+
*
32+
* @return host
33+
*/
34+
String getHost();
35+
36+
/**
37+
* Admin的监听端口
38+
*
39+
* @return port
40+
*/
41+
int getPort();
42+
}

polaris-common/polaris-config/src/main/java/com/tencent/polaris/api/config/global/GlobalConfig.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ public interface GlobalConfig extends Verifier {
7979
*/
8080
EventReporterConfig getEventReporter();
8181

82+
/**
83+
* Configuration of prefix of "global.admin"
84+
*
85+
* @return
86+
*/
87+
AdminConfig getAdmin();
88+
8289
/**
8390
* 获取地理位置提供者配置信息
8491
*

polaris-common/polaris-config/src/main/java/com/tencent/polaris/api/config/provider/LosslessConfig.java

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,30 +24,21 @@ public interface LosslessConfig extends Verifier {
2424

2525
/**
2626
* 是否启用无损上下线
27+
*
2728
* @return 启用无损上下线
2829
*/
2930
boolean isEnable();
3031

31-
/**
32-
* 无损下线和获取上线状态的监听IP
33-
* @return host
34-
*/
35-
String getHost();
36-
37-
/**
38-
* 无损下线和获取上线状态的监听端口
39-
* @return port
40-
*/
41-
int getPort();
42-
4332
/**
4433
* 如果没有实现健康检查,延迟注册的时间,单位毫秒
34+
*
4535
* @return long
4636
*/
4737
long getDelayRegisterInterval();
4838

4939
/**
5040
* 获取健康探测的间隔时间
41+
*
5142
* @return long
5243
*/
5344
long getHealthCheckInterval();
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Tencent is pleased to support the open source community by making Polaris available.
3+
*
4+
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
5+
*
6+
* Licensed under the BSD 3-Clause License (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* https://opensource.org/licenses/BSD-3-Clause
11+
*
12+
* Unless required by applicable law or agreed to in writing, software distributed
13+
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
14+
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
15+
* specific language governing permissions and limitations under the License.
16+
*/
17+
18+
package com.tencent.polaris.factory.config.global;
19+
20+
import com.fasterxml.jackson.annotation.JsonProperty;
21+
import com.tencent.polaris.api.config.global.AdminConfig;
22+
import com.tencent.polaris.api.utils.StringUtils;
23+
import com.tencent.polaris.factory.util.ConfigUtils;
24+
25+
public class AdminConfigImpl implements AdminConfig {
26+
27+
@JsonProperty
28+
private String host;
29+
30+
@JsonProperty
31+
private Integer port;
32+
33+
@Override
34+
public String getHost() {
35+
return host;
36+
}
37+
38+
public void setHost(String host) {
39+
this.host = host;
40+
}
41+
42+
@Override
43+
public int getPort() {
44+
return port;
45+
}
46+
47+
public void setPort(Integer port) {
48+
this.port = port;
49+
}
50+
51+
@Override
52+
public void verify() {
53+
ConfigUtils.validateString(host, "global.admin.host");
54+
ConfigUtils.validatePositiveInteger(port, "global.admin.port");
55+
}
56+
57+
@Override
58+
public void setDefault(Object defaultObject) {
59+
if (null != defaultObject) {
60+
AdminConfig adminConfig = (AdminConfig) defaultObject;
61+
if (null == port || 0 == port) {
62+
setPort(adminConfig.getPort());
63+
}
64+
if (StringUtils.isBlank(host)) {
65+
setHost(adminConfig.getHost());
66+
}
67+
}
68+
}
69+
}

polaris-common/polaris-config/src/main/java/com/tencent/polaris/factory/config/global/GlobalConfigImpl.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,10 @@ public class GlobalConfigImpl implements GlobalConfig {
6262
private EventReporterConfigImpl eventReporter;
6363

6464
@JsonProperty
65-
private LocationConfigImpl location;
65+
private AdminConfigImpl admin;
6666

67+
@JsonProperty
68+
private LocationConfigImpl location;
6769

6870
@Override
6971
public SystemConfigImpl getSystem() {
@@ -80,7 +82,6 @@ public ServerConnectorConfigImpl getServerConnector() {
8082
return serverConnector;
8183
}
8284

83-
8485
@Override
8586
public LocationConfig getLocation() {
8687
return location;
@@ -127,6 +128,11 @@ public EventReporterConfigImpl getEventReporter() {
127128
return eventReporter;
128129
}
129130

131+
@Override
132+
public AdminConfigImpl getAdmin() {
133+
return admin;
134+
}
135+
130136
@Override
131137
public void verify() {
132138
ConfigUtils.validateNull(system, "system");
@@ -138,6 +144,7 @@ public void verify() {
138144
ConfigUtils.validateNull(statReporter, "statReporter");
139145
ConfigUtils.validateNull(traceReporter, "traceReporter");
140146
ConfigUtils.validateNull(eventReporter, "eventReporter");
147+
ConfigUtils.validateNull(admin, "admin");
141148

142149
system.verify();
143150
api.verify();
@@ -155,6 +162,7 @@ public void verify() {
155162
statReporter.verify();
156163
traceReporter.verify();
157164
eventReporter.verify();
165+
admin.verify();
158166
}
159167

160168
@Override
@@ -177,6 +185,9 @@ public void setDefault(Object defaultObject) {
177185
if (null == eventReporter) {
178186
eventReporter = new EventReporterConfigImpl();
179187
}
188+
if (null == admin) {
189+
admin = new AdminConfigImpl();
190+
}
180191
if (null == location) {
181192
location = new LocationConfigImpl();
182193
}
@@ -201,6 +212,7 @@ public void setDefault(Object defaultObject) {
201212
statReporter.setDefault(globalConfig.getStatReporter());
202213
traceReporter.setDefault(globalConfig.getTraceReporter());
203214
eventReporter.setDefault(globalConfig.getEventReporter());
215+
admin.setDefault(globalConfig.getAdmin());
204216
location.setDefault(globalConfig.getLocation());
205217
}
206218
}
@@ -215,6 +227,7 @@ public String toString() {
215227
", statReporter=" + statReporter +
216228
", traceReporter=" + traceReporter +
217229
", eventReporter=" + eventReporter +
230+
", admin=" + admin +
218231
", location=" + location +
219232
'}';
220233
}

0 commit comments

Comments
 (0)