diff --git a/sentinel-demo/pom.xml b/sentinel-demo/pom.xml index a4a0457e..15a65280 100755 --- a/sentinel-demo/pom.xml +++ b/sentinel-demo/pom.xml @@ -31,6 +31,7 @@ sentinel-demo-cluster sentinel-demo-command-handler sentinel-demo-spring-webflux + sentinel-demo-apache-dubbo diff --git a/sentinel-demo/sentinel-demo-apache-dubbo/README.md b/sentinel-demo/sentinel-demo-apache-dubbo/README.md new file mode 100644 index 00000000..bf18d607 --- /dev/null +++ b/sentinel-demo/sentinel-demo-apache-dubbo/README.md @@ -0,0 +1,18 @@ +# Sentinel Apache Dubbo Demo + +This demo shows how to integrate Apache Dubbo **2.7.x or above version** with Sentinel +using `sentinel-apache-dubbo-adapter` module. + +## Run the demo + +For the provider demo `FooProviderBootstrap`, you need to add the following parameters when startup: + +```shell +-Djava.net.preferIPv4Stack=true -Dproject.name=dubbo-provider-demo +``` + +For the consumer demo `FooConsumerBootstrap`, you need to add the following parameters when startup: + +```shell +-Djava.net.preferIPv4Stack=true -Dproject.name=dubbo-consumer-demo +``` diff --git a/sentinel-demo/sentinel-demo-apache-dubbo/pom.xml b/sentinel-demo/sentinel-demo-apache-dubbo/pom.xml new file mode 100644 index 00000000..889a7cd8 --- /dev/null +++ b/sentinel-demo/sentinel-demo-apache-dubbo/pom.xml @@ -0,0 +1,49 @@ + + + + sentinel-demo + com.alibaba.csp + 1.5.1-SNAPSHOT + + 4.0.0 + + sentinel-demo-apache-dubbo + + + + org.apache.dubbo + dubbo + 2.7.1 + + + + + + + io.netty + netty-all + 4.1.31.Final + + + + + org.springframework + spring-context-support + 5.1.5.RELEASE + + + + + com.alibaba.csp + sentinel-apache-dubbo-adapter + ${project.version} + + + com.alibaba.csp + sentinel-transport-simple-http + ${project.version} + + + \ No newline at end of file diff --git a/sentinel-demo/sentinel-demo-apache-dubbo/src/main/java/com/alibaba/csp/sentinel/demo/apache/dubbo/FooConsumerBootstrap.java b/sentinel-demo/sentinel-demo-apache-dubbo/src/main/java/com/alibaba/csp/sentinel/demo/apache/dubbo/FooConsumerBootstrap.java new file mode 100644 index 00000000..dfe1dbcb --- /dev/null +++ b/sentinel-demo/sentinel-demo-apache-dubbo/src/main/java/com/alibaba/csp/sentinel/demo/apache/dubbo/FooConsumerBootstrap.java @@ -0,0 +1,54 @@ +/* + * 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.apache.dubbo; + +import com.alibaba.csp.sentinel.demo.apache.dubbo.consumer.ConsumerConfiguration; +import com.alibaba.csp.sentinel.demo.apache.dubbo.consumer.FooServiceConsumer; +import com.alibaba.csp.sentinel.slots.block.SentinelRpcException; + +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +/** + * Please add the following VM arguments: + *
+ * -Djava.net.preferIPv4Stack=true
+ * -Dcsp.sentinel.api.port=8721
+ * -Dproject.name=dubbo-consumer-demo
+ * 
+ * + * @author Eric Zhao + */ +public class FooConsumerBootstrap { + + public static void main(String[] args) { + AnnotationConfigApplicationContext consumerContext = new AnnotationConfigApplicationContext(); + consumerContext.register(ConsumerConfiguration.class); + consumerContext.refresh(); + + FooServiceConsumer service = consumerContext.getBean(FooServiceConsumer.class); + + for (int i = 0; i < 15; i++) { + try { + String message = service.sayHello("Eric"); + System.out.println("Success: " + message); + } catch (SentinelRpcException ex) { + System.out.println("Blocked"); + } catch (Exception ex) { + ex.printStackTrace(); + } + } + } +} diff --git a/sentinel-demo/sentinel-demo-apache-dubbo/src/main/java/com/alibaba/csp/sentinel/demo/apache/dubbo/FooProviderBootstrap.java b/sentinel-demo/sentinel-demo-apache-dubbo/src/main/java/com/alibaba/csp/sentinel/demo/apache/dubbo/FooProviderBootstrap.java new file mode 100644 index 00000000..a23c0ed0 --- /dev/null +++ b/sentinel-demo/sentinel-demo-apache-dubbo/src/main/java/com/alibaba/csp/sentinel/demo/apache/dubbo/FooProviderBootstrap.java @@ -0,0 +1,63 @@ +/* + * 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.apache.dubbo; + +import java.util.Collections; + +import com.alibaba.csp.sentinel.demo.apache.dubbo.provider.ProviderConfiguration; +import com.alibaba.csp.sentinel.init.InitExecutor; +import com.alibaba.csp.sentinel.slots.block.RuleConstant; +import com.alibaba.csp.sentinel.slots.block.flow.FlowRule; +import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager; + +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +/** + * Provider demo for Apache Dubbo 2.7.x or above. Please add the following VM arguments: + *
+ * -Djava.net.preferIPv4Stack=true
+ * -Dcsp.sentinel.api.port=8720
+ * -Dproject.name=dubbo-provider-demo
+ * 
+ * + * @author Eric Zhao + */ +public class FooProviderBootstrap { + + private static final String INTERFACE_RES_KEY = FooService.class.getName(); + private static final String RES_KEY = INTERFACE_RES_KEY + ":sayHello(java.lang.String)"; + + public static void main(String[] args) { + // Users don't need to manually call this method. + // Only for eager initialization. + InitExecutor.doInit(); + + initFlowRule(); + + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); + context.register(ProviderConfiguration.class); + context.refresh(); + + System.out.println("Service provider is ready"); + } + + private static void initFlowRule() { + FlowRule flowRule = new FlowRule(INTERFACE_RES_KEY) + .setCount(10) + .setGrade(RuleConstant.FLOW_GRADE_QPS); + FlowRuleManager.loadRules(Collections.singletonList(flowRule)); + } +} diff --git a/sentinel-demo/sentinel-demo-apache-dubbo/src/main/java/com/alibaba/csp/sentinel/demo/apache/dubbo/FooService.java b/sentinel-demo/sentinel-demo-apache-dubbo/src/main/java/com/alibaba/csp/sentinel/demo/apache/dubbo/FooService.java new file mode 100644 index 00000000..dcdabc8c --- /dev/null +++ b/sentinel-demo/sentinel-demo-apache-dubbo/src/main/java/com/alibaba/csp/sentinel/demo/apache/dubbo/FooService.java @@ -0,0 +1,26 @@ +/* + * 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.apache.dubbo; + +/** + * @author Eric Zhao + */ +public interface FooService { + + String sayHello(String name); + + String doAnother(); +} diff --git a/sentinel-demo/sentinel-demo-apache-dubbo/src/main/java/com/alibaba/csp/sentinel/demo/apache/dubbo/consumer/ConsumerConfiguration.java b/sentinel-demo/sentinel-demo-apache-dubbo/src/main/java/com/alibaba/csp/sentinel/demo/apache/dubbo/consumer/ConsumerConfiguration.java new file mode 100644 index 00000000..6e356dfc --- /dev/null +++ b/sentinel-demo/sentinel-demo-apache-dubbo/src/main/java/com/alibaba/csp/sentinel/demo/apache/dubbo/consumer/ConsumerConfiguration.java @@ -0,0 +1,58 @@ +/* + * 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.apache.dubbo.consumer; + + +import org.apache.dubbo.config.ApplicationConfig; +import org.apache.dubbo.config.ConsumerConfig; +import org.apache.dubbo.config.RegistryConfig; +import org.apache.dubbo.config.spring.context.annotation.DubboComponentScan; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * @author Eric Zhao + */ +@Configuration +@DubboComponentScan +public class ConsumerConfiguration { + @Bean + public ApplicationConfig applicationConfig() { + ApplicationConfig applicationConfig = new ApplicationConfig(); + applicationConfig.setName("demo-consumer"); + return applicationConfig; + } + + @Bean + public RegistryConfig registryConfig() { + RegistryConfig registryConfig = new RegistryConfig(); + registryConfig.setAddress("multicast://224.5.6.7:1234"); + return registryConfig; + } + + @Bean + public ConsumerConfig consumerConfig() { + ConsumerConfig consumerConfig = new ConsumerConfig(); + // Uncomment below line if you don't want to enable Sentinel for Dubbo service consumers. + // consumerConfig.setFilter("-sentinel.dubbo.consumer.filter"); + return consumerConfig; + } + + @Bean + public FooServiceConsumer annotationDemoServiceConsumer() { + return new FooServiceConsumer(); + } +} diff --git a/sentinel-demo/sentinel-demo-apache-dubbo/src/main/java/com/alibaba/csp/sentinel/demo/apache/dubbo/consumer/FooServiceConsumer.java b/sentinel-demo/sentinel-demo-apache-dubbo/src/main/java/com/alibaba/csp/sentinel/demo/apache/dubbo/consumer/FooServiceConsumer.java new file mode 100644 index 00000000..f83d98a0 --- /dev/null +++ b/sentinel-demo/sentinel-demo-apache-dubbo/src/main/java/com/alibaba/csp/sentinel/demo/apache/dubbo/consumer/FooServiceConsumer.java @@ -0,0 +1,37 @@ +/* + * 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.apache.dubbo.consumer; + +import com.alibaba.csp.sentinel.demo.apache.dubbo.FooService; + +import org.apache.dubbo.config.annotation.Reference; + +/** + * @author Eric Zhao + */ +public class FooServiceConsumer { + + @Reference(url = "dubbo://127.0.0.1:25758", timeout = 3000) + private FooService fooService; + + public String sayHello(String name) { + return fooService.sayHello(name); + } + + public String doAnother() { + return fooService.doAnother(); + } +} diff --git a/sentinel-demo/sentinel-demo-apache-dubbo/src/main/java/com/alibaba/csp/sentinel/demo/apache/dubbo/provider/FooServiceImpl.java b/sentinel-demo/sentinel-demo-apache-dubbo/src/main/java/com/alibaba/csp/sentinel/demo/apache/dubbo/provider/FooServiceImpl.java new file mode 100644 index 00000000..bd92567a --- /dev/null +++ b/sentinel-demo/sentinel-demo-apache-dubbo/src/main/java/com/alibaba/csp/sentinel/demo/apache/dubbo/provider/FooServiceImpl.java @@ -0,0 +1,39 @@ +/* + * 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.apache.dubbo.provider; + +import java.time.LocalDateTime; + +import com.alibaba.csp.sentinel.demo.apache.dubbo.FooService; + +import org.apache.dubbo.config.annotation.Service; + +/** + * @author Eric Zhao + */ +@Service +public class FooServiceImpl implements FooService { + + @Override + public String sayHello(String name) { + return String.format("Hello, %s at %s", name, LocalDateTime.now()); + } + + @Override + public String doAnother() { + return LocalDateTime.now().toString(); + } +} diff --git a/sentinel-demo/sentinel-demo-apache-dubbo/src/main/java/com/alibaba/csp/sentinel/demo/apache/dubbo/provider/ProviderConfiguration.java b/sentinel-demo/sentinel-demo-apache-dubbo/src/main/java/com/alibaba/csp/sentinel/demo/apache/dubbo/provider/ProviderConfiguration.java new file mode 100644 index 00000000..ecb4539d --- /dev/null +++ b/sentinel-demo/sentinel-demo-apache-dubbo/src/main/java/com/alibaba/csp/sentinel/demo/apache/dubbo/provider/ProviderConfiguration.java @@ -0,0 +1,53 @@ +/* + * 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.apache.dubbo.provider; + +import org.apache.dubbo.config.ApplicationConfig; +import org.apache.dubbo.config.ProtocolConfig; +import org.apache.dubbo.config.RegistryConfig; +import org.apache.dubbo.config.spring.context.annotation.DubboComponentScan; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * @author Eric Zhao + */ +@Configuration +@DubboComponentScan +public class ProviderConfiguration { + + @Bean + public ApplicationConfig applicationConfig() { + ApplicationConfig applicationConfig = new ApplicationConfig(); + applicationConfig.setName("demo-provider"); + return applicationConfig; + } + + @Bean + public RegistryConfig registryConfig() { + RegistryConfig registryConfig = new RegistryConfig(); + registryConfig.setAddress("multicast://224.5.6.7:1234"); + return registryConfig; + } + + @Bean + public ProtocolConfig protocolConfig() { + ProtocolConfig protocolConfig = new ProtocolConfig(); + protocolConfig.setName("dubbo"); + protocolConfig.setPort(25758); + return protocolConfig; + } +}