alibaba / alibaba/Sentinel

We Used JSR269, Java Annotation Processor to help access sentinel

Open
#1,485 3 comments 1 reaction 0 assignees View on GitHub
kind/feature
Dominant language
Java
Stars
23.1k
Forks
8.1k
PR merge metrics
No merged PRs in 30d

Description

# Issue Description
This is not a bug issue。
we used JSR269, Java Annotation Processor to help access sentinel, just only a maven, n+1 annotation, can easy access Sentinel。

中文:
老铁们好, 我们基于JSR269做了sentinel便捷接入SDK,来自荐了。只需要简单的引入pom, 然后再在main方法上打一个注解, 在需要做熔断限流的N个地方分别打上注解, 就可以开开心心的使用Sentinel了。

实现基于如下三步:
1. 编译植入, 因为Sentinel的代码很格式化, 因此很容易实现编译植入, 就像`@YuAnCallback`下面这样:
原始代码:
```
@YuAnCallback
public void helloWorldWillBeLimited(Integer xxx) {
System.out.println(sdf.format(new Date()) + "\t" + xxx);
}
```
编译后的代码:
```
// 原始方法全体被重命名了一个后缀 _sentinel
public void helloWorldWillBeLimited_sentinel(Integer xxx) {
System.out.println(this.sdf.format(new Date()) + "\t" + xxx);
}
// 照着sentinel的规范重新copy了一份。 注解什么的全保留在新方法上了↓↓↓。
public void helloWorldWillBeLimited(Integer xxx) {
if (SentinelOpener.shouldOpen()) {
Entry entry = null;
String resource_key = "HelloWorldLimitDemo.helloWorldWillBeLimited";

try {
entry = SphU.entry(resource_key);
this.helloWorldWillBeLimited_sentinel(xxx);
} catch (Throwable var11) {
Throwable throwTheExp = var11;
if (!BlockException.isBlockException(var11)) {
Tracer.trace(var11);

try {
throw throwTheExp;
} catch (BlockException var10) {
}
}

OnlyThrow.th(var11);
} finally {
if (null != entry) {
entry.exit();
}

}
} else {
this.helloWorldWillBeLimited_sentinel(xxx);
}

}
```
2. main方法初始化
也是编译植入, 在普通Java main 方法和Spring、Spring Boot环境中的处理有差异。
```
@EnableYuan
public class YuanDemo {

public static void main(String[] args) {
SpringApplication.run(X.class);
}
}
```
`@EnableYuan`这个注解会在main方法的的第一行插入Sentinel初始化的语句, 处理各项配置依赖。
3. 用法就和Sentinel推荐的用法无差别了。 只是做了个编译植入。
这样的好处就是接入简单, 代码超低耦合。

很期望能对开源世界做一些贡献。 如果有兴趣的话, 欢迎联系我❤

Contributor guide

Open the contributing guide

Research direction

The issue describes an external JSR269 annotation processor using @YuAnCallback and @EnableYuan around Java main methods, with Spring and Spring Boot handling mentioned. Start by comparing those integration examples with Sentinel's existing entry points and documentation; a maintainer would first need to define whether this is an accepted feature, an integration, or an external project reference.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring, spring-boot
Domain
backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
15/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.