com.alibaba.nacos.spring.util.NacosUtils Java Examples
The following examples show how to use
com.alibaba.nacos.spring.util.NacosUtils.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: NacosRateLimiterConfigCentre.java From api-boot with Apache License 2.0 | 5 votes |
/** * load rate limiter config data * * @return config data * @throws ApiBootException ApiBoot Exception */ protected String loadConfigData() throws ApiBootException { try { return configService.getConfig(DATA_ID, Constants.DEFAULT_GROUP, NacosUtils.DEFAULT_TIMEOUT); } catch (NacosException e) { logger.error(e.getMessage(), e); } throw new ApiBootException("Load ApiBoot RateLimiter config data fail."); }
Example #2
Source File: NacosConfigurationPropertiesBinder.java From nacos-spring-project with Apache License 2.0 | 5 votes |
protected void doBind(Object bean, String beanName, String dataId, String groupId, String type, NacosConfigurationProperties properties, String content, ConfigService configService) { final String prefix = properties.prefix(); PropertyValues propertyValues = NacosUtils.resolvePropertyValues(bean, prefix, dataId, groupId, content, type); doBind(bean, properties, propertyValues); publishBoundEvent(bean, beanName, dataId, groupId, properties, content, configService); publishMetadataEvent(bean, beanName, dataId, groupId, properties); }
Example #3
Source File: NacosConfigLoader.java From nacos-spring-project with Apache License 2.0 | 5 votes |
/** * Load Nacos config vid dataId, groupId and {@link Properties acos Properties} * * @param dataId dataId * @param groupId groupId * @param nacosProperties {@link Properties acos Properties} * @return Nacos config * @throws RuntimeException If {@link ConfigService} creating is failed. */ public String load(String dataId, String groupId, Properties nacosProperties) throws RuntimeException { try { configService = nacosServiceFactory != null ? nacosServiceFactory.createConfigService(nacosProperties) : NacosFactory.createConfigService(nacosProperties); } catch (NacosException e) { throw new RuntimeException("ConfigService can't be created with dataId :" + dataId + " , groupId : " + groupId + " , properties : " + nacosProperties, e); } return NacosUtils.getContent(configService, dataId, groupId); }
Example #4
Source File: NacosConfigEndpoint.java From nacos-spring-boot-project with Apache License 2.0 | 5 votes |
private String buildMetadataKey(NacosConfigMetadataEvent event) { if (event.getXmlResource() != null) { return event.getGroupId() + NacosUtils.SEPARATOR + event.getDataId() + NacosUtils.SEPARATOR + event.getXmlResource(); } else { if (event.getBeanType() == null && event.getAnnotatedElement() == null) { return StringUtils.EMPTY; } return event.getGroupId() + NacosUtils.SEPARATOR + event.getDataId() + NacosUtils.SEPARATOR + event.getBeanType() + NacosUtils.SEPARATOR + event.getAnnotatedElement(); } }
Example #5
Source File: NacosDiscoveryEndpoint.java From nacos-spring-boot-project with Apache License 2.0 | 5 votes |
@ReadOperation public Map<String, Object> invoke() { Map<String, Object> result = new HashMap<>(8); result.put("nacosDiscoveryGlobalProperties", PropertiesUtils.extractSafeProperties(applicationContext.getBean( DISCOVERY_GLOBAL_NACOS_PROPERTIES_BEAN_NAME, Properties.class))); NacosServiceFactory nacosServiceFactory = CacheableEventPublishingNacosServiceFactory .getSingleton(); JSONArray array = new JSONArray(); for (NamingService namingService : nacosServiceFactory.getNamingServices()) { JSONObject jsonObject = new JSONObject(); try { jsonObject.put("servicesOfServer", namingService.getServicesOfServer(0, PAGE_SIZE)); jsonObject.put("subscribeServices", namingService.getSubscribeServices()); array.add(jsonObject); } catch (Exception e) { jsonObject.put("serverStatus", namingService.getServerStatus() + ": " + NacosUtils.SEPARATOR + e.getMessage()); } } result.put("namingServersStatus", array); return result; }
Example #6
Source File: NacosConfigLoader.java From nacos-spring-boot-project with Apache License 2.0 | 5 votes |
private NacosPropertySource[] reqNacosConfig(Properties configProperties, String[] dataIds, String groupId, ConfigType type, boolean isAutoRefresh) { final NacosPropertySource[] propertySources = new NacosPropertySource[dataIds.length]; for (int i = 0; i < dataIds.length; i++) { if (StringUtils.isEmpty(dataIds[i])) { continue; } // Remove excess Spaces final String dataId = environment.resolvePlaceholders(dataIds[i].trim()); final String config = NacosUtils.getContent(builder.apply(configProperties), dataId, groupId); final NacosPropertySource nacosPropertySource = new NacosPropertySource( dataId, groupId, buildDefaultPropertySourceName(dataId, groupId, configProperties), config, type.getType()); nacosPropertySource.setDataId(dataId); nacosPropertySource.setType(type.getType()); nacosPropertySource.setGroupId(groupId); nacosPropertySource.setAutoRefreshed(isAutoRefresh); logger.info("load config from nacos, data-id is : {}, group is : {}", nacosPropertySource.getDataId(), nacosPropertySource.getGroupId()); propertySources[i] = nacosPropertySource; DeferNacosPropertySource defer = new DeferNacosPropertySource( nacosPropertySource, configProperties, environment); nacosPropertySources.add(defer); } return propertySources; }
Example #7
Source File: NacosConfigListenerMethodProcessor.java From nacos-spring-project with Apache License 2.0 | 4 votes |
@Override protected void processListenerMethod(String beanName, final Object bean, Class<?> beanClass, final NacosConfigListener listener, final Method method, ApplicationContext applicationContext) { final String dataId = NacosUtils.readFromEnvironment(listener.dataId(), environment); final String groupId = NacosUtils.readFromEnvironment(listener.groupId(), environment); final String type = StringUtils.isEmpty(NacosUtils.readTypeFromDataId(dataId)) ? listener.type().getType() : NacosUtils.readTypeFromDataId(dataId); long timeout = listener.timeout(); Assert.isTrue(StringUtils.hasText(dataId), "dataId must have content"); Assert.isTrue(StringUtils.hasText(groupId), "groupId must have content"); Assert.isTrue(timeout > 0, "timeout must be greater than zero"); ConfigService configService = configServiceBeanBuilder .build(listener.properties()); try { configService.addListener(dataId, groupId, new TimeoutNacosConfigListener(dataId, groupId, timeout) { @Override protected void onReceived(String config) { Class<?> targetType = method.getParameterTypes()[0]; NacosConfigConverter configConverter = determineNacosConfigConverter( targetType, listener, type); Object parameterValue = configConverter.convert(config); // Execute target method ReflectionUtils.invokeMethod(method, bean, parameterValue); } }); } catch (NacosException e) { logger.error("ConfigService can't add Listener for dataId : " + dataId + " , groupId : " + groupId, e); } publishMetadataEvent(beanName, bean, beanClass, dataId, groupId, listener, method); }
Example #8
Source File: NacosConfigurationPropertiesBinder.java From nacos-spring-project with Apache License 2.0 | 4 votes |
protected void bind(final Object bean, final String beanName, final NacosConfigurationProperties properties) { Assert.notNull(bean, "Bean must not be null!"); Assert.notNull(properties, "NacosConfigurationProperties must not be null!"); // support read data-id and group-id from spring environment final String dataId = NacosUtils.readFromEnvironment(properties.dataId(), environment); final String groupId = NacosUtils.readFromEnvironment(properties.groupId(), environment); String fileType = NacosUtils.readTypeFromDataId(dataId); final String type = StringUtils.isEmpty(fileType) ? (properties.yaml() ? ConfigType.YAML.getType() : properties.type().getType()) : fileType; final ConfigService configService = configServiceBeanBuilder .build(properties.properties()); // Add a Listener if auto-refreshed if (properties.autoRefreshed()) { Listener listener = new AbstractListener() { @Override public void receiveConfigInfo(String config) { doBind(bean, beanName, dataId, groupId, type, properties, config, configService); } }; try {// if (configService instanceof EventPublishingConfigService) { ((EventPublishingConfigService) configService).addListener(dataId, groupId, type, listener); } else { configService.addListener(dataId, groupId, listener); } } catch (NacosException e) { if (logger.isErrorEnabled()) { logger.error(e.getMessage(), e); } } } String content = getContent(configService, dataId, groupId); if (hasText(content)) { doBind(bean, beanName, dataId, groupId, type, properties, content, configService); } }
Example #9
Source File: AbstractNacosPropertySourceBuilder.java From nacos-spring-project with Apache License 2.0 | 4 votes |
protected NacosPropertySource doBuild(String beanName, T beanDefinition, Map<String, Object> runtimeAttributes) { // Get annotation metadata String name = (String) runtimeAttributes.get(NAME_ATTRIBUTE_NAME); String dataId = (String) runtimeAttributes.get(DATA_ID_ATTRIBUTE_NAME); String groupId = (String) runtimeAttributes.get(GROUP_ID_ATTRIBUTE_NAME); String type = ((ConfigType) runtimeAttributes.get(CONFIG_TYPE_ATTRIBUTE_NAME)) .getType(); dataId = NacosUtils.readFromEnvironment(dataId, environment); groupId = NacosUtils.readFromEnvironment(groupId, environment); type = StringUtils.isEmpty(NacosUtils.readTypeFromDataId(dataId)) ? type : NacosUtils.readTypeFromDataId(dataId); Map<String, Object> nacosPropertiesAttributes = (Map<String, Object>) runtimeAttributes .get(PROPERTIES_ATTRIBUTE_NAME); Properties nacosProperties = resolveProperties(nacosPropertiesAttributes, environment, globalNacosProperties); String nacosConfig = nacosConfigLoader.load(dataId, groupId, nacosProperties); if (!StringUtils.hasText(nacosConfig)) { if (logger.isWarnEnabled()) { logger.warn(format( "There is no content for NacosPropertySource from dataId[%s] , groupId[%s] , properties[%s].", dataId, groupId, nacosPropertiesAttributes)); } } if (!StringUtils.hasText(name)) { name = buildDefaultPropertySourceName(dataId, groupId, nacosProperties); } NacosPropertySource nacosPropertySource = new NacosPropertySource(dataId, groupId, name, nacosConfig, type); nacosPropertySource.setBeanName(beanName); String beanClassName = beanDefinition.getBeanClassName(); if (StringUtils.hasText(beanClassName)) { nacosPropertySource.setBeanType(resolveClassName(beanClassName, classLoader)); } nacosPropertySource.setGroupId(groupId); nacosPropertySource.setDataId(dataId); nacosPropertySource.setProperties(nacosProperties); initNacosPropertySource(nacosPropertySource, beanDefinition, runtimeAttributes); return nacosPropertySource; }
Example #10
Source File: AbstractNacosServiceBeanBuilder.java From nacos-spring-project with Apache License 2.0 | 3 votes |
/** * Resolve Nacos {@link Properties} from {@link NacosProperties @NacosProperties} * * @param nacosPropertiesAttributes {@link NacosProperties Nacos Properties}'s * attributes * @return non-null */ public final Properties resolveProperties( Map<String, Object> nacosPropertiesAttributes) { Properties globalNacosProperties = resolveGlobalNacosProperties(); return NacosUtils.resolveProperties(nacosPropertiesAttributes, environment, globalNacosProperties); }
Example #11
Source File: AbstractNacosServiceBeanBuilder.java From nacos-spring-project with Apache License 2.0 | 2 votes |
/** * Resolve Nacos {@link Properties} from {@link NacosProperties @NacosProperties} * * @param nacosProperties {@link NacosProperties @NacosProperties} * @return non-null */ public final Properties resolveProperties(NacosProperties nacosProperties) { Properties globalNacosProperties = resolveGlobalNacosProperties(); return NacosUtils.resolveProperties(nacosProperties, environment, globalNacosProperties); }