From a25f25bea6b4c77fc3469b368eee222521f5de52 Mon Sep 17 00:00:00 2001 From: Eric Zhao Date: Tue, 9 Oct 2018 17:33:37 +0800 Subject: [PATCH] Add demo for SlotChainBuilder SPI Signed-off-by: Eric Zhao --- sentinel-demo/pom.xml | 1 + .../sentinel-demo-slot-chain-spi/pom.xml | 14 ++++++ .../csp/sentinel/demo/slot/DemoSlot.java | 45 +++++++++++++++++++ .../demo/slot/DemoSlotChainBuilder.java | 36 +++++++++++++++ .../demo/slot/SlotChainBuilderSpiDemo.java | 41 +++++++++++++++++ ...ba.csp.sentinel.slotchain.SlotChainBuilder | 2 + 6 files changed, 139 insertions(+) create mode 100644 sentinel-demo/sentinel-demo-slot-chain-spi/pom.xml create mode 100644 sentinel-demo/sentinel-demo-slot-chain-spi/src/main/java/com/alibaba/csp/sentinel/demo/slot/DemoSlot.java create mode 100644 sentinel-demo/sentinel-demo-slot-chain-spi/src/main/java/com/alibaba/csp/sentinel/demo/slot/DemoSlotChainBuilder.java create mode 100644 sentinel-demo/sentinel-demo-slot-chain-spi/src/main/java/com/alibaba/csp/sentinel/demo/slot/SlotChainBuilderSpiDemo.java create mode 100644 sentinel-demo/sentinel-demo-slot-chain-spi/src/main/resources/META-INF/services/com.alibaba.csp.sentinel.slotchain.SlotChainBuilder diff --git a/sentinel-demo/pom.xml b/sentinel-demo/pom.xml index ef8fdd0a..de26351e 100755 --- a/sentinel-demo/pom.xml +++ b/sentinel-demo/pom.xml @@ -27,6 +27,7 @@ sentinel-demo-apollo-datasource sentinel-demo-annotation-spring-aop sentinel-demo-parameter-flow-control + sentinel-demo-slot-chain-spi diff --git a/sentinel-demo/sentinel-demo-slot-chain-spi/pom.xml b/sentinel-demo/sentinel-demo-slot-chain-spi/pom.xml new file mode 100644 index 00000000..fd12f52a --- /dev/null +++ b/sentinel-demo/sentinel-demo-slot-chain-spi/pom.xml @@ -0,0 +1,14 @@ + + + + sentinel-demo + com.alibaba.csp + 0.2.1-SNAPSHOT + + 4.0.0 + + sentinel-demo-slot-chain-spi + + \ No newline at end of file diff --git a/sentinel-demo/sentinel-demo-slot-chain-spi/src/main/java/com/alibaba/csp/sentinel/demo/slot/DemoSlot.java b/sentinel-demo/sentinel-demo-slot-chain-spi/src/main/java/com/alibaba/csp/sentinel/demo/slot/DemoSlot.java new file mode 100644 index 00000000..b85d1cf3 --- /dev/null +++ b/sentinel-demo/sentinel-demo-slot-chain-spi/src/main/java/com/alibaba/csp/sentinel/demo/slot/DemoSlot.java @@ -0,0 +1,45 @@ +/* + * 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.slot; + +import com.alibaba.csp.sentinel.context.Context; +import com.alibaba.csp.sentinel.node.DefaultNode; +import com.alibaba.csp.sentinel.slotchain.AbstractLinkedProcessorSlot; +import com.alibaba.csp.sentinel.slotchain.ResourceWrapper; + +/** + * An example slot that records current context and entry resource. + * + * @author Eric Zhao + */ +public class DemoSlot extends AbstractLinkedProcessorSlot { + + @Override + public void entry(Context context, ResourceWrapper resourceWrapper, DefaultNode node, int count, Object... args) + throws Throwable { + System.out.println("Current context: " + context.getName()); + System.out.println("Current entry resource: " + context.getCurEntry().getResourceWrapper().getName()); + + fireEntry(context, resourceWrapper, node, count, args); + } + + @Override + public void exit(Context context, ResourceWrapper resourceWrapper, int count, Object... args) { + System.out.println("Exiting for entry on DemoSlot: " + context.getCurEntry().getResourceWrapper().getName()); + + fireExit(context, resourceWrapper, count, args); + } +} diff --git a/sentinel-demo/sentinel-demo-slot-chain-spi/src/main/java/com/alibaba/csp/sentinel/demo/slot/DemoSlotChainBuilder.java b/sentinel-demo/sentinel-demo-slot-chain-spi/src/main/java/com/alibaba/csp/sentinel/demo/slot/DemoSlotChainBuilder.java new file mode 100644 index 00000000..c311c89d --- /dev/null +++ b/sentinel-demo/sentinel-demo-slot-chain-spi/src/main/java/com/alibaba/csp/sentinel/demo/slot/DemoSlotChainBuilder.java @@ -0,0 +1,36 @@ +/* + * 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.slot; + +import com.alibaba.csp.sentinel.slotchain.ProcessorSlotChain; +import com.alibaba.csp.sentinel.slotchain.SlotChainBuilder; +import com.alibaba.csp.sentinel.slots.DefaultSlotChainBuilder; + +/** + * An example slot chain builder. To activate this slot chain builder, + * add the class name to corresponding SPI file in `resource/META-INF/services` directory. + * + * @author Eric Zhao + */ +public class DemoSlotChainBuilder implements SlotChainBuilder { + + @Override + public ProcessorSlotChain build() { + ProcessorSlotChain chain = new DefaultSlotChainBuilder().build(); + chain.addLast(new DemoSlot()); + return chain; + } +} diff --git a/sentinel-demo/sentinel-demo-slot-chain-spi/src/main/java/com/alibaba/csp/sentinel/demo/slot/SlotChainBuilderSpiDemo.java b/sentinel-demo/sentinel-demo-slot-chain-spi/src/main/java/com/alibaba/csp/sentinel/demo/slot/SlotChainBuilderSpiDemo.java new file mode 100644 index 00000000..7eea5caf --- /dev/null +++ b/sentinel-demo/sentinel-demo-slot-chain-spi/src/main/java/com/alibaba/csp/sentinel/demo/slot/SlotChainBuilderSpiDemo.java @@ -0,0 +1,41 @@ +/* + * 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.slot; + +import com.alibaba.csp.sentinel.Entry; +import com.alibaba.csp.sentinel.SphU; +import com.alibaba.csp.sentinel.slots.block.BlockException; + +/** + * @author Eric Zhao + */ +public class SlotChainBuilderSpiDemo { + + public static void main(String[] args) { + // You will see this in record.log, indicating that the custom slot chain builder is activated: + // [SlotChainProvider] Global slot chain builder resolved: com.alibaba.csp.sentinel.demo.slot.DemoSlotChainBuilder + Entry entry = null; + try { + entry = SphU.entry("abc"); + } catch (BlockException ex) { + ex.printStackTrace(); + } finally { + if (entry != null) { + entry.exit(); + } + } + } +} diff --git a/sentinel-demo/sentinel-demo-slot-chain-spi/src/main/resources/META-INF/services/com.alibaba.csp.sentinel.slotchain.SlotChainBuilder b/sentinel-demo/sentinel-demo-slot-chain-spi/src/main/resources/META-INF/services/com.alibaba.csp.sentinel.slotchain.SlotChainBuilder new file mode 100644 index 00000000..529b016a --- /dev/null +++ b/sentinel-demo/sentinel-demo-slot-chain-spi/src/main/resources/META-INF/services/com.alibaba.csp.sentinel.slotchain.SlotChainBuilder @@ -0,0 +1,2 @@ +# Custom slot chain builder +com.alibaba.csp.sentinel.demo.slot.DemoSlotChainBuilder \ No newline at end of file