Ver código fonte

Add demo module for integration with Apache Dubbo 2.7.x and above version

Signed-off-by: Eric Zhao <sczyh16@gmail.com>
master
Eric Zhao Carpenter Lee 5 anos atrás
pai
commit
f59b134b3c
10 arquivos alterados com 398 adições e 0 exclusões
  1. +1
    -0
      sentinel-demo/pom.xml
  2. +18
    -0
      sentinel-demo/sentinel-demo-apache-dubbo/README.md
  3. +49
    -0
      sentinel-demo/sentinel-demo-apache-dubbo/pom.xml
  4. +54
    -0
      sentinel-demo/sentinel-demo-apache-dubbo/src/main/java/com/alibaba/csp/sentinel/demo/apache/dubbo/FooConsumerBootstrap.java
  5. +63
    -0
      sentinel-demo/sentinel-demo-apache-dubbo/src/main/java/com/alibaba/csp/sentinel/demo/apache/dubbo/FooProviderBootstrap.java
  6. +26
    -0
      sentinel-demo/sentinel-demo-apache-dubbo/src/main/java/com/alibaba/csp/sentinel/demo/apache/dubbo/FooService.java
  7. +58
    -0
      sentinel-demo/sentinel-demo-apache-dubbo/src/main/java/com/alibaba/csp/sentinel/demo/apache/dubbo/consumer/ConsumerConfiguration.java
  8. +37
    -0
      sentinel-demo/sentinel-demo-apache-dubbo/src/main/java/com/alibaba/csp/sentinel/demo/apache/dubbo/consumer/FooServiceConsumer.java
  9. +39
    -0
      sentinel-demo/sentinel-demo-apache-dubbo/src/main/java/com/alibaba/csp/sentinel/demo/apache/dubbo/provider/FooServiceImpl.java
  10. +53
    -0
      sentinel-demo/sentinel-demo-apache-dubbo/src/main/java/com/alibaba/csp/sentinel/demo/apache/dubbo/provider/ProviderConfiguration.java

+ 1
- 0
sentinel-demo/pom.xml Ver arquivo

@@ -31,6 +31,7 @@
<module>sentinel-demo-cluster</module>
<module>sentinel-demo-command-handler</module>
<module>sentinel-demo-spring-webflux</module>
<module>sentinel-demo-apache-dubbo</module>
</modules>

<dependencies>


+ 18
- 0
sentinel-demo/sentinel-demo-apache-dubbo/README.md Ver arquivo

@@ -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
```

+ 49
- 0
sentinel-demo/sentinel-demo-apache-dubbo/pom.xml Ver arquivo

@@ -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
- 0
sentinel-demo/sentinel-demo-apache-dubbo/src/main/java/com/alibaba/csp/sentinel/demo/apache/dubbo/FooConsumerBootstrap.java Ver arquivo

@@ -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
- 0
sentinel-demo/sentinel-demo-apache-dubbo/src/main/java/com/alibaba/csp/sentinel/demo/apache/dubbo/FooProviderBootstrap.java Ver arquivo

@@ -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
- 0
sentinel-demo/sentinel-demo-apache-dubbo/src/main/java/com/alibaba/csp/sentinel/demo/apache/dubbo/FooService.java Ver arquivo

@@ -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
- 0
sentinel-demo/sentinel-demo-apache-dubbo/src/main/java/com/alibaba/csp/sentinel/demo/apache/dubbo/consumer/ConsumerConfiguration.java Ver arquivo

@@ -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
- 0
sentinel-demo/sentinel-demo-apache-dubbo/src/main/java/com/alibaba/csp/sentinel/demo/apache/dubbo/consumer/FooServiceConsumer.java Ver arquivo

@@ -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
- 0
sentinel-demo/sentinel-demo-apache-dubbo/src/main/java/com/alibaba/csp/sentinel/demo/apache/dubbo/provider/FooServiceImpl.java Ver arquivo

@@ -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();
}
}

+ 53
- 0
sentinel-demo/sentinel-demo-apache-dubbo/src/main/java/com/alibaba/csp/sentinel/demo/apache/dubbo/provider/ProviderConfiguration.java Ver arquivo

@@ -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;
}
}

Carregando…
Cancelar
Salvar