From 6598768195dfe1260dd4ef4ac15c41880062b948 Mon Sep 17 00:00:00 2001 From: fangjian0423 Date: Fri, 17 Aug 2018 11:18:20 +0800 Subject: [PATCH] refactor dubbo examples & add datasource feature in example & update docs --- spring-cloud-alibaba-examples/pom.xml | 3 + .../sentinel-dubbo-api/pom.xml | 30 ++++++ .../alibaba/cloud/examples/FooService.java | 10 ++ .../sentinel-dubbo-consumer-example/pom.xml | 54 ++++++++++ .../readme-zh.md | 99 +++++++++++++++++ .../sentinel-dubbo-consumer-example/readme.md | 97 +++++++++++++++++ .../cloud/examples}/FooServiceConsumer.java | 7 +- .../examples/SentinelDubboConsumerApp.java} | 45 ++------ .../src/main/resources/application.properties | 11 ++ .../sentinel-dubbo-provider-example/pom.xml | 54 ++++++++++ .../readme-zh.md | 101 ++++++++++++++++++ .../sentinel-dubbo-provider-example/readme.md | 97 +++++++++++++++++ .../cloud/examples/FooServiceImpl.java | 20 ++++ .../examples/SentinelDubboProviderApp.java | 19 ++++ .../src/main/resources/application.properties | 16 +++ .../sentinel-example/pom.xml | 6 -- .../sentinel-example/readme-zh.md | 89 ++++++++++----- .../sentinel-example/readme.md | 86 ++++++++++----- .../examples/JsonFlowRuleListParser.java | 18 ++++ .../cloud/examples/ServiceApplication.java | 6 ++ .../examples/dubbo/DubboProviderRunner.java | 24 ----- .../cloud/examples/dubbo/FooService.java | 10 -- .../dubbo/provider/FooServiceImpl.java | 17 --- .../dubbo/provider/ProviderApplication.java | 38 ------- .../src/main/resources/application.properties | 11 +- 25 files changed, 783 insertions(+), 185 deletions(-) create mode 100644 spring-cloud-alibaba-examples/sentinel-dubbo-api/pom.xml create mode 100644 spring-cloud-alibaba-examples/sentinel-dubbo-api/src/main/java/org/springframework/cloud/alibaba/cloud/examples/FooService.java create mode 100644 spring-cloud-alibaba-examples/sentinel-dubbo-consumer-example/pom.xml create mode 100644 spring-cloud-alibaba-examples/sentinel-dubbo-consumer-example/readme-zh.md create mode 100644 spring-cloud-alibaba-examples/sentinel-dubbo-consumer-example/readme.md rename spring-cloud-alibaba-examples/{sentinel-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/dubbo/consumer => sentinel-dubbo-consumer-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples}/FooServiceConsumer.java (52%) rename spring-cloud-alibaba-examples/{sentinel-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/dubbo/consumer/ConsumerApplication.java => sentinel-dubbo-consumer-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/SentinelDubboConsumerApp.java} (53%) create mode 100644 spring-cloud-alibaba-examples/sentinel-dubbo-consumer-example/src/main/resources/application.properties create mode 100644 spring-cloud-alibaba-examples/sentinel-dubbo-provider-example/pom.xml create mode 100644 spring-cloud-alibaba-examples/sentinel-dubbo-provider-example/readme-zh.md create mode 100644 spring-cloud-alibaba-examples/sentinel-dubbo-provider-example/readme.md create mode 100644 spring-cloud-alibaba-examples/sentinel-dubbo-provider-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/FooServiceImpl.java create mode 100644 spring-cloud-alibaba-examples/sentinel-dubbo-provider-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/SentinelDubboProviderApp.java create mode 100644 spring-cloud-alibaba-examples/sentinel-dubbo-provider-example/src/main/resources/application.properties create mode 100644 spring-cloud-alibaba-examples/sentinel-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/JsonFlowRuleListParser.java delete mode 100644 spring-cloud-alibaba-examples/sentinel-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/dubbo/DubboProviderRunner.java delete mode 100644 spring-cloud-alibaba-examples/sentinel-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/dubbo/FooService.java delete mode 100644 spring-cloud-alibaba-examples/sentinel-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/dubbo/provider/FooServiceImpl.java delete mode 100644 spring-cloud-alibaba-examples/sentinel-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/dubbo/provider/ProviderApplication.java diff --git a/spring-cloud-alibaba-examples/pom.xml b/spring-cloud-alibaba-examples/pom.xml index 1fa113cc0..d1dfdfb55 100644 --- a/spring-cloud-alibaba-examples/pom.xml +++ b/spring-cloud-alibaba-examples/pom.xml @@ -16,6 +16,9 @@ sentinel-example + sentinel-dubbo-provider-example + sentinel-dubbo-consumer-example + sentinel-dubbo-api diff --git a/spring-cloud-alibaba-examples/sentinel-dubbo-api/pom.xml b/spring-cloud-alibaba-examples/sentinel-dubbo-api/pom.xml new file mode 100644 index 000000000..376f2a96a --- /dev/null +++ b/spring-cloud-alibaba-examples/sentinel-dubbo-api/pom.xml @@ -0,0 +1,30 @@ + + + + + org.springframework.cloud + spring-cloud-alibaba-examples + 0.2.0.BUILD-SNAPSHOT + + 4.0.0 + + + sentinel-dubbo-api + jar + api for sentinel dubbo example + + + + + org.apache.maven.plugins + maven-deploy-plugin + ${maven-deploy-plugin.version} + + true + + + + + + diff --git a/spring-cloud-alibaba-examples/sentinel-dubbo-api/src/main/java/org/springframework/cloud/alibaba/cloud/examples/FooService.java b/spring-cloud-alibaba-examples/sentinel-dubbo-api/src/main/java/org/springframework/cloud/alibaba/cloud/examples/FooService.java new file mode 100644 index 000000000..e07540b6f --- /dev/null +++ b/spring-cloud-alibaba-examples/sentinel-dubbo-api/src/main/java/org/springframework/cloud/alibaba/cloud/examples/FooService.java @@ -0,0 +1,10 @@ +package org.springframework.cloud.alibaba.cloud.examples; + +/** + * @author fangjian + */ +public interface FooService { + + String hello(String name); + +} diff --git a/spring-cloud-alibaba-examples/sentinel-dubbo-consumer-example/pom.xml b/spring-cloud-alibaba-examples/sentinel-dubbo-consumer-example/pom.xml new file mode 100644 index 000000000..2757c1237 --- /dev/null +++ b/spring-cloud-alibaba-examples/sentinel-dubbo-consumer-example/pom.xml @@ -0,0 +1,54 @@ + + + + + org.springframework.cloud + spring-cloud-alibaba-examples + 0.2.0.BUILD-SNAPSHOT + + 4.0.0 + + + sentinel-dubbo-consumer-example + jar + Example demonstrating how to use sentinel with dubbo + + + + + org.springframework.cloud + spring-cloud-starter-sentinel + + + + org.springframework.cloud + sentinel-dubbo-api + + + + com.alibaba.boot + dubbo-spring-boot-starter + 0.2.0 + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + org.apache.maven.plugins + maven-deploy-plugin + ${maven-deploy-plugin.version} + + true + + + + + + diff --git a/spring-cloud-alibaba-examples/sentinel-dubbo-consumer-example/readme-zh.md b/spring-cloud-alibaba-examples/sentinel-dubbo-consumer-example/readme-zh.md new file mode 100644 index 000000000..b2a27e83c --- /dev/null +++ b/spring-cloud-alibaba-examples/sentinel-dubbo-consumer-example/readme-zh.md @@ -0,0 +1,99 @@ +# Sentinel Dubbo Consumer Example + +## 项目说明 + +本项目演示如何使用 Sentinel starter 完成 Dubbo 应用的限流管理。 + +[Sentinel](https://github.com/alibaba/Sentinel) 是阿里巴巴开源的分布式系统的流量防卫组件,Sentinel 把流量作为切入点,从流量控制,熔断降级,系统负载保护等多个维度保护服务的稳定性。 + +[Dubbo](http://dubbo.apache.org/)是一款高性能Java RPC框架,有对应的[SpringBoot工程](https://github.com/apache/incubator-dubbo-spring-boot-project)。 + +本项目需要配合`sentinel-dubbo-provider-example`模块一起完成演示,并且需要先启动sentinel-dubbo-provider-example。 + +本项目专注于Sentinel与Dubbo的整合,关于Sentinel的更多特性可以查看[sentinel-example](https://github.com/spring-cloud-incubator/spring-cloud-alibaba/tree/master/spring-cloud-alibaba-examples/sentinel-example)。 + +## 示例 + +### 如何接入 +在启动示例进行演示之前,我们先了解一下 Dubbo 如何接入 Sentinel。 +**注意 本章节只是为了便于您理解接入方式,本示例代码中已经完成接入工作,您无需再进行修改。** + +1. 首先,修改 pom.xml 文件,引入 Sentinel starter 和 Dubbo starter。 + + + org.springframework.cloud + spring-cloud-starter-sentinel + + + + com.alibaba.boot + dubbo-spring-boot-starter + + +2. 配置限流规则 + + Sentinel提供了[sentinel-dubbo-adapter](https://github.com/alibaba/Sentinel/tree/master/sentinel-adapter/sentinel-dubbo-adapter)模块用来支持Dubbo服务调用的限流降级。sentinel-starter默认也集成了该功能。 + + sentinel-dubbo-adapter内部的Dubbo Filter会根据资源名进行限流降级处理。只需要配置规则即可: + + FlowRule flowRule = new FlowRule(); + flowRule.setResource( + "org.springframework.cloud.alibaba.cloud.examples.FooService:hello(java.lang.String)"); + flowRule.setCount(10); + flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS); + flowRule.setLimitApp("default"); + FlowRuleManager.loadRules(Collections.singletonList(flowRule)); + +### 规则定义 + +`sentinel-dubbo-api`模块中定义了FooService服务,内容如下: + + package org.springframework.cloud.alibaba.cloud.examples.FooService; + public interface FooService { + String hello(String name); + } + +该服务在Sentinel下对应的资源名是 `org.springframework.cloud.alibaba.cloud.examples.dubbo.FooService:hello(java.lang.String)` 。 + +定义该资源名对应的限流规则: + + FlowRule flowRule = new FlowRule(); + flowRule.setResource("dubboResource"); + flowRule.setCount(10); + flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS); + flowRule.setLimitApp("default"); + FlowRuleManager.loadRules(Collections.singletonList(flowRule)); + +### 服务调用 + +根据`sentinel-dubbo-provider-example`中发布的定义,使用Dubbo的@Reference注解注入服务对应的Bean。 + + @Reference(version = "${foo.service.version}", application = "${dubbo.application.id}", + url = "dubbo://localhost:12345", timeout = 30000) + private FooService fooService; + +由于设置的qps是10。调用15次查看是否被限流: + + FooServiceConsumer service = applicationContext.getBean(FooServiceConsumer.class); + + for (int i = 0; i < 15; i++) { + try { + String message = service.hello("Jim"); + System.out.println((i + 1) + " -> Success: " + message); + } + catch (SentinelRpcException ex) { + System.out.println("Blocked"); + } + catch (Exception ex) { + ex.printStackTrace(); + } + } + +### 应用启动 + +支持 IDE 直接启动和编译打包后启动。 + +1. IDE直接启动:找到主类 `SentinelDubboConsumerApp`,执行 main 方法启动应用。 +2. 打包编译后启动:首先执行 `mvn clean package` 将工程编译打包,然后执行 `java -jar sentinel-dubbo-consumer-example.jar`启动应用。 + + diff --git a/spring-cloud-alibaba-examples/sentinel-dubbo-consumer-example/readme.md b/spring-cloud-alibaba-examples/sentinel-dubbo-consumer-example/readme.md new file mode 100644 index 000000000..42bd4561c --- /dev/null +++ b/spring-cloud-alibaba-examples/sentinel-dubbo-consumer-example/readme.md @@ -0,0 +1,97 @@ +# Sentinel Dubbo Consumer Example +## Project Instruction + +This example illustrates how to use Sentinel starter to implement flow control for Spring Cloud applications. + +[Sentinel](https://github.com/alibaba/Sentinel) is an open-source project of Alibaba. Sentinel takes "traffic flow" as the breakthrough point, and provides solutions in areas such as flow control, concurrency, circuit breaking, and load protection to protect service stability. + +[Dubbo](http://dubbo.apache.org/) is a high-performance, java based open source RPC framework. + +This example work with 'sentinel-dubbo-provider-example' module and `sentinel-dubbo-provider-example` module should startup firstly. + +This example focus on the integration of Sentinel and Dubbo. You can see more features on [sentinel-example](https://github.com/spring-cloud-incubator/spring-cloud-alibaba/tree/master/spring-cloud-alibaba-examples/sentinel-example). + +## Demo + +### Connect to Sentinel +Before we start the demo, let's learn how to connect Sentinel with Dubbo to a Spring Cloud application. +**Note: This section is to show you how to connect to Sentinel. The configurations have been completed in the following example, so you don't need modify the code any more.** + +1. Add dependency spring-cloud-starter-sentinel and dubbo-spring-boot-starter in the pom.xml file in your Spring Cloud project. + + + org.springframework.cloud + spring-cloud-starter-sentinel + + + + com.alibaba.boot + dubbo-spring-boot-starter + + +2. Configure flow control rules + + Sentinel provide [sentinel-dubbo-adapter](https://github.com/alibaba/Sentinel/tree/master/sentinel-adapter/sentinel-dubbo-adapter) module to support dubbo. to support dubbo. sentinel-starter integrates this feature by default. + + sentinel-dubbo-adapter will using Sentinel to handle resource by Dubbo Filter. You just need to define rules. + + FlowRule flowRule = new FlowRule(); + flowRule.setResource("dubboResource"); + flowRule.setCount(10); + flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS); + flowRule.setLimitApp("default"); + FlowRuleManager.loadRules(Collections.singletonList(flowRule)); + +### Configure Rules + +`sentinel-dubbo-api` define a service named FooService: + + package org.springframework.cloud.alibaba.cloud.examples.FooService; + public interface FooService { + String hello(String name); + } + +The resource name of this service's `hello` method is `org.springframework.cloud.alibaba.cloud.examples.dubbo.FooService:hello(java.lang.String)` . + +Configure rules: + + FlowRule flowRule = new FlowRule(); + flowRule.setResource("dubboResource"); + flowRule.setCount(10); + flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS); + flowRule.setLimitApp("default"); + FlowRuleManager.loadRules(Collections.singletonList(flowRule)); + +### Service Invocation + +Using the `@Reference` annotation to inject service: + + @Reference(version = "${foo.service.version}", application = "${dubbo.application.id}", + url = "dubbo://localhost:12345", timeout = 30000) + private FooService fooService; + +Because QPS is 10, we can see that flow control takes effect in this invocation: + + FooServiceConsumer service = applicationContext.getBean(FooServiceConsumer.class); + + for (int i = 0; i < 15; i++) { + try { + String message = service.hello("Jim"); + System.out.println((i + 1) + " -> Success: " + message); + } + catch (SentinelRpcException ex) { + System.out.println("Blocked"); + } + catch (Exception ex) { + ex.printStackTrace(); + } + } + +### Start Application + +Start the application in IDE or by building a fatjar. + +1. Start in IDE: Find main class `SentinelDubboConsumerApp`, and execute the main method. +2. Build a fatjar:Execute command `mvn clean package` to build a fatjar,and run command `java -jar sentinel-dubbo-consumer-example.jar` to start the application. + + diff --git a/spring-cloud-alibaba-examples/sentinel-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/dubbo/consumer/FooServiceConsumer.java b/spring-cloud-alibaba-examples/sentinel-dubbo-consumer-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/FooServiceConsumer.java similarity index 52% rename from spring-cloud-alibaba-examples/sentinel-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/dubbo/consumer/FooServiceConsumer.java rename to spring-cloud-alibaba-examples/sentinel-dubbo-consumer-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/FooServiceConsumer.java index 2fe62bf5a..0caae7294 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/dubbo/consumer/FooServiceConsumer.java +++ b/spring-cloud-alibaba-examples/sentinel-dubbo-consumer-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/FooServiceConsumer.java @@ -1,6 +1,4 @@ -package org.springframework.cloud.alibaba.cloud.examples.dubbo.consumer; - -import org.springframework.cloud.alibaba.cloud.examples.dubbo.FooService; +package org.springframework.cloud.alibaba.cloud.examples; import com.alibaba.dubbo.config.annotation.Reference; @@ -9,7 +7,8 @@ import com.alibaba.dubbo.config.annotation.Reference; */ public class FooServiceConsumer { - @Reference(url = "dubbo://127.0.0.1:25758", timeout = 3000) + @Reference(version = "${foo.service.version}", application = "${dubbo.application.id}", + url = "dubbo://localhost:12345", timeout = 30000) private FooService fooService; public String hello(String name) { diff --git a/spring-cloud-alibaba-examples/sentinel-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/dubbo/consumer/ConsumerApplication.java b/spring-cloud-alibaba-examples/sentinel-dubbo-consumer-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/SentinelDubboConsumerApp.java similarity index 53% rename from spring-cloud-alibaba-examples/sentinel-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/dubbo/consumer/ConsumerApplication.java rename to spring-cloud-alibaba-examples/sentinel-dubbo-consumer-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/SentinelDubboConsumerApp.java index 7dda1974e..d0aba6857 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/dubbo/consumer/ConsumerApplication.java +++ b/spring-cloud-alibaba-examples/sentinel-dubbo-consumer-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/SentinelDubboConsumerApp.java @@ -1,9 +1,9 @@ -package org.springframework.cloud.alibaba.cloud.examples.dubbo.consumer; +package org.springframework.cloud.alibaba.cloud.examples; import java.util.Collections; -import org.springframework.boot.Banner; import org.springframework.boot.WebApplicationType; +import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; @@ -12,36 +12,12 @@ import com.alibaba.csp.sentinel.slots.block.RuleConstant; import com.alibaba.csp.sentinel.slots.block.SentinelRpcException; import com.alibaba.csp.sentinel.slots.block.flow.FlowRule; import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager; -import com.alibaba.dubbo.config.ApplicationConfig; -import com.alibaba.dubbo.config.ConsumerConfig; -import com.alibaba.dubbo.config.RegistryConfig; -import com.alibaba.dubbo.config.spring.context.annotation.DubboComponentScan; /** * @author fangjian */ -@DubboComponentScan("org.springframework.cloud.alibaba.cloud.examples.dubbo.provider") -public class ConsumerApplication { - - @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(); - return consumerConfig; - } +@SpringBootApplication(scanBasePackages = "org.springframework.cloud.alibaba.cloud.examples") +public class SentinelDubboConsumerApp { @Bean public FooServiceConsumer annotationDemoServiceConsumer() { @@ -50,20 +26,19 @@ public class ConsumerApplication { public static void main(String[] args) { - SpringApplicationBuilder consumerBuilder = new SpringApplicationBuilder() - .bannerMode(Banner.Mode.OFF).registerShutdownHook(false) - .logStartupInfo(false).web(WebApplicationType.NONE); - ApplicationContext applicationContext = consumerBuilder - .sources(ConsumerApplication.class).run(args); - FlowRule flowRule = new FlowRule(); flowRule.setResource( - "org.springframework.cloud.alibaba.cloud.examples.dubbo.FooService:hello(java.lang.String)"); + "org.springframework.cloud.alibaba.cloud.examples.FooService:hello(java.lang.String)"); flowRule.setCount(10); flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS); flowRule.setLimitApp("default"); FlowRuleManager.loadRules(Collections.singletonList(flowRule)); + SpringApplicationBuilder consumerBuilder = new SpringApplicationBuilder(); + ApplicationContext applicationContext = consumerBuilder + .web(WebApplicationType.NONE).sources(SentinelDubboConsumerApp.class) + .run(args); + FooServiceConsumer service = applicationContext.getBean(FooServiceConsumer.class); for (int i = 0; i < 15; i++) { diff --git a/spring-cloud-alibaba-examples/sentinel-dubbo-consumer-example/src/main/resources/application.properties b/spring-cloud-alibaba-examples/sentinel-dubbo-consumer-example/src/main/resources/application.properties new file mode 100644 index 000000000..08fc91acb --- /dev/null +++ b/spring-cloud-alibaba-examples/sentinel-dubbo-consumer-example/src/main/resources/application.properties @@ -0,0 +1,11 @@ +spring.application.name = dubbo-consumer-demo + +foo.service.version = 1.0.0 + +dubbo.application.id = dubbo-consumer-demo +dubbo.application.name = dubbo-consumer-demo + +dubbo.protocol.id = dubbo +dubbo.protocol.name = dubbo +dubbo.protocol.port = 12345 + diff --git a/spring-cloud-alibaba-examples/sentinel-dubbo-provider-example/pom.xml b/spring-cloud-alibaba-examples/sentinel-dubbo-provider-example/pom.xml new file mode 100644 index 000000000..1ffda73c3 --- /dev/null +++ b/spring-cloud-alibaba-examples/sentinel-dubbo-provider-example/pom.xml @@ -0,0 +1,54 @@ + + + + + org.springframework.cloud + spring-cloud-alibaba-examples + 0.2.0.BUILD-SNAPSHOT + + 4.0.0 + + + sentinel-dubbo-provider-example + jar + Example demonstrating how to use sentinel with dubbo + + + + + org.springframework.cloud + spring-cloud-starter-sentinel + + + + org.springframework.cloud + sentinel-dubbo-api + + + + com.alibaba.boot + dubbo-spring-boot-starter + 0.2.0 + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + org.apache.maven.plugins + maven-deploy-plugin + ${maven-deploy-plugin.version} + + true + + + + + + diff --git a/spring-cloud-alibaba-examples/sentinel-dubbo-provider-example/readme-zh.md b/spring-cloud-alibaba-examples/sentinel-dubbo-provider-example/readme-zh.md new file mode 100644 index 000000000..866885fd7 --- /dev/null +++ b/spring-cloud-alibaba-examples/sentinel-dubbo-provider-example/readme-zh.md @@ -0,0 +1,101 @@ +# Sentinel Dubbo Provider Example + +## 项目说明 + +本项目演示如何使用 Sentinel starter 完成 Dubbo 应用的限流管理。 + +[Sentinel](https://github.com/alibaba/Sentinel) 是阿里巴巴开源的分布式系统的流量防卫组件,Sentinel 把流量作为切入点,从流量控制,熔断降级,系统负载保护等多个维度保护服务的稳定性。 + +[Dubbo](http://dubbo.apache.org/)是一款高性能Java RPC框架,有对应的[SpringBoot工程](https://github.com/apache/incubator-dubbo-spring-boot-project)。 + +本项目需要配合`sentinel-dubbo-consumer-example`模块一起完成演示。 + +本项目专注于Sentinel与Dubbo的整合,关于Sentinel的更多特性可以查看[sentinel-example](https://github.com/spring-cloud-incubator/spring-cloud-alibaba/tree/master/spring-cloud-alibaba-examples/sentinel-example)。 + +## 示例 + +### 如何接入 +在启动示例进行演示之前,我们先了解一下 Dubbo 如何接入 Sentinel。 +**注意 本章节只是为了便于您理解接入方式,本示例代码中已经完成接入工作,您无需再进行修改。** + +1. 首先,修改 pom.xml 文件,引入 Sentinel starter 和 Dubbo starter。 + + + org.springframework.cloud + spring-cloud-starter-sentinel + + + + com.alibaba.boot + dubbo-spring-boot-starter + + +2. 配置限流规则 + + Sentinel提供了[sentinel-dubbo-adapter](https://github.com/alibaba/Sentinel/tree/master/sentinel-adapter/sentinel-dubbo-adapter)模块用来支持Dubbo服务调用的限流降级。sentinel-starter默认也集成了该功能。 + + sentinel-dubbo-adapter内部的Dubbo Filter会根据资源名进行限流降级处理。只需要配置规则即可: + + FlowRule flowRule = new FlowRule(); + flowRule.setResource("dubboResource"); + flowRule.setCount(10); + flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS); + flowRule.setLimitApp("default"); + FlowRuleManager.loadRules(Collections.singletonList(flowRule)); + + +### 服务定义及发布 + +在application.properties文件中定义dubbo相关的配置,比如协议,注册中心: + + spring.application.name = dubbo-provider-demo + + foo.service.version = 1.0.0 + + dubbo.scan.basePackages = org.springframework.cloud.alibaba.cloud.examples + + dubbo.application.id = dubbo-provider-demo + dubbo.application.name = dubbo-provider-demo + + dubbo.protocol.id = dubbo + dubbo.protocol.name = dubbo + dubbo.protocol.port = 12345 + dubbo.protocol.status = server + + dubbo.registry.id = my-registry + dubbo.registry.address = N/A + + +`sentinel-dubbo-api`模块中定义了FooService服务,内容如下: + + package org.springframework.cloud.alibaba.cloud.examples.FooService; + public interface FooService { + String hello(String name); + } + +定义具体的服务: + + @Service( + version = "${foo.service.version}", + application = "${dubbo.application.id}", + protocol = "${dubbo.protocol.id}", + registry = "${dubbo.registry.id}" + ) + public class FooServiceImpl implements FooService { + + @Override + public String hello(String name) { + return "hello, " + name; + } + } + + +### 应用启动 + + +支持 IDE 直接启动和编译打包后启动。 + +1. IDE直接启动:找到主类 `SentinelDubboProviderApp`,执行 main 方法启动应用。 +2. 打包编译后启动:首先执行 `mvn clean package` 将工程编译打包,然后执行 `java -jar sentinel-dubbo-provider-example.jar`启动应用。 + + diff --git a/spring-cloud-alibaba-examples/sentinel-dubbo-provider-example/readme.md b/spring-cloud-alibaba-examples/sentinel-dubbo-provider-example/readme.md new file mode 100644 index 000000000..885296f4c --- /dev/null +++ b/spring-cloud-alibaba-examples/sentinel-dubbo-provider-example/readme.md @@ -0,0 +1,97 @@ +# Sentinel Dubbo Provider Example +## Project Instruction + +This example illustrates how to use Sentinel starter to implement flow control for Spring Cloud applications. + +[Sentinel](https://github.com/alibaba/Sentinel) is an open-source project of Alibaba. Sentinel takes "traffic flow" as the breakthrough point, and provides solutions in areas such as flow control, concurrency, circuit breaking, and load protection to protect service stability. + +[Dubbo](http://dubbo.apache.org/) is a high-performance, java based open source RPC framework. + +This example work with 'sentinel-dubbo-consumer-example' module and `sentinel-dubbo-provider-example` module should startup firstly. + +This example focus on the integration of Sentinel and Dubbo. You can see more features on [sentinel-example](https://github.com/spring-cloud-incubator/spring-cloud-alibaba/tree/master/spring-cloud-alibaba-examples/sentinel-example). + +## Demo + +### Connect to Sentinel +Before we start the demo, let's learn how to connect Sentinel with Dubbo to a Spring Cloud application. +**Note: This section is to show you how to connect to Sentinel. The configurations have been completed in the following example, so you don't need modify the code any more.** + +1. Add dependency spring-cloud-starter-sentinel and dubbo-spring-boot-starter in the pom.xml file in your Spring Cloud project. + + + org.springframework.cloud + spring-cloud-starter-sentinel + + + + com.alibaba.boot + dubbo-spring-boot-starter + + +2. Configure flow control rules + + Sentinel provide [sentinel-dubbo-adapter](https://github.com/alibaba/Sentinel/tree/master/sentinel-adapter/sentinel-dubbo-adapter) module to support dubbo. to support dubbo. sentinel-starter integrates this feature by default. + + sentinel-dubbo-adapter will using Sentinel to handle resource by Dubbo Filter. You just need to define rules. + + FlowRule flowRule = new FlowRule(); + flowRule.setResource("dubboResource"); + flowRule.setCount(10); + flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS); + flowRule.setLimitApp("default"); + FlowRuleManager.loadRules(Collections.singletonList(flowRule)); + +### Configure and Publish Service + +Define some configs of dubbo in `application.properties`, like protocol, config registry: + + spring.application.name = dubbo-provider-demo + + foo.service.version = 1.0.0 + + dubbo.scan.basePackages = org.springframework.cloud.alibaba.cloud.examples + + dubbo.application.id = dubbo-provider-demo + dubbo.application.name = dubbo-provider-demo + + dubbo.protocol.id = dubbo + dubbo.protocol.name = dubbo + dubbo.protocol.port = 12345 + dubbo.protocol.status = server + + dubbo.registry.id = my-registry + dubbo.registry.address = N/A + + +`sentinel-dubbo-api` define a service named FooService: + + package org.springframework.cloud.alibaba.cloud.examples.FooService; + public interface FooService { + String hello(String name); + } + +Define the implement Service annotated by `@Service`: + + @Service( + version = "${foo.service.version}", + application = "${dubbo.application.id}", + protocol = "${dubbo.protocol.id}", + registry = "${dubbo.registry.id}" + ) + public class FooServiceImpl implements FooService { + + @Override + public String hello(String name) { + return "hello, " + name; + } + } + +### Start Application + +Start the application in IDE or by building a fatjar. + +1. Start in IDE: Find main class `SentinelDubboProviderApp`, and execute the main method. +2. Build a fatjar:Execute command `mvn clean package` to build a fatjar,and run command `java -jar sentinel-dubbo-provider-example.jar` to start the application. + + diff --git a/spring-cloud-alibaba-examples/sentinel-dubbo-provider-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/FooServiceImpl.java b/spring-cloud-alibaba-examples/sentinel-dubbo-provider-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/FooServiceImpl.java new file mode 100644 index 000000000..11dd22637 --- /dev/null +++ b/spring-cloud-alibaba-examples/sentinel-dubbo-provider-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/FooServiceImpl.java @@ -0,0 +1,20 @@ +package org.springframework.cloud.alibaba.cloud.examples; + +import com.alibaba.dubbo.config.annotation.Service; + +/** + * @author fangjian + */ +@Service( + version = "${foo.service.version}", + application = "${dubbo.application.id}", + protocol = "${dubbo.protocol.id}", + registry = "${dubbo.registry.id}" +) +public class FooServiceImpl implements FooService { + + @Override + public String hello(String name) { + return "hello, " + name; + } +} \ No newline at end of file diff --git a/spring-cloud-alibaba-examples/sentinel-dubbo-provider-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/SentinelDubboProviderApp.java b/spring-cloud-alibaba-examples/sentinel-dubbo-provider-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/SentinelDubboProviderApp.java new file mode 100644 index 000000000..346fb48fc --- /dev/null +++ b/spring-cloud-alibaba-examples/sentinel-dubbo-provider-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/SentinelDubboProviderApp.java @@ -0,0 +1,19 @@ +package org.springframework.cloud.alibaba.cloud.examples; + +import org.springframework.boot.WebApplicationType; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.builder.SpringApplicationBuilder; + +/** + * @author fangjian + */ +@SpringBootApplication +public class SentinelDubboProviderApp { + + public static void main(String[] args) { + SpringApplicationBuilder providerBuilder = new SpringApplicationBuilder(); + providerBuilder.web(WebApplicationType.NONE) + .sources(SentinelDubboProviderApp.class).run(args); + } + +} diff --git a/spring-cloud-alibaba-examples/sentinel-dubbo-provider-example/src/main/resources/application.properties b/spring-cloud-alibaba-examples/sentinel-dubbo-provider-example/src/main/resources/application.properties new file mode 100644 index 000000000..f7e84bcbe --- /dev/null +++ b/spring-cloud-alibaba-examples/sentinel-dubbo-provider-example/src/main/resources/application.properties @@ -0,0 +1,16 @@ +spring.application.name = dubbo-provider-demo + +foo.service.version = 1.0.0 + +dubbo.scan.basePackages = org.springframework.cloud.alibaba.cloud.examples + +dubbo.application.id = dubbo-provider-demo +dubbo.application.name = dubbo-provider-demo + +dubbo.protocol.id = dubbo +dubbo.protocol.name = dubbo +dubbo.protocol.port = 12345 +dubbo.protocol.status = server + +dubbo.registry.id = my-registry +dubbo.registry.address = N/A diff --git a/spring-cloud-alibaba-examples/sentinel-example/pom.xml b/spring-cloud-alibaba-examples/sentinel-example/pom.xml index 34e3f71a4..6da7873c5 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/pom.xml +++ b/spring-cloud-alibaba-examples/sentinel-example/pom.xml @@ -30,12 +30,6 @@ spring-boot-starter-actuator - - com.alibaba.boot - dubbo-spring-boot-starter - 0.2.0 - - diff --git a/spring-cloud-alibaba-examples/sentinel-example/readme-zh.md b/spring-cloud-alibaba-examples/sentinel-example/readme-zh.md index 5649e2003..4ae6cfaad 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/readme-zh.md +++ b/spring-cloud-alibaba-examples/sentinel-example/readme-zh.md @@ -158,42 +158,81 @@ Sentinel 控制台支持实时监控查看,您可以通过 Sentinel 控制台

-## Dubbo支持 +## DataSource支持 -[Dubbo](http://dubbo.apache.org/)是一款高性能Java RPC框架。 +Sentinel内部提供了[动态规则的扩展实现DataSource](https://github.com/alibaba/Sentinel/wiki/%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%99%E6%89%A9%E5%B1%95#datasource-%E6%89%A9%E5%B1%95)。 -Sentinel提供了[sentinel-dubbo-adapter](https://github.com/alibaba/Sentinel/tree/master/sentinel-adapter/sentinel-dubbo-adapter)模块用来支持Dubbo服务调用的限流降级。sentinel-starter默认也集成了该功能。 +Sentinel starter整合了目前存在的4类DataSource。只需要在配置文件中进行相关配置,即可在Spring容器中自动注册DataSource。 -比如有个FooService服务,定义如下: +比如要定义一个FileRefreshableDataSource,配置如下: - package org.springframework.cloud.alibaba.cloud.examples.dubbo.FooService; - public interface FooService { - String hello(String name); - } + spring.cloud.sentinel.datasource.type=file + spring.cloud.sentinel.datasource.recommendRefreshMs=2000 + spring.cloud.sentinel.datasource.bufSize=2048 + spring.cloud.sentinel.datasource.charset=utf-8 + spring.cloud.sentinel.datasource.configParser=myParser + spring.cloud.sentinel.datasource.file=/Users/you/rule.json + +然后使用`@SentinelDataSource`注解修饰DataSource即可注入: + + @SentinelDataSource("spring.cloud.sentinel.datasource") + private DataSource dataSource; + +`@SentinelDataSource`注解的value属性可以不填。默认值就是spring.cloud.sentinel.datasource。 -该服务在Sentinel下对应的资源名是 `org.springframework.cloud.alibaba.cloud.examples.dubbo.FooService:hello(java.lang.String)` 。 +value属性代表配置前缀。示例中会去找spring.cloud.sentinel.datasource.xxx相关的配置。 -在Consumer端进行限流的话,需要处理SentinelRpcException。 +spring.cloud.sentinel.datasource.type就是对应的DataSource类型。 - FooService service = applicationContext.getBean(FooService.class); - - for (int i = 0; i < 15; i++) { - try { - String message = service.hello("Jim"); - } catch (SentinelRpcException ex) { - System.out.println("Blocked"); - } catch (Exception ex) { - ex.printStackTrace(); - } - } +spring.cloud.sentinel.datasource.recommendRefreshMs里的recommendRefreshMs对应相关DataSource的属性。 -在Provider端进行限流的话,Consumer端调用的话会抛出RpcException。因为Provider端被限流抛出了SentinelRpcException。 +spring.cloud.sentinel.datasource.configParser代表ConfigParser在Spring容器里的name。如果没找到,会抛出异常。 + +type目前支持file, nacos, zk, apollo。 -### 应用启动 +### 自定义DataSource -在启动ServiceApplication的前提下,再启动ConsumerApplication。 +自定义DataSource只需要两部。 -ConsumerApplication在Consumer端设置的限流规则,所以启动完成后查看控制台的打印信息,会发现部分调用被Block。 +1. 定义DataSource + + public class CustomDataSource implements DataSource { + private String fieldA; + private String fieldB; + ... + } + +2. 装配DataSource。有两种方式处理。 + + * 直接构造DataSource + + @Bean + public CustomDataSource customDataSource() { + CustomDataSource customDataSource = + new CustomDataSource(); + customDataSource.setFieldA("valueA"); + customDataSource.setFieldB("valueB"); + ... + return customDataSource; + } + + * 在classpath:/META-INF/sentinel-datasource.properties中管理DataSource信息 + + custom = yourpackage.CustomDataSource + + 在application.properties中定义DataSource + + spring.cloud.sentinel.datasource.type = custom + spring.cloud.sentinel.datasource.fieldA = valueA + spring.cloud.sentinel.datasource.fieldB = valueB + + 注意:由于目前Sentinel的AbstractDataSource需要有个ConfigParser作为构造函数中的参数,并且它的子类的构造都是通过多个参数的构造函数构造的。 + 所以目前所有的Sentinel starter中的DataSource都是基于FactoryBean并且通过设置属性构造的。如果有这方面的需求,需要再多加一个registerFactoryBean过程。 + + SentinelDataSourceRegistry.registerFactoryBean("custeom", CustomDataSourceFactoryBean.class); + + 如果自定义DataSource可以注入属性,那么没有必要使用SentinelDataSourceRegistry注册FactoryBean。 + ## More Sentinel 是一款功能强大的中间件,从流量控制,熔断降级,系统负载保护等多个维度保护服务的稳定性。此 Demo 仅演示了 使用 Sentinel 作为限流工具的使用,更多 Sentinel 相关的信息,请参考 [Sentinel 项目](https://github.com/alibaba/Sentinel)。 diff --git a/spring-cloud-alibaba-examples/sentinel-example/readme.md b/spring-cloud-alibaba-examples/sentinel-example/readme.md index 4b140f094..85a44863e 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/readme.md +++ b/spring-cloud-alibaba-examples/sentinel-example/readme.md @@ -165,42 +165,78 @@ To see the metrics, click **实时监控(Real-time Monitoring)** in the left-sid

-## Dubbo +## DataSource -[Dubbo](http://dubbo.apache.org/) is a high-performance, java based open source RPC framework. +Sentinel provide [DataSource](https://github.com/alibaba/Sentinel/blob/master/sentinel-extension/sentinel-datasource-extension/src/main/java/com/alibaba/csp/sentinel/datasource/DataSource.java) to manage dynamic rules. -Sentinel provide a module named [sentinel-dubbo-adapter](https://github.com/alibaba/Sentinel/tree/master/sentinel-adapter/sentinel-dubbo-adapter)to support dubbo。sentinel-starter integrates this feature by default. +Sentinel starter integrated 4 DataSources provided by Sentinel. It will be register into Spring Context if you write some configs in `application.properties`. -For example, a service named FooService, see the code below: +If you want to define FileRefreshableDataSource: - package org.springframework.cloud.alibaba.cloud.examples.dubbo.FooService; - public interface FooService { - String hello(String name); - } + spring.cloud.sentinel.datasource.type=file + spring.cloud.sentinel.datasource.recommendRefreshMs=2000 + spring.cloud.sentinel.datasource.bufSize=2048 + spring.cloud.sentinel.datasource.charset=utf-8 + spring.cloud.sentinel.datasource.configParser=myParser + spring.cloud.sentinel.datasource.file=/Users/you/rule.json + +then use `@SentinelDataSource` to annotate DataSource: + + @SentinelDataSource("spring.cloud.sentinel.datasource") + private DataSource dataSource; + +The value() of `@SentinelDataSource` is not required, it means the prefix of configuration. Default value is `spring.cloud.sentinel.datasource`. -The resource name of this service is `org.springframework.cloud.alibaba.cloud.examples.dubbo.FooService:hello(java.lang.String)` . +spring.cloud.sentinel.datasource.type means the type of DataSource. -You should handle SentinelRpcException if rpc invocation was be limited on Consumer side: +spring.cloud.sentinel.datasource.recommendRefreshMs means the recommendRefreshMs property of specified DataSource. - FooService service = applicationContext.getBean(FooService.class); - - for (int i = 0; i < 15; i++) { - try { - String message = service.hello("Jim"); - } catch (SentinelRpcException ex) { - System.out.println("Blocked"); - } catch (Exception ex) { - ex.printStackTrace(); - } - } +spring.cloud.sentinel.datasource.configParser means the name of spring bean that type is ConfigParser. If the bean is not exists, will throw exception. + +Now datasource type support 4 categories: file, nacos, zk, apollo. -It will throw RpcException on Consumer side if it was be limited on Provider side, because Provider side throw SentinelRpcException in this scene. +### User-defined DataSource -### Start Application +User-defined DataSource need 2 steps. + +1. Define DataSource + + public class CustomDataSource implements DataSource { + private String fieldA; + private String fieldB; + ... + } + +2. Assemble DataSource. There are 2 ways to do this. -You can startup ConsumerApplication after ServiceApplication startup. + * Construct DataSource directly + + @Bean + public CustomDataSource customDataSource() { + CustomDataSource customDataSource = new CustomDataSource(); + customDataSource.setFieldA("valueA"); + customDataSource.setFieldB("valueB"); + ... + return customDataSource; + } -ConsumerApplication init flow control rules after startup, so you will find some invocations have been blocked in console. + * define DataSource metadata in `classpath:/META-INF/sentinel-datasource.properties` + + custom = yourpackage.CustomDataSource + + define configuration in `application.properties` + + spring.cloud.sentinel.datasource.type = custom + spring.cloud.sentinel.datasource.fieldA = valueA + spring.cloud.sentinel.datasource.fieldB = valueB + +Note: The AbstractDataSource of Sentinel need a ConfigParser as a constructor param and the subclass of AbstractDataSource was construct by multi-param constructor. +Now All DataSources in starter was construct by FactoryBean. If you want to do it in this way, you should register FactoryBean by SentinelDataSourceRegistry. + + SentinelDataSourceRegistry.registerFactoryBean("custeom", CustomDataSourceFactoryBean.class); + +It is no need to using SentinelDataSourceRegistry to register FactoryBean if your User-defined DataSource can inject fields. + ## More For more information about Sentinel, see [Sentinel Project](https://github.com/alibaba/Sentinel). diff --git a/spring-cloud-alibaba-examples/sentinel-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/JsonFlowRuleListParser.java b/spring-cloud-alibaba-examples/sentinel-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/JsonFlowRuleListParser.java new file mode 100644 index 000000000..255eb7892 --- /dev/null +++ b/spring-cloud-alibaba-examples/sentinel-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/JsonFlowRuleListParser.java @@ -0,0 +1,18 @@ +package org.springframework.cloud.alibaba.cloud.examples; + +import java.util.List; + +import com.alibaba.csp.sentinel.datasource.ConfigParser; +import com.alibaba.csp.sentinel.slots.block.flow.FlowRule; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.TypeReference; + +/** + * @author fangjian + */ +public class JsonFlowRuleListParser implements ConfigParser> { + @Override + public List parse(String source) { + return JSON.parseObject(source, new TypeReference>() {}); + } +} diff --git a/spring-cloud-alibaba-examples/sentinel-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/ServiceApplication.java b/spring-cloud-alibaba-examples/sentinel-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/ServiceApplication.java index 2c12ad322..0dccda2f8 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/ServiceApplication.java +++ b/spring-cloud-alibaba-examples/sentinel-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/ServiceApplication.java @@ -1,5 +1,6 @@ package org.springframework.cloud.alibaba.cloud.examples; +import com.alibaba.csp.sentinel.datasource.ConfigParser; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.alibaba.sentinel.annotation.SentinelProtect; @@ -23,6 +24,11 @@ public class ServiceApplication { return new RestTemplate(); } + @Bean + public ConfigParser myParser() { + return new JsonFlowRuleListParser(); + } + public static void main(String[] args) { SpringApplication.run(ServiceApplication.class, args); } diff --git a/spring-cloud-alibaba-examples/sentinel-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/dubbo/DubboProviderRunner.java b/spring-cloud-alibaba-examples/sentinel-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/dubbo/DubboProviderRunner.java deleted file mode 100644 index 61efe3cb0..000000000 --- a/spring-cloud-alibaba-examples/sentinel-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/dubbo/DubboProviderRunner.java +++ /dev/null @@ -1,24 +0,0 @@ -package org.springframework.cloud.alibaba.cloud.examples.dubbo; - -import org.springframework.boot.Banner; -import org.springframework.boot.CommandLineRunner; -import org.springframework.boot.WebApplicationType; -import org.springframework.boot.builder.SpringApplicationBuilder; -import org.springframework.cloud.alibaba.cloud.examples.dubbo.provider.ProviderApplication; -import org.springframework.stereotype.Component; - -/** - * @author fangjian - */ -@Component -public class DubboProviderRunner implements CommandLineRunner { - - @Override - public void run(String... args) throws Exception { - SpringApplicationBuilder providerBuilder = new SpringApplicationBuilder() - .bannerMode(Banner.Mode.OFF).registerShutdownHook(false) - .logStartupInfo(false).web(WebApplicationType.NONE); - providerBuilder.sources(ProviderApplication.class).run(args); - } - -} diff --git a/spring-cloud-alibaba-examples/sentinel-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/dubbo/FooService.java b/spring-cloud-alibaba-examples/sentinel-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/dubbo/FooService.java deleted file mode 100644 index d44b5c996..000000000 --- a/spring-cloud-alibaba-examples/sentinel-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/dubbo/FooService.java +++ /dev/null @@ -1,10 +0,0 @@ -package org.springframework.cloud.alibaba.cloud.examples.dubbo; - -/** - * @author fangjian - */ -public interface FooService { - - String hello(String name); - -} diff --git a/spring-cloud-alibaba-examples/sentinel-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/dubbo/provider/FooServiceImpl.java b/spring-cloud-alibaba-examples/sentinel-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/dubbo/provider/FooServiceImpl.java deleted file mode 100644 index bb9e81d66..000000000 --- a/spring-cloud-alibaba-examples/sentinel-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/dubbo/provider/FooServiceImpl.java +++ /dev/null @@ -1,17 +0,0 @@ -package org.springframework.cloud.alibaba.cloud.examples.dubbo.provider; - -import org.springframework.cloud.alibaba.cloud.examples.dubbo.FooService; - -import com.alibaba.dubbo.config.annotation.Service; - -/** - * @author fangjian - */ -@Service -public class FooServiceImpl implements FooService { - - @Override - public String hello(String name) { - return "hello, " + name; - } -} diff --git a/spring-cloud-alibaba-examples/sentinel-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/dubbo/provider/ProviderApplication.java b/spring-cloud-alibaba-examples/sentinel-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/dubbo/provider/ProviderApplication.java deleted file mode 100644 index 3664ad692..000000000 --- a/spring-cloud-alibaba-examples/sentinel-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/dubbo/provider/ProviderApplication.java +++ /dev/null @@ -1,38 +0,0 @@ -package org.springframework.cloud.alibaba.cloud.examples.dubbo.provider; - -import org.springframework.context.annotation.Bean; - -import com.alibaba.dubbo.config.ApplicationConfig; -import com.alibaba.dubbo.config.ProtocolConfig; -import com.alibaba.dubbo.config.RegistryConfig; -import com.alibaba.dubbo.config.spring.context.annotation.DubboComponentScan; - -/** - * @author fangjian - */ -@DubboComponentScan("org.springframework.cloud.alibaba.cloud.examples.dubbo.provider") -public class ProviderApplication { - - @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; - } - -} diff --git a/spring-cloud-alibaba-examples/sentinel-example/src/main/resources/application.properties b/spring-cloud-alibaba-examples/sentinel-example/src/main/resources/application.properties index 61ddbca7f..8f8ade551 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/src/main/resources/application.properties +++ b/spring-cloud-alibaba-examples/sentinel-example/src/main/resources/application.properties @@ -2,4 +2,13 @@ spring.application.name=sentinel-example server.port=18083 management.endpoints.web.exposure.include=* spring.cloud.sentinel.port=8721 -spring.cloud.sentinel.dashboard=localhost:8080 \ No newline at end of file +spring.cloud.sentinel.dashboard=localhost:8080 + + + +spring.cloud.sentinel.datasource.type=file +spring.cloud.sentinel.datasource.recommendRefreshMs=3000 +spring.cloud.sentinel.datasource.bufSize=4056196 +spring.cloud.sentinel.datasource.charset=utf-8 +spring.cloud.sentinel.datasource.configParser=myParser +spring.cloud.sentinel.datasource.file=/Users/you/rule.json \ No newline at end of file