Skip to content

Commit 0e74551

Browse files
committed
feat(sentinel): add initial implementation of sentinel example with HTTP transport
1 parent ce729ea commit 0e74551

File tree

12 files changed

+316
-41
lines changed

12 files changed

+316
-41
lines changed

.github/workflows/maven-build.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: Maven Build
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
8+
jobs:
9+
build:
10+
name: Maven Build
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- name: Checkout Code
15+
uses: actions/checkout@v3
16+
17+
- name: Set Up JDK 21
18+
uses: actions/setup-java@v3
19+
with:
20+
java-version: '21'
21+
distribution: 'temurin'
22+
cache: 'maven'
23+
24+
- name: Build All Modules
25+
run: |
26+
mvn -B clean install \
27+
-pl .,grpc-spring,pattern-design,rocketmq \
28+
-am

.github/workflows/maven.yml

Lines changed: 0 additions & 40 deletions
This file was deleted.

build/.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
**/data/
2-
**/log/
2+
**/log/
3+
4+
.flattened-pom.xml
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.helltractor.demo;
2+
3+
4+
public class Printer{
5+
private int count = 0;
6+
private final int maxCount = 200;
7+
private final Object lock = new Object();
8+
private boolean isOdd = false;
9+
10+
public static void main(String[] args) {
11+
Printer printer = new Printer();
12+
13+
Thread oddThread = new Thread(() -> {
14+
printer.print("Thread Odd", true);
15+
});
16+
Thread evenThread = new Thread(() -> {
17+
printer.print("Thread Even", false);
18+
});
19+
20+
oddThread.start();
21+
evenThread.start();
22+
}
23+
24+
public void print(String name, boolean isOdd) {
25+
synchronized (lock) {
26+
while (count < maxCount) {
27+
if (this.isOdd != isOdd) {
28+
try {
29+
lock.wait();
30+
} catch (InterruptedException e) {
31+
Thread.currentThread().interrupt();
32+
}
33+
}
34+
System.out.println(name + ": " + count);
35+
count++;
36+
this.isOdd = !this.isOdd;
37+
lock.notifyAll();
38+
}
39+
}
40+
}
41+
}

sentinel/.gitignore

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
HELP.md
2+
target/
3+
!.mvn/wrapper/maven-wrapper.jar
4+
!**/src/main/**/target/
5+
!**/src/test/**/target/
6+
7+
### STS ###
8+
.apt_generated
9+
.classpath
10+
.factorypath
11+
.project
12+
.settings
13+
.springBeans
14+
.sts4-cache
15+
16+
### IntelliJ IDEA ###
17+
.idea
18+
*.iws
19+
*.iml
20+
*.ipr
21+
22+
### NetBeans ###
23+
/nbproject/private/
24+
/nbbuild/
25+
/dist/
26+
/nbdist/
27+
/.nb-gradle/
28+
build/
29+
!**/src/main/**/build/
30+
!**/src/test/**/build/
31+
32+
### VS Code ###
33+
.vscode/

sentinel/pom.xml

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<parent>
7+
<groupId>org.springframework.boot</groupId>
8+
<artifactId>spring-boot-starter-parent</artifactId>
9+
<version>3.2.8</version>
10+
<relativePath/>
11+
</parent>
12+
13+
<groupId>com.helltractor.demo</groupId>
14+
<artifactId>sentinel</artifactId>
15+
<version>${revision}</version>
16+
<name>sentinel</name>
17+
<packaging>pom</packaging>
18+
<description>Sentinel Example</description>
19+
20+
<modules>
21+
<module>sentinel-simple-http</module>
22+
</modules>
23+
24+
<properties>
25+
<revision>1.0-SNAPSHOT</revision>
26+
<java.version>17</java.version>
27+
<maven.compiler.source>${java.version}</maven.compiler.source>
28+
<maven.compiler.target>${java.version}</maven.compiler.target>
29+
<spring-boot.version>3.2.8</spring-boot.version>
30+
<spring-cloud.version>2023.0.3</spring-cloud.version>
31+
<alibaba-cloud.version>2023.0.1.3</alibaba-cloud.version>
32+
<alibaba-csp.version>1.8.8</alibaba-csp.version>
33+
</properties>
34+
35+
<dependencyManagement>
36+
<dependencies>
37+
<dependency>
38+
<groupId>org.springframework.boot</groupId>
39+
<artifactId>spring-boot-dependencies</artifactId>
40+
<version>${spring-boot.version}</version>
41+
</dependency>
42+
<dependency>
43+
<groupId>org.springframework.cloud</groupId>
44+
<artifactId>spring-cloud-dependencies</artifactId>
45+
<version>${spring-cloud.version}</version>
46+
</dependency>
47+
<dependency>
48+
<groupId>com.alibaba.cloud</groupId>
49+
<artifactId>spring-cloud-alibaba</artifactId>
50+
<version>${alibaba-cloud.version}</version>
51+
</dependency>
52+
</dependencies>
53+
</dependencyManagement>
54+
55+
<build>
56+
<plugins>
57+
<plugin>
58+
<groupId>org.springframework.boot</groupId>
59+
<artifactId>spring-boot-maven-plugin</artifactId>
60+
</plugin>
61+
</plugins>
62+
</build>
63+
64+
</project>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="http://maven.apache.org/POM/4.0.0"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<parent>
8+
<groupId>com.helltractor.demo</groupId>
9+
<artifactId>sentinel</artifactId>
10+
<version>${revision}</version>
11+
<relativePath>../pom.xml</relativePath>
12+
</parent>
13+
14+
<artifactId>sentinel-simple-http</artifactId>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>org.springframework.boot</groupId>
19+
<artifactId>spring-boot-starter</artifactId>
20+
</dependency>
21+
<dependency>
22+
<groupId>org.springframework.boot</groupId>
23+
<artifactId>spring-boot-starter-web</artifactId>
24+
</dependency>
25+
<dependency>
26+
<groupId>org.springframework.boot</groupId>
27+
<artifactId>spring-boot-starter-actuator</artifactId>
28+
</dependency>
29+
30+
<!-- <dependency>-->
31+
<!-- <groupId>com.alibaba.cloud</groupId>-->
32+
<!-- <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>-->
33+
<!-- <version>${alibaba-cloud.version}</version>-->
34+
<!-- </dependency>-->
35+
36+
<dependency>
37+
<groupId>com.alibaba.csp</groupId>
38+
<artifactId>sentinel-core</artifactId>
39+
<version>${alibaba-csp.version}</version>
40+
<scope>compile</scope>
41+
</dependency>
42+
<dependency>
43+
<groupId>com.alibaba.csp</groupId>
44+
<artifactId>sentinel-transport-simple-http</artifactId>
45+
<version>${alibaba-csp.version}</version>
46+
</dependency>
47+
</dependencies>
48+
49+
<build>
50+
<plugins>
51+
<plugin>
52+
<groupId>org.springframework.boot</groupId>
53+
<artifactId>spring-boot-maven-plugin</artifactId>
54+
</plugin>
55+
</plugins>
56+
</build>
57+
58+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.helltractor.demo;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class SentinelApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(SentinelApplication.class, args);
11+
}
12+
13+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.helltractor.demo.config;
2+
3+
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
4+
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
5+
import jakarta.annotation.PostConstruct;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
import org.springframework.stereotype.Component;
9+
10+
import java.util.ArrayList;
11+
import java.util.List;
12+
13+
@Component
14+
public class SentinelRuleConfiguration {
15+
16+
private static final Logger logger = LoggerFactory.getLogger(SentinelRuleConfiguration.class);
17+
18+
@PostConstruct
19+
public void init() {
20+
logger.info("Load Sentinel Rules start!");
21+
// TODO: Load Sentinel Rules
22+
List<FlowRule> flowRules = new ArrayList<FlowRule>();
23+
FlowRule flowRule = new FlowRule();
24+
flowRule.setResource("GET:https://localhost:18080/hello");
25+
flowRule.setCount(1);
26+
flowRules.add(flowRule);
27+
FlowRuleManager.loadRules(flowRules);
28+
logger.info("Load Sentinel Rules end!");
29+
}
30+
31+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.helltractor.demo.controller;
2+
3+
import com.helltractor.demo.service.HelloService;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.PathVariable;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
@RestController
10+
public class HelloController {
11+
12+
@Autowired
13+
private HelloService service;
14+
15+
@GetMapping(value = "/hello/{name}")
16+
public String apiHello(@PathVariable("name") String name) {
17+
return service.sayHello(name);
18+
}
19+
20+
}

0 commit comments

Comments
 (0)