forked from alibaba/Sentinel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Apollo data source extension (alibaba#46)
- Loading branch information
Showing
9 changed files
with
422 additions
and
3 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
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 @@ | ||
<?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>0.1.1-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>sentinel-demo-apollo-datasource</artifactId> | ||
|
||
<properties> | ||
<java.source.version>1.8</java.source.version> | ||
<java.target.version>1.8</java.target.version> | ||
<log4j2.version>2.9.1</log4j2.version> | ||
<slf4j.version>1.7.25</slf4j.version> | ||
</properties> | ||
|
||
<dependencyManagement> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-api</artifactId> | ||
<version>${slf4j.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.logging.log4j</groupId> | ||
<artifactId>log4j-core</artifactId> | ||
<version>${log4j2.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.logging.log4j</groupId> | ||
<artifactId>log4j-api</artifactId> | ||
<version>${log4j2.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.logging.log4j</groupId> | ||
<artifactId>log4j-slf4j-impl</artifactId> | ||
<version>${log4j2.version}</version> | ||
</dependency> | ||
</dependencies> | ||
</dependencyManagement> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.alibaba.csp</groupId> | ||
<artifactId>sentinel-datasource-apollo</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.alibaba</groupId> | ||
<artifactId>fastjson</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.apache.logging.log4j</groupId> | ||
<artifactId>log4j-slf4j-impl</artifactId> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
70 changes: 70 additions & 0 deletions
70
...e/src/main/java/com/alibaba/csp/sentinel/demo/datasource/apollo/ApolloDataSourceDemo.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,70 @@ | ||
package com.alibaba.csp.sentinel.demo.datasource.apollo; | ||
|
||
import com.alibaba.csp.sentinel.datasource.DataSource; | ||
import com.alibaba.csp.sentinel.datasource.apollo.ApolloDataSource; | ||
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule; | ||
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager; | ||
import com.alibaba.fastjson.JSON; | ||
import com.alibaba.fastjson.TypeReference; | ||
import java.util.List; | ||
|
||
/** | ||
* This demo shows how to use Apollo as the data source of Sentinel rules. | ||
* <br /> | ||
* You need to first set up data as follows: | ||
* <ol> | ||
* <li>Create an application with app id as sentinel-demo in Apollo</li> | ||
* <li> | ||
* Create a configuration with key as flowRules and value as follows: | ||
* <pre> | ||
* [ | ||
{ | ||
"resource": "TestResource", | ||
"controlBehavior": 0, | ||
"count": 5.0, | ||
"grade": 1, | ||
"limitApp": "default", | ||
"strategy": 0 | ||
} | ||
] | ||
* </pre> | ||
* </li> | ||
* <li>Publish the application namespace</li> | ||
* </ol> | ||
* Then you could start this demo and adjust the rule configuration as you wish. | ||
* The rule changes will take effect in real time. | ||
* | ||
* @author Jason Song | ||
*/ | ||
public class ApolloDataSourceDemo { | ||
private static final String KEY = "TestResource"; | ||
|
||
public static void main(String[] args) { | ||
loadRules(); | ||
// Assume we config: resource is `TestResource`, initial QPS threshold is 5. | ||
FlowQpsRunner runner = new FlowQpsRunner(KEY, 1, 100); | ||
runner.simulateTraffic(); | ||
runner.tick(); | ||
} | ||
|
||
private static void loadRules() { | ||
/** | ||
* Set up basic information, only for demo purpose. You may adjust them based on your actual environment. | ||
* <br /> | ||
* For more information, please refer https://github.com/ctripcorp/apollo | ||
*/ | ||
String appId = "sentinel-demo"; | ||
String apolloMetaServerAddress = "http://localhost:8080"; | ||
System.setProperty("app.id", appId); | ||
System.setProperty("apollo.meta", apolloMetaServerAddress); | ||
|
||
String namespaceName = "application"; | ||
String flowRuleKey = "flowRules"; | ||
String defaultFlowRules = "[]"; //it's better to provide a meaningful default value | ||
|
||
DataSource<String, List<FlowRule>> flowRuleDataSource = new ApolloDataSource<>(namespaceName, flowRuleKey, | ||
defaultFlowRules, source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() { | ||
})); | ||
FlowRuleManager.register2Property(flowRuleDataSource.getProperty()); | ||
} | ||
} |
139 changes: 139 additions & 0 deletions
139
...tasource/src/main/java/com/alibaba/csp/sentinel/demo/datasource/apollo/FlowQpsRunner.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,139 @@ | ||
/* | ||
* 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.datasource.apollo; | ||
|
||
import java.util.Random; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import com.alibaba.csp.sentinel.Entry; | ||
import com.alibaba.csp.sentinel.SphU; | ||
import com.alibaba.csp.sentinel.slots.block.BlockException; | ||
import com.alibaba.csp.sentinel.util.TimeUtil; | ||
|
||
/** | ||
* Flow QPS runner. | ||
* | ||
* @author Carpenter Lee | ||
* @author Eric Zhao | ||
*/ | ||
class FlowQpsRunner { | ||
|
||
private final String resourceName; | ||
private final int threadCount; | ||
private int seconds; | ||
|
||
public FlowQpsRunner(String resourceName, int threadCount, int seconds) { | ||
this.resourceName = resourceName; | ||
this.threadCount = threadCount; | ||
this.seconds = seconds; | ||
} | ||
|
||
private final AtomicInteger pass = new AtomicInteger(); | ||
private final AtomicInteger block = new AtomicInteger(); | ||
private final AtomicInteger total = new AtomicInteger(); | ||
|
||
private volatile boolean stop = false; | ||
|
||
public void simulateTraffic() { | ||
for (int i = 0; i < threadCount; i++) { | ||
Thread t = new Thread(new RunTask()); | ||
t.setName("simulate-traffic-Task"); | ||
t.start(); | ||
} | ||
} | ||
|
||
public void tick() { | ||
Thread timer = new Thread(new TimerTask()); | ||
timer.setName("sentinel-timer-task"); | ||
timer.start(); | ||
} | ||
|
||
final class RunTask implements Runnable { | ||
@Override | ||
public void run() { | ||
while (!stop) { | ||
Entry entry = null; | ||
|
||
try { | ||
entry = SphU.entry(resourceName); | ||
// token acquired, means pass | ||
pass.addAndGet(1); | ||
} catch (BlockException e1) { | ||
block.incrementAndGet(); | ||
} catch (Exception e2) { | ||
// biz exception | ||
} finally { | ||
total.incrementAndGet(); | ||
if (entry != null) { | ||
entry.exit(); | ||
} | ||
} | ||
|
||
Random random2 = new Random(); | ||
try { | ||
TimeUnit.MILLISECONDS.sleep(random2.nextInt(50)); | ||
} catch (InterruptedException e) { | ||
// ignore | ||
} | ||
} | ||
} | ||
} | ||
|
||
final class TimerTask implements Runnable { | ||
@Override | ||
public void run() { | ||
long start = System.currentTimeMillis(); | ||
System.out.println("begin to statistic!!!"); | ||
|
||
long oldTotal = 0; | ||
long oldPass = 0; | ||
long oldBlock = 0; | ||
while (!stop) { | ||
try { | ||
TimeUnit.SECONDS.sleep(1); | ||
} catch (InterruptedException e) { | ||
} | ||
long globalTotal = total.get(); | ||
long oneSecondTotal = globalTotal - oldTotal; | ||
oldTotal = globalTotal; | ||
|
||
long globalPass = pass.get(); | ||
long oneSecondPass = globalPass - oldPass; | ||
oldPass = globalPass; | ||
|
||
long globalBlock = block.get(); | ||
long oneSecondBlock = globalBlock - oldBlock; | ||
oldBlock = globalBlock; | ||
|
||
System.out.println(seconds + " send qps is: " + oneSecondTotal); | ||
System.out.println(TimeUtil.currentTimeMillis() + ", total:" + oneSecondTotal | ||
+ ", pass:" + oneSecondPass | ||
+ ", block:" + oneSecondBlock); | ||
|
||
if (seconds-- <= 0) { | ||
stop = true; | ||
} | ||
} | ||
|
||
long cost = System.currentTimeMillis() - start; | ||
System.out.println("time cost: " + cost + " ms"); | ||
System.out.println("total:" + total.get() + ", pass:" + pass.get() | ||
+ ", block:" + block.get()); | ||
System.exit(0); | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
sentinel-demo/sentinel-demo-apollo-datasource/src/main/resources/log4j2.xml
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,16 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<configuration monitorInterval="60"> | ||
<appenders> | ||
<Console name="Console" target="SYSTEM_OUT"> | ||
<PatternLayout pattern="[apollo-datasource-demo][%t]%d %-5p [%c] %m%n"/> | ||
</Console> | ||
<Async name="Async" includeLocation="true"> | ||
<AppenderRef ref="Console"/> | ||
</Async> | ||
</appenders> | ||
<loggers> | ||
<root level="INFO"> | ||
<AppenderRef ref="Async"/> | ||
</root> | ||
</loggers> | ||
</configuration> |
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,31 @@ | ||
<?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-extension</artifactId> | ||
<groupId>com.alibaba.csp</groupId> | ||
<version>0.1.1-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>sentinel-datasource-apollo</artifactId> | ||
|
||
<properties> | ||
<apollo.version>1.0.0</apollo.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.alibaba.csp</groupId> | ||
<artifactId>sentinel-datasource-extension</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.ctrip.framework.apollo</groupId> | ||
<artifactId>apollo-client</artifactId> | ||
<version>${apollo.version}</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
Oops, something went wrong.