com.alibaba.csp.sentinel.datasource.nacos.NacosDataSource Java Examples

The following examples show how to use com.alibaba.csp.sentinel.datasource.nacos.NacosDataSource. 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: NacosDataSourceFactoryBeanTests.java    From spring-cloud-alibaba with Apache License 2.0 6 votes vote down vote up
@Test
public void testNacosFactoryBeanServerAddr() throws Exception {
	NacosDataSourceFactoryBean factoryBean = spy(new NacosDataSourceFactoryBean());

	Converter converter = mock(SentinelConverter.class);

	factoryBean.setDataId(dataId);
	factoryBean.setGroupId(groupId);
	factoryBean.setServerAddr(serverAddr);
	factoryBean.setConverter(converter);

	NacosDataSource nacosDataSource = mock(NacosDataSource.class);

	doReturn(nacosDataSource).when(factoryBean).getObject();
	when(nacosDataSource.readSource()).thenReturn("{}");

	assertThat(factoryBean.getObject()).isEqualTo(nacosDataSource);
	assertThat(factoryBean.getObject().readSource()).isEqualTo("{}");
	assertThat(factoryBean.getConverter()).isEqualTo(converter);
	assertThat(factoryBean.getDataId()).isEqualTo(dataId);
	assertThat(factoryBean.getGroupId()).isEqualTo(groupId);
	assertThat(factoryBean.getServerAddr()).isEqualTo(serverAddr);
}
 
Example #2
Source File: NacosDataSourceFactoryBean.java    From spring-cloud-alibaba with Apache License 2.0 6 votes vote down vote up
@Override
public NacosDataSource getObject() throws Exception {
	Properties properties = new Properties();
	if (!StringUtils.isEmpty(this.serverAddr)) {
		properties.setProperty(PropertyKeyConst.SERVER_ADDR, this.serverAddr);
	}
	else {
		properties.setProperty(PropertyKeyConst.ACCESS_KEY, this.accessKey);
		properties.setProperty(PropertyKeyConst.SECRET_KEY, this.secretKey);
		properties.setProperty(PropertyKeyConst.ENDPOINT, this.endpoint);
	}
	if (!StringUtils.isEmpty(this.namespace)) {
		properties.setProperty(PropertyKeyConst.NAMESPACE, this.namespace);
	}
	properties.setProperty(PropertyKeyConst.USERNAME, this.username);
	properties.setProperty(PropertyKeyConst.PASSWORD, this.password);
	return new NacosDataSource(properties, groupId, dataId, converter);
}
 
Example #3
Source File: ClusterFlowClientController.java    From sentinel-tutorial with Apache License 2.0 5 votes vote down vote up
/**
 * 为ClusterClientConfig注册一个SentinelProperty
 * 这样的话可以动态的更改这些配置
 */
private void registerClusterClientProperty() {
    String clientConfigDataId = "cluster-client-config";
    // 初始化一个配置ClusterClientConfig的 Nacos 数据源
    ReadableDataSource<String, ClusterClientConfig> clientConfigDS = new NacosDataSource<>(REMOTE_ADDRESS, GROUP_ID, clientConfigDataId,
            source -> JSON.parseObject(source, new TypeReference<ClusterClientConfig>() {}));
    ClusterClientConfigManager.registerClientConfigProperty(clientConfigDS.getProperty());

    String clientAssignConfigDataId = "cluster-client-assign-config";
    // 初始化一个配置ClusterClientAssignConfig的 Nacos 数据源
    ReadableDataSource<String, ClusterClientAssignConfig> clientAssignConfigDS = new NacosDataSource<>(REMOTE_ADDRESS, GROUP_ID, clientAssignConfigDataId,
            source -> JSON.parseObject(source, new TypeReference<ClusterClientAssignConfig>() {}));
    ClusterClientConfigManager.registerServerAssignProperty(clientAssignConfigDS.getProperty());

}
 
Example #4
Source File: NacosDataSourceFactoryBeanTests.java    From spring-cloud-alibaba with Apache License 2.0 5 votes vote down vote up
@Test
public void testNacosFactoryBeanProperties() throws Exception {
	NacosDataSourceFactoryBean factoryBean = spy(new NacosDataSourceFactoryBean());

	Converter converter = mock(SentinelConverter.class);

	factoryBean.setDataId(dataId);
	factoryBean.setGroupId(groupId);
	factoryBean.setAccessKey(accessKey);
	factoryBean.setSecretKey(secretKey);
	factoryBean.setEndpoint(endpoint);
	factoryBean.setNamespace(namespace);
	factoryBean.setConverter(converter);

	NacosDataSource nacosDataSource = mock(NacosDataSource.class);

	doReturn(nacosDataSource).when(factoryBean).getObject();
	when(nacosDataSource.readSource()).thenReturn("{}");

	assertThat(factoryBean.getObject()).isEqualTo(nacosDataSource);
	assertThat(factoryBean.getObject().readSource()).isEqualTo("{}");
	assertThat(factoryBean.getConverter()).isEqualTo(converter);
	assertThat(factoryBean.getDataId()).isEqualTo(dataId);
	assertThat(factoryBean.getGroupId()).isEqualTo(groupId);
	assertThat(factoryBean.getNamespace()).isEqualTo(namespace);
	assertThat(factoryBean.getEndpoint()).isEqualTo(endpoint);
	assertThat(factoryBean.getAccessKey()).isEqualTo(accessKey);
	assertThat(factoryBean.getSecretKey()).isEqualTo(secretKey);
}
 
Example #5
Source File: NacosDataSourceDemo.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
private static void loadMyNamespaceRules() {
    Properties properties = new Properties();
    properties.put(PropertyKeyConst.SERVER_ADDR, remoteAddress);
    properties.put(PropertyKeyConst.NAMESPACE, NACOS_NAMESPACE_ID);

    ReadableDataSource<String, List<FlowRule>> flowRuleDataSource = new NacosDataSource<>(properties, groupId, dataId,
            source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {
            }));
    FlowRuleManager.register2Property(flowRuleDataSource.getProperty());
}
 
Example #6
Source File: DemoClusterInitFunc.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
private void initStateProperty() {
    // Cluster map format:
    // [{"clientSet":["112.12.88.66@8729","112.12.88.67@8727"],"ip":"112.12.88.68","machineId":"112.12.88.68@8728","port":11111}]
    // machineId: <ip@commandPort>, commandPort for port exposed to Sentinel dashboard (transport module)
    ReadableDataSource<String, Integer> clusterModeDs = new NacosDataSource<>(remoteAddress, groupId,
        clusterMapDataId, source -> {
        List<ClusterGroupEntity> groupList = JSON.parseObject(source, new TypeReference<List<ClusterGroupEntity>>() {});
        return Optional.ofNullable(groupList)
            .map(this::extractMode)
            .orElse(ClusterStateManager.CLUSTER_NOT_STARTED);
    });
    ClusterStateManager.registerProperty(clusterModeDs.getProperty());
}
 
Example #7
Source File: DemoClusterInitFunc.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
private void initClientServerAssignProperty() {
    // Cluster map format:
    // [{"clientSet":["112.12.88.66@8729","112.12.88.67@8727"],"ip":"112.12.88.68","machineId":"112.12.88.68@8728","port":11111}]
    // machineId: <ip@commandPort>, commandPort for port exposed to Sentinel dashboard (transport module)
    ReadableDataSource<String, ClusterClientAssignConfig> clientAssignDs = new NacosDataSource<>(remoteAddress, groupId,
        clusterMapDataId, source -> {
        List<ClusterGroupEntity> groupList = JSON.parseObject(source, new TypeReference<List<ClusterGroupEntity>>() {});
        return Optional.ofNullable(groupList)
            .flatMap(this::extractClientAssignment)
            .orElse(null);
    });
    ClusterClientConfigManager.registerServerAssignProperty(clientAssignDs.getProperty());
}
 
Example #8
Source File: DemoClusterInitFunc.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
private void initServerTransportConfigProperty() {
    ReadableDataSource<String, ServerTransportConfig> serverTransportDs = new NacosDataSource<>(remoteAddress, groupId,
        clusterMapDataId, source -> {
        List<ClusterGroupEntity> groupList = JSON.parseObject(source, new TypeReference<List<ClusterGroupEntity>>() {});
        return Optional.ofNullable(groupList)
            .flatMap(this::extractServerTransportConfig)
            .orElse(null);
    });
    ClusterServerConfigManager.registerServerTransportProperty(serverTransportDs.getProperty());
}
 
Example #9
Source File: DemoClusterInitFunc.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
private void initDynamicRuleProperty() {
    ReadableDataSource<String, List<FlowRule>> ruleSource = new NacosDataSource<>(remoteAddress, groupId,
        flowDataId, source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {}));
    FlowRuleManager.register2Property(ruleSource.getProperty());

    ReadableDataSource<String, List<ParamFlowRule>> paramRuleSource = new NacosDataSource<>(remoteAddress, groupId,
        paramDataId, source -> JSON.parseObject(source, new TypeReference<List<ParamFlowRule>>() {}));
    ParamFlowRuleManager.register2Property(paramRuleSource.getProperty());
}
 
Example #10
Source File: ClusterFlowClientController.java    From sentinel-tutorial with Apache License 2.0 5 votes vote down vote up
/**
 * 注册动态规则Property
 * 当client与Server连接中断,退化为本地限流时需要用到的该规则
 */
private void registerClusterFlowRuleProperty(){
    // 使用 Nacos 数据源作为配置中心,需要在 REMOTE_ADDRESS 上启动一个 Nacos 的服务
    ReadableDataSource<String, List<FlowRule>> ds = new NacosDataSource<>(REMOTE_ADDRESS, GROUP_ID, APP_NAME+FLOW_POSTFIX,
            source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {}));
    // 为集群客户端注册动态规则源
    FlowRuleManager.register2Property(ds.getProperty());
}
 
Example #11
Source File: DemoClusterInitFunc.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
private void initDynamicRuleProperty() {
    ReadableDataSource<String, List<FlowRule>> ruleSource = new NacosDataSource<>(remoteAddress, groupId,
        flowDataId, source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {}));
    FlowRuleManager.register2Property(ruleSource.getProperty());

    ReadableDataSource<String, List<ParamFlowRule>> paramRuleSource = new NacosDataSource<>(remoteAddress, groupId,
        paramDataId, source -> JSON.parseObject(source, new TypeReference<List<ParamFlowRule>>() {}));
    ParamFlowRuleManager.register2Property(paramRuleSource.getProperty());
}
 
Example #12
Source File: ClusterFlowClientController.java    From sentinel-tutorial with Apache License 2.0 5 votes vote down vote up
/**
 * 注册动态规则Property
 * 当client与Server连接中断,退化为本地限流时需要用到的该规则
 */
private void registerClusterFlowRuleProperty(){
    // 使用 Nacos 数据源作为配置中心,需要在 REMOTE_ADDRESS 上启动一个 Nacos 的服务
    ReadableDataSource<String, List<FlowRule>> ds = new NacosDataSource<>(REMOTE_ADDRESS, GROUP_ID, APP_NAME+FLOW_POSTFIX,
            source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {}));
    // 为集群客户端注册动态规则源
    FlowRuleManager.register2Property(ds.getProperty());
}
 
Example #13
Source File: ClusterFlowClientController.java    From sentinel-tutorial with Apache License 2.0 5 votes vote down vote up
/**
 * 为ClusterClientConfig注册一个SentinelProperty
 * 这样的话可以动态的更改这些配置
 */
private void registerClusterClientProperty() {
    String clientConfigDataId = "cluster-client-config";
    // 初始化一个配置ClusterClientConfig的 Nacos 数据源
    ReadableDataSource<String, ClusterClientConfig> clientConfigDS = new NacosDataSource<>(REMOTE_ADDRESS, GROUP_ID, clientConfigDataId,
            source -> JSON.parseObject(source, new TypeReference<ClusterClientConfig>() {}));
    ClusterClientConfigManager.registerClientConfigProperty(clientConfigDS.getProperty());

    String clientAssignConfigDataId = "cluster-client-assign-config";
    // 初始化一个配置ClusterClientAssignConfig的 Nacos 数据源
    ReadableDataSource<String, ClusterClientAssignConfig> clientAssignConfigDS = new NacosDataSource<>(REMOTE_ADDRESS, GROUP_ID, clientAssignConfigDataId,
            source -> JSON.parseObject(source, new TypeReference<ClusterClientAssignConfig>() {}));
    ClusterClientConfigManager.registerServerAssignProperty(clientAssignConfigDS.getProperty());
}
 
Example #14
Source File: ClusterServer.java    From sentinel-tutorial with Apache License 2.0 5 votes vote down vote up
/**
 * 为ServerTransportConfig注册一个SentinelProperty
 * 这样的话可以动态的更改这些配置
 */
private void registerServerTransportProperty() {
    String serverTransportDataId = "cluster-server-transport-config";
    // 初始化一个配置服务端通道配置的 Nacos 数据源
    ReadableDataSource<String, ServerTransportConfig> transportConfigDs = new NacosDataSource<>(REMOTE_ADDRESS,
            GROUP_ID, serverTransportDataId,
            source -> JSON.parseObject(source, new TypeReference<ServerTransportConfig>() {}));
    ClusterServerConfigManager.registerServerTransportProperty(transportConfigDs.getProperty());
}
 
Example #15
Source File: ClusterServer.java    From sentinel-tutorial with Apache License 2.0 5 votes vote down vote up
/**
 * 为Namespace的集合注册一个SentinelProperty
 * 这样如果后期namespace集合发生变更的话,系统可以自动感知到
 */
private void registerNamespaceProperty() {
    String namespaceSetDataId = "cluster-server-namespace-set";
    // 初始化一个配置 namespace 的 Nacos 数据源
    ReadableDataSource<String, Set<String>> namespaceDs = new NacosDataSource<>(REMOTE_ADDRESS, GROUP_ID,
            namespaceSetDataId, source -> JSON.parseObject(source, new TypeReference<Set<String>>() {}));
    ClusterServerConfigManager.registerNamespaceSetProperty(namespaceDs.getProperty());
}
 
Example #16
Source File: ClusterServer.java    From sentinel-tutorial with Apache License 2.0 5 votes vote down vote up
/**
 * 初始化集群热点参数限流的Supplier
 * 这样如果后期集群热点参数限流的规则发生变更的话,系统可以自动感知到
 */
public void initClusterParamFlowSupplier() {
    // 为集群热点参数流控注册一个Supplier,该Supplier会根据namespace动态创建数据源
    ClusterParamFlowRuleManager.setPropertySupplier(namespace -> {
        // 使用 Nacos 数据源作为配置中心,需要在 REMOTE_ADDRESS 上启动一个 Nacos 的服务
        ReadableDataSource<String, List<ParamFlowRule>> ds = new NacosDataSource<>(REMOTE_ADDRESS, GROUP_ID,
                namespace + PARAM_FLOW_POSTFIX,
                source -> JSON.parseObject(source, new TypeReference<List<ParamFlowRule>>() {}));
        return ds.getProperty();
    });
}
 
Example #17
Source File: ClusterServer.java    From sentinel-tutorial with Apache License 2.0 5 votes vote down vote up
/**
 * 初始化集群限流的Supplier
 * 这样如果后期集群限流的规则发生变更的话,系统可以自动感知到
 */
private void initClusterFlowSupplier() {
    // 为集群流控注册一个Supplier,该Supplier会根据namespace动态创建数据源
    ClusterFlowRuleManager.setPropertySupplier(namespace -> {
        // 使用 Nacos 数据源作为配置中心,需要在 REMOTE_ADDRESS 上启动一个 Nacos 的服务
        ReadableDataSource<String, List<FlowRule>> ds = new NacosDataSource<>(REMOTE_ADDRESS, GROUP_ID,
                namespace + FLOW_POSTFIX,
                source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {}));
        return ds.getProperty();
    });
}
 
Example #18
Source File: NacosDataSourceDemo.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
private static void loadMyNamespaceRules() {
    Properties properties = new Properties();
    properties.put(PropertyKeyConst.SERVER_ADDR, remoteAddress);
    properties.put(PropertyKeyConst.NAMESPACE, NACOS_NAMESPACE_ID);

    ReadableDataSource<String, List<FlowRule>> flowRuleDataSource = new NacosDataSource<>(properties, groupId, dataId,
            source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {
            }));
    FlowRuleManager.register2Property(flowRuleDataSource.getProperty());
}
 
Example #19
Source File: DemoClusterInitFunc.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
private void initStateProperty() {
    // Cluster map format:
    // [{"clientSet":["112.12.88.66@8729","112.12.88.67@8727"],"ip":"112.12.88.68","machineId":"112.12.88.68@8728","port":11111}]
    // machineId: <ip@commandPort>, commandPort for port exposed to Sentinel dashboard (transport module)
    ReadableDataSource<String, Integer> clusterModeDs = new NacosDataSource<>(remoteAddress, groupId,
        clusterMapDataId, source -> {
        List<ClusterGroupEntity> groupList = JSON.parseObject(source, new TypeReference<List<ClusterGroupEntity>>() {});
        return Optional.ofNullable(groupList)
            .map(this::extractMode)
            .orElse(ClusterStateManager.CLUSTER_NOT_STARTED);
    });
    ClusterStateManager.registerProperty(clusterModeDs.getProperty());
}
 
Example #20
Source File: DemoClusterInitFunc.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
private void initClientServerAssignProperty() {
    // Cluster map format:
    // [{"clientSet":["112.12.88.66@8729","112.12.88.67@8727"],"ip":"112.12.88.68","machineId":"112.12.88.68@8728","port":11111}]
    // machineId: <ip@commandPort>, commandPort for port exposed to Sentinel dashboard (transport module)
    ReadableDataSource<String, ClusterClientAssignConfig> clientAssignDs = new NacosDataSource<>(remoteAddress, groupId,
        clusterMapDataId, source -> {
        List<ClusterGroupEntity> groupList = JSON.parseObject(source, new TypeReference<List<ClusterGroupEntity>>() {});
        return Optional.ofNullable(groupList)
            .flatMap(this::extractClientAssignment)
            .orElse(null);
    });
    ClusterClientConfigManager.registerServerAssignProperty(clientAssignDs.getProperty());
}
 
Example #21
Source File: DemoClusterInitFunc.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
private void initServerTransportConfigProperty() {
    ReadableDataSource<String, ServerTransportConfig> serverTransportDs = new NacosDataSource<>(remoteAddress, groupId,
        clusterMapDataId, source -> {
        List<ClusterGroupEntity> groupList = JSON.parseObject(source, new TypeReference<List<ClusterGroupEntity>>() {});
        return Optional.ofNullable(groupList)
            .flatMap(this::extractServerTransportConfig)
            .orElse(null);
    });
    ClusterServerConfigManager.registerServerTransportProperty(serverTransportDs.getProperty());
}
 
Example #22
Source File: DemoClusterInitFunc.java    From Sentinel with Apache License 2.0 4 votes vote down vote up
private void initClientConfigProperty() {
    ReadableDataSource<String, ClusterClientConfig> clientConfigDs = new NacosDataSource<>(remoteAddress, groupId,
        configDataId, source -> JSON.parseObject(source, new TypeReference<ClusterClientConfig>() {}));
    ClusterClientConfigManager.registerClientConfigProperty(clientConfigDs.getProperty());
}
 
Example #23
Source File: NacosDataSourceDemo.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 4 votes vote down vote up
private static void loadRules() {
    ReadableDataSource<String, List<FlowRule>> flowRuleDataSource = new NacosDataSource<>(remoteAddress, groupId, dataId,
            source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {
            }));
    FlowRuleManager.register2Property(flowRuleDataSource.getProperty());
}
 
Example #24
Source File: NacosDataSourceDemo.java    From Sentinel with Apache License 2.0 4 votes vote down vote up
private static void loadRules() {
    ReadableDataSource<String, List<FlowRule>> flowRuleDataSource = new NacosDataSource<>(remoteAddress, groupId, dataId,
            source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {
            }));
    FlowRuleManager.register2Property(flowRuleDataSource.getProperty());
}
 
Example #25
Source File: NacosDataSourceFactoryBean.java    From spring-cloud-alibaba with Apache License 2.0 4 votes vote down vote up
@Override
public Class<?> getObjectType() {
	return NacosDataSource.class;
}
 
Example #26
Source File: DemoClusterInitFunc.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 4 votes vote down vote up
private void initClientConfigProperty() {
    ReadableDataSource<String, ClusterClientConfig> clientConfigDs = new NacosDataSource<>(remoteAddress, groupId,
        configDataId, source -> JSON.parseObject(source, new TypeReference<ClusterClientConfig>() {}));
    ClusterClientConfigManager.registerClientConfigProperty(clientConfigDs.getProperty());
}