Java Code Examples for com.alibaba.nacos.api.NacosFactory#createConfigService()
The following examples show how to use
com.alibaba.nacos.api.NacosFactory#createConfigService() .
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: NacosUtil.java From anyline with Apache License 2.0 | 6 votes |
/** * 读取配置文件 并 监听配置更新 * @param group group * @param data data * @param listener listent * @return String * @throws NacosException NacosException */ public String config(String group, String data, Listener listener) throws NacosException{ if(BasicUtil.isEmpty(group)){ group = config.GROUP; } log.warn("[nacos config][group:{}][data:{}][listener:{}]", group, data, listener); Properties properties = new Properties(); properties.put(PropertyKeyConst.NAMESPACE, config.NAMESPACE); properties.put(PropertyKeyConst.SERVER_ADDR, config.ADDRESS+":"+config.PORT); ConfigService configService = NacosFactory.createConfigService(properties); String content = configService.getConfig(data, group, config.TIMEOUT); if(null != listener) { configService.addListener(data, group, listener); } return content; }
Example 2
Source File: AbstractNacosConfigService.java From nacos-tutorial with Apache License 2.0 | 6 votes |
public AbstractNacosConfigService(String serverAddress, String dataId, String group) { this.unitMap = new ConcurrentHashMap<>(); this.configMap = new ConcurrentHashMap<>(); // 刷新 unit this.refreshUnit(); this.dataId = dataId; this.group = group; Properties properties = new Properties(); properties.put("serverAddr", serverAddress); try { this.configService = NacosFactory.createConfigService(properties); // 增加监听器 Listener listener = new ConfigListener(); this.configService.addListener(dataId, group, listener); } catch (NacosException e) { e.printStackTrace(); } }
Example 3
Source File: NacosConfigSender.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 6 votes |
public static void main(String[] args) throws Exception { final String remoteAddress = "localhost"; final String groupId = "Sentinel:Demo"; final String dataId = "com.alibaba.csp.sentinel.demo.flow.rule"; final String rule = "[\n" + " {\n" + " \"resource\": \"TestResource\",\n" + " \"controlBehavior\": 0,\n" + " \"count\": 5.0,\n" + " \"grade\": 1,\n" + " \"limitApp\": \"default\",\n" + " \"strategy\": 0\n" + " }\n" + "]"; ConfigService configService = NacosFactory.createConfigService(remoteAddress); System.out.println(configService.publishConfig(dataId, groupId, rule)); }
Example 4
Source File: NacosConfigManager.java From spring-cloud-alibaba with Apache License 2.0 | 6 votes |
/** * Compatible with old design,It will be perfected in the future. */ static ConfigService createConfigService( NacosConfigProperties nacosConfigProperties) { if (Objects.isNull(service)) { synchronized (NacosConfigManager.class) { try { if (Objects.isNull(service)) { service = NacosFactory.createConfigService( nacosConfigProperties.assembleConfigServiceProperties()); } } catch (NacosException e) { log.error(e.getMessage()); throw new NacosConnectionFailureException( nacosConfigProperties.getServerAddr(), e.getMessage(), e); } } } return service; }
Example 5
Source File: NacosConfiguration.java From soul with Apache License 2.0 | 6 votes |
/** * register configService in spring ioc. * * @param nacosConfig the nacos configuration * @return ConfigService {@linkplain ConfigService} * @throws Exception the exception */ @Bean public ConfigService nacosConfigService(final NacosConfig nacosConfig) throws Exception { Properties properties = new Properties(); if (nacosConfig.getAcm() != null && nacosConfig.getAcm().isEnabled()) { //使用阿里云ACM服务 properties.put(PropertyKeyConst.ENDPOINT, nacosConfig.getAcm().getEndpoint()); properties.put(PropertyKeyConst.NAMESPACE, nacosConfig.getAcm().getNamespace()); //使用子账户ACM管理权限 properties.put(PropertyKeyConst.ACCESS_KEY, nacosConfig.getAcm().getAccessKey()); properties.put(PropertyKeyConst.SECRET_KEY, nacosConfig.getAcm().getSecretKey()); } else { properties.put(PropertyKeyConst.SERVER_ADDR, nacosConfig.getUrl()); properties.put(PropertyKeyConst.NAMESPACE, nacosConfig.getNamespace()); } return NacosFactory.createConfigService(properties); }
Example 6
Source File: DynamicRouteServiceImplByNacos.java From spring-cloud-gateway-nacos with Apache License 2.0 | 6 votes |
/** * 监听Nacos Server下发的动态路由配置 * @param dataId * @param group */ public void dynamicRouteByNacosListener (String dataId, String group){ try { ConfigService configService=NacosFactory.createConfigService("127.0.0.1:8848"); String content = configService.getConfig(dataId, group, 5000); System.out.println(content); configService.addListener(dataId, group, new Listener() { @Override public void receiveConfigInfo(String configInfo) { RouteDefinition definition= JSON.parseObject(configInfo,RouteDefinition.class); dynamicRouteService.update(definition); } @Override public Executor getExecutor() { return null; } }); } catch (NacosException e) { //todo 提醒:异常自行处理此处省略 } }
Example 7
Source File: CacheableEventPublishingNacosServiceFactory.java From nacos-spring-project with Apache License 2.0 | 6 votes |
@Override public ConfigService run(Properties properties, ConfigService service) throws NacosException { String cacheKey = identify(properties); ConfigService configService = configServicesCache.get(cacheKey); if (configService == null) { if (service == null) { service = NacosFactory.createConfigService(properties); } configService = new EventPublishingConfigService(service, properties, getSingleton().context, getSingleton().nacosConfigListenerExecutor); configServicesCache.put(cacheKey, configService); } return configService; }
Example 8
Source File: NacosConfigSender.java From Sentinel with Apache License 2.0 | 6 votes |
public static void main(String[] args) throws Exception { final String remoteAddress = "localhost"; final String groupId = "Sentinel:Demo"; final String dataId = "com.alibaba.csp.sentinel.demo.flow.rule"; final String rule = "[\n" + " {\n" + " \"resource\": \"TestResource\",\n" + " \"controlBehavior\": 0,\n" + " \"count\": 5.0,\n" + " \"grade\": 1,\n" + " \"limitApp\": \"default\",\n" + " \"strategy\": 0\n" + " }\n" + "]"; ConfigService configService = NacosFactory.createConfigService(remoteAddress); System.out.println(configService.publishConfig(dataId, groupId, rule)); }
Example 9
Source File: NacosUtils.java From dubbo-samples with Apache License 2.0 | 6 votes |
public static void writeDubboProperties() throws Throwable { String serverAddr = System.getProperty("nacos.address", "localhost"); String dataId = "dubbo.properties"; String group = "dubbo"; Properties properties = new Properties(); properties.put(PropertyKeyConst.SERVER_ADDR, serverAddr); ConfigService configService = NacosFactory.createConfigService(properties); StringBuilder content = new StringBuilder(); File file = new File(NacosUtils.class.getClassLoader().getResource("config-center.properties").getFile()); try (FileReader reader = new FileReader(file)) { BufferedReader br = new BufferedReader(reader); String line; while ((line = br.readLine()) != null) { if (line.startsWith("dubbo.registry.address=")) { line = "dubbo.registry.address=nacos://" + serverAddr + ":8848"; } content.append(line).append("\n"); } } if (configService.publishConfig(dataId, group, content.toString())) { System.out.println("write " + dataId + ":" + group + " successfully."); } }
Example 10
Source File: PropertiesAssemble.java From spring-cloud-zuul-nacos with Apache License 2.0 | 6 votes |
private List<ZuulRouteEntity> listenerNacos (String dataId, String group) { try { Properties properties = new Properties(); properties.put(PropertyKeyConst.SERVER_ADDR, "localhost:8848"); ConfigService configService = NacosFactory.createConfigService(properties); String content = configService.getConfig(dataId, group, 5000); System.out.println("从Nacos返回的配置:" + content); //注册Nacos配置更新监听器 // configService.addListener(dataId, group, new Listener() { // @Override // public void receiveConfigInfo(String configInfo) { // System.out.println("Nacos更新了!"); // // } // @Override // public Executor getExecutor() { // return null; // } // }); return JSONObject.parseArray(content, ZuulRouteEntity.class); } catch (NacosException e) { e.printStackTrace(); } return new ArrayList<>(); }
Example 11
Source File: NacosCenterRepository.java From shardingsphere with Apache License 2.0 | 5 votes |
/** * Initialize nacos instance. * * @param config config center configuration */ @Override public void init(final CenterConfiguration config) { try { nacosProperties = new NacosProperties(props); Properties props = new Properties(); props.put(PropertyKeyConst.SERVER_ADDR, config.getServerLists()); props.put(PropertyKeyConst.NAMESPACE, null == config.getNamespace() ? "" : config.getNamespace()); configService = NacosFactory.createConfigService(props); } catch (final NacosException ex) { log.error("Init nacos config center exception for: {}", ex.toString()); } }
Example 12
Source File: NacosConfigManager.java From jboot with Apache License 2.0 | 5 votes |
/** * 初始化 nacos 配置监听 */ public void init(JbootConfigManager configManager) { NacosServerConfig nacosServerConfig = configManager.get(NacosServerConfig.class); if (!nacosServerConfig.isEnable() || !nacosServerConfig.isConfigOk()) { return; } try { ConfigService configService = NacosFactory.createConfigService(nacosServerConfig.toProperties()); String content = configService.getConfig(nacosServerConfig.getDataId() , nacosServerConfig.getGroup(), 3000); if (StrUtil.isNotBlank(content)) { contentProperties = str2Properties(content); if (contentProperties != null) { configManager.setRemoteProperties(contentProperties); } } new NacosConfigIniter(this, configManager).initListener(configService, nacosServerConfig); } catch (Exception e) { e.printStackTrace(); } }
Example 13
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 14
Source File: NacosUtils.java From dubbo-samples with Apache License 2.0 | 5 votes |
public static void writeAppRule() throws Throwable { String serverAddr = System.getProperty("nacos.address", "localhost"); String dataId = "governance-conditionrouter-consumer.condition-router"; String group = "dubbo"; Properties properties = new Properties(); properties.put(PropertyKeyConst.SERVER_ADDR, serverAddr); ConfigService configService = NacosFactory.createConfigService(properties); try (InputStream is = NacosUtils.class.getClassLoader().getResourceAsStream("dubbo-routers-condition.yml")) { String content = IOUtils.toString(is); if (configService.publishConfig(dataId, group, content)) { System.out.println("write " + dataId + ":" + group + " successfully."); } } }
Example 15
Source File: NacosDataSource.java From Sentinel with Apache License 2.0 | 5 votes |
private void initNacosListener() { try { this.configService = NacosFactory.createConfigService(this.properties); // Add config listener. configService.addListener(dataId, groupId, configListener); } catch (Exception e) { RecordLog.warn("[NacosDataSource] Error occurred when initializing Nacos data source", e); e.printStackTrace(); } }
Example 16
Source File: NacosSyncDataConfiguration.java From soul with Apache License 2.0 | 5 votes |
/** * Nacos config service config service. * * @param nacosConfig the nacos config * @return the config service * @throws Exception the exception */ @Bean public ConfigService nacosConfigService(final NacosConfig nacosConfig) throws Exception { Properties properties = new Properties(); if (nacosConfig.getAcm() != null && nacosConfig.getAcm().isEnabled()) { properties.put(PropertyKeyConst.ENDPOINT, nacosConfig.getAcm().getEndpoint()); properties.put(PropertyKeyConst.NAMESPACE, nacosConfig.getAcm().getNamespace()); properties.put(PropertyKeyConst.ACCESS_KEY, nacosConfig.getAcm().getAccessKey()); properties.put(PropertyKeyConst.SECRET_KEY, nacosConfig.getAcm().getSecretKey()); } else { properties.put(PropertyKeyConst.SERVER_ADDR, nacosConfig.getUrl()); properties.put(PropertyKeyConst.NAMESPACE, nacosConfig.getNamespace()); } return NacosFactory.createConfigService(properties); }
Example 17
Source File: ApiBootRateLimiterNacosConfigConfiguration.java From api-boot with Apache License 2.0 | 5 votes |
/** * init ConfigService Instance * * @return NacosConfigService * @throws NacosException Nacos Exception */ @Bean @ConditionalOnMissingBean(name = NACOS_CONFIG_SERVICE_NAME) public ConfigService nacosConfigService() throws NacosException { Properties properties = new Properties(); properties.put(SERVER_ADDR, Objects.toString(nacosConfigProperties.getServerAddr(), "")); properties.put(ENCODE, Objects.toString(nacosConfigProperties.getEncode(), "")); properties.put(NAMESPACE, Objects.toString(nacosConfigProperties.getNamespace(), "")); properties.put(ACCESS_KEY, Objects.toString(nacosConfigProperties.getAccessKey(), "")); properties.put(SECRET_KEY, Objects.toString(nacosConfigProperties.getSecretKey(), "")); properties.put(CONTEXT_PATH, Objects.toString(nacosConfigProperties.getContextPath(), "")); properties.put(ENDPOINT, Objects.toString(nacosConfigProperties.getEndpoint(), "")); return NacosFactory.createConfigService(properties); }
Example 18
Source File: NacosDataSource.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 5 votes |
private void initNacosListener() { try { this.configService = NacosFactory.createConfigService(this.properties); // Add config listener. configService.addListener(dataId, groupId, configListener); } catch (Exception e) { RecordLog.warn("[NacosDataSource] Error occurred when initializing Nacos data source", e); e.printStackTrace(); } }
Example 19
Source File: FlinkNacosTest2.java From flink-learning with Apache License 2.0 | 4 votes |
public static void main(String[] args) throws Exception { StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); env.getConfig().setGlobalJobParameters(ParameterTool.fromArgs(args)); env.setParallelism(1); String serverAddr = "localhost"; String dataId = "test"; String group = "DEFAULT_GROUP"; Properties properties = new Properties(); properties.put("serverAddr", serverAddr); ConfigService configService = NacosFactory.createConfigService(properties); final String[] content = {configService.getConfig(dataId, group, 5000)}; configService.addListener(dataId, group, new Listener() { @Override public Executor getExecutor() { return null; } @Override public void receiveConfigInfo(String configInfo) { System.out.println("==============="); content[0] = configInfo; env.getCheckpointConfig().setCheckpointInterval(Long.valueOf(content[0])); System.out.println("----------"); System.out.println(env.getCheckpointConfig().getCheckpointInterval()); } }); System.out.println(content[0]); env.getCheckpointConfig().setCheckpointInterval(Long.valueOf(content[0])); env.getCheckpointConfig().setCheckpointTimeout(1000); env.getCheckpointConfig().setCheckpointingMode(CheckpointingMode.EXACTLY_ONCE); env.getCheckpointConfig().enableExternalizedCheckpoints(CheckpointConfig.ExternalizedCheckpointCleanup.DELETE_ON_CANCELLATION); env.addSource(new SourceFunction<Tuple2<String, Long>>() { @Override public void run(SourceContext<Tuple2<String, Long>> ctx) throws Exception { while (true) { ctx.collect(new Tuple2<>("zhisheng", System.currentTimeMillis())); Thread.sleep(800); } } @Override public void cancel() { } }).print(); env.execute(); }
Example 20
Source File: FlinkNacosTest2.java From flink-learning with Apache License 2.0 | 4 votes |
public static void main(String[] args) throws Exception { StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); env.getConfig().setGlobalJobParameters(ParameterTool.fromArgs(args)); env.setParallelism(1); String serverAddr = "localhost"; String dataId = "test"; String group = "DEFAULT_GROUP"; Properties properties = new Properties(); properties.put("serverAddr", serverAddr); ConfigService configService = NacosFactory.createConfigService(properties); final String[] content = {configService.getConfig(dataId, group, 5000)}; configService.addListener(dataId, group, new Listener() { @Override public Executor getExecutor() { return null; } @Override public void receiveConfigInfo(String configInfo) { System.out.println("==============="); content[0] = configInfo; env.getCheckpointConfig().setCheckpointInterval(Long.valueOf(content[0])); System.out.println("----------"); System.out.println(env.getCheckpointConfig().getCheckpointInterval()); } }); System.out.println(content[0]); env.getCheckpointConfig().setCheckpointInterval(Long.valueOf(content[0])); env.getCheckpointConfig().setCheckpointTimeout(1000); env.getCheckpointConfig().setCheckpointingMode(CheckpointingMode.EXACTLY_ONCE); env.getCheckpointConfig().enableExternalizedCheckpoints(CheckpointConfig.ExternalizedCheckpointCleanup.DELETE_ON_CANCELLATION); env.addSource(new SourceFunction<Tuple2<String, Long>>() { @Override public void run(SourceContext<Tuple2<String, Long>> ctx) throws Exception { while (true) { ctx.collect(new Tuple2<>("zhisheng", System.currentTimeMillis())); Thread.sleep(800); } } @Override public void cancel() { } }).print(); env.execute(); }