|
| 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.storage; |
| 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.slf4j.Logger; |
| 24 | +import org.slf4j.LoggerFactory; |
| 25 | + |
| 26 | +import javax.sql.DataSource; |
| 27 | +import java.sql.*; |
| 28 | +import java.util.List; |
| 29 | +import java.util.UUID; |
| 30 | + |
| 31 | +/** |
| 32 | + * @author:恒宇少年 - 于起宇 |
| 33 | + * <p> |
| 34 | + * DateTime:2019-07-22 17:01 |
| 35 | + * Blog:http://blog.yuqiyu.com |
| 36 | + * WebSite:http://www.jianshu.com/u/092df3f77bca |
| 37 | + * Gitee:https://gitee.com/hengboy |
| 38 | + * GitHub:https://github.com/hengboy |
| 39 | + */ |
| 40 | +public class LoggingDataSourceStorage implements LoggingStorage { |
| 41 | + /** |
| 42 | + * logger instance |
| 43 | + */ |
| 44 | + static Logger logger = LoggerFactory.getLogger(LoggingDataSourceStorage.class); |
| 45 | + /** |
| 46 | + * Insert ServiceDetail SQL |
| 47 | + */ |
| 48 | + private static final String SQL_INSERT_SERVICE_DETAILS = "insert into logging_service_details (lsd_id, lsd_service_id, lsd_service_ip, lsd_service_port) values (?,?,?,?)"; |
| 49 | + /** |
| 50 | + * Select ServiceDetails Id SQL |
| 51 | + */ |
| 52 | + private static final String SQL_SELECT_SERVICE_DETAILS_ID = "select lsd_id from logging_service_details where lsd_service_id = ? and lsd_service_ip = ? and lsd_service_port = ? limit 1"; |
| 53 | + /** |
| 54 | + * Update ServiceDetail Last Report Time SQL |
| 55 | + */ |
| 56 | + private static final String SQL_UPDATE_LAST_REPORT_SERVICE_DETAILS = "update logging_service_details set lsd_last_report_time = ? where lsd_id = ?"; |
| 57 | + /** |
| 58 | + * Insert Request Log SQL |
| 59 | + */ |
| 60 | + private static final String SQL_INSERT_LOG = "insert into logging_request_logs (lrl_id, lrl_service_detail_id, lrl_trace_id, lrl_parent_span_id, lrl_span_id,\n" + |
| 61 | + " lrl_start_time, lrl_end_time, lrl_http_status, lrl_request_body, lrl_request_headers,\n" + |
| 62 | + " lrl_request_ip, lrl_request_method, lrl_request_uri, lrl_response_body,\n" + |
| 63 | + " lrl_response_headers, lrl_time_consuming) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);"; |
| 64 | + |
| 65 | + private DataSource dataSource; |
| 66 | + |
| 67 | + public LoggingDataSourceStorage(DataSource dataSource) { |
| 68 | + this.dataSource = dataSource; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Insert Request Log |
| 73 | + * |
| 74 | + * @param serviceDetailId ServiceDetail ID |
| 75 | + * @param log ApiBootLog |
| 76 | + * @throws SQLException SqlException |
| 77 | + */ |
| 78 | + @Override |
| 79 | + public void insertLog(String serviceDetailId, ApiBootLog log) throws SQLException { |
| 80 | + Connection connection = getConnection(); |
| 81 | + PreparedStatement ps = connection.prepareStatement(SQL_INSERT_LOG); |
| 82 | + ps.setString(1, UUID.randomUUID().toString()); |
| 83 | + ps.setString(2, serviceDetailId); |
| 84 | + ps.setString(3, log.getTraceId()); |
| 85 | + ps.setString(4, log.getParentSpanId()); |
| 86 | + ps.setString(5, log.getSpanId()); |
| 87 | + ps.setLong(6, log.getStartTime()); |
| 88 | + ps.setLong(7, log.getEndTime()); |
| 89 | + ps.setInt(8, log.getHttpStatus()); |
| 90 | + ps.setString(9, log.getRequestBody()); |
| 91 | + ps.setString(10, JSON.toJSONString(log.getRequestHeaders())); |
| 92 | + ps.setString(11, log.getRequestIp()); |
| 93 | + ps.setString(12, log.getRequestMethod()); |
| 94 | + ps.setString(13, log.getRequestUri()); |
| 95 | + ps.setString(14, log.getResponseBody()); |
| 96 | + ps.setString(15, JSON.toJSONString(log.getResponseHeaders())); |
| 97 | + ps.setLong(16, log.getTimeConsuming()); |
| 98 | + ps.executeUpdate(); |
| 99 | + ps.close(); |
| 100 | + closeConnection(connection); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Insert ServiceDetails |
| 105 | + * |
| 106 | + * @param serviceId ServiceId |
| 107 | + * @param serviceIp Service Ip Address |
| 108 | + * @param servicePort ServicePort |
| 109 | + * @return ServiceDetails Pk Value |
| 110 | + * @throws SQLException Sql Exception |
| 111 | + */ |
| 112 | + @Override |
| 113 | + public String insertServiceDetail(String serviceId, String serviceIp, int servicePort) throws SQLException { |
| 114 | + String serviceDetailId = UUID.randomUUID().toString(); |
| 115 | + Connection connection = getConnection(); |
| 116 | + PreparedStatement ps = connection.prepareStatement(SQL_INSERT_SERVICE_DETAILS); |
| 117 | + ps.setString(1, serviceDetailId); |
| 118 | + ps.setString(2, serviceId); |
| 119 | + ps.setString(3, serviceIp); |
| 120 | + ps.setInt(4, servicePort); |
| 121 | + ps.executeUpdate(); |
| 122 | + ps.close(); |
| 123 | + closeConnection(connection); |
| 124 | + return serviceDetailId; |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Select ServiceDetails Id |
| 129 | + * |
| 130 | + * @param serviceId Service Id |
| 131 | + * @param serviceIp Service Ip Address |
| 132 | + * @param servicePort Service Port |
| 133 | + * @return ServiceDetail Id |
| 134 | + * @throws SQLException Sql Exception |
| 135 | + */ |
| 136 | + @Override |
| 137 | + public String selectServiceDetailId(String serviceId, String serviceIp, int servicePort) throws SQLException { |
| 138 | + Connection connection = getConnection(); |
| 139 | + PreparedStatement ps = connection.prepareStatement(SQL_SELECT_SERVICE_DETAILS_ID); |
| 140 | + ps.setString(1, serviceId); |
| 141 | + ps.setString(2, serviceIp); |
| 142 | + ps.setInt(3, servicePort); |
| 143 | + ResultSet rs = ps.executeQuery(); |
| 144 | + String serviceDetailId = null; |
| 145 | + while (rs.next()) { |
| 146 | + serviceDetailId = rs.getString(1); |
| 147 | + break; |
| 148 | + } |
| 149 | + rs.close(); |
| 150 | + ps.close(); |
| 151 | + closeConnection(connection); |
| 152 | + return serviceDetailId; |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Update ServiceDetails Last Report Time |
| 157 | + * |
| 158 | + * @param serviceDetailId ServiceDetail Pk Value |
| 159 | + * @throws SQLException Sql Exception |
| 160 | + */ |
| 161 | + @Override |
| 162 | + public void updateLastReportTime(String serviceDetailId) throws SQLException { |
| 163 | + Connection connection = getConnection(); |
| 164 | + PreparedStatement ps = connection.prepareStatement(SQL_UPDATE_LAST_REPORT_SERVICE_DETAILS); |
| 165 | + ps.setTimestamp(1, new Timestamp(System.currentTimeMillis())); |
| 166 | + ps.setString(2, serviceDetailId); |
| 167 | + ps.executeUpdate(); |
| 168 | + ps.close(); |
| 169 | + closeConnection(connection); |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Get DataSource Connection |
| 174 | + * |
| 175 | + * @return Connection |
| 176 | + * @throws SQLException Sql Exception |
| 177 | + */ |
| 178 | + Connection getConnection() throws SQLException { |
| 179 | + return dataSource.getConnection(); |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * Close DataSource Connection |
| 184 | + * |
| 185 | + * @param connection DataSource Connection |
| 186 | + * @throws SQLException Sql Exception |
| 187 | + */ |
| 188 | + void closeConnection(Connection connection) throws SQLException { |
| 189 | + connection.close(); |
| 190 | + } |
| 191 | +} |
0 commit comments