当前位置: 首页 > wzjs >正文

做网站不花钱佛山市新城开发建设有限公司网站

做网站不花钱,佛山市新城开发建设有限公司网站,东莞有哪些好企业,鄂州seo多少钱Nacos当前命名空间下的配置文件需要跨命名空间读取其他配置文件的内容。可以先通过Nacos提供的API接口获取配置文件内容&#xff0c;然后解析数据将其放入环境的PropertySource中。 相关依赖包 <!-- Nacos依赖包 --> <dependency><groupId>com.alibaba.clo…
Nacos当前命名空间下的配置文件需要跨命名空间读取其他配置文件的内容。可以先通过Nacos提供的API接口获取配置文件内容,然后解析数据将其放入环境的PropertySource中。
  • 相关依赖包
<!-- Nacos依赖包 -->
<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId><version>2.2.6.RELEASE</version><exclusions><exclusion><groupId>com.alibaba.nacos</groupId><artifactId>nacos-client</artifactId></exclusion></exclusions>
</dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId><version>2.2.6.RELEASE</version><exclusions><exclusion><groupId>com.alibaba.nacos</groupId><artifactId>nacos-client</artifactId></exclusion></exclusions>
</dependency><dependency><groupId>com.alibaba.nacos</groupId><artifactId>nacos-client</artifactId><version>2.3.0</version>
</dependency>
  • 部分代码

@Order(Ordered.HIGHEST_PRECEDENCE)
public class NacosEnvironmentPostProcessor implements EnvironmentPostProcessor {private static final String NACOS_PROPERTY_SOURCE_NAME = "NACOS";private static final String[] dataIds = new String[]{"db-sample.yml"};private static final String LOGIN_URL = "http://$host/nacos/v1/auth/users/login";private static final String CONFIGS_URL = "http://$host/nacos/v1/cs/configs?dataId=&group=&appName=&config_tags=&pageNo=1&pageSize=100&tenant=$tenant&search=blur&accessToken=$accessToken";@Overridepublic void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {if (environment instanceof StandardServletEnvironment || environment instanceof StandardReactiveWebEnvironment) {return;}addPropertySource(environment);}private void addPropertySource(ConfigurableEnvironment environment) {Map<String, Object> systemEnvironment = environment.getSystemEnvironment();Object namespaceIdObj = systemEnvironment.get("NACOS_NAMESPACE");if (null == namespaceIdObj) {return;}String host = String.valueOf(systemEnvironment.get("NACOS_HOST"));String username = String.valueOf(systemEnvironment.get("NACOS_USERNAME"));String password = String.valueOf(systemEnvironment.get("NACOS_PASSWORD"));String group = String.valueOf(systemEnvironment.get("NACOS_GROUP"));String namespaceId = String.valueOf(namespaceIdObj);log.info("{} {} {} {} {}", host, username, password, group, namespaceId);processV2(environment, host, username, password, namespaceId);}private void processV1(ConfigurableEnvironment environment, String host, String username, String password,String namespaceId, String group) {NacosConfigProperties nacosConfigProperties = new NacosConfigProperties();nacosConfigProperties.setEnvironment(new StandardEnvironment());nacosConfigProperties.setServerAddr(host);nacosConfigProperties.setUsername(username);nacosConfigProperties.setPassword(password);nacosConfigProperties.setNamespace(namespaceId);nacosConfigProperties.setGroup(group);ConfigService configService = null;try {configService = NacosFactory.createConfigService(nacosConfigProperties.assembleConfigServiceProperties());} catch (NacosException e) {log.error(e.getMessage(), e);}if (null == configService) {return;}Properties properties = new Properties();for (String dataId : dataIds) {addProperties(dataId, getPropertySources(configService, dataId, group), properties);}environment.getPropertySources().addFirst(new PropertiesPropertySource(NACOS_PROPERTY_SOURCE_NAME, properties));}private List<PropertySource<?>> getPropertySources(ConfigService configService, String dataId, String group) {List<PropertySource<?>> propertySources = null;try {String config = configService.getConfig(dataId, group, 10000);log.info("{} {} config {}", dataId, group, config);propertySources = NacosDataParserHandler.getInstance().parseNacosData(dataId, config, null);} catch (Exception e) {log.error(e.getMessage(), e);}return propertySources;}private void processV2(ConfigurableEnvironment environment, String host, String username, String password,String namespaceId) {if (null != host && host.contains(",")) {host = host.split(",")[0];}Map<String, String> params = new HashMap<>();params.put("username", username);params.put("password", password);String loginResp = HttpClientUtils.sendPost(LOGIN_URL.replace("$host", host), params, "UTF-8");log.info("login response {}", loginResp);LoginDTO loginDTO = JsonUtils.json(loginResp, LoginDTO.class);if (null == loginDTO) {return;}String configsResp = HttpClientUtils.sendGet(CONFIGS_URL.replace("$host", host).replace("$tenant", namespaceId).replace("$accessToken", loginDTO.getAccessToken()), "UTF-8");ConfigDTO configDTO = JsonUtils.json(configsResp, ConfigDTO.class);if (null == configDTO) {return;}List<ConfigDTO.Item> pageItems = configDTO.getPageItems();if (null == pageItems) {return;}Properties properties = new Properties();pageItems.forEach(item -> addProperties(item.getDataId(), getPropertySources(item), properties));environment.getPropertySources().addFirst(new PropertiesPropertySource(NACOS_PROPERTY_SOURCE_NAME, properties));}private List<PropertySource<?>> getPropertySources(ConfigDTO.Item item) {List<PropertySource<?>> propertySources = null;try {propertySources = NacosDataParserHandler.getInstance().parseNacosData(item.getDataId(), item.getContent(), null);} catch (Exception e) {log.error(e.getMessage(), e);}return propertySources;}private void addProperties(String dataId, List<PropertySource<?>> propertySources, Properties properties) {if (null == propertySources || propertySources.isEmpty()) {return;}propertySources.forEach(propertySource -> {Object source = propertySource.getSource();if (source instanceof Map) {Map<String, Object> map = (Map) source;map.forEach((key, value) -> {if (!key.equals("spring.application.name") && !key.startsWith("server")) {int i = dataId.lastIndexOf(".");if (i != -1) {properties.setProperty(dataId.substring(0, i + 1) + key, String.valueOf(value));}}});}});}}
http://www.dtcms.com/wzjs/790871.html

相关文章:

  • 中国空间站合作国家名单请多记几个本站域名防止失联
  • 云南省建设厅网站怎么进不去文字生成图片在线使用
  • 贸易公司网站设计案例网站后台页面模板下载
  • 沈阳网站定制亿网通官网
  • 营销型网站的基础建设猎头公司应坚持的原则有
  • 沈阳军成网站建设做网站就上凡科建站
  • 动漫网站建设前期策划做课件需要的纯音乐网站
  • sql可以做网站吗重庆做网站制作公司
  • 网站建设营销怎么做wordpress 找不到文章
  • 网站策划书撰写宠物医疗设计素材网站
  • 受欢迎的宜昌网站建设云南省新农村建设网站
  • 济南高端网站设计某班级网站建设方案
  • 数字资产交易网站开发网页的网站建设
  • 泰顺机械网站建设学佛网站开发项目需求分析
  • 济南外贸网站推广wordpress 图片加文字
  • 专门做二手书的网站新手学习网站建设
  • 程序员是不是都是做网站的优质的低价网站建设
  • 巩义网站优化培训国内电商运营是做什么的
  • 做宣传网站大概多少钱无货源开店已确认违法
  • wordpress导购站主题网站翻译建设
  • 成都网站维护公司上海网站建设哪里好
  • 跨平台网站开发网站在百度的图标显示不正常显示
  • 整形网站专题素材广州网络引流公司
  • 广州市黄埔区建设局网站佛山深圳建网站
  • 网站首页域名有后缀影响搜索吗wordpress的标签有什么用
  • 咸阳网站开发软件开发公司专业的有哪些
  • 上海专业做网站的公司东莞做微信小程序的公司
  • 怎样建立自己的网站赚钱南康网站建设
  • 如何帮人做网站赚钱全国软件公司排名
  • 太原做网站排名跨境电商最好的平台