| | |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.io.OutputStream; |
| | | import java.io.InputStream; |
| | | import java.net.HttpURLConnection; |
| | | import java.net.URL; |
| | | import java.nio.charset.StandardCharsets; |
| | |
| | | public void append(Map<String, Object> audit) { |
| | | try { |
| | | List<ServiceInstance> instances = discoveryClient.getInstances("mb-cloud-other"); |
| | | if (instances == null || instances.isEmpty()) return; |
| | | if (instances == null || instances.isEmpty()) { |
| | | log.warn("管理端审计写入失败:未发现审计服务 mb-cloud-other"); |
| | | return; |
| | | } |
| | | URL url = new URL(instances.get(0).getUri().toString() + "/internal/audit-logs"); |
| | | HttpURLConnection connection = (HttpURLConnection) url.openConnection(); |
| | | connection.setConnectTimeout(1000); |
| | |
| | | connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8"); |
| | | byte[] body = JSON.toJSONString(audit).getBytes(StandardCharsets.UTF_8); |
| | | try (OutputStream output = connection.getOutputStream()) { output.write(body); } |
| | | connection.getResponseCode(); |
| | | int status = connection.getResponseCode(); |
| | | if (status < HttpURLConnection.HTTP_OK || status >= HttpURLConnection.HTTP_MULT_CHOICE) { |
| | | log.warn("管理端审计写入失败,响应状态: {}, 响应内容: {}", status, |
| | | readResponse(connection.getErrorStream())); |
| | | } |
| | | connection.disconnect(); |
| | | } catch (Exception e) { |
| | | log.warn("管理端审计写入失败", e); |
| | | } |
| | | } |
| | | |
| | | private String readResponse(InputStream input) { |
| | | if (input == null) return ""; |
| | | try (InputStream stream = input) { |
| | | byte[] buffer = new byte[1024]; |
| | | int length = stream.read(buffer); |
| | | return length <= 0 ? "" : new String(buffer, 0, length, StandardCharsets.UTF_8); |
| | | } catch (Exception ignored) { |
| | | return ""; |
| | | } |
| | | } |
| | | } |