init
This commit is contained in:
42
sentinel/sentinel-demo/sentinel-demo-sofa-rpc/README.md
Normal file
42
sentinel/sentinel-demo/sentinel-demo-sofa-rpc/README.md
Normal file
@@ -0,0 +1,42 @@
|
||||
# Sentinel SOFARPC Demo
|
||||
|
||||
Sentinel 提供了与 SOFARPC 整合的模块 - `sentinel-sofa-rpc-adapter`,主要包括针对 Service Provider 和 Service Consumer 实现的 Filter。使用时用户只需引入以下模块(以 Maven 为例):
|
||||
|
||||
```xml
|
||||
<dependency>
|
||||
<groupId>com.alibaba.csp</groupId>
|
||||
<artifactId>sentinel-sofa-rpc-adapter</artifactId>
|
||||
<version>x.y.z</version>
|
||||
</dependency>
|
||||
```
|
||||
|
||||
引入此依赖后,SOFARPC 的服务接口和方法(包括调用端和服务端)就会成为 Sentinel 中的资源,在配置了规则后就可以自动享受到 Sentinel 的防护能力。
|
||||
|
||||
> **注:若希望接入 Dashboard,请参考 demo 中的注释添加启动参数。只引入 `sentinel-sofa-rpc-adapter` 依赖无法接入控制台!**
|
||||
|
||||
若不希望开启 Sentinel SOFARPC Adapter 中的某个 Filter,可以手动关闭对应的 Filter,比如:
|
||||
|
||||
```java
|
||||
providerConfig.setParameter("sofa.rpc.sentinel.enabled", "false");
|
||||
consumerConfig.setParameter("sofa.rpc.sentinel.enabled", "false");
|
||||
```
|
||||
|
||||
或者在 `rpc-config.json` 文件中设置,它的优先级要低一些。
|
||||
|
||||
```json
|
||||
{
|
||||
"sofa.rpc.sentinel.enabled": true
|
||||
}
|
||||
```
|
||||
|
||||
## 运行 Demo
|
||||
|
||||
1. 启动控制台,运行 `DashboardApplication`
|
||||
|
||||
2. 启动 Provider,运行 `DemoProvider`(VM参数:`-Dproject.name=DemoProvider -Dcsp.sentinel.dashboard.server=localhost:8080`)
|
||||
|
||||
3. 启动 Consumer,运行 `DemoConsumer`(VM参数:`-Dproject.name=DemoConsumer -Dcsp.sentinel.dashboard.server=localhost:8080`)
|
||||
|
||||
通过控制台实时监控、簇点链路菜单观察接口调用、资源情况;对资源设置不同流控规则,进行观察和调试。
|
||||
|
||||
参考:[Sentinel 控制台文档](https://github.com/alibaba/Sentinel/wiki/控制台).
|
||||
44
sentinel/sentinel-demo/sentinel-demo-sofa-rpc/pom.xml
Normal file
44
sentinel/sentinel-demo/sentinel-demo-sofa-rpc/pom.xml
Normal file
@@ -0,0 +1,44 @@
|
||||
<?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.8.3</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>sentinel-demo-sofa-rpc</artifactId>
|
||||
|
||||
<properties>
|
||||
<sofa-rpc-all.version>5.6.4</sofa-rpc-all.version>
|
||||
<slf4j-log4j12.version>1.7.21</slf4j-log4j12.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.csp</groupId>
|
||||
<artifactId>sentinel-sofa-rpc-adapter</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.alibaba.csp</groupId>
|
||||
<artifactId>sentinel-transport-simple-http</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.alipay.sofa</groupId>
|
||||
<artifactId>sofa-rpc-all</artifactId>
|
||||
<version>${sofa-rpc-all.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-log4j12</artifactId>
|
||||
<version>${slf4j-log4j12.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
* 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.sofa.rpc;
|
||||
|
||||
import com.alibaba.csp.sentinel.demo.sofa.rpc.service.DemoService;
|
||||
import com.alipay.sofa.rpc.common.RpcConstants;
|
||||
import com.alipay.sofa.rpc.config.ApplicationConfig;
|
||||
import com.alipay.sofa.rpc.config.ConsumerConfig;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* Demo consumer of SOFARPC.
|
||||
*
|
||||
* Interact with Sentinel Dashboard, add the following VM arguments:
|
||||
* <pre>
|
||||
* -Dproject.name=DemoProvider -Dcsp.sentinel.dashboard.server=localhost:8080
|
||||
* </pre>
|
||||
*
|
||||
* @author cdfive
|
||||
*/
|
||||
public class DemoConsumer {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
ApplicationConfig application = new ApplicationConfig().setAppName("DemoConsumer");
|
||||
|
||||
ConsumerConfig<DemoService> consumerConfig = new ConsumerConfig<DemoService>()
|
||||
.setApplication(application)
|
||||
.setInterfaceId(DemoService.class.getName())
|
||||
.setProtocol("bolt")
|
||||
.setDirectUrl("bolt://127.0.0.1:12001")
|
||||
.setInvokeType(RpcConstants.INVOKER_TYPE_SYNC);
|
||||
|
||||
// 设置是否启用Sentinel,默认启用
|
||||
// 也可在rpc-config.json全局设置
|
||||
// consumerConfig.setParameter("sofa.rpc.sentinel.enabled", "false");
|
||||
|
||||
DemoService helloService = consumerConfig.refer();
|
||||
|
||||
System.out.println("DemoConsumer started!");
|
||||
|
||||
long sleepMs = 5;
|
||||
int total = 5000;
|
||||
int index = 0;
|
||||
System.out.println("Total call " + total + " times and sleep " + sleepMs + "ms after each call.");
|
||||
|
||||
while (true) {
|
||||
try {
|
||||
index++;
|
||||
String result = helloService.sayHello(index, "SOFARPC", 2020);
|
||||
System.out.println("[" + index + "][Consumer]receive response: " + result);
|
||||
} catch (Exception e) {
|
||||
System.out.println("[" + index + "][Consumer]receive exception: " + e.getMessage());
|
||||
}
|
||||
|
||||
TimeUnit.MILLISECONDS.sleep(sleepMs);
|
||||
|
||||
if (index == total) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("DemoConsumer exit!");
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
||||
@@ -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.sofa.rpc;
|
||||
|
||||
import com.alibaba.csp.sentinel.demo.sofa.rpc.service.DemoService;
|
||||
import com.alibaba.csp.sentinel.demo.sofa.rpc.service.impl.DemoServiceImpl;
|
||||
import com.alipay.sofa.rpc.config.ProviderConfig;
|
||||
import com.alipay.sofa.rpc.config.ServerConfig;
|
||||
|
||||
/**
|
||||
* Demo provider of SOFARPC
|
||||
*
|
||||
* Interact with Sentinel Dashboard, add the following VM arguments:
|
||||
* <pre>
|
||||
* -Dproject.name=DemoProvider -Dcsp.sentinel.dashboard.server=localhost:8080
|
||||
* </pre>
|
||||
*
|
||||
* @author cdfive
|
||||
*/
|
||||
public class DemoProvider {
|
||||
|
||||
public static void main(String[] args) {
|
||||
ServerConfig serverConfig = new ServerConfig()
|
||||
.setProtocol("bolt")
|
||||
.setPort(12001)
|
||||
.setDaemon(false);
|
||||
|
||||
ProviderConfig<DemoService> providerConfig = new ProviderConfig<DemoService>()
|
||||
.setInterfaceId(DemoService.class.getName())
|
||||
.setRef(new DemoServiceImpl())
|
||||
.setServer(serverConfig);
|
||||
|
||||
// 设置是否启用Sentinel,默认启用
|
||||
// 也可在rpc-config.json全局设置
|
||||
// providerConfig.setParameter("sofa.rpc.sentinel.enabled", "false");
|
||||
|
||||
providerConfig.export();
|
||||
|
||||
System.out.println("DemoProvider started!");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* 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.sofa.rpc.service;
|
||||
|
||||
/**
|
||||
* @author cdfive
|
||||
*/
|
||||
public interface DemoService {
|
||||
|
||||
String sayHello(Integer index, String name, int year);
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright 1999-2018 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.csp.sentinel.demo.sofa.rpc.service.impl;
|
||||
|
||||
import com.alibaba.csp.sentinel.demo.sofa.rpc.service.DemoService;
|
||||
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* @author cdfive
|
||||
*/
|
||||
public class DemoServiceImpl implements DemoService {
|
||||
|
||||
@Override
|
||||
public String sayHello(Integer index, String name, int year) {
|
||||
System.out.println("[" + index + "][Provider]receive request: " + name + "," + year);
|
||||
|
||||
int sleepMs = ThreadLocalRandom.current().nextInt(50);
|
||||
try {
|
||||
TimeUnit.MILLISECONDS.sleep(sleepMs);
|
||||
} catch (InterruptedException e) {
|
||||
System.err.println(e.getMessage());
|
||||
}
|
||||
|
||||
return "Hello " + name + " " + year + "[" + sleepMs + "ms]";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
|
||||
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
|
||||
|
||||
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
|
||||
<layout class="org.apache.log4j.PatternLayout">
|
||||
<param name="ConversionPattern" value="%d %t %5p [%c:%M:%L] - %m%n"/>
|
||||
</layout>
|
||||
</appender>
|
||||
|
||||
<root>
|
||||
<level value="INFO"/>
|
||||
<appender-ref ref="CONSOLE"/>
|
||||
</root>
|
||||
|
||||
</log4j:configuration>
|
||||
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"rpc.config.order": 999,
|
||||
"logger.impl": "com.alipay.sofa.rpc.log.SLF4JLoggerImpl",
|
||||
// 是否启用Sentinel,不设置默认为true
|
||||
"sofa.rpc.sentinel.enabled": true
|
||||
}
|
||||
Reference in New Issue
Block a user