¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.vci.ubcs.system.cache; |
| | | |
| | | import com.alibaba.nacos.api.config.ConfigFactory; |
| | | import com.alibaba.nacos.api.config.ConfigService; |
| | | import com.alibaba.nacos.api.exception.NacosException; |
| | | import com.vci.ubcs.common.constant.LauncherConstant; |
| | | import org.springblade.core.log.exception.ServiceException; |
| | | import org.springblade.core.cache.utils.CacheUtil; |
| | | import org.springblade.core.secure.BladeUser; |
| | | import org.springblade.core.tool.utils.Func; |
| | | import org.springframework.stereotype.Component; |
| | | import org.yaml.snakeyaml.Yaml; |
| | | |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * redisä¸åå¨çnacosä¸é
ç½®çè¶
管é
ç½®ä¿¡æ¯ |
| | | * @author ludc |
| | | * @date 2023/8/31 13:02 |
| | | */ |
| | | @Component |
| | | public class NacosConfigCache { |
| | | |
| | | private static final String NACOS_CONFIG_CACHE = "nacos:config"; |
| | | |
| | | private static final String ADMIN_INFO_CODE = "admin:info:"; |
| | | |
| | | private static ConfigService configService; |
| | | |
| | | private static final String GROUP_ID = "DEFAULT_GROUP"; |
| | | private static final String DATA_ID = "ubcs.yaml"; |
| | | |
| | | static { |
| | | try { |
| | | configService = ConfigFactory.createConfigService(LauncherConstant.NACOS_DEV_ADDR); |
| | | } catch (NacosException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * éææ¹æ³ä¸è·åé
ç½® |
| | | * @return |
| | | */ |
| | | public static Map<String, Map<String,String>> getConfig() { |
| | | try { |
| | | String config = configService.getConfig(DATA_ID, GROUP_ID, 5000); |
| | | Yaml yaml = new Yaml(); |
| | | return yaml.load(config); |
| | | } catch (NacosException e) { |
| | | e.printStackTrace(); |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * è·ååå
¥redisä¸ |
| | | * nacosä¸é
ç½®çè¶
ç®¡ä¿¡æ¯ |
| | | * @return |
| | | */ |
| | | public static BladeUser getAdminUserInfo() { |
| | | try { |
| | | BladeUser user = CacheUtil.getCache(NACOS_CONFIG_CACHE).get(ADMIN_INFO_CODE,BladeUser.class); |
| | | Map<String, Map<String,String>> configMap = getConfig(); |
| | | String tenantId = configMap.get("user-info").get("tenant-id"); |
| | | //ä»ç¼å䏿ªè·åå°è¶
级管çåé
ç½®ä¿¡æ¯æè
ånacosä¸é
ç½®çç§æ·ä¸ä¸è´ï¼ä»nacosä¸éæ°è·åå¹¶åå
¥ç¼åã |
| | | if(Func.isEmpty(user) || !user.getTenantId().equals(tenantId)){ |
| | | BladeUser adminUser = new BladeUser(); |
| | | adminUser.setTenantId(tenantId); |
| | | adminUser.setUserName(configMap.get("user-info").get("user-name")); |
| | | adminUser.setUserId(Func.toLong(configMap.get("user-info").get("id"))); |
| | | CacheUtil.getCache(NACOS_CONFIG_CACHE).put(ADMIN_INFO_CODE,adminUser); |
| | | return adminUser; |
| | | } |
| | | return user; |
| | | }catch (Exception e){ |
| | | throw new ServiceException("ä»nacosä¸è·åè¶
级管çåé
置失败ï¼"); |
| | | } |
| | | } |
| | | |
| | | } |