use of com.alibaba.nacos.api.config.ConfigService in project Sentinel by alibaba.
the class NacosConfigSender method main.
public static void main(String[] args) throws Exception {
final String remoteAddress = "localhost:8848";
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));
}
use of com.alibaba.nacos.api.config.ConfigService in project blog_demos by zq2599.
the class RouteConfigListener method dynamicRouteByNacosListener.
@PostConstruct
public void dynamicRouteByNacosListener() throws NacosException {
ConfigService configService = NacosFactory.createConfigService(serverAddr);
// 添加监听,nacos上的配置变更后会执行
configService.addListener(dataId, group, new Listener() {
public void receiveConfigInfo(String configInfo) {
// 解析和处理都交给RouteOperator完成
routeOperator.refreshAll(configInfo);
}
public Executor getExecutor() {
return null;
}
});
// 获取当前的配置
String initConfig = configService.getConfig(dataId, group, 5000);
// 立即更新
routeOperator.refreshAll(initConfig);
}
use of com.alibaba.nacos.api.config.ConfigService in project nacos-spring-boot-project by nacos-group.
the class NacosConfigHealthIndicator method doHealthCheck.
@Override
protected void doHealthCheck(Health.Builder builder) throws Exception {
builder.up();
NacosServiceFactory nacosServiceFactory = CacheableEventPublishingNacosServiceFactory.getSingleton();
for (ConfigService configService : nacosServiceFactory.getConfigServices()) {
if (configService instanceof NacosServiceMetaData) {
NacosServiceMetaData nacosServiceMetaData = (NacosServiceMetaData) configService;
Properties properties = nacosServiceMetaData.getProperties();
builder.withDetail(JacksonUtils.toJson(PropertiesUtils.extractSafeProperties(properties)), configService.getServerStatus());
}
if (!configService.getServerStatus().toLowerCase().equals(UP_STATUS)) {
builder.down();
}
}
}
use of com.alibaba.nacos.api.config.ConfigService in project incubator-shenyu by apache.
the class NacosServerRegisterRepositoryTest method mockConfigService.
private ConfigService mockConfigService() throws NacosException {
ConfigService configService = mock(ConfigService.class);
doAnswer(invocationOnMock -> {
this.configListener = invocationOnMock.getArgument(2);
return null;
}).when(configService).addListener(anyString(), anyString(), any(Listener.class));
doAnswer(invocationOnMock -> {
List<String> list = new ArrayList<>();
list.add(GsonUtils.getInstance().toJson(MetaDataRegisterDTO.builder().build()));
return GsonUtils.getInstance().toJson(list);
}).when(configService).getConfig(anyString(), anyString(), anyLong());
return configService;
}
use of com.alibaba.nacos.api.config.ConfigService in project incubator-shenyu by apache.
the class NacosConfigurationTest method testNacosConfigService.
@Test
public void testNacosConfigService() {
String[] inlinedProperties = new String[] { "shenyu.sync.nacos.url=localhost:8848", "shenyu.sync.nacos.namespace=1c10d748-af86-43b9-8265-75f487d20c6c", "shenyu.sync.nacos.acm.enabled=true", "shenyu.sync.nacos.acm.endpoint=localhost:8849", "shenyu.sync.nacos.acm.namespace=namespace", "shenyu.sync.nacos.acm.accessKey=accessKey", "shenyu.sync.nacos.acm.secretKey=secretKey" };
try (MockedStatic<NacosFactory> nacosFactoryMockedStatic = Mockito.mockStatic(NacosFactory.class)) {
final ConfigService configServiceMock = Mockito.mock(ConfigService.class);
ArgumentCaptor<Properties> argument = ArgumentCaptor.forClass(Properties.class);
nacosFactoryMockedStatic.when(() -> NacosFactory.createConfigService(argument.capture())).thenReturn(configServiceMock);
load(NacosConfiguration.class, inlinedProperties);
assertTrue(argument.getValue().containsKey(PropertyKeyConst.ENDPOINT));
}
ConfigService configService = (ConfigService) getContext().getBean("nacosConfigService");
assertNotNull(configService);
}
Aggregations