Search in sources :

Example 1 with NacosPropertySource

use of com.alibaba.cloud.nacos.client.NacosPropertySource in project spring-cloud-alibaba by alibaba.

the class NacosContextRefresher method registerNacosListenersForApplications.

/**
 * register Nacos Listeners.
 */
private void registerNacosListenersForApplications() {
    if (isRefreshEnabled()) {
        for (NacosPropertySource propertySource : NacosPropertySourceRepository.getAll()) {
            if (!propertySource.isRefreshable()) {
                continue;
            }
            String dataId = propertySource.getDataId();
            registerNacosListener(propertySource.getGroup(), dataId);
        }
    }
}
Also used : NacosPropertySource(com.alibaba.cloud.nacos.client.NacosPropertySource)

Example 2 with NacosPropertySource

use of com.alibaba.cloud.nacos.client.NacosPropertySource in project spring-cloud-alibaba by alibaba.

the class NacosConfigEndpoint method invoke.

@ReadOperation
public Map<String, Object> invoke() {
    Map<String, Object> result = new HashMap<>(16);
    result.put("NacosConfigProperties", properties);
    List<NacosPropertySource> all = NacosPropertySourceRepository.getAll();
    List<Map<String, Object>> sources = new ArrayList<>();
    for (NacosPropertySource ps : all) {
        Map<String, Object> source = new HashMap<>(16);
        source.put("dataId", ps.getDataId());
        source.put("lastSynced", dateFormat.get().format(ps.getTimestamp()));
        sources.add(source);
    }
    result.put("Sources", sources);
    result.put("RefreshHistory", refreshHistory.getRecords());
    return result;
}
Also used : NacosPropertySource(com.alibaba.cloud.nacos.client.NacosPropertySource) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) ReadOperation(org.springframework.boot.actuate.endpoint.annotation.ReadOperation)

Example 3 with NacosPropertySource

use of com.alibaba.cloud.nacos.client.NacosPropertySource in project xuxiaowei-cloud by xuxiaowei-cloud.

the class EnvironmentTests method getEnvironment.

/**
 * 获取环境变量
 */
@Test
void getEnvironment() {
    Environment environment = nacosConfigProperties.getEnvironment();
    MutablePropertySources mutablePropertySources = configurableEnvironment.getPropertySources();
    mutablePropertySources.forEach(propertySource -> {
        if (propertySource instanceof OriginTrackedMapPropertySource) {
            OriginTrackedMapPropertySource originTrackedMapPropertySource = (OriginTrackedMapPropertySource) propertySource;
            String name = originTrackedMapPropertySource.getName();
            Map<String, Object> source = originTrackedMapPropertySource.getSource();
            System.out.println("# 配置文件:" + name);
            for (String key : source.keySet()) {
                System.out.println(key + "= " + source.get(key));
            }
            System.out.println();
        } else if (propertySource instanceof MapPropertySource) {
            MapPropertySource mapPropertySource = (MapPropertySource) propertySource;
            String name = mapPropertySource.getName();
            Object property = mapPropertySource.getProperty(name);
        } else if (propertySource instanceof BootstrapPropertySource) {
            PropertySource<?> delegate = ((BootstrapPropertySource<?>) propertySource).getDelegate();
            if (delegate instanceof NacosPropertySource) {
                String name = delegate.getName();
                if (environment instanceof StandardServletEnvironment) {
                    StandardServletEnvironment standardServletEnvironment = (StandardServletEnvironment) environment;
                    MutablePropertySources propertySources = standardServletEnvironment.getPropertySources();
                    PropertySource<?> bootstrapProperties = propertySources.get("bootstrapProperties-" + name);
                    assert bootstrapProperties != null;
                    Object source = bootstrapProperties.getSource();
                    if (source instanceof LinkedHashMap) {
                        LinkedHashMap<?, ?> linkedHashMap = (LinkedHashMap<?, ?>) source;
                        System.out.println("# 配置文件:" + name);
                        for (Object key : linkedHashMap.keySet()) {
                            System.out.println(key + "= " + linkedHashMap.get(key));
                        }
                        System.out.println();
                    }
                }
            }
        }
    });
}
Also used : BootstrapPropertySource(org.springframework.cloud.bootstrap.config.BootstrapPropertySource) StandardServletEnvironment(org.springframework.web.context.support.StandardServletEnvironment) LinkedHashMap(java.util.LinkedHashMap) OriginTrackedMapPropertySource(org.springframework.boot.env.OriginTrackedMapPropertySource) NacosPropertySource(com.alibaba.cloud.nacos.client.NacosPropertySource) OriginTrackedMapPropertySource(org.springframework.boot.env.OriginTrackedMapPropertySource) StandardServletEnvironment(org.springframework.web.context.support.StandardServletEnvironment) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

NacosPropertySource (com.alibaba.cloud.nacos.client.NacosPropertySource)3 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Test (org.junit.jupiter.api.Test)1 ReadOperation (org.springframework.boot.actuate.endpoint.annotation.ReadOperation)1 OriginTrackedMapPropertySource (org.springframework.boot.env.OriginTrackedMapPropertySource)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1 BootstrapPropertySource (org.springframework.cloud.bootstrap.config.BootstrapPropertySource)1 StandardServletEnvironment (org.springframework.web.context.support.StandardServletEnvironment)1