Skip to content

Commit ea9e2ed

Browse files
committed
日志定时上报ApiBoot Logging Admin
1 parent 36074a0 commit ea9e2ed

File tree

4 files changed

+336
-0
lines changed

4 files changed

+336
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright [2019] [恒宇少年 - 于起宇]
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package org.minbox.framework.api.boot.plugin.logging.admin.discovery.support;
19+
20+
import org.apache.tomcat.util.codec.binary.Base64;
21+
import org.minbox.framework.api.boot.plugin.logging.admin.discovery.LoggingAdminDiscovery;
22+
23+
/**
24+
* ApiBoot Logging Abstract Admin Discovery
25+
*
26+
* @author:恒宇少年 - 于起宇
27+
* <p>
28+
* DateTime:2019-07-19 17:17
29+
* Blog:http://blog.yuqiyu.com
30+
* WebSite:http://www.jianshu.com/u/092df3f77bca
31+
* Gitee:https://gitee.com/hengboy
32+
* GitHub:https://github.com/hengboy
33+
*/
34+
public abstract class LoggingAbstractAdminDiscovery implements LoggingAdminDiscovery {
35+
/**
36+
* basic auth
37+
*/
38+
private static final String BASIC_AUTH = "Basic %s";
39+
40+
/**
41+
* get basic auth base64 string
42+
*
43+
* @param basicInfo basic info
44+
* @return basic auth base64 string
45+
*/
46+
protected String getBasicBase64(String basicInfo) {
47+
String basicBase64 = Base64.encodeBase64String(basicInfo.getBytes());
48+
return String.format(BASIC_AUTH, basicBase64);
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright [2019] [恒宇少年 - 于起宇]
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package org.minbox.framework.api.boot.plugin.logging.admin.report;
19+
20+
import org.minbox.framework.api.boot.common.exception.ApiBootException;
21+
import org.springframework.beans.factory.InitializingBean;
22+
23+
/**
24+
* Batch Report Request Logs To Admin
25+
*
26+
* @author:恒宇少年 - 于起宇
27+
* <p>
28+
* DateTime:2019-07-21 13:46
29+
* Blog:http://blog.yuqiyu.com
30+
* WebSite:http://www.jianshu.com/u/092df3f77bca
31+
* Gitee:https://gitee.com/hengboy
32+
* GitHub:https://github.com/hengboy
33+
*/
34+
public interface LoggingAdminReport extends InitializingBean {
35+
/**
36+
* Report Request Logs To Admin
37+
*
38+
* @throws ApiBootException ApiBoot Exception
39+
*/
40+
void report() throws ApiBootException;
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright [2019] [恒宇少年 - 于起宇]
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package org.minbox.framework.api.boot.plugin.logging.admin.report;
19+
20+
import java.util.concurrent.Executors;
21+
import java.util.concurrent.ScheduledExecutorService;
22+
import java.util.concurrent.TimeUnit;
23+
24+
/**
25+
* ApiBoot Logging Report Scheduled
26+
*
27+
* @author:恒宇少年 - 于起宇
28+
* <p>
29+
* DateTime:2019-07-21 14:54
30+
* Blog:http://blog.yuqiyu.com
31+
* WebSite:http://www.jianshu.com/u/092df3f77bca
32+
* Gitee:https://gitee.com/hengboy
33+
* GitHub:https://github.com/hengboy
34+
*/
35+
public class LoggingReportScheduled {
36+
/**
37+
* Scheduled Executor Service
38+
*/
39+
private static final ScheduledExecutorService executorService = Executors.newScheduledThreadPool(10);
40+
41+
/**
42+
* Scheduled Report
43+
*
44+
* @param loggingAdminReport Logging Admin Report
45+
*/
46+
public LoggingReportScheduled(LoggingAdminReport loggingAdminReport, int reportInitialDelaySecond, int reportIntervalSecond) {
47+
// scheduled report request logs
48+
executorService.scheduleAtFixedRate(() -> loggingAdminReport.report(), reportInitialDelaySecond, reportIntervalSecond, TimeUnit.SECONDS);
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
/*
2+
* Copyright [2019] [恒宇少年 - 于起宇]
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package org.minbox.framework.api.boot.plugin.logging.admin.report.support;
19+
20+
import com.alibaba.fastjson.JSON;
21+
import org.minbox.framework.api.boot.common.exception.ApiBootException;
22+
import org.minbox.framework.api.boot.plugin.logging.ApiBootLog;
23+
import org.minbox.framework.api.boot.plugin.logging.ApiBootLogClientNotice;
24+
import org.minbox.framework.api.boot.plugin.logging.ApiBootLogThreadLocal;
25+
import org.minbox.framework.api.boot.plugin.logging.ReportResponse;
26+
import org.minbox.framework.api.boot.plugin.logging.admin.discovery.LoggingAdminDiscovery;
27+
import org.minbox.framework.api.boot.plugin.logging.admin.report.LoggingAdminReport;
28+
import org.minbox.framework.api.boot.plugin.logging.cache.LoggingCache;
29+
import org.slf4j.Logger;
30+
import org.slf4j.LoggerFactory;
31+
import org.springframework.beans.factory.DisposableBean;
32+
import org.springframework.core.env.ConfigurableEnvironment;
33+
import org.springframework.http.*;
34+
import org.springframework.util.ObjectUtils;
35+
import org.springframework.web.client.RestTemplate;
36+
37+
import java.net.InetAddress;
38+
import java.util.List;
39+
40+
/**
41+
* ApiBoot Logging Admin Report Support
42+
*
43+
* @author:恒宇少年 - 于起宇
44+
* <p>
45+
* DateTime:2019-07-21 14:24
46+
* Blog:http://blog.yuqiyu.com
47+
* WebSite:http://www.jianshu.com/u/092df3f77bca
48+
* Gitee:https://gitee.com/hengboy
49+
* GitHub:https://github.com/hengboy
50+
*/
51+
public class LoggingAdminReportSupport implements LoggingAdminReport, DisposableBean {
52+
/**
53+
* logger instance
54+
*/
55+
static Logger logger = LoggerFactory.getLogger(LoggingAdminReportSupport.class);
56+
57+
/**
58+
* Report Request Logging Uri
59+
*/
60+
private static final String REPORT_LOG_URI = "/logging/report";
61+
/**
62+
* Content-Type HEADER NAME
63+
*/
64+
private static final String HEADER_CONTENT_TYPE = "Content-Type";
65+
/**
66+
* Authorization HEADER NAME
67+
*/
68+
private static final String HEADER_AUTHORIZATION = "Authorization";
69+
/**
70+
* ApiBoot Logging Admin Discovery
71+
*/
72+
private LoggingAdminDiscovery adminDiscovery;
73+
/**
74+
* Rest Template
75+
*/
76+
private RestTemplate restTemplate;
77+
/**
78+
* Logging Cache
79+
*
80+
* @see org.minbox.framework.api.boot.plugin.logging.cache.support.LoggingMemoryCache
81+
*/
82+
private LoggingCache loggingCache;
83+
/**
84+
* Report Number Of Request Log
85+
*/
86+
private Integer numberOfRequestLog;
87+
/**
88+
* ServiceId
89+
* application name
90+
*/
91+
private String serviceId;
92+
/**
93+
* Service Address
94+
*/
95+
private String serviceAddress;
96+
/**
97+
* Service Port
98+
*/
99+
private Integer servicePort;
100+
101+
public LoggingAdminReportSupport(LoggingAdminDiscovery adminDiscovery, RestTemplate restTemplate, LoggingCache loggingCache, Integer numberOfRequestLog, ConfigurableEnvironment environment) {
102+
this.adminDiscovery = adminDiscovery;
103+
this.restTemplate = restTemplate;
104+
this.loggingCache = loggingCache;
105+
this.numberOfRequestLog = numberOfRequestLog;
106+
this.serviceId = environment.getProperty("spring.application.name");
107+
this.servicePort = Integer.valueOf(environment.getProperty("server.port"));
108+
}
109+
110+
/**
111+
* Report Logs Interval
112+
*
113+
* @throws ApiBootException ApiBoot Exception
114+
*/
115+
@Override
116+
public void report() throws ApiBootException {
117+
try {
118+
List<ApiBootLog> logs = loggingCache.getLogs(numberOfRequestLog);
119+
reportToAdmin(logs);
120+
} catch (Exception e) {
121+
logger.error(e.getMessage(), e);
122+
}
123+
}
124+
125+
/**
126+
* Report Logs To Admin
127+
* get number logs from cache
128+
* lookup a available api-boot-logging-admin service url
129+
* report request logs ro admin service
130+
* if admin use spring security, set restTemplate header basic auth info
131+
*
132+
* @param logs
133+
*/
134+
private void reportToAdmin(List<ApiBootLog> logs) {
135+
if (ObjectUtils.isEmpty(logs)) {
136+
return;
137+
}
138+
// ApiBoot Logging Admin Server Url
139+
String adminServiceUrl = getAfterFormatAdminUrl();
140+
// client notice entity
141+
ApiBootLogClientNotice clientNotice = new ApiBootLogClientNotice();
142+
clientNotice.getLoggers().addAll(logs);
143+
clientNotice.setClientServiceId(serviceId);
144+
clientNotice.setClientServiceIp(serviceAddress);
145+
clientNotice.setClientServicePort(servicePort);
146+
147+
HttpHeaders headers = new HttpHeaders();
148+
headers.add(HEADER_CONTENT_TYPE, MediaType.APPLICATION_JSON_UTF8_VALUE);
149+
String basicAuth = adminDiscovery.getBasicAuth();
150+
if (!ObjectUtils.isEmpty(basicAuth)) {
151+
headers.add(HEADER_AUTHORIZATION, basicAuth);
152+
}
153+
HttpEntity<String> httpEntity = new HttpEntity(JSON.toJSONString(clientNotice), headers);
154+
155+
ResponseEntity<ReportResponse> response = restTemplate.postForEntity(adminServiceUrl, httpEntity, ReportResponse.class);
156+
if (response.getStatusCode().is2xxSuccessful() && response.getBody().getStatus().equals(ReportResponse.SUCCESS)) {
157+
logger.debug("Report Request Logging Successfully To Admin.");
158+
} else {
159+
logger.error("Report Request Logging Error To Admin.");
160+
}
161+
}
162+
163+
/**
164+
* Get After Format Admin URL
165+
*
166+
* @return ApiBoot Logging Admin URL
167+
*/
168+
private String getAfterFormatAdminUrl() {
169+
// api boot admin service uri
170+
String adminServiceUri = adminDiscovery.lookup();
171+
// api boot admin service url
172+
return String.format("%s%s", adminServiceUri, REPORT_LOG_URI);
173+
}
174+
175+
@Override
176+
public void afterPropertiesSet() throws Exception {
177+
// service ip address
178+
InetAddress inetAddress = InetAddress.getLocalHost();
179+
this.serviceAddress = inetAddress.getHostAddress();
180+
}
181+
182+
/**
183+
* Bean Destroy
184+
* When destroyed, report all request logs in the cache to admin
185+
*
186+
* @throws Exception exception
187+
*/
188+
@Override
189+
public void destroy() throws Exception {
190+
// get all cache logs
191+
List<ApiBootLog> logs = loggingCache.getAll();
192+
// report to admin
193+
reportToAdmin(logs);
194+
}
195+
}

0 commit comments

Comments
 (0)