init
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>sentinel-demo-cluster</artifactId>
|
||||
<groupId>com.alibaba.csp</groupId>
|
||||
<version>1.8.3</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>sentinel-demo-cluster-server-alone</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.csp</groupId>
|
||||
<artifactId>sentinel-core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.csp</groupId>
|
||||
<artifactId>sentinel-transport-simple-http</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.csp</groupId>
|
||||
<artifactId>sentinel-parameter-flow-control</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.alibaba.csp</groupId>
|
||||
<artifactId>sentinel-cluster-server-default</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.alibaba.csp</groupId>
|
||||
<artifactId>sentinel-datasource-nacos</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-classic</artifactId>
|
||||
<version>1.2.3</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright 1999-2018 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.csp.sentinel.demo.cluster;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import com.alibaba.csp.sentinel.cluster.server.ClusterTokenServer;
|
||||
import com.alibaba.csp.sentinel.cluster.server.SentinelDefaultTokenServer;
|
||||
import com.alibaba.csp.sentinel.cluster.server.config.ClusterServerConfigManager;
|
||||
import com.alibaba.csp.sentinel.cluster.server.config.ServerTransportConfig;
|
||||
|
||||
/**
|
||||
* <p>Cluster server demo (alone mode).</p>
|
||||
* <p>Here we init the cluster server dynamic data sources in
|
||||
* {@link com.alibaba.csp.sentinel.demo.cluster.init.DemoClusterServerInitFunc}.</p>
|
||||
*
|
||||
* @author Eric Zhao
|
||||
* @since 1.4.0
|
||||
*/
|
||||
public class ClusterServerDemo {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
// Not embedded mode by default (alone mode).
|
||||
ClusterTokenServer tokenServer = new SentinelDefaultTokenServer();
|
||||
|
||||
// A sample for manually load config for cluster server.
|
||||
// It's recommended to use dynamic data source to cluster manage config and rules.
|
||||
// See the sample in DemoClusterServerInitFunc for detail.
|
||||
ClusterServerConfigManager.loadGlobalTransportConfig(new ServerTransportConfig()
|
||||
.setIdleSeconds(600)
|
||||
.setPort(11111));
|
||||
ClusterServerConfigManager.loadServerNamespaceSet(Collections.singleton(DemoConstants.APP_NAME));
|
||||
|
||||
// Start the server.
|
||||
tokenServer.start();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* Copyright 1999-2018 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.csp.sentinel.demo.cluster;
|
||||
|
||||
/**
|
||||
* @author Eric Zhao
|
||||
*/
|
||||
public final class DemoConstants {
|
||||
|
||||
public static final String APP_NAME = "appA";
|
||||
|
||||
public static final String FLOW_POSTFIX = "-flow-rules";
|
||||
public static final String PARAM_FLOW_POSTFIX = "-param-rules";
|
||||
|
||||
private DemoConstants() {}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
* Copyright 1999-2018 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.csp.sentinel.demo.cluster.init;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import com.alibaba.csp.sentinel.cluster.flow.rule.ClusterFlowRuleManager;
|
||||
import com.alibaba.csp.sentinel.cluster.flow.rule.ClusterParamFlowRuleManager;
|
||||
import com.alibaba.csp.sentinel.cluster.server.config.ClusterServerConfigManager;
|
||||
import com.alibaba.csp.sentinel.cluster.server.config.ServerTransportConfig;
|
||||
import com.alibaba.csp.sentinel.datasource.ReadableDataSource;
|
||||
import com.alibaba.csp.sentinel.datasource.nacos.NacosDataSource;
|
||||
import com.alibaba.csp.sentinel.demo.cluster.DemoConstants;
|
||||
import com.alibaba.csp.sentinel.init.InitFunc;
|
||||
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
|
||||
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.TypeReference;
|
||||
|
||||
/**
|
||||
* @author Eric Zhao
|
||||
*/
|
||||
public class DemoClusterServerInitFunc implements InitFunc {
|
||||
|
||||
private final String remoteAddress = "localhost:8848";
|
||||
private final String groupId = "SENTINEL_GROUP";
|
||||
private final String namespaceSetDataId = "cluster-server-namespace-set";
|
||||
private final String serverTransportDataId = "cluster-server-transport-config";
|
||||
|
||||
@Override
|
||||
public void init() throws Exception {
|
||||
// Register cluster flow rule property supplier which creates data source by namespace.
|
||||
ClusterFlowRuleManager.setPropertySupplier(namespace -> {
|
||||
ReadableDataSource<String, List<FlowRule>> ds = new NacosDataSource<>(remoteAddress, groupId,
|
||||
namespace + DemoConstants.FLOW_POSTFIX,
|
||||
source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {}));
|
||||
return ds.getProperty();
|
||||
});
|
||||
// Register cluster parameter flow rule property supplier.
|
||||
ClusterParamFlowRuleManager.setPropertySupplier(namespace -> {
|
||||
ReadableDataSource<String, List<ParamFlowRule>> ds = new NacosDataSource<>(remoteAddress, groupId,
|
||||
namespace + DemoConstants.PARAM_FLOW_POSTFIX,
|
||||
source -> JSON.parseObject(source, new TypeReference<List<ParamFlowRule>>() {}));
|
||||
return ds.getProperty();
|
||||
});
|
||||
|
||||
// Server namespace set (scope) data source.
|
||||
ReadableDataSource<String, Set<String>> namespaceDs = new NacosDataSource<>(remoteAddress, groupId,
|
||||
namespaceSetDataId, source -> JSON.parseObject(source, new TypeReference<Set<String>>() {}));
|
||||
ClusterServerConfigManager.registerNamespaceSetProperty(namespaceDs.getProperty());
|
||||
// Server transport configuration data source.
|
||||
ReadableDataSource<String, ServerTransportConfig> transportConfigDs = new NacosDataSource<>(remoteAddress,
|
||||
groupId, serverTransportDataId,
|
||||
source -> JSON.parseObject(source, new TypeReference<ServerTransportConfig>() {}));
|
||||
ClusterServerConfigManager.registerServerTransportProperty(transportConfigDs.getProperty());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
com.alibaba.csp.sentinel.demo.cluster.init.DemoClusterServerInitFunc
|
||||
Reference in New Issue
Block a user