-
Notifications
You must be signed in to change notification settings - Fork 8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add demo module for integration with Apache Dubbo 2.7.x and above ver…
…sion Signed-off-by: Eric Zhao <[email protected]>
- Loading branch information
1 parent
0389974
commit f59b134
Showing
10 changed files
with
398 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
``` |
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,49 @@ | ||
<?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</artifactId> | ||
<groupId>com.alibaba.csp</groupId> | ||
<version>1.5.1-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>sentinel-demo-apache-dubbo</artifactId> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.apache.dubbo</groupId> | ||
<artifactId>dubbo</artifactId> | ||
<version>2.7.1</version> | ||
</dependency> | ||
|
||
<!-- Dubbo provides qos plugin and is enable by default. --> | ||
<!-- The dubbo-qos module is optional and it depends Netty 4.x, so add it explicitly --> | ||
<!-- @see http://dubbo.apache.org/zh-cn/docs/user/references/qos.html --> | ||
<dependency> | ||
<groupId>io.netty</groupId> | ||
<artifactId>netty-all</artifactId> | ||
<version>4.1.31.Final</version> | ||
</dependency> | ||
|
||
<!-- Dependency for Spring Context --> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-context-support</artifactId> | ||
<version>5.1.5.RELEASE</version> | ||
</dependency> | ||
|
||
<!-- Sentinel adapter and transport --> | ||
<dependency> | ||
<groupId>com.alibaba.csp</groupId> | ||
<artifactId>sentinel-apache-dubbo-adapter</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.alibaba.csp</groupId> | ||
<artifactId>sentinel-transport-simple-http</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
</dependencies> | ||
</project> |
54 changes: 54 additions & 0 deletions
54
...-dubbo/src/main/java/com/alibaba/csp/sentinel/demo/apache/dubbo/FooConsumerBootstrap.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,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: | ||
* <pre> | ||
* -Djava.net.preferIPv4Stack=true | ||
* -Dcsp.sentinel.api.port=8721 | ||
* -Dproject.name=dubbo-consumer-demo | ||
* </pre> | ||
* | ||
* @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(); | ||
} | ||
} | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
...-dubbo/src/main/java/com/alibaba/csp/sentinel/demo/apache/dubbo/FooProviderBootstrap.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,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: | ||
* <pre> | ||
* -Djava.net.preferIPv4Stack=true | ||
* -Dcsp.sentinel.api.port=8720 | ||
* -Dproject.name=dubbo-provider-demo | ||
* </pre> | ||
* | ||
* @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)); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...emo-apache-dubbo/src/main/java/com/alibaba/csp/sentinel/demo/apache/dubbo/FooService.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,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(); | ||
} |
58 changes: 58 additions & 0 deletions
58
.../main/java/com/alibaba/csp/sentinel/demo/apache/dubbo/consumer/ConsumerConfiguration.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,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(); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...src/main/java/com/alibaba/csp/sentinel/demo/apache/dubbo/consumer/FooServiceConsumer.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,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(); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
...bbo/src/main/java/com/alibaba/csp/sentinel/demo/apache/dubbo/provider/FooServiceImpl.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,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(); | ||
} | ||
} |
Oops, something went wrong.