From fbca82f5994ec49092fb05bc236f0956fecd387c Mon Sep 17 00:00:00 2001 From: 13693261870 <252740454@qq.com> Date: 星期一, 30 十二月 2024 10:58:35 +0800 Subject: [PATCH] 1 --- se-modules/se-system/src/main/java/com/se/system/utils/CaffeineUtils.java | 2 se-modules/se-system/src/main/java/com/se/system/service/NacosService.java | 85 ++++++++++++++++++++++++++++ se-modules/se-system/src/main/java/com/se/system/controller/SysSoftController.java | 49 ++++++++++----- 3 files changed, 119 insertions(+), 17 deletions(-) diff --git a/se-modules/se-system/src/main/java/com/se/system/controller/SysSoftController.java b/se-modules/se-system/src/main/java/com/se/system/controller/SysSoftController.java index 204f917..76154c2 100644 --- a/se-modules/se-system/src/main/java/com/se/system/controller/SysSoftController.java +++ b/se-modules/se-system/src/main/java/com/se/system/controller/SysSoftController.java @@ -1,8 +1,12 @@ package com.se.system.controller; import java.util.List; +import javax.annotation.Resource; +import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import org.springframework.beans.factory.annotation.Autowired; + +import com.se.system.service.NacosService; +import org.omg.CORBA.ServerRequest; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; @@ -28,10 +32,13 @@ * @date 2024-11-22 */ @RestController +@SuppressWarnings("ALL") @RequestMapping("/soft") -public class SysSoftController extends BaseController -{ - @Autowired +public class SysSoftController extends BaseController { + @Resource + NacosService nacosService; + + @Resource private ISysSoftService sysSoftService; /** @@ -39,8 +46,7 @@ */ @RequiresPermissions("system:soft:list") @GetMapping("/list") - public TableDataInfo list(SysSoft sysSoft) - { + public TableDataInfo list(SysSoft sysSoft) { startPage(); List<SysSoft> list = sysSoftService.selectSysSoftList(sysSoft); return getDataTable(list); @@ -52,8 +58,7 @@ @RequiresPermissions("system:soft:export") @Log(title = "杞欢", businessType = BusinessType.EXPORT) @PostMapping("/export") - public void export(HttpServletResponse response, SysSoft sysSoft) - { + public void export(HttpServletResponse response, SysSoft sysSoft) { List<SysSoft> list = sysSoftService.selectSysSoftList(sysSoft); ExcelUtil<SysSoft> util = new ExcelUtil<SysSoft>(SysSoft.class); util.exportExcel(response, list, "杞欢鏁版嵁"); @@ -64,8 +69,7 @@ */ @RequiresPermissions("system:soft:query") @GetMapping(value = "/{softId}") - public AjaxResult getInfo(@PathVariable("softId") Long softId) - { + public AjaxResult getInfo(@PathVariable("softId") Long softId) { return success(sysSoftService.selectSysSoftBySoftId(softId)); } @@ -75,8 +79,7 @@ @RequiresPermissions("system:soft:add") @Log(title = "杞欢", businessType = BusinessType.INSERT) @PostMapping - public AjaxResult add(@RequestBody SysSoft sysSoft) - { + public AjaxResult add(@RequestBody SysSoft sysSoft) { return toAjax(sysSoftService.insertSysSoft(sysSoft)); } @@ -86,8 +89,7 @@ @RequiresPermissions("system:soft:edit") @Log(title = "杞欢", businessType = BusinessType.UPDATE) @PutMapping - public AjaxResult edit(@RequestBody SysSoft sysSoft) - { + public AjaxResult edit(@RequestBody SysSoft sysSoft) { return toAjax(sysSoftService.updateSysSoft(sysSoft)); } @@ -96,9 +98,22 @@ */ @RequiresPermissions("system:soft:remove") @Log(title = "杞欢", businessType = BusinessType.DELETE) - @DeleteMapping("/{softIds}") - public AjaxResult remove(@PathVariable Long[] softIds) - { + @DeleteMapping("/{softIds}") + public AjaxResult remove(@PathVariable Long[] softIds) { return toAjax(sysSoftService.deleteSysSoftBySoftIds(softIds)); } + + @RequiresPermissions("system:soft:query") + @GetMapping(value = "getNacosConfig") + public void getNacosConfig(String dataId, HttpServletRequest req, HttpServletResponse res) { + try { + nacosService.getNacosConfig(dataId, req, res); + } catch (Exception e) { + try { + res.getWriter().print(e.getMessage()); + } catch (Exception ex) { + logger.error(ex.getMessage(), ex); + } + } + } } 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 new file mode 100644 index 0000000..0a55268 --- /dev/null +++ b/se-modules/se-system/src/main/java/com/se/system/service/NacosService.java @@ -0,0 +1,85 @@ +package com.se.system.service; + +import com.alibaba.fastjson.JSONObject; +import com.se.system.utils.CaffeineUtils; +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.stereotype.Component; +import org.springframework.util.LinkedMultiValueMap; +import org.springframework.util.MultiValueMap; +import org.springframework.web.client.RestTemplate; + +import javax.annotation.Resource; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.PrintWriter; +import java.util.Map; + +@Component +@SuppressWarnings("ALL") +public class NacosService { + @Value("${spring.cloud.nacos.config.server-addr}") + String serverAddr; + + @Value("${spring.cloud.nacos.username}") + String username; + + @Value("${spring.cloud.nacos.password}") + String password; + + @Resource + RestTemplate restTemplate; + + final static String key = "nacos:login:token"; + + private String getToken() { + Object obj = CaffeineUtils.get(key); + if (obj instanceof String) { + return (String) obj; + } + + String token = login(); + if (null != token) { + CaffeineUtils.put(key, token); + } + + return token; + } + + private String login() { + String url = "http://" + serverAddr + "/nacos/v1/auth/users/login"; + + MultiValueMap<String, Object> map = new LinkedMultiValueMap(); + map.add("username", username); + map.add("password", password); + + JSONObject obj = restTemplate.postForObject(url, map, JSONObject.class); + if (null == obj || !obj.containsKey("accessToken")) { + return null; + } + + return obj.getString("accessToken"); + } + + 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"; + + HttpHeaders headers = new HttpHeaders(); + headers.set("accessToken", token); + + HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<>(headers); + ResponseEntity<String> re = restTemplate.exchange(url, HttpMethod.GET, requestEntity, String.class); + + res.setContentType("application/json;charset=UTF-8"); + PrintWriter out = res.getWriter(); + out.print(re.getBody()); + out.flush(); + out.close(); + } +} diff --git a/se-modules/se-system/src/main/java/com/se/system/utils/CaffeineUtils.java b/se-modules/se-system/src/main/java/com/se/system/utils/CaffeineUtils.java index 91d7945..bc82b2e 100644 --- a/se-modules/se-system/src/main/java/com/se/system/utils/CaffeineUtils.java +++ b/se-modules/se-system/src/main/java/com/se/system/utils/CaffeineUtils.java @@ -6,12 +6,14 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; +import org.springframework.cloud.context.config.annotation.RefreshScope; import java.math.BigInteger; import java.security.MessageDigest; import java.util.List; import java.util.concurrent.TimeUnit; +@RefreshScope @SuppressWarnings("ALL") public class CaffeineUtils { static Integer cacheTime; -- Gitblit v1.9.3