Search in sources :

Example 1 with ConfigService

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));
}
Also used : ConfigService(com.alibaba.nacos.api.config.ConfigService)

Example 2 with ConfigService

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);
}
Also used : ConfigService(com.alibaba.nacos.api.config.ConfigService) Listener(com.alibaba.nacos.api.config.listener.Listener) Executor(java.util.concurrent.Executor) PostConstruct(javax.annotation.PostConstruct)

Example 3 with ConfigService

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();
        }
    }
}
Also used : ConfigService(com.alibaba.nacos.api.config.ConfigService) NacosServiceMetaData(com.alibaba.nacos.spring.metadata.NacosServiceMetaData) CacheableEventPublishingNacosServiceFactory(com.alibaba.nacos.spring.factory.CacheableEventPublishingNacosServiceFactory) NacosServiceFactory(com.alibaba.nacos.spring.factory.NacosServiceFactory) Properties(java.util.Properties)

Example 4 with ConfigService

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;
}
Also used : ConfigService(com.alibaba.nacos.api.config.ConfigService) Listener(com.alibaba.nacos.api.config.listener.Listener) EventListener(com.alibaba.nacos.api.naming.listener.EventListener) ArrayList(java.util.ArrayList) Mockito.anyString(org.mockito.Mockito.anyString)

Example 5 with 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);
}
Also used : NacosFactory(com.alibaba.nacos.api.NacosFactory) ConfigService(com.alibaba.nacos.api.config.ConfigService) NacosProperties(org.apache.shenyu.admin.config.properties.NacosProperties) Properties(java.util.Properties) EnableConfigurationProperties(org.springframework.boot.context.properties.EnableConfigurationProperties) AbstractConfigurationTest(org.apache.shenyu.admin.AbstractConfigurationTest) Test(org.junit.jupiter.api.Test)

Aggregations

ConfigService (com.alibaba.nacos.api.config.ConfigService)35 Properties (java.util.Properties)19 Listener (com.alibaba.nacos.api.config.listener.Listener)11 Executor (java.util.concurrent.Executor)9 Test (org.junit.Test)8 NacosConfigProperties (com.alibaba.cloud.nacos.NacosConfigProperties)3 NacosException (com.alibaba.nacos.api.exception.NacosException)3 AbstractConfigurationTest (org.apache.shenyu.admin.AbstractConfigurationTest)3 Test (org.junit.jupiter.api.Test)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 PostConstruct (javax.annotation.PostConstruct)2 ConfigTable (org.apache.skywalking.oap.server.configuration.api.ConfigTable)2 EnableConfigurationProperties (org.springframework.boot.context.properties.EnableConfigurationProperties)2 Bean (org.springframework.context.annotation.Bean)2 NacosFactory (com.alibaba.nacos.api.NacosFactory)1 ConfigChangeEvent (com.alibaba.nacos.api.config.ConfigChangeEvent)1 ConfigChangeItem (com.alibaba.nacos.api.config.ConfigChangeItem)1