From 2d8dc64971a203e5cb2485bf1714892a8005fc0f Mon Sep 17 00:00:00 2001 From: 13693261870 <252740454@qq.com> Date: 星期一, 30 十二月 2024 14:54:52 +0800 Subject: [PATCH] 1 --- se-modules/se-system/src/main/java/com/se/system/service/NacosService.java | 63 ++++++++++++++++++++++++++----- 1 files changed, 52 insertions(+), 11 deletions(-) diff --git a/se-modules/se-system/src/main/java/com/se/system/service/NacosService.java b/se-modules/se-system/src/main/java/com/se/system/service/NacosService.java index 0a55268..5c642e4 100644 --- a/se-modules/se-system/src/main/java/com/se/system/service/NacosService.java +++ b/se-modules/se-system/src/main/java/com/se/system/service/NacosService.java @@ -1,12 +1,13 @@ package com.se.system.service; +import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; +import com.se.system.domain.vo.NacosConfigVo; import com.se.system.utils.CaffeineUtils; +import com.se.system.utils.StringUtils; +import nonapi.io.github.classgraph.json.JSONUtils; import org.springframework.beans.factory.annotation.Value; -import org.springframework.http.HttpEntity; -import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpMethod; -import org.springframework.http.ResponseEntity; +import org.springframework.http.*; import org.springframework.stereotype.Component; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; @@ -16,6 +17,8 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.PrintWriter; +import java.lang.reflect.Field; +import java.nio.charset.StandardCharsets; import java.util.Map; @Component @@ -35,16 +38,16 @@ final static String key = "nacos:login:token"; - private String getToken() { + private String getToken() throws Exception { Object obj = CaffeineUtils.get(key); if (obj instanceof String) { return (String) obj; } String token = login(); - if (null != token) { - CaffeineUtils.put(key, token); - } + if (null == token) throw new Exception("Nacos浠ょ墝涓虹┖"); + + CaffeineUtils.put(key, token); return token; } @@ -66,9 +69,8 @@ public void getNacosConfig(String dataId, HttpServletRequest req, HttpServletResponse res) throws Exception { String token = getToken(); - if (null == token) throw new Exception("Nacos浠ょ墝涓虹┖"); - - String url = "http://" + serverAddr + "/nacos/v1/cs/configs?dataId=se-system-dev.yml&group=&appName=&pageNo=1&pageSize=10&search=accurate"; + // String url = "http://" + serverAddr + "/nacos/v1/cs/configs?dataId=" + dataId + "&group=&appName=&pageNo=1&pageSize=10&search=accurate"; + String url = "http://" + serverAddr + "/nacos/v1/cs/configs?dataId=" + dataId + "&group=DEFAULT_GROUP&namespaceId=&tenant=&show=all&username="; HttpHeaders headers = new HttpHeaders(); headers.set("accessToken", token); @@ -82,4 +84,43 @@ out.flush(); out.close(); } + + public void updateNacosConfig(NacosConfigVo vo, HttpServletRequest req, HttpServletResponse res) throws Exception { + if (null == vo || null == vo.getId() || null == vo.getDataId() || null == vo.getContent()) + throw new Exception("NacosConfigVo鏁版嵁涓嶆纭�"); + + String token = getToken(); + vo.setModifyTime(System.currentTimeMillis()); + //vo.setMd5(StringUtils.md5(vo.getContent())); + String url = "http://" + serverAddr + "/nacos/v1/cs/configs?username=" + username; + + HttpHeaders headers = new HttpHeaders(); + // headers.setContentType(MediaType.APPLICATION_JSON); + // headers.setContentType(new MediaType("application", "json", StandardCharsets.UTF_8)); + headers.setContentType(MediaType.valueOf("application/x-www-form-urlencoded")); + headers.set("accessToken", token); + headers.set("casMd5", vo.getMd5()); + + MultiValueMap<String, Object> map = createMap(vo); + HttpEntity<MultiValueMap<String, Object>> entity = new HttpEntity<>(map, headers); + ResponseEntity<String> re = restTemplate.exchange(url, HttpMethod.POST, entity, String.class); + + res.setContentType("application/json;charset=UTF-8"); + PrintWriter out = res.getWriter(); + out.print(re.getBody()); + out.flush(); + out.close(); + } + + private <T> MultiValueMap<String, Object> createMap(T t) throws Exception { + MultiValueMap<String, Object> map = new LinkedMultiValueMap<>(); + + Field[] fields = t.getClass().getDeclaredFields(); + for (Field field : fields) { + field.setAccessible(true); + map.add(field.getName(), field.get(t)); + } + + return map; + } } -- Gitblit v1.9.3