|
| 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