-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Add `close` method in WritableDataSource (to extend AutoCloseable in JDK 1.7 later) - Separate the writable file data source from original class - Add a sample to show how to register data sources via Sentinel init mechanism - Separate a writable data source registry from original handler to make it clear Signed-off-by: Eric Zhao <[email protected]>
- Loading branch information
Showing
8 changed files
with
210 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
...c-file-rule/src/main/java/com/alibaba/csp/sentinel/demo/file/rule/FileDataSourceInit.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* 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.file.rule; | ||
|
||
import java.util.List; | ||
|
||
import com.alibaba.csp.sentinel.datasource.FileRefreshableDataSource; | ||
import com.alibaba.csp.sentinel.datasource.FileWritableDataSource; | ||
import com.alibaba.csp.sentinel.datasource.ReadableDataSource; | ||
import com.alibaba.csp.sentinel.datasource.WritableDataSource; | ||
import com.alibaba.csp.sentinel.init.InitFunc; | ||
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule; | ||
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager; | ||
import com.alibaba.csp.sentinel.transport.util.WritableDataSourceRegistry; | ||
import com.alibaba.fastjson.JSON; | ||
import com.alibaba.fastjson.TypeReference; | ||
|
||
/** | ||
* <p> | ||
* A sample showing how to register readable and writable data source via Sentinel init SPI mechanism. | ||
* </p> | ||
* <p> | ||
* To activate this, you can add the class name to `com.alibaba.csp.sentinel.init.InitFunc` file | ||
* in `META-INF/services/` directory of the resource directory. Then the data source will be automatically | ||
* registered during the initialization of Sentinel. | ||
* </p> | ||
* | ||
* @author Eric Zhao | ||
*/ | ||
public class FileDataSourceInit implements InitFunc { | ||
|
||
@Override | ||
public void init() throws Exception { | ||
// A fake path. | ||
String flowRuleDir = System.getProperty("user.home") + "/sentinel/rules"; | ||
String flowRuleFile = "flowRule.json"; | ||
String flowRulePath = flowRuleDir + "/" + flowRuleFile; | ||
|
||
ReadableDataSource<String, List<FlowRule>> ds = new FileRefreshableDataSource<>( | ||
flowRulePath, source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {}) | ||
); | ||
// Register to flow rule manager. | ||
FlowRuleManager.register2Property(ds.getProperty()); | ||
|
||
WritableDataSource<List<FlowRule>> wds = new FileWritableDataSource<>(flowRulePath, this::encodeJson); | ||
// Register to writable data source registry so that rules can be updated to file | ||
// when there are rules pushed from the Sentinel Dashboard. | ||
WritableDataSourceRegistry.registerFlowDataSource(wds); | ||
} | ||
|
||
private <T> String encodeJson(T t) { | ||
return JSON.toJSONString(t); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
...e-extension/src/main/java/com/alibaba/csp/sentinel/datasource/FileWritableDataSource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* 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.datasource; | ||
|
||
import java.io.File; | ||
|
||
/** | ||
* A {@link WritableDataSource} based on file. | ||
* | ||
* @param <T> data type | ||
* @author Eric Zhao | ||
* @since 0.2.0 | ||
*/ | ||
public class FileWritableDataSource<T> implements WritableDataSource<T> { | ||
|
||
private final Converter<T, String> configEncoder; | ||
private File file; | ||
|
||
public FileWritableDataSource(String filePath, Converter<T, String> configEncoder) { | ||
this(new File(filePath), configEncoder); | ||
} | ||
|
||
public FileWritableDataSource(File file, Converter<T, String> configEncoder) { | ||
if (file == null || file.isDirectory()) { | ||
throw new IllegalArgumentException("Bad file"); | ||
} | ||
if (configEncoder == null) { | ||
throw new IllegalArgumentException("Config encoder cannot be null"); | ||
} | ||
this.configEncoder = configEncoder; | ||
this.file = file; | ||
} | ||
|
||
@Override | ||
public void write(T value) throws Exception { | ||
throw new UnsupportedOperationException("Not implemented"); | ||
} | ||
|
||
@Override | ||
public void close() throws Exception { | ||
// Nothing | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
...mon/src/main/java/com/alibaba/csp/sentinel/transport/util/WritableDataSourceRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.alibaba.csp.sentinel.transport.util; | ||
|
||
import java.util.List; | ||
|
||
import com.alibaba.csp.sentinel.datasource.WritableDataSource; | ||
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule; | ||
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule; | ||
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule; | ||
import com.alibaba.csp.sentinel.slots.system.SystemRule; | ||
|
||
/** | ||
* Writable data source registry for modifying rules via HTTP API. | ||
* | ||
* @author Eric Zhao | ||
*/ | ||
public final class WritableDataSourceRegistry { | ||
|
||
private static WritableDataSource<List<FlowRule>> flowDataSource = null; | ||
private static WritableDataSource<List<AuthorityRule>> authorityDataSource = null; | ||
private static WritableDataSource<List<DegradeRule>> degradeDataSource = null; | ||
private static WritableDataSource<List<SystemRule>> systemSource = null; | ||
|
||
public static synchronized void registerFlowDataSource(WritableDataSource<List<FlowRule>> datasource) { | ||
flowDataSource = datasource; | ||
} | ||
|
||
public static synchronized void registerAuthorityDataSource(WritableDataSource<List<AuthorityRule>> dataSource) { | ||
authorityDataSource = dataSource; | ||
} | ||
|
||
public static synchronized void registerDegradeDataSource(WritableDataSource<List<DegradeRule>> dataSource) { | ||
degradeDataSource = dataSource; | ||
} | ||
|
||
public static synchronized void registerSystemDataSource(WritableDataSource<List<SystemRule>> dataSource) { | ||
systemSource = dataSource; | ||
} | ||
|
||
public static WritableDataSource<List<FlowRule>> getFlowDataSource() { | ||
return flowDataSource; | ||
} | ||
|
||
public static WritableDataSource<List<AuthorityRule>> getAuthorityDataSource() { | ||
return authorityDataSource; | ||
} | ||
|
||
public static WritableDataSource<List<DegradeRule>> getDegradeDataSource() { | ||
return degradeDataSource; | ||
} | ||
|
||
public static WritableDataSource<List<SystemRule>> getSystemSource() { | ||
return systemSource; | ||
} | ||
|
||
private WritableDataSourceRegistry() {} | ||
} |