mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
Merge branch 'master' into master
This commit is contained in:
commit
9134654964
2
pom.xml
2
pom.xml
@ -93,7 +93,7 @@
|
|||||||
<module>spring-cloud-alibaba-dependencies</module>
|
<module>spring-cloud-alibaba-dependencies</module>
|
||||||
<module>spring-cloud-alibaba-sentinel</module>
|
<module>spring-cloud-alibaba-sentinel</module>
|
||||||
<module>spring-cloud-alibaba-sentinel-datasource</module>
|
<module>spring-cloud-alibaba-sentinel-datasource</module>
|
||||||
<module>spring-cloud-alibaba-sentinel-zuul</module>
|
<module>spring-cloud-alibaba-sentinel-gateway</module>
|
||||||
<module>spring-cloud-alibaba-nacos-config</module>
|
<module>spring-cloud-alibaba-nacos-config</module>
|
||||||
<module>spring-cloud-alibaba-nacos-discovery</module>
|
<module>spring-cloud-alibaba-nacos-discovery</module>
|
||||||
<module>spring-cloud-alibaba-seata</module>
|
<module>spring-cloud-alibaba-seata</module>
|
||||||
|
@ -31,7 +31,7 @@
|
|||||||
<schedulerX.client.version>2.1.6</schedulerX.client.version>
|
<schedulerX.client.version>2.1.6</schedulerX.client.version>
|
||||||
<dubbo.version>2.7.1</dubbo.version>
|
<dubbo.version>2.7.1</dubbo.version>
|
||||||
<dubbo-spring-boot.version>2.7.1</dubbo-spring-boot.version>
|
<dubbo-spring-boot.version>2.7.1</dubbo-spring-boot.version>
|
||||||
<dubbo-registry-nacos.version>0.0.2</dubbo-registry-nacos.version>
|
<dubbo-registry-nacos.version>2.7.1</dubbo-registry-nacos.version>
|
||||||
<aliyun.java.sdk.dysmsapi>1.1.0</aliyun.java.sdk.dysmsapi>
|
<aliyun.java.sdk.dysmsapi>1.1.0</aliyun.java.sdk.dysmsapi>
|
||||||
<aliyun.sdk.mns>1.1.8.6</aliyun.sdk.mns>
|
<aliyun.sdk.mns>1.1.8.6</aliyun.sdk.mns>
|
||||||
<aliyun.java.sdk.dyvmsapi>1.1.1</aliyun.java.sdk.dyvmsapi>
|
<aliyun.java.sdk.dyvmsapi>1.1.1</aliyun.java.sdk.dyvmsapi>
|
||||||
@ -152,6 +152,11 @@
|
|||||||
<artifactId>sentinel-zuul-adapter</artifactId>
|
<artifactId>sentinel-zuul-adapter</artifactId>
|
||||||
<version>${sentinel.version}</version>
|
<version>${sentinel.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.csp</groupId>
|
||||||
|
<artifactId>sentinel-spring-cloud-gateway-adapter</artifactId>
|
||||||
|
<version>${sentinel.version}</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.alibaba.csp</groupId>
|
<groupId>com.alibaba.csp</groupId>
|
||||||
<artifactId>sentinel-transport-simple-http</artifactId>
|
<artifactId>sentinel-transport-simple-http</artifactId>
|
||||||
@ -192,6 +197,11 @@
|
|||||||
<artifactId>sentinel-spring-webflux-adapter</artifactId>
|
<artifactId>sentinel-spring-webflux-adapter</artifactId>
|
||||||
<version>${sentinel.version}</version>
|
<version>${sentinel.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.csp</groupId>
|
||||||
|
<artifactId>sentinel-api-gateway-adapter-common</artifactId>
|
||||||
|
<version>${sentinel.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -233,7 +243,7 @@
|
|||||||
|
|
||||||
<!-- Dubbo Nacos registry dependency -->
|
<!-- Dubbo Nacos registry dependency -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.alibaba</groupId>
|
<groupId>org.apache.dubbo</groupId>
|
||||||
<artifactId>dubbo-registry-nacos</artifactId>
|
<artifactId>dubbo-registry-nacos</artifactId>
|
||||||
<version>${dubbo-registry-nacos.version}</version>
|
<version>${dubbo-registry-nacos.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
@ -258,7 +268,7 @@
|
|||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.cloud</groupId>
|
<groupId>org.springframework.cloud</groupId>
|
||||||
<artifactId>spring-cloud-alibaba-sentinel-zuul</artifactId>
|
<artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
|
||||||
<version>${project.version}</version>
|
<version>${project.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
|
@ -60,6 +60,7 @@ public class DefaultHttpRequest implements HttpRequest {
|
|||||||
return HttpMethod.resolve(getMethodValue());
|
return HttpMethod.resolve(getMethodValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public String getMethodValue() {
|
public String getMethodValue() {
|
||||||
return method;
|
return method;
|
||||||
}
|
}
|
||||||
|
@ -73,6 +73,7 @@ public class MutableHttpServerRequest implements HttpServerRequest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Override method since Spring Framework 5.0
|
// Override method since Spring Framework 5.0
|
||||||
|
@Override
|
||||||
public String getMethodValue() {
|
public String getMethodValue() {
|
||||||
return httpMethod.name();
|
return httpMethod.name();
|
||||||
}
|
}
|
||||||
|
@ -44,8 +44,12 @@ public class DubboRestServiceMetadata {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
if (this == o) return true;
|
if (this == o) {
|
||||||
if (!(o instanceof DubboRestServiceMetadata)) return false;
|
return true;
|
||||||
|
}
|
||||||
|
if (!(o instanceof DubboRestServiceMetadata)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
DubboRestServiceMetadata that = (DubboRestServiceMetadata) o;
|
DubboRestServiceMetadata that = (DubboRestServiceMetadata) o;
|
||||||
return Objects.equals(serviceRestMetadata, that.serviceRestMetadata) &&
|
return Objects.equals(serviceRestMetadata, that.serviceRestMetadata) &&
|
||||||
Objects.equals(restMethodMetadata, that.restMethodMetadata);
|
Objects.equals(restMethodMetadata, that.restMethodMetadata);
|
||||||
|
@ -77,8 +77,12 @@ public class DubboTransportedMethodMetadata {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
if (this == o) return true;
|
if (this == o) {
|
||||||
if (!(o instanceof DubboTransportedMethodMetadata)) return false;
|
return true;
|
||||||
|
}
|
||||||
|
if (!(o instanceof DubboTransportedMethodMetadata)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
DubboTransportedMethodMetadata that = (DubboTransportedMethodMetadata) o;
|
DubboTransportedMethodMetadata that = (DubboTransportedMethodMetadata) o;
|
||||||
return Objects.equals(methodMetadata, that.methodMetadata) &&
|
return Objects.equals(methodMetadata, that.methodMetadata) &&
|
||||||
Objects.equals(attributes, that.attributes);
|
Objects.equals(attributes, that.attributes);
|
||||||
|
@ -110,8 +110,12 @@ public class MethodMetadata {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
if (this == o) return true;
|
if (this == o) {
|
||||||
if (o == null || getClass() != o.getClass()) return false;
|
return true;
|
||||||
|
}
|
||||||
|
if (o == null || getClass() != o.getClass()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
MethodMetadata that = (MethodMetadata) o;
|
MethodMetadata that = (MethodMetadata) o;
|
||||||
return Objects.equals(name, that.name) &&
|
return Objects.equals(name, that.name) &&
|
||||||
Objects.equals(returnType, that.returnType) &&
|
Objects.equals(returnType, that.returnType) &&
|
||||||
|
@ -61,8 +61,12 @@ public class MethodParameterMetadata {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
if (this == o) return true;
|
if (this == o) {
|
||||||
if (o == null || getClass() != o.getClass()) return false;
|
return true;
|
||||||
|
}
|
||||||
|
if (o == null || getClass() != o.getClass()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
MethodParameterMetadata that = (MethodParameterMetadata) o;
|
MethodParameterMetadata that = (MethodParameterMetadata) o;
|
||||||
return index == that.index &&
|
return index == that.index &&
|
||||||
Objects.equals(name, that.name) &&
|
Objects.equals(name, that.name) &&
|
||||||
|
@ -235,8 +235,12 @@ public class RequestMetadata {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
if (this == o) return true;
|
if (this == o) {
|
||||||
if (!(o instanceof RequestMetadata)) return false;
|
return true;
|
||||||
|
}
|
||||||
|
if (!(o instanceof RequestMetadata)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
RequestMetadata that = (RequestMetadata) o;
|
RequestMetadata that = (RequestMetadata) o;
|
||||||
return Objects.equals(method, that.method) &&
|
return Objects.equals(method, that.method) &&
|
||||||
Objects.equals(path, that.path) &&
|
Objects.equals(path, that.path) &&
|
||||||
|
@ -183,8 +183,12 @@ public class RestMethodMetadata {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
if (this == o) return true;
|
if (this == o) {
|
||||||
if (!(o instanceof RestMethodMetadata)) return false;
|
return true;
|
||||||
|
}
|
||||||
|
if (!(o instanceof RestMethodMetadata)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
RestMethodMetadata that = (RestMethodMetadata) o;
|
RestMethodMetadata that = (RestMethodMetadata) o;
|
||||||
return queryMapEncoded == that.queryMapEncoded &&
|
return queryMapEncoded == that.queryMapEncoded &&
|
||||||
Objects.equals(method, that.method) &&
|
Objects.equals(method, that.method) &&
|
||||||
|
@ -52,8 +52,12 @@ public class ServiceRestMetadata {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
if (this == o) return true;
|
if (this == o) {
|
||||||
if (!(o instanceof ServiceRestMetadata)) return false;
|
return true;
|
||||||
|
}
|
||||||
|
if (!(o instanceof ServiceRestMetadata)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
ServiceRestMetadata that = (ServiceRestMetadata) o;
|
ServiceRestMetadata that = (ServiceRestMetadata) o;
|
||||||
return Objects.equals(url, that.url) &&
|
return Objects.equals(url, that.url) &&
|
||||||
Objects.equals(meta, that.meta);
|
Objects.equals(meta, that.meta);
|
||||||
|
@ -64,7 +64,7 @@ public abstract class AbstractSpringCloudRegistry extends FailbackRegistry {
|
|||||||
|
|
||||||
protected static final String DUBBO_METADATA_SERVICE_CLASS_NAME = DubboMetadataService.class.getName();
|
protected static final String DUBBO_METADATA_SERVICE_CLASS_NAME = DubboMetadataService.class.getName();
|
||||||
|
|
||||||
private static final Set<String> schedulerTasks = new HashSet<>();
|
private static final Set<String> SCHEDULER_TASKS = new HashSet<>();
|
||||||
|
|
||||||
protected final Logger logger = LoggerFactory.getLogger(getClass());
|
protected final Logger logger = LoggerFactory.getLogger(getClass());
|
||||||
|
|
||||||
@ -164,7 +164,7 @@ public abstract class AbstractSpringCloudRegistry extends FailbackRegistry {
|
|||||||
|
|
||||||
private void submitSchedulerTaskIfAbsent(URL url, NotifyListener listener) {
|
private void submitSchedulerTaskIfAbsent(URL url, NotifyListener listener) {
|
||||||
String taskId = url.toIdentityString();
|
String taskId = url.toIdentityString();
|
||||||
if (schedulerTasks.add(taskId)) {
|
if (SCHEDULER_TASKS.add(taskId)) {
|
||||||
schedule(() -> doSubscribeDubboServiceURLs(url, listener));
|
schedule(() -> doSubscribeDubboServiceURLs(url, listener));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -110,6 +110,7 @@ public class DubboGenericServiceFactory {
|
|||||||
dataBinder.registerCustomEditor(String.class, "listener", new StringTrimmerEditor(true));
|
dataBinder.registerCustomEditor(String.class, "listener", new StringTrimmerEditor(true));
|
||||||
dataBinder.registerCustomEditor(Map.class, "parameters", new PropertyEditorSupport() {
|
dataBinder.registerCustomEditor(Map.class, "parameters", new PropertyEditorSupport() {
|
||||||
|
|
||||||
|
@Override
|
||||||
public void setAsText(String text) throws java.lang.IllegalArgumentException {
|
public void setAsText(String text) throws java.lang.IllegalArgumentException {
|
||||||
// Trim all whitespace
|
// Trim all whitespace
|
||||||
String content = StringUtils.trimAllWhitespace(text);
|
String content = StringUtils.trimAllWhitespace(text);
|
||||||
|
@ -22,6 +22,8 @@
|
|||||||
<module>sentinel-example/sentinel-dubbo-example/sentinel-dubbo-consumer-example</module>
|
<module>sentinel-example/sentinel-dubbo-example/sentinel-dubbo-consumer-example</module>
|
||||||
<module>sentinel-example/sentinel-dubbo-example/sentinel-dubbo-api</module>
|
<module>sentinel-example/sentinel-dubbo-example/sentinel-dubbo-api</module>
|
||||||
<module>sentinel-example/sentinel-webflux-example</module>
|
<module>sentinel-example/sentinel-webflux-example</module>
|
||||||
|
<module>sentinel-example/sentinel-spring-cloud-gateway-example</module>
|
||||||
|
<module>sentinel-example/sentinel-zuul-example</module>
|
||||||
<module>nacos-example/nacos-discovery-example</module>
|
<module>nacos-example/nacos-discovery-example</module>
|
||||||
<module>nacos-example/nacos-config-example</module>
|
<module>nacos-example/nacos-config-example</module>
|
||||||
<module>nacos-example/nacos-gateway-example</module>
|
<module>nacos-example/nacos-gateway-example</module>
|
||||||
|
@ -20,15 +20,35 @@ transport {
|
|||||||
worker-thread-size = 8
|
worker-thread-size = 8
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
## transaction log store
|
||||||
store {
|
store {
|
||||||
# branch session size , if exceeded first try compress lockkey, still exceeded throws exceptions
|
## store mode: file、db
|
||||||
max-branch-session-size = 16384
|
mode = "file"
|
||||||
# globe session size , if exceeded throws exceptions
|
|
||||||
max-global-session-size = 512
|
## file store
|
||||||
# file buffer size , if exceeded allocate new buffer
|
file {
|
||||||
file-write-buffer-cache-size = 16384
|
dir = "sessionStore"
|
||||||
# when recover batch read size
|
|
||||||
session.reload.read_size = 100
|
# branch session size , if exceeded first try compress lockkey, still exceeded throws exceptions
|
||||||
|
max-branch-session-size = 16384
|
||||||
|
# globe session size , if exceeded throws exceptions
|
||||||
|
max-global-session-size = 512
|
||||||
|
# file buffer size , if exceeded allocate new buffer
|
||||||
|
file-write-buffer-cache-size = 16384
|
||||||
|
# when recover batch read size
|
||||||
|
session.reload.read_size = 100
|
||||||
|
# async, sync
|
||||||
|
flush-disk-mode = async
|
||||||
|
}
|
||||||
|
|
||||||
|
## database store
|
||||||
|
db {
|
||||||
|
driver_class = ""
|
||||||
|
url = ""
|
||||||
|
user = ""
|
||||||
|
password = ""
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
service {
|
service {
|
||||||
#vgroup->rgroup
|
#vgroup->rgroup
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
registry {
|
registry {
|
||||||
# file 、nacos 、eureka、redis、zk、consul
|
# file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
|
||||||
type = "file"
|
type = "file"
|
||||||
|
|
||||||
nacos {
|
nacos {
|
||||||
@ -26,6 +26,19 @@ registry {
|
|||||||
cluster = "default"
|
cluster = "default"
|
||||||
serverAddr = "127.0.0.1:8500"
|
serverAddr = "127.0.0.1:8500"
|
||||||
}
|
}
|
||||||
|
etcd3 {
|
||||||
|
cluster = "default"
|
||||||
|
serverAddr = "http://localhost:2379"
|
||||||
|
}
|
||||||
|
sofa {
|
||||||
|
serverAddr = "127.0.0.1:9603"
|
||||||
|
application = "default"
|
||||||
|
region = "DEFAULT_ZONE"
|
||||||
|
datacenter = "DefaultDataCenter"
|
||||||
|
cluster = "default"
|
||||||
|
group = "SEATA_GROUP"
|
||||||
|
addressWaitTime = "3000"
|
||||||
|
}
|
||||||
file {
|
file {
|
||||||
name = "file.conf"
|
name = "file.conf"
|
||||||
}
|
}
|
||||||
@ -41,7 +54,7 @@ config {
|
|||||||
cluster = "default"
|
cluster = "default"
|
||||||
}
|
}
|
||||||
apollo {
|
apollo {
|
||||||
app.id = "fescar-server"
|
app.id = "seata-server"
|
||||||
apollo.meta = "http://192.168.1.204:8801"
|
apollo.meta = "http://192.168.1.204:8801"
|
||||||
}
|
}
|
||||||
zk {
|
zk {
|
||||||
@ -52,4 +65,4 @@ config {
|
|||||||
file {
|
file {
|
||||||
name = "file.conf"
|
name = "file.conf"
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -20,15 +20,35 @@ transport {
|
|||||||
worker-thread-size = 8
|
worker-thread-size = 8
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
## transaction log store
|
||||||
store {
|
store {
|
||||||
# branch session size , if exceeded first try compress lockkey, still exceeded throws exceptions
|
## store mode: file、db
|
||||||
max-branch-session-size = 16384
|
mode = "file"
|
||||||
# globe session size , if exceeded throws exceptions
|
|
||||||
max-global-session-size = 512
|
## file store
|
||||||
# file buffer size , if exceeded allocate new buffer
|
file {
|
||||||
file-write-buffer-cache-size = 16384
|
dir = "sessionStore"
|
||||||
# when recover batch read size
|
|
||||||
session.reload.read_size = 100
|
# branch session size , if exceeded first try compress lockkey, still exceeded throws exceptions
|
||||||
|
max-branch-session-size = 16384
|
||||||
|
# globe session size , if exceeded throws exceptions
|
||||||
|
max-global-session-size = 512
|
||||||
|
# file buffer size , if exceeded allocate new buffer
|
||||||
|
file-write-buffer-cache-size = 16384
|
||||||
|
# when recover batch read size
|
||||||
|
session.reload.read_size = 100
|
||||||
|
# async, sync
|
||||||
|
flush-disk-mode = async
|
||||||
|
}
|
||||||
|
|
||||||
|
## database store
|
||||||
|
db {
|
||||||
|
driver_class = ""
|
||||||
|
url = ""
|
||||||
|
user = ""
|
||||||
|
password = ""
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
service {
|
service {
|
||||||
#vgroup->rgroup
|
#vgroup->rgroup
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
registry {
|
registry {
|
||||||
# file 、nacos 、eureka、redis、zk、consul
|
# file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
|
||||||
type = "file"
|
type = "file"
|
||||||
|
|
||||||
nacos {
|
nacos {
|
||||||
@ -26,6 +26,19 @@ registry {
|
|||||||
cluster = "default"
|
cluster = "default"
|
||||||
serverAddr = "127.0.0.1:8500"
|
serverAddr = "127.0.0.1:8500"
|
||||||
}
|
}
|
||||||
|
etcd3 {
|
||||||
|
cluster = "default"
|
||||||
|
serverAddr = "http://localhost:2379"
|
||||||
|
}
|
||||||
|
sofa {
|
||||||
|
serverAddr = "127.0.0.1:9603"
|
||||||
|
application = "default"
|
||||||
|
region = "DEFAULT_ZONE"
|
||||||
|
datacenter = "DefaultDataCenter"
|
||||||
|
cluster = "default"
|
||||||
|
group = "SEATA_GROUP"
|
||||||
|
addressWaitTime = "3000"
|
||||||
|
}
|
||||||
file {
|
file {
|
||||||
name = "file.conf"
|
name = "file.conf"
|
||||||
}
|
}
|
||||||
@ -41,7 +54,7 @@ config {
|
|||||||
cluster = "default"
|
cluster = "default"
|
||||||
}
|
}
|
||||||
apollo {
|
apollo {
|
||||||
app.id = "fescar-server"
|
app.id = "seata-server"
|
||||||
apollo.meta = "http://192.168.1.204:8801"
|
apollo.meta = "http://192.168.1.204:8801"
|
||||||
}
|
}
|
||||||
zk {
|
zk {
|
||||||
@ -52,4 +65,4 @@ config {
|
|||||||
file {
|
file {
|
||||||
name = "file.conf"
|
name = "file.conf"
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -101,7 +101,7 @@ CREATE TABLE `account_tbl` (
|
|||||||
进入解压之后的 bin 目录,执行如下命令来启动
|
进入解压之后的 bin 目录,执行如下命令来启动
|
||||||
|
|
||||||
```$shell
|
```$shell
|
||||||
sh seata-server.sh $LISTEN_PORT $MODE
|
sh seata-server.sh $LISTEN_PORT $MODE(file or db)
|
||||||
```
|
```
|
||||||
|
|
||||||
在这个示例中,采用如下命令来启动 Seata Server
|
在这个示例中,采用如下命令来启动 Seata Server
|
||||||
@ -110,7 +110,7 @@ sh seata-server.sh $LISTEN_PORT $MODE
|
|||||||
sh seata-server.sh 8091 file
|
sh seata-server.sh 8091 file
|
||||||
```
|
```
|
||||||
|
|
||||||
**注意** 如果你修改了endpoint且注册中心使用默认file类型,那么记得需要在各个示例工程中的 `file.conf` 文件中,修改 grouplist 的值。
|
**注意** 如果你修改了endpoint且注册中心使用默认file类型,那么记得需要在各个示例工程中的 `file.conf` 文件中,修改 grouplist 的值(当registry.conf 中registry.type 或 config.type 为file 时会读取内部的file节点中的文件名,若type不为file将直接从配置类型的对应元数据的注册配置中心读取数据),推荐大家使用 nacos 作为配置注册中心。
|
||||||
|
|
||||||
|
|
||||||
## 运行示例
|
## 运行示例
|
||||||
|
@ -20,15 +20,35 @@ transport {
|
|||||||
worker-thread-size = 8
|
worker-thread-size = 8
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
## transaction log store
|
||||||
store {
|
store {
|
||||||
# branch session size , if exceeded first try compress lockkey, still exceeded throws exceptions
|
## store mode: file、db
|
||||||
max-branch-session-size = 16384
|
mode = "file"
|
||||||
# globe session size , if exceeded throws exceptions
|
|
||||||
max-global-session-size = 512
|
## file store
|
||||||
# file buffer size , if exceeded allocate new buffer
|
file {
|
||||||
file-write-buffer-cache-size = 16384
|
dir = "sessionStore"
|
||||||
# when recover batch read size
|
|
||||||
session.reload.read_size = 100
|
# branch session size , if exceeded first try compress lockkey, still exceeded throws exceptions
|
||||||
|
max-branch-session-size = 16384
|
||||||
|
# globe session size , if exceeded throws exceptions
|
||||||
|
max-global-session-size = 512
|
||||||
|
# file buffer size , if exceeded allocate new buffer
|
||||||
|
file-write-buffer-cache-size = 16384
|
||||||
|
# when recover batch read size
|
||||||
|
session.reload.read_size = 100
|
||||||
|
# async, sync
|
||||||
|
flush-disk-mode = async
|
||||||
|
}
|
||||||
|
|
||||||
|
## database store
|
||||||
|
db {
|
||||||
|
driver_class = ""
|
||||||
|
url = ""
|
||||||
|
user = ""
|
||||||
|
password = ""
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
service {
|
service {
|
||||||
#vgroup->rgroup
|
#vgroup->rgroup
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
registry {
|
registry {
|
||||||
# file 、nacos 、eureka、redis、zk、consul
|
# file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
|
||||||
type = "file"
|
type = "file"
|
||||||
|
|
||||||
nacos {
|
nacos {
|
||||||
@ -26,6 +26,19 @@ registry {
|
|||||||
cluster = "default"
|
cluster = "default"
|
||||||
serverAddr = "127.0.0.1:8500"
|
serverAddr = "127.0.0.1:8500"
|
||||||
}
|
}
|
||||||
|
etcd3 {
|
||||||
|
cluster = "default"
|
||||||
|
serverAddr = "http://localhost:2379"
|
||||||
|
}
|
||||||
|
sofa {
|
||||||
|
serverAddr = "127.0.0.1:9603"
|
||||||
|
application = "default"
|
||||||
|
region = "DEFAULT_ZONE"
|
||||||
|
datacenter = "DefaultDataCenter"
|
||||||
|
cluster = "default"
|
||||||
|
group = "SEATA_GROUP"
|
||||||
|
addressWaitTime = "3000"
|
||||||
|
}
|
||||||
file {
|
file {
|
||||||
name = "file.conf"
|
name = "file.conf"
|
||||||
}
|
}
|
||||||
@ -41,7 +54,7 @@ config {
|
|||||||
cluster = "default"
|
cluster = "default"
|
||||||
}
|
}
|
||||||
apollo {
|
apollo {
|
||||||
app.id = "fescar-server"
|
app.id = "seata-server"
|
||||||
apollo.meta = "http://192.168.1.204:8801"
|
apollo.meta = "http://192.168.1.204:8801"
|
||||||
}
|
}
|
||||||
zk {
|
zk {
|
||||||
@ -52,4 +65,4 @@ config {
|
|||||||
file {
|
file {
|
||||||
name = "file.conf"
|
name = "file.conf"
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -0,0 +1,82 @@
|
|||||||
|
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-alibaba-examples</artifactId>
|
||||||
|
<version>0.9.1.BUILD-SNAPSHOT</version>
|
||||||
|
<relativePath>../../pom.xml</relativePath>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
|
||||||
|
<artifactId>sentinel-spring-cloud-gateway-example</artifactId>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
<description>Example demonstrating how to use sentinel with spring cloud gateway</description>
|
||||||
|
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-webflux</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-gateway</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!--<dependency>-->
|
||||||
|
<!--<groupId>com.alibaba.csp</groupId>-->
|
||||||
|
<!--<artifactId>sentinel-datasource-nacos</artifactId>-->
|
||||||
|
<!--</dependency>-->
|
||||||
|
<!--<dependency>-->
|
||||||
|
<!--<groupId>com.alibaba.csp</groupId>-->
|
||||||
|
<!--<artifactId>sentinel-datasource-zookeeper</artifactId>-->
|
||||||
|
<!--</dependency>-->
|
||||||
|
<!--<dependency>-->
|
||||||
|
<!--<groupId>com.alibaba.csp</groupId>-->
|
||||||
|
<!--<artifactId>sentinel-datasource-apollo</artifactId>-->
|
||||||
|
<!--</dependency>-->
|
||||||
|
<!-- define in spring-boot-autoconfigure module -->
|
||||||
|
<!--<dependency>-->
|
||||||
|
<!--<groupId>com.fasterxml.jackson.dataformat</groupId>-->
|
||||||
|
<!--<artifactId>jackson-dataformat-xml</artifactId>-->
|
||||||
|
<!--</dependency>-->
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-deploy-plugin</artifactId>
|
||||||
|
<version>${maven-deploy-plugin.version}</version>
|
||||||
|
<configuration>
|
||||||
|
<skip>true</skip>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,48 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2018 the original author or authors.
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
*
|
||||||
|
* https://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 org.springframework.cloud.alibaba.cloud.examples;
|
||||||
|
|
||||||
|
import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.BlockRequestHandler;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.web.reactive.function.server.ServerResponse;
|
||||||
|
import org.springframework.web.server.ServerWebExchange;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
import static org.springframework.web.reactive.function.BodyInserters.fromObject;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
public class MySCGConfiguration {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public BlockRequestHandler blockRequestHandler() {
|
||||||
|
return new BlockRequestHandler() {
|
||||||
|
@Override
|
||||||
|
public Mono<ServerResponse> handleRequest(ServerWebExchange exchange, Throwable t) {
|
||||||
|
return ServerResponse.status(444)
|
||||||
|
.contentType(MediaType.APPLICATION_JSON_UTF8)
|
||||||
|
.body(fromObject("SCS Sentinel block"));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,54 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2018 the original author or authors.
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
*
|
||||||
|
* https://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 org.springframework.cloud.alibaba.cloud.examples;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition;
|
||||||
|
import com.alibaba.csp.sentinel.adapter.gateway.common.api.GatewayApiDefinitionManager;
|
||||||
|
import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule;
|
||||||
|
import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayRuleManager;
|
||||||
|
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
|
||||||
|
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
|
||||||
|
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
public class RulesWebFluxController {
|
||||||
|
|
||||||
|
@GetMapping("/api")
|
||||||
|
public Mono<Set<ApiDefinition>> apiRules() {
|
||||||
|
return Mono.just(GatewayApiDefinitionManager.getApiDefinitions());
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/gateway")
|
||||||
|
public Mono<Set<GatewayFlowRule>> apiGateway() {
|
||||||
|
return Mono.just(GatewayRuleManager.getRules());
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/flow")
|
||||||
|
public Mono<List<FlowRule>> apiFlow() {
|
||||||
|
return Mono.just(FlowRuleManager.getRules());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,33 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2018 the original author or authors.
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
*
|
||||||
|
* https://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 org.springframework.cloud.alibaba.cloud.examples;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
||||||
|
*/
|
||||||
|
@SpringBootApplication
|
||||||
|
public class SentinelSpringCloudGatewayApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//GatewayCallbackManager.setRequestOriginParser(s -> "123");
|
||||||
|
SpringApplication.run(SentinelSpringCloudGatewayApplication.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,32 @@
|
|||||||
|
[
|
||||||
|
{
|
||||||
|
"apiName": "some_customized_api",
|
||||||
|
"predicateItems": [
|
||||||
|
{
|
||||||
|
"pattern": "/product/baz"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"pattern": "/product/foo/**",
|
||||||
|
"matchStrategy": 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"items": [
|
||||||
|
{
|
||||||
|
"pattern": "/spring-cloud/**"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"pattern": "/spring-cloud-alibaba/**"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"apiName": "another_customized_api",
|
||||||
|
"predicateItems": [
|
||||||
|
{
|
||||||
|
"pattern": "/ahas"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
@ -0,0 +1,37 @@
|
|||||||
|
server:
|
||||||
|
port: 18085
|
||||||
|
spring:
|
||||||
|
application:
|
||||||
|
name: sentinel-spring-cloud-gateway
|
||||||
|
cloud:
|
||||||
|
gateway:
|
||||||
|
enabled: true
|
||||||
|
discovery:
|
||||||
|
locator:
|
||||||
|
lower-case-service-id: true
|
||||||
|
routes:
|
||||||
|
# Add your routes here.
|
||||||
|
- id: aliyun_route
|
||||||
|
uri: https://www.aliyun.com/
|
||||||
|
predicates:
|
||||||
|
- Path=/product/**
|
||||||
|
- id: httpbin_route
|
||||||
|
uri: https://httpbin.org
|
||||||
|
predicates:
|
||||||
|
- Path=/httpbin/**
|
||||||
|
filters:
|
||||||
|
- RewritePath=/httpbin/(?<segment>.*), /$\{segment}
|
||||||
|
|
||||||
|
sentinel:
|
||||||
|
datasource.ds2.file:
|
||||||
|
file: "classpath: gateway.json"
|
||||||
|
ruleType: gw-flow
|
||||||
|
datasource.ds1.file:
|
||||||
|
file: "classpath: api.json"
|
||||||
|
ruleType: gw-api-group
|
||||||
|
transport:
|
||||||
|
dashboard: localhost:8080
|
||||||
|
filter:
|
||||||
|
enabled: true
|
||||||
|
|
||||||
|
management.endpoints.web.exposure.include: "*"
|
@ -0,0 +1,22 @@
|
|||||||
|
[
|
||||||
|
{
|
||||||
|
"resource": "some_customized_api",
|
||||||
|
"count": 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"resource": "httpbin_route",
|
||||||
|
"count": 0,
|
||||||
|
"paramItem": {
|
||||||
|
"parseStrategy": 2,
|
||||||
|
"fieldName": "Spring-Cloud-Alibaba"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"resource": "httpbin_route",
|
||||||
|
"count": 0,
|
||||||
|
"paramItem": {
|
||||||
|
"parseStrategy": 3,
|
||||||
|
"fieldName": "name"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
@ -0,0 +1,82 @@
|
|||||||
|
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-alibaba-examples</artifactId>
|
||||||
|
<version>0.9.1.BUILD-SNAPSHOT</version>
|
||||||
|
<relativePath>../../pom.xml</relativePath>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
|
||||||
|
<artifactId>sentinel-zuul-example</artifactId>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
<description>Example demonstrating how to use sentinel with zuul</description>
|
||||||
|
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!--<dependency>-->
|
||||||
|
<!--<groupId>com.alibaba.csp</groupId>-->
|
||||||
|
<!--<artifactId>sentinel-datasource-nacos</artifactId>-->
|
||||||
|
<!--</dependency>-->
|
||||||
|
<!--<dependency>-->
|
||||||
|
<!--<groupId>com.alibaba.csp</groupId>-->
|
||||||
|
<!--<artifactId>sentinel-datasource-zookeeper</artifactId>-->
|
||||||
|
<!--</dependency>-->
|
||||||
|
<!--<dependency>-->
|
||||||
|
<!--<groupId>com.alibaba.csp</groupId>-->
|
||||||
|
<!--<artifactId>sentinel-datasource-apollo</artifactId>-->
|
||||||
|
<!--</dependency>-->
|
||||||
|
<!-- define in spring-boot-autoconfigure module -->
|
||||||
|
<!--<dependency>-->
|
||||||
|
<!--<groupId>com.fasterxml.jackson.dataformat</groupId>-->
|
||||||
|
<!--<artifactId>jackson-dataformat-xml</artifactId>-->
|
||||||
|
<!--</dependency>-->
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-deploy-plugin</artifactId>
|
||||||
|
<version>${maven-deploy-plugin.version}</version>
|
||||||
|
<configuration>
|
||||||
|
<skip>true</skip>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,53 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2018 the original author or authors.
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
*
|
||||||
|
* https://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 org.springframework.cloud.alibaba.cloud.examples;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition;
|
||||||
|
import com.alibaba.csp.sentinel.adapter.gateway.common.api.GatewayApiDefinitionManager;
|
||||||
|
import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule;
|
||||||
|
import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayRuleManager;
|
||||||
|
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
|
||||||
|
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
public class RulesController {
|
||||||
|
|
||||||
|
@GetMapping("/api")
|
||||||
|
public Set<ApiDefinition> apiRules() {
|
||||||
|
return GatewayApiDefinitionManager.getApiDefinitions();
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/gateway")
|
||||||
|
public Set<GatewayFlowRule> apiGateway() {
|
||||||
|
return GatewayRuleManager.getRules();
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/flow")
|
||||||
|
public List<FlowRule> apiFlow() {
|
||||||
|
return FlowRuleManager.getRules();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,34 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2018 the original author or authors.
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
*
|
||||||
|
* https://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 org.springframework.cloud.alibaba.cloud.examples;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
||||||
|
*/
|
||||||
|
@SpringBootApplication
|
||||||
|
@EnableZuulProxy
|
||||||
|
public class SentinelZuulApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(SentinelZuulApplication.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,66 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2018 the original author or authors.
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
*
|
||||||
|
* https://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 org.springframework.cloud.alibaba.cloud.examples;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
|
import com.alibaba.csp.sentinel.adapter.gateway.zuul.callback.RequestOriginParser;
|
||||||
|
import com.alibaba.csp.sentinel.adapter.gateway.zuul.fallback.BlockResponse;
|
||||||
|
import com.alibaba.csp.sentinel.adapter.gateway.zuul.fallback.ZuulBlockFallbackProvider;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
public class ZuulConfiguration {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public ZuulBlockFallbackProvider zuulBlockFallbackProvider1() {
|
||||||
|
return new ZuulBlockFallbackProvider() {
|
||||||
|
@Override
|
||||||
|
public String getRoute() {
|
||||||
|
return "*";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BlockResponse fallbackResponse(String route, Throwable cause) {
|
||||||
|
if (route.equals("my-service3")) {
|
||||||
|
return new BlockResponse(433, "Sentinel Block3", route);
|
||||||
|
} else if (route.equals("my-service4")) {
|
||||||
|
return new BlockResponse(444, "my-service4", route);
|
||||||
|
} else {
|
||||||
|
return new BlockResponse(499, "Sentinel Block 499", route);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public RequestOriginParser requestOriginParser() {
|
||||||
|
return new RequestOriginParser() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String parseOrigin(HttpServletRequest request) {
|
||||||
|
return "123";
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,32 @@
|
|||||||
|
[
|
||||||
|
{
|
||||||
|
"apiName": "some_customized_api",
|
||||||
|
"predicateItems": [
|
||||||
|
{
|
||||||
|
"pattern": "/product/baz"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"pattern": "/product/foo/**",
|
||||||
|
"matchStrategy": 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"items": [
|
||||||
|
{
|
||||||
|
"pattern": "/spring-cloud/**"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"pattern": "/spring-cloud-alibaba/**"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"apiName": "another_customized_api",
|
||||||
|
"predicateItems": [
|
||||||
|
{
|
||||||
|
"pattern": "/ahas"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
@ -0,0 +1,40 @@
|
|||||||
|
server:
|
||||||
|
port: 18086
|
||||||
|
spring:
|
||||||
|
application:
|
||||||
|
name: sentinel-zuul
|
||||||
|
cloud:
|
||||||
|
sentinel:
|
||||||
|
datasource.ds2.file:
|
||||||
|
file: "classpath: gateway.json"
|
||||||
|
ruleType: gw-flow
|
||||||
|
datasource.ds1.file:
|
||||||
|
file: "classpath: api.json"
|
||||||
|
ruleType: gw-api-group
|
||||||
|
transport:
|
||||||
|
dashboard: localhost:8080
|
||||||
|
filter:
|
||||||
|
enabled: false
|
||||||
|
|
||||||
|
management.endpoints.web.exposure.include: "*"
|
||||||
|
|
||||||
|
|
||||||
|
zuul.routes.my-service.path: "/product/foo/**"
|
||||||
|
zuul.routes.my-service.service-id: "my-service"
|
||||||
|
|
||||||
|
zuul.routes.my-service2.path: "/my-service2/**"
|
||||||
|
zuul.routes.my-service2.service-id: "my-service2"
|
||||||
|
|
||||||
|
zuul.routes.my-service3.path: "/my-service3/**"
|
||||||
|
zuul.routes.my-service3.service-id: "my-service3"
|
||||||
|
|
||||||
|
zuul.routes.my-service4.path: "/my-service4/**"
|
||||||
|
zuul.routes.my-service4.service-id: "my-service4"
|
||||||
|
|
||||||
|
|
||||||
|
spring.cloud.sentinel.zuul.order.pre: 2000
|
||||||
|
spring.cloud.sentinel.zuul.order.post: 500
|
||||||
|
spring.cloud.sentinel.zuul.order.error: -100
|
||||||
|
|
||||||
|
|
||||||
|
spring.cloud.sentinel.zuul.enabled: true
|
@ -0,0 +1,26 @@
|
|||||||
|
[
|
||||||
|
{
|
||||||
|
"resource": "some_customized_api",
|
||||||
|
"count": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"resource": "my-service2",
|
||||||
|
"count": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"resource": "my-service3",
|
||||||
|
"count": 0,
|
||||||
|
"paramItem": {
|
||||||
|
"parseStrategy": 2,
|
||||||
|
"fieldName": "Spring-Cloud-Alibaba"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"resource": "my-service4",
|
||||||
|
"count": 0,
|
||||||
|
"paramItem": {
|
||||||
|
"parseStrategy": 3,
|
||||||
|
"fieldName": "name"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
@ -76,6 +76,7 @@ public class DubboGatewayServlet extends HttpServletBean {
|
|||||||
return serviceName;
|
return serviceName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
|
public void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
|
||||||
|
|
||||||
String serviceName = resolveServiceName(request);
|
String serviceName = resolveServiceName(request);
|
||||||
|
@ -28,25 +28,25 @@ import java.util.concurrent.ConcurrentHashMap;
|
|||||||
*/
|
*/
|
||||||
public class NacosPropertySourceRepository {
|
public class NacosPropertySourceRepository {
|
||||||
|
|
||||||
private final static ConcurrentHashMap<String, NacosPropertySource> nacosPropertySourceRepository = new ConcurrentHashMap<>();
|
private final static ConcurrentHashMap<String, NacosPropertySource> NACOS_PROPERTY_SOURCE_REPOSITORY = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return all nacos properties from application context
|
* @return all nacos properties from application context
|
||||||
*/
|
*/
|
||||||
public static List<NacosPropertySource> getAll() {
|
public static List<NacosPropertySource> getAll() {
|
||||||
List<NacosPropertySource> result = new ArrayList<>();
|
List<NacosPropertySource> result = new ArrayList<>();
|
||||||
result.addAll(nacosPropertySourceRepository.values());
|
result.addAll(NACOS_PROPERTY_SOURCE_REPOSITORY.values());
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void collectNacosPropertySources(
|
public static void collectNacosPropertySources(
|
||||||
NacosPropertySource nacosPropertySource) {
|
NacosPropertySource nacosPropertySource) {
|
||||||
nacosPropertySourceRepository.putIfAbsent(nacosPropertySource.getDataId(),
|
NACOS_PROPERTY_SOURCE_REPOSITORY.putIfAbsent(nacosPropertySource.getDataId(),
|
||||||
nacosPropertySource);
|
nacosPropertySource);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static NacosPropertySource getNacosPropertySource(String dataId) {
|
public static NacosPropertySource getNacosPropertySource(String dataId) {
|
||||||
|
|
||||||
return nacosPropertySourceRepository.get(dataId);
|
return NACOS_PROPERTY_SOURCE_REPOSITORY.get(dataId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -118,57 +118,10 @@ public class NacosWatch implements ApplicationEventPublisherAware, SmartLifecycl
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void nacosServicesWatch() {
|
public void nacosServicesWatch() {
|
||||||
try {
|
|
||||||
|
|
||||||
boolean changed = false;
|
// nacos doesn't support watch now , publish an event every 30 seconds.
|
||||||
NamingService namingService = properties.namingServiceInstance();
|
this.publisher.publishEvent(
|
||||||
|
new HeartbeatEvent(this, nacosWatchIndex.getAndIncrement()));
|
||||||
|
|
||||||
ListView<String> listView = properties.namingServiceInstance()
|
|
||||||
.getServicesOfServer(1, Integer.MAX_VALUE);
|
|
||||||
|
|
||||||
List<String> serviceList = listView.getData();
|
|
||||||
|
|
||||||
// if there are new services found, publish event
|
|
||||||
Set<String> currentServices = new HashSet<>(serviceList);
|
|
||||||
currentServices.removeAll(cacheServices);
|
|
||||||
if (currentServices.size() > 0) {
|
|
||||||
changed = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// if some services disappear, publish event
|
|
||||||
if (cacheServices.removeAll(new HashSet<>(serviceList))
|
|
||||||
&& cacheServices.size() > 0) {
|
|
||||||
changed = true;
|
|
||||||
|
|
||||||
for (String serviceName : cacheServices) {
|
|
||||||
namingService.unsubscribe(serviceName,
|
|
||||||
subscribeListeners.get(serviceName));
|
|
||||||
subscribeListeners.remove(serviceName);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
cacheServices = new HashSet<>(serviceList);
|
|
||||||
|
|
||||||
// subscribe services's node change, publish event if nodes changed
|
|
||||||
for (String serviceName : cacheServices) {
|
|
||||||
if (!subscribeListeners.containsKey(serviceName)) {
|
|
||||||
EventListener eventListener = event -> NacosWatch.this.publisher
|
|
||||||
.publishEvent(new HeartbeatEvent(NacosWatch.this,
|
|
||||||
nacosWatchIndex.getAndIncrement()));
|
|
||||||
subscribeListeners.put(serviceName, eventListener);
|
|
||||||
namingService.subscribe(serviceName, eventListener);
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (changed) {
|
|
||||||
this.publisher.publishEvent(
|
|
||||||
new HeartbeatEvent(this, nacosWatchIndex.getAndIncrement()));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
catch (Exception e) {
|
|
||||||
log.error("Error watching Nacos Service change", e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -27,6 +27,12 @@
|
|||||||
<optional>true</optional>
|
<optional>true</optional>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.csp</groupId>
|
||||||
|
<artifactId>sentinel-api-gateway-adapter-common</artifactId>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.alibaba.csp</groupId>
|
<groupId>com.alibaba.csp</groupId>
|
||||||
<artifactId>sentinel-datasource-nacos</artifactId>
|
<artifactId>sentinel-datasource-nacos</artifactId>
|
||||||
|
@ -6,6 +6,8 @@ import java.util.Optional;
|
|||||||
import org.springframework.cloud.alibaba.sentinel.datasource.config.AbstractDataSourceProperties;
|
import org.springframework.cloud.alibaba.sentinel.datasource.config.AbstractDataSourceProperties;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
|
import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition;
|
||||||
|
import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule;
|
||||||
import com.alibaba.csp.sentinel.slots.block.AbstractRule;
|
import com.alibaba.csp.sentinel.slots.block.AbstractRule;
|
||||||
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule;
|
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule;
|
||||||
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
|
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
|
||||||
@ -40,7 +42,15 @@ public enum RuleType {
|
|||||||
/**
|
/**
|
||||||
* authority
|
* authority
|
||||||
*/
|
*/
|
||||||
AUTHORITY("authority", AuthorityRule.class);
|
AUTHORITY("authority", AuthorityRule.class),
|
||||||
|
/**
|
||||||
|
* gateway flow
|
||||||
|
*/
|
||||||
|
GW_FLOW("gw-flow", GatewayFlowRule.class),
|
||||||
|
/**
|
||||||
|
* api
|
||||||
|
*/
|
||||||
|
GW_API_GROUP("gw-api-group", ApiDefinition.class);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* alias for {@link AbstractRule}
|
* alias for {@link AbstractRule}
|
||||||
|
@ -4,7 +4,10 @@ import javax.validation.constraints.NotEmpty;
|
|||||||
import javax.validation.constraints.NotNull;
|
import javax.validation.constraints.NotNull;
|
||||||
|
|
||||||
import org.springframework.cloud.alibaba.sentinel.datasource.RuleType;
|
import org.springframework.cloud.alibaba.sentinel.datasource.RuleType;
|
||||||
|
import org.springframework.core.env.Environment;
|
||||||
|
|
||||||
|
import com.alibaba.csp.sentinel.adapter.gateway.common.api.GatewayApiDefinitionManager;
|
||||||
|
import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayRuleManager;
|
||||||
import com.alibaba.csp.sentinel.datasource.AbstractDataSource;
|
import com.alibaba.csp.sentinel.datasource.AbstractDataSource;
|
||||||
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRuleManager;
|
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRuleManager;
|
||||||
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;
|
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;
|
||||||
@ -28,6 +31,8 @@ public class AbstractDataSourceProperties {
|
|||||||
private String converterClass;
|
private String converterClass;
|
||||||
@JsonIgnore
|
@JsonIgnore
|
||||||
private final String factoryBeanName;
|
private final String factoryBeanName;
|
||||||
|
@JsonIgnore
|
||||||
|
private Environment env;
|
||||||
|
|
||||||
public AbstractDataSourceProperties(String factoryBeanName) {
|
public AbstractDataSourceProperties(String factoryBeanName) {
|
||||||
this.factoryBeanName = factoryBeanName;
|
this.factoryBeanName = factoryBeanName;
|
||||||
@ -61,6 +66,14 @@ public class AbstractDataSourceProperties {
|
|||||||
return factoryBeanName;
|
return factoryBeanName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected Environment getEnv() {
|
||||||
|
return env;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEnv(Environment env) {
|
||||||
|
this.env = env;
|
||||||
|
}
|
||||||
|
|
||||||
public void preCheck(String dataSourceName) {
|
public void preCheck(String dataSourceName) {
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -82,6 +95,12 @@ public class AbstractDataSourceProperties {
|
|||||||
case AUTHORITY:
|
case AUTHORITY:
|
||||||
AuthorityRuleManager.register2Property(dataSource.getProperty());
|
AuthorityRuleManager.register2Property(dataSource.getProperty());
|
||||||
break;
|
break;
|
||||||
|
case GW_FLOW:
|
||||||
|
GatewayRuleManager.register2Property(dataSource.getProperty());
|
||||||
|
break;
|
||||||
|
case GW_API_GROUP:
|
||||||
|
GatewayApiDefinitionManager.register2Property(dataSource.getProperty());
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -2,8 +2,6 @@ package org.springframework.cloud.alibaba.sentinel.datasource.config;
|
|||||||
|
|
||||||
import javax.validation.constraints.NotEmpty;
|
import javax.validation.constraints.NotEmpty;
|
||||||
|
|
||||||
import org.springframework.cloud.alibaba.sentinel.datasource.RuleType;
|
|
||||||
import org.springframework.cloud.alibaba.sentinel.datasource.SentinelDataSourceConstants;
|
|
||||||
import org.springframework.cloud.alibaba.sentinel.datasource.factorybean.NacosDataSourceFactoryBean;
|
import org.springframework.cloud.alibaba.sentinel.datasource.factorybean.NacosDataSourceFactoryBean;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
@ -34,9 +32,13 @@ public class NacosDataSourceProperties extends AbstractDataSourceProperties {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void preCheck(String dataSourceName) {
|
public void preCheck(String dataSourceName) {
|
||||||
if (StringUtils.isEmpty(serverAddr) && acmPropertiesInvalid()) {
|
if (StringUtils.isEmpty(serverAddr)) {
|
||||||
throw new IllegalArgumentException(
|
serverAddr = this.getEnv().getProperty(
|
||||||
"NacosDataSource properties value not correct. serverAddr is empty but there is empty value in accessKey, secretKey, endpoint, namespace property");
|
"spring.cloud.sentinel.datasource.nacos.server-addr", "");
|
||||||
|
if (StringUtils.isEmpty(serverAddr)) {
|
||||||
|
throw new IllegalArgumentException(
|
||||||
|
"NacosDataSource server-addr is empty");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -96,9 +98,4 @@ public class NacosDataSourceProperties extends AbstractDataSourceProperties {
|
|||||||
this.secretKey = secretKey;
|
this.secretKey = secretKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean acmPropertiesInvalid() {
|
|
||||||
return StringUtils.isEmpty(endpoint) || StringUtils.isEmpty(accessKey)
|
|
||||||
|| StringUtils.isEmpty(secretKey) || StringUtils.isEmpty(namespace);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,7 @@
|
|||||||
package org.springframework.cloud.alibaba.sentinel.datasource.config;
|
package org.springframework.cloud.alibaba.sentinel.datasource.config;
|
||||||
|
|
||||||
import javax.validation.constraints.NotEmpty;
|
|
||||||
|
|
||||||
import org.springframework.cloud.alibaba.sentinel.datasource.factorybean.ZookeeperDataSourceFactoryBean;
|
import org.springframework.cloud.alibaba.sentinel.datasource.factorybean.ZookeeperDataSourceFactoryBean;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Zookeeper Properties class Using by {@link DataSourcePropertiesConfiguration} and
|
* Zookeeper Properties class Using by {@link DataSourcePropertiesConfiguration} and
|
||||||
@ -16,7 +15,6 @@ public class ZookeeperDataSourceProperties extends AbstractDataSourceProperties
|
|||||||
super(ZookeeperDataSourceFactoryBean.class.getName());
|
super(ZookeeperDataSourceFactoryBean.class.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotEmpty
|
|
||||||
private String serverAddr;
|
private String serverAddr;
|
||||||
|
|
||||||
private String path;
|
private String path;
|
||||||
@ -25,6 +23,18 @@ public class ZookeeperDataSourceProperties extends AbstractDataSourceProperties
|
|||||||
|
|
||||||
private String dataId;
|
private String dataId;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void preCheck(String dataSourceName) {
|
||||||
|
if (StringUtils.isEmpty(serverAddr)) {
|
||||||
|
serverAddr = this.getEnv()
|
||||||
|
.getProperty("spring.cloud.sentinel.datasource.zk.server-addr", "");
|
||||||
|
if (StringUtils.isEmpty(serverAddr)) {
|
||||||
|
throw new IllegalArgumentException(
|
||||||
|
"ZookeeperDataSource server-addr is empty");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public String getServerAddr() {
|
public String getServerAddr() {
|
||||||
return serverAddr;
|
return serverAddr;
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,20 @@
|
|||||||
package org.springframework.cloud.alibaba.sentinel.datasource.converter;
|
package org.springframework.cloud.alibaba.sentinel.datasource.converter;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.cloud.alibaba.sentinel.datasource.RuleType;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
|
import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition;
|
||||||
|
import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule;
|
||||||
import com.alibaba.csp.sentinel.datasource.Converter;
|
import com.alibaba.csp.sentinel.datasource.Converter;
|
||||||
import com.alibaba.csp.sentinel.slots.block.AbstractRule;
|
|
||||||
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule;
|
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule;
|
||||||
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
|
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
|
||||||
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;
|
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;
|
||||||
@ -13,15 +26,6 @@ import com.alibaba.csp.sentinel.slots.system.SystemRule;
|
|||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
import org.springframework.cloud.alibaba.sentinel.datasource.RuleType;
|
|
||||||
import org.springframework.util.StringUtils;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert sentinel rules for json or xml array Using strict mode to parse json or xml
|
* Convert sentinel rules for json or xml array Using strict mode to parse json or xml
|
||||||
@ -34,8 +38,8 @@ import java.util.Optional;
|
|||||||
* @see ParamFlowRule
|
* @see ParamFlowRule
|
||||||
* @see ObjectMapper
|
* @see ObjectMapper
|
||||||
*/
|
*/
|
||||||
public abstract class SentinelConverter<T extends AbstractRule>
|
public abstract class SentinelConverter<T extends Object>
|
||||||
implements Converter<String, List<AbstractRule>> {
|
implements Converter<String, Collection<Object>> {
|
||||||
|
|
||||||
private static final Logger log = LoggerFactory.getLogger(SentinelConverter.class);
|
private static final Logger log = LoggerFactory.getLogger(SentinelConverter.class);
|
||||||
|
|
||||||
@ -49,11 +53,20 @@ public abstract class SentinelConverter<T extends AbstractRule>
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<AbstractRule> convert(String source) {
|
public Collection<Object> convert(String source) {
|
||||||
List<AbstractRule> ruleList = new ArrayList<>();
|
Collection<Object> ruleCollection;
|
||||||
|
|
||||||
|
// hard code
|
||||||
|
if (ruleClass == GatewayFlowRule.class || ruleClass == ApiDefinition.class) {
|
||||||
|
ruleCollection = new HashSet<>();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
ruleCollection = new ArrayList<>();
|
||||||
|
}
|
||||||
|
|
||||||
if (StringUtils.isEmpty(source)) {
|
if (StringUtils.isEmpty(source)) {
|
||||||
log.warn("converter can not convert rules because source is empty");
|
log.warn("converter can not convert rules because source is empty");
|
||||||
return ruleList;
|
return ruleCollection;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
List sourceArray = objectMapper.readValue(source,
|
List sourceArray = objectMapper.readValue(source,
|
||||||
@ -70,11 +83,11 @@ public abstract class SentinelConverter<T extends AbstractRule>
|
|||||||
}
|
}
|
||||||
|
|
||||||
Optional.ofNullable(convertRule(item))
|
Optional.ofNullable(convertRule(item))
|
||||||
.ifPresent(convertRule -> ruleList.add(convertRule));
|
.ifPresent(convertRule -> ruleCollection.add(convertRule));
|
||||||
});
|
});
|
||||||
|
|
||||||
if (ruleList.size() != sourceArray.size()) {
|
if (ruleCollection.size() != sourceArray.size()) {
|
||||||
throw new IllegalArgumentException("convert " + ruleList.size()
|
throw new IllegalArgumentException("convert " + ruleCollection.size()
|
||||||
+ " rules but there are " + sourceArray.size()
|
+ " rules but there are " + sourceArray.size()
|
||||||
+ " rules from datasource. RuleClass: "
|
+ " rules from datasource. RuleClass: "
|
||||||
+ ruleClass.getSimpleName());
|
+ ruleClass.getSimpleName());
|
||||||
@ -83,12 +96,12 @@ public abstract class SentinelConverter<T extends AbstractRule>
|
|||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
throw new RuntimeException("convert error: " + e.getMessage(), e);
|
throw new RuntimeException("convert error: " + e.getMessage(), e);
|
||||||
}
|
}
|
||||||
return ruleList;
|
return ruleCollection;
|
||||||
}
|
}
|
||||||
|
|
||||||
private AbstractRule convertRule(String ruleStr) {
|
private Object convertRule(String ruleStr) {
|
||||||
try {
|
try {
|
||||||
final AbstractRule rule = objectMapper.readValue(ruleStr, ruleClass);
|
final Object rule = objectMapper.readValue(ruleStr, ruleClass);
|
||||||
RuleType ruleType = RuleType.getByClass(ruleClass).get();
|
RuleType ruleType = RuleType.getByClass(ruleClass).get();
|
||||||
switch (ruleType) {
|
switch (ruleType) {
|
||||||
case FLOW:
|
case FLOW:
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
package org.springframework.cloud.alibaba.sentinel.datasource.factorybean;
|
package org.springframework.cloud.alibaba.sentinel.datasource.factorybean;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.FactoryBean;
|
||||||
|
|
||||||
import com.alibaba.csp.sentinel.datasource.Converter;
|
import com.alibaba.csp.sentinel.datasource.Converter;
|
||||||
import com.alibaba.csp.sentinel.datasource.apollo.ApolloDataSource;
|
import com.alibaba.csp.sentinel.datasource.apollo.ApolloDataSource;
|
||||||
|
|
||||||
import org.springframework.beans.factory.FactoryBean;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A {@link FactoryBean} for creating {@link ApolloDataSource} instance.
|
* A {@link FactoryBean} for creating {@link ApolloDataSource} instance.
|
||||||
*
|
*
|
||||||
@ -13,51 +13,51 @@ import org.springframework.beans.factory.FactoryBean;
|
|||||||
*/
|
*/
|
||||||
public class ApolloDataSourceFactoryBean implements FactoryBean<ApolloDataSource> {
|
public class ApolloDataSourceFactoryBean implements FactoryBean<ApolloDataSource> {
|
||||||
|
|
||||||
private String namespaceName;
|
private String namespaceName;
|
||||||
private String flowRulesKey;
|
private String flowRulesKey;
|
||||||
private String defaultFlowRuleValue;
|
private String defaultFlowRuleValue;
|
||||||
private Converter converter;
|
private Converter converter;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ApolloDataSource getObject() throws Exception {
|
public ApolloDataSource getObject() throws Exception {
|
||||||
return new ApolloDataSource(namespaceName, flowRulesKey, defaultFlowRuleValue,
|
return new ApolloDataSource(namespaceName, flowRulesKey, defaultFlowRuleValue,
|
||||||
converter);
|
converter);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Class<?> getObjectType() {
|
public Class<?> getObjectType() {
|
||||||
return ApolloDataSource.class;
|
return ApolloDataSource.class;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getNamespaceName() {
|
public String getNamespaceName() {
|
||||||
return namespaceName;
|
return namespaceName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setNamespaceName(String namespaceName) {
|
public void setNamespaceName(String namespaceName) {
|
||||||
this.namespaceName = namespaceName;
|
this.namespaceName = namespaceName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getFlowRulesKey() {
|
public String getFlowRulesKey() {
|
||||||
return flowRulesKey;
|
return flowRulesKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setFlowRulesKey(String flowRulesKey) {
|
public void setFlowRulesKey(String flowRulesKey) {
|
||||||
this.flowRulesKey = flowRulesKey;
|
this.flowRulesKey = flowRulesKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getDefaultFlowRuleValue() {
|
public String getDefaultFlowRuleValue() {
|
||||||
return defaultFlowRuleValue;
|
return defaultFlowRuleValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setDefaultFlowRuleValue(String defaultFlowRuleValue) {
|
public void setDefaultFlowRuleValue(String defaultFlowRuleValue) {
|
||||||
this.defaultFlowRuleValue = defaultFlowRuleValue;
|
this.defaultFlowRuleValue = defaultFlowRuleValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Converter getConverter() {
|
public Converter getConverter() {
|
||||||
return converter;
|
return converter;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setConverter(Converter converter) {
|
public void setConverter(Converter converter) {
|
||||||
this.converter = converter;
|
this.converter = converter;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,11 +3,11 @@ package org.springframework.cloud.alibaba.sentinel.datasource.factorybean;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.FactoryBean;
|
||||||
|
|
||||||
import com.alibaba.csp.sentinel.datasource.Converter;
|
import com.alibaba.csp.sentinel.datasource.Converter;
|
||||||
import com.alibaba.csp.sentinel.datasource.FileRefreshableDataSource;
|
import com.alibaba.csp.sentinel.datasource.FileRefreshableDataSource;
|
||||||
|
|
||||||
import org.springframework.beans.factory.FactoryBean;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A {@link FactoryBean} for creating {@link FileRefreshableDataSource} instance.
|
* A {@link FactoryBean} for creating {@link FileRefreshableDataSource} instance.
|
||||||
*
|
*
|
||||||
@ -23,7 +23,7 @@ public class FileRefreshableDataSourceFactoryBean
|
|||||||
private int bufSize;
|
private int bufSize;
|
||||||
private Converter converter;
|
private Converter converter;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public FileRefreshableDataSource getObject() throws Exception {
|
public FileRefreshableDataSource getObject() throws Exception {
|
||||||
return new FileRefreshableDataSource(new File(file), converter,
|
return new FileRefreshableDataSource(new File(file), converter,
|
||||||
recommendRefreshMs, bufSize, Charset.forName(charset));
|
recommendRefreshMs, bufSize, Charset.forName(charset));
|
||||||
|
@ -1,12 +1,13 @@
|
|||||||
package org.springframework.cloud.alibaba.sentinel.datasource.factorybean;
|
package org.springframework.cloud.alibaba.sentinel.datasource.factorybean;
|
||||||
|
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.FactoryBean;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
import com.alibaba.csp.sentinel.datasource.Converter;
|
import com.alibaba.csp.sentinel.datasource.Converter;
|
||||||
import com.alibaba.csp.sentinel.datasource.nacos.NacosDataSource;
|
import com.alibaba.csp.sentinel.datasource.nacos.NacosDataSource;
|
||||||
import com.alibaba.nacos.api.PropertyKeyConst;
|
import com.alibaba.nacos.api.PropertyKeyConst;
|
||||||
import org.springframework.beans.factory.FactoryBean;
|
|
||||||
import org.springframework.util.StringUtils;
|
|
||||||
|
|
||||||
import java.util.Properties;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A {@link FactoryBean} for creating {@link NacosDataSource} instance.
|
* A {@link FactoryBean} for creating {@link NacosDataSource} instance.
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
package org.springframework.cloud.alibaba.sentinel.datasource.factorybean;
|
package org.springframework.cloud.alibaba.sentinel.datasource.factorybean;
|
||||||
|
|
||||||
import com.alibaba.csp.sentinel.datasource.Converter;
|
|
||||||
import com.alibaba.csp.sentinel.datasource.zookeeper.ZookeeperDataSource;
|
|
||||||
|
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.springframework.beans.factory.FactoryBean;
|
import org.springframework.beans.factory.FactoryBean;
|
||||||
|
|
||||||
|
import com.alibaba.csp.sentinel.datasource.Converter;
|
||||||
|
import com.alibaba.csp.sentinel.datasource.zookeeper.ZookeeperDataSource;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A {@link FactoryBean} for creating {@link ZookeeperDataSource} instance.
|
* A {@link FactoryBean} for creating {@link ZookeeperDataSource} instance.
|
||||||
*
|
*
|
||||||
@ -14,68 +14,69 @@ import org.springframework.beans.factory.FactoryBean;
|
|||||||
*/
|
*/
|
||||||
public class ZookeeperDataSourceFactoryBean implements FactoryBean<ZookeeperDataSource> {
|
public class ZookeeperDataSourceFactoryBean implements FactoryBean<ZookeeperDataSource> {
|
||||||
|
|
||||||
private String serverAddr;
|
private String serverAddr;
|
||||||
|
|
||||||
private String path;
|
private String path;
|
||||||
|
|
||||||
private String groupId;
|
private String groupId;
|
||||||
private String dataId;
|
private String dataId;
|
||||||
|
|
||||||
private Converter converter;
|
private Converter converter;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ZookeeperDataSource getObject() throws Exception {
|
public ZookeeperDataSource getObject() throws Exception {
|
||||||
if (StringUtils.isNotEmpty(groupId) && StringUtils.isNotEmpty(dataId)) {
|
if (StringUtils.isNotEmpty(groupId) && StringUtils.isNotEmpty(dataId)) {
|
||||||
// the path will be /{groupId}/{dataId}
|
// the path will be /{groupId}/{dataId}
|
||||||
return new ZookeeperDataSource(serverAddr, groupId, dataId, converter);
|
return new ZookeeperDataSource(serverAddr, groupId, dataId, converter);
|
||||||
} else {
|
}
|
||||||
// using path directly
|
else {
|
||||||
return new ZookeeperDataSource(serverAddr, path, converter);
|
// using path directly
|
||||||
}
|
return new ZookeeperDataSource(serverAddr, path, converter);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Class<?> getObjectType() {
|
public Class<?> getObjectType() {
|
||||||
return ZookeeperDataSource.class;
|
return ZookeeperDataSource.class;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getServerAddr() {
|
public String getServerAddr() {
|
||||||
return serverAddr;
|
return serverAddr;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setServerAddr(String serverAddr) {
|
public void setServerAddr(String serverAddr) {
|
||||||
this.serverAddr = serverAddr;
|
this.serverAddr = serverAddr;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getPath() {
|
public String getPath() {
|
||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPath(String path) {
|
public void setPath(String path) {
|
||||||
this.path = path;
|
this.path = path;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getGroupId() {
|
public String getGroupId() {
|
||||||
return groupId;
|
return groupId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setGroupId(String groupId) {
|
public void setGroupId(String groupId) {
|
||||||
this.groupId = groupId;
|
this.groupId = groupId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getDataId() {
|
public String getDataId() {
|
||||||
return dataId;
|
return dataId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setDataId(String dataId) {
|
public void setDataId(String dataId) {
|
||||||
this.dataId = dataId;
|
this.dataId = dataId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Converter getConverter() {
|
public Converter getConverter() {
|
||||||
return converter;
|
return converter;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setConverter(Converter converter) {
|
public void setConverter(Converter converter) {
|
||||||
this.converter = converter;
|
this.converter = converter;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -46,7 +46,7 @@ public class SentinelConverterTests {
|
|||||||
@Test
|
@Test
|
||||||
public void testJsonConverter() {
|
public void testJsonConverter() {
|
||||||
JsonConverter jsonConverter = new JsonConverter(objectMapper, FlowRule.class);
|
JsonConverter jsonConverter = new JsonConverter(objectMapper, FlowRule.class);
|
||||||
List<FlowRule> flowRules = jsonConverter
|
List<FlowRule> flowRules = (List<FlowRule>) jsonConverter
|
||||||
.convert(readFileContent("classpath: flowrule.json"));
|
.convert(readFileContent("classpath: flowrule.json"));
|
||||||
assertEquals("json converter flow rule size was wrong", 1, flowRules.size());
|
assertEquals("json converter flow rule size was wrong", 1, flowRules.size());
|
||||||
assertEquals("json converter flow rule resource name was wrong", "resource",
|
assertEquals("json converter flow rule resource name was wrong", "resource",
|
||||||
@ -67,7 +67,7 @@ public class SentinelConverterTests {
|
|||||||
@Test
|
@Test
|
||||||
public void testConverterEmptyContent() {
|
public void testConverterEmptyContent() {
|
||||||
JsonConverter jsonConverter = new JsonConverter(objectMapper, FlowRule.class);
|
JsonConverter jsonConverter = new JsonConverter(objectMapper, FlowRule.class);
|
||||||
List<FlowRule> flowRules = jsonConverter.convert("");
|
List<FlowRule> flowRules = (List<FlowRule>) jsonConverter.convert("");
|
||||||
assertEquals("json converter flow rule size was not empty", 0, flowRules.size());
|
assertEquals("json converter flow rule size was not empty", 0, flowRules.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -86,7 +86,7 @@ public class SentinelConverterTests {
|
|||||||
@Test
|
@Test
|
||||||
public void testXmlConverter() {
|
public void testXmlConverter() {
|
||||||
XmlConverter jsonConverter = new XmlConverter(xmlMapper, FlowRule.class);
|
XmlConverter jsonConverter = new XmlConverter(xmlMapper, FlowRule.class);
|
||||||
List<FlowRule> flowRules = jsonConverter
|
List<FlowRule> flowRules = (List<FlowRule>) jsonConverter
|
||||||
.convert(readFileContent("classpath: flowrule.xml"));
|
.convert(readFileContent("classpath: flowrule.xml"));
|
||||||
assertEquals("xml converter flow rule size was wrong", 2, flowRules.size());
|
assertEquals("xml converter flow rule size was wrong", 2, flowRules.size());
|
||||||
assertEquals("xml converter flow rule1 resource name was wrong", "resource",
|
assertEquals("xml converter flow rule1 resource name was wrong", "resource",
|
||||||
|
@ -14,7 +14,7 @@ Sentinel can provide `ServiceId` level and `API Path` level flow control for spr
|
|||||||
```xml
|
```xml
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.cloud</groupId>
|
<groupId>org.springframework.cloud</groupId>
|
||||||
<artifactId>spring-cloud-alibaba-sentinel-zuul</artifactId>
|
<artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
|
||||||
<version>x.y.z</version>
|
<version>x.y.z</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
@ -10,19 +10,28 @@
|
|||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
<groupId>org.springframework.cloud</groupId>
|
<groupId>org.springframework.cloud</groupId>
|
||||||
<artifactId>spring-cloud-alibaba-sentinel-zuul</artifactId>
|
<artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
|
||||||
<name>Spring Cloud Alibaba Sentinel Zuul</name>
|
<name>Spring Cloud Alibaba Sentinel Gateway</name>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.cloud</groupId>
|
<groupId>org.springframework.cloud</groupId>
|
||||||
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
|
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
|
||||||
<scope>provided</scope>
|
<optional>true</optional>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.alibaba.csp</groupId>
|
<groupId>com.alibaba.csp</groupId>
|
||||||
<artifactId>sentinel-zuul-adapter</artifactId>
|
<artifactId>sentinel-zuul-adapter</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.csp</groupId>
|
||||||
|
<artifactId>sentinel-spring-cloud-gateway-adapter</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-gateway</artifactId>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-configuration-processor</artifactId>
|
<artifactId>spring-boot-configuration-processor</artifactId>
|
@ -0,0 +1,91 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2018 the original author or authors.
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
*
|
||||||
|
* https://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 org.springframework.cloud.alibaba.sentinel.gateway;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import javax.annotation.PostConstruct;
|
||||||
|
|
||||||
|
import com.alibaba.csp.sentinel.adapter.gateway.sc.SentinelGatewayFilter;
|
||||||
|
import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.BlockRequestHandler;
|
||||||
|
import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.GatewayCallbackManager;
|
||||||
|
import com.alibaba.csp.sentinel.adapter.gateway.sc.exception.SentinelGatewayBlockExceptionHandler;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.beans.factory.ObjectProvider;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
|
import org.springframework.cloud.gateway.filter.GlobalFilter;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.core.Ordered;
|
||||||
|
import org.springframework.core.annotation.Order;
|
||||||
|
import org.springframework.http.codec.ServerCodecConfigurer;
|
||||||
|
import org.springframework.web.reactive.result.view.ViewResolver;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
@ConditionalOnClass(GlobalFilter.class)
|
||||||
|
@ConditionalOnProperty(prefix = "spring.cloud.sentinel.spring-cloud-gateway", name = "enabled", havingValue = "true",
|
||||||
|
matchIfMissing = true)
|
||||||
|
public class SentinelSpringCloudGatewayAutoConfiguration {
|
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory
|
||||||
|
.getLogger(SentinelSpringCloudGatewayAutoConfiguration.class);
|
||||||
|
|
||||||
|
private final List<ViewResolver> viewResolvers;
|
||||||
|
private final ServerCodecConfigurer serverCodecConfigurer;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private Optional<BlockRequestHandler> blockRequestHandlerOptional;
|
||||||
|
|
||||||
|
@PostConstruct
|
||||||
|
private void init() {
|
||||||
|
blockRequestHandlerOptional
|
||||||
|
.ifPresent(GatewayCallbackManager::setBlockHandler);
|
||||||
|
}
|
||||||
|
|
||||||
|
public SentinelSpringCloudGatewayAutoConfiguration(
|
||||||
|
ObjectProvider<List<ViewResolver>> viewResolversProvider,
|
||||||
|
ServerCodecConfigurer serverCodecConfigurer) {
|
||||||
|
this.viewResolvers = viewResolversProvider.getIfAvailable(Collections::emptyList);
|
||||||
|
this.serverCodecConfigurer = serverCodecConfigurer;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@Order(Ordered.HIGHEST_PRECEDENCE)
|
||||||
|
public SentinelGatewayBlockExceptionHandler sentinelGatewayBlockExceptionHandler() {
|
||||||
|
// Register the block exception handler for Spring Cloud Gateway.
|
||||||
|
logger.info("[Sentinel SpringCloudGateway] register SentinelGatewayBlockExceptionHandler");
|
||||||
|
return new SentinelGatewayBlockExceptionHandler(viewResolvers,
|
||||||
|
serverCodecConfigurer);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@Order(-1)
|
||||||
|
public GlobalFilter sentinelGatewayFilter() {
|
||||||
|
logger.info("[Sentinel SpringCloudGateway] register SentinelGatewayFilter");
|
||||||
|
return new SentinelGatewayFilter();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,115 @@
|
|||||||
|
/*
|
||||||
|
* 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 org.springframework.cloud.alibaba.sentinel.gateway;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import javax.annotation.PostConstruct;
|
||||||
|
|
||||||
|
import com.alibaba.csp.sentinel.adapter.gateway.zuul.callback.RequestOriginParser;
|
||||||
|
import com.alibaba.csp.sentinel.adapter.gateway.zuul.callback.ZuulGatewayCallbackManager;
|
||||||
|
import com.alibaba.csp.sentinel.adapter.gateway.zuul.filters.SentinelZuulErrorFilter;
|
||||||
|
import com.alibaba.csp.sentinel.adapter.gateway.zuul.filters.SentinelZuulPostFilter;
|
||||||
|
import com.alibaba.csp.sentinel.adapter.gateway.zuul.filters.SentinelZuulPreFilter;
|
||||||
|
|
||||||
|
import com.netflix.zuul.ZuulFilter;
|
||||||
|
import com.netflix.zuul.http.ZuulServlet;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
|
import org.springframework.cloud.alibaba.sentinel.gateway.handler.FallBackProviderHandler;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.core.env.Environment;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sentinel Spring Cloud Zuul AutoConfiguration
|
||||||
|
*
|
||||||
|
* @author tiger
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
@ConditionalOnClass(ZuulServlet.class)
|
||||||
|
@ConditionalOnProperty(prefix = SentinelZuulAutoConfiguration.PREFIX, name = "enabled", havingValue = "true",
|
||||||
|
matchIfMissing = true)
|
||||||
|
public class SentinelZuulAutoConfiguration {
|
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory
|
||||||
|
.getLogger(SentinelZuulAutoConfiguration.class);
|
||||||
|
|
||||||
|
public static final String PREFIX = "spring.cloud.sentinel.zuul";
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private Environment environment;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private Optional<RequestOriginParser> requestOriginParserOptional;
|
||||||
|
|
||||||
|
@PostConstruct
|
||||||
|
private void init() {
|
||||||
|
requestOriginParserOptional
|
||||||
|
.ifPresent(ZuulGatewayCallbackManager::setOriginParser);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public ZuulFilter sentinelZuulPreFilter() {
|
||||||
|
String preOrderStr = environment.getProperty(PREFIX + "." + "order.pre");
|
||||||
|
int order = 10000;
|
||||||
|
try {
|
||||||
|
order = Integer.parseInt(preOrderStr);
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
// ignore
|
||||||
|
}
|
||||||
|
logger.info("[Sentinel Zuul] register SentinelZuulPreFilter {}", order);
|
||||||
|
return new SentinelZuulPreFilter(order);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public ZuulFilter sentinelZuulPostFilter() {
|
||||||
|
String postOrderStr = environment.getProperty(PREFIX + "." + "order.post");
|
||||||
|
int order = 1000;
|
||||||
|
try {
|
||||||
|
order = Integer.parseInt(postOrderStr);
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
// ignore
|
||||||
|
}
|
||||||
|
logger.info("[Sentinel Zuul] register SentinelZuulPostFilter {}", order);
|
||||||
|
return new SentinelZuulPostFilter(order);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public ZuulFilter sentinelZuulErrorFilter() {
|
||||||
|
String errorOrderStr = environment.getProperty(PREFIX + "." + "order.error");
|
||||||
|
int order = -1;
|
||||||
|
try {
|
||||||
|
order = Integer.parseInt(errorOrderStr);
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
// ignore
|
||||||
|
}
|
||||||
|
logger.info("[Sentinel Zuul] register SentinelZuulErrorFilter {}", order);
|
||||||
|
return new SentinelZuulErrorFilter(order);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public FallBackProviderHandler fallBackProviderHandler(
|
||||||
|
DefaultListableBeanFactory beanFactory) {
|
||||||
|
return new FallBackProviderHandler(beanFactory);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,44 @@
|
|||||||
|
package org.springframework.cloud.alibaba.sentinel.gateway.handler;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import com.alibaba.csp.sentinel.adapter.gateway.zuul.fallback.DefaultBlockFallbackProvider;
|
||||||
|
import com.alibaba.csp.sentinel.adapter.gateway.zuul.fallback.ZuulBlockFallbackManager;
|
||||||
|
import com.alibaba.csp.sentinel.adapter.gateway.zuul.fallback.ZuulBlockFallbackProvider;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.beans.factory.SmartInitializingSingleton;
|
||||||
|
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||||
|
import org.springframework.util.CollectionUtils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author tiger
|
||||||
|
*/
|
||||||
|
public class FallBackProviderHandler implements SmartInitializingSingleton {
|
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory
|
||||||
|
.getLogger(FallBackProviderHandler.class);
|
||||||
|
|
||||||
|
private final DefaultListableBeanFactory beanFactory;
|
||||||
|
|
||||||
|
public FallBackProviderHandler(DefaultListableBeanFactory beanFactory) {
|
||||||
|
this.beanFactory = beanFactory;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void afterSingletonsInstantiated() {
|
||||||
|
Map<String, ZuulBlockFallbackProvider> providerMap = beanFactory
|
||||||
|
.getBeansOfType(ZuulBlockFallbackProvider.class);
|
||||||
|
if (!CollectionUtils.isEmpty(providerMap)) {
|
||||||
|
providerMap.forEach((k, v) -> {
|
||||||
|
logger.info("[Sentinel Zuul] Register provider name:{}, instance: {}", k,
|
||||||
|
v);
|
||||||
|
ZuulBlockFallbackManager.registerProvider(v);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
logger.info("[Sentinel Zuul] Register default fallback provider. ");
|
||||||
|
ZuulBlockFallbackManager.registerProvider(new DefaultBlockFallbackProvider());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,3 @@
|
|||||||
|
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||||
|
org.springframework.cloud.alibaba.sentinel.gateway.SentinelZuulAutoConfiguration,\
|
||||||
|
org.springframework.cloud.alibaba.sentinel.gateway.SentinelSpringCloudGatewayAutoConfiguration
|
@ -1,73 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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 org.springframework.cloud.alibaba.sentinel.zuul;
|
|
||||||
|
|
||||||
import static org.springframework.cloud.commons.util.InetUtilsProperties.PREFIX;
|
|
||||||
|
|
||||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
|
||||||
import org.springframework.cloud.alibaba.sentinel.zuul.handler.FallBackProviderHandler;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
|
|
||||||
import com.alibaba.csp.sentinel.adapter.gateway.zuul.callback.DefaultRequestOriginParser;
|
|
||||||
import com.alibaba.csp.sentinel.adapter.gateway.zuul.callback.RequestOriginParser;
|
|
||||||
import com.alibaba.csp.sentinel.adapter.gateway.zuul.filters.SentinelZuulErrorFilter;
|
|
||||||
import com.alibaba.csp.sentinel.adapter.gateway.zuul.filters.SentinelZuulPostFilter;
|
|
||||||
import com.alibaba.csp.sentinel.adapter.gateway.zuul.filters.SentinelZuulPreFilter;
|
|
||||||
|
|
||||||
import com.netflix.zuul.ZuulFilter;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sentinel Spring Cloud Zuul AutoConfiguration
|
|
||||||
*
|
|
||||||
* @author tiger
|
|
||||||
*/
|
|
||||||
@Configuration
|
|
||||||
@ConditionalOnProperty(prefix = PREFIX, name = "enabled", havingValue = "true", matchIfMissing = true)
|
|
||||||
public class SentinelZuulAutoConfiguration {
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
@ConditionalOnMissingBean(RequestOriginParser.class)
|
|
||||||
public RequestOriginParser requestOriginParser() {
|
|
||||||
return new DefaultRequestOriginParser();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public ZuulFilter sentinelZuulPreFilter() {
|
|
||||||
// We can also provider the filter order in the constructor.
|
|
||||||
return new SentinelZuulPreFilter();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public ZuulFilter sentinelZuulPostFilter() {
|
|
||||||
return new SentinelZuulPostFilter();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public ZuulFilter sentinelZuulErrorFilter() {
|
|
||||||
return new SentinelZuulErrorFilter();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public FallBackProviderHandler fallBackProviderListener(
|
|
||||||
DefaultListableBeanFactory beanFactory) {
|
|
||||||
return new FallBackProviderHandler(beanFactory);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,45 +0,0 @@
|
|||||||
package org.springframework.cloud.alibaba.sentinel.zuul.handler;
|
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import org.apache.commons.collections.MapUtils;
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
import org.springframework.beans.factory.SmartInitializingSingleton;
|
|
||||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
|
||||||
|
|
||||||
import com.alibaba.csp.sentinel.adapter.gateway.zuul.fallback.DefaultBlockFallbackProvider;
|
|
||||||
import com.alibaba.csp.sentinel.adapter.gateway.zuul.fallback.ZuulBlockFallbackManager;
|
|
||||||
import com.alibaba.csp.sentinel.adapter.gateway.zuul.fallback.ZuulBlockFallbackProvider;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author tiger
|
|
||||||
*/
|
|
||||||
public class FallBackProviderHandler implements SmartInitializingSingleton {
|
|
||||||
|
|
||||||
private static final Logger logger = LoggerFactory
|
|
||||||
.getLogger(FallBackProviderHandler.class);
|
|
||||||
|
|
||||||
private final DefaultListableBeanFactory beanFactory;
|
|
||||||
|
|
||||||
public FallBackProviderHandler(DefaultListableBeanFactory beanFactory) {
|
|
||||||
this.beanFactory = beanFactory;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void afterSingletonsInstantiated() {
|
|
||||||
Map<String, ZuulBlockFallbackProvider> providerMap = beanFactory
|
|
||||||
.getBeansOfType(ZuulBlockFallbackProvider.class);
|
|
||||||
if (MapUtils.isNotEmpty(providerMap)) {
|
|
||||||
providerMap.forEach((k, v) -> {
|
|
||||||
logger.info("[Sentinel Zuul] Register provider name:{}, instance: {}", k,
|
|
||||||
v);
|
|
||||||
ZuulBlockFallbackManager.registerProvider(v);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
logger.info("[Sentinel Zuul] Register default fallback provider. ");
|
|
||||||
ZuulBlockFallbackManager.registerProvider(new DefaultBlockFallbackProvider());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,2 +0,0 @@
|
|||||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
|
||||||
org.springframework.cloud.alibaba.sentinel.zuul.SentinelZuulAutoConfiguration
|
|
@ -105,6 +105,11 @@
|
|||||||
<scope>provided</scope>
|
<scope>provided</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.csp</groupId>
|
||||||
|
<artifactId>sentinel-api-gateway-adapter-common</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!--spring boot-->
|
<!--spring boot-->
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
|
@ -16,6 +16,12 @@
|
|||||||
|
|
||||||
package org.springframework.cloud.alibaba.sentinel.custom;
|
package org.springframework.cloud.alibaba.sentinel.custom;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Map.Entry;
|
||||||
|
|
||||||
import javax.annotation.PostConstruct;
|
import javax.annotation.PostConstruct;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@ -31,8 +37,14 @@ import org.springframework.cloud.alibaba.sentinel.datasource.converter.XmlConver
|
|||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.core.env.Environment;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
|
import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition;
|
||||||
|
import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPathPredicateItem;
|
||||||
|
import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPredicateGroupItem;
|
||||||
|
import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPredicateItem;
|
||||||
|
import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule;
|
||||||
import com.alibaba.csp.sentinel.adapter.servlet.config.WebServletConfig;
|
import com.alibaba.csp.sentinel.adapter.servlet.config.WebServletConfig;
|
||||||
import com.alibaba.csp.sentinel.annotation.aspectj.SentinelResourceAspect;
|
import com.alibaba.csp.sentinel.annotation.aspectj.SentinelResourceAspect;
|
||||||
import com.alibaba.csp.sentinel.config.SentinelConfig;
|
import com.alibaba.csp.sentinel.config.SentinelConfig;
|
||||||
@ -46,8 +58,15 @@ import com.alibaba.csp.sentinel.slots.system.SystemRule;
|
|||||||
import com.alibaba.csp.sentinel.transport.config.TransportConfig;
|
import com.alibaba.csp.sentinel.transport.config.TransportConfig;
|
||||||
import com.alibaba.csp.sentinel.util.AppNameUtil;
|
import com.alibaba.csp.sentinel.util.AppNameUtil;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonParser;
|
||||||
|
import com.fasterxml.jackson.core.Version;
|
||||||
|
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||||
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
|
||||||
|
import com.fasterxml.jackson.databind.module.SimpleModule;
|
||||||
|
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||||
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
|
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -152,81 +171,164 @@ public class SentinelAutoConfiguration {
|
|||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public SentinelDataSourceHandler sentinelDataSourceHandler(
|
public SentinelDataSourceHandler sentinelDataSourceHandler(
|
||||||
DefaultListableBeanFactory beanFactory) {
|
DefaultListableBeanFactory beanFactory, SentinelProperties sentinelProperties,
|
||||||
return new SentinelDataSourceHandler(beanFactory);
|
Environment env) {
|
||||||
|
return new SentinelDataSourceHandler(beanFactory, sentinelProperties, env);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ConditionalOnClass(ObjectMapper.class)
|
@ConditionalOnClass(ObjectMapper.class)
|
||||||
|
@Configuration
|
||||||
protected static class SentinelConverterConfiguration {
|
protected static class SentinelConverterConfiguration {
|
||||||
|
|
||||||
private ObjectMapper objectMapper = new ObjectMapper();
|
static class ApiPredicateItemDeserializer
|
||||||
|
extends StdDeserializer<ApiPredicateItem> {
|
||||||
|
private Map<String, Class<? extends ApiPredicateItem>> registry = new HashMap<String, Class<? extends ApiPredicateItem>>();
|
||||||
|
|
||||||
public SentinelConverterConfiguration() {
|
ApiPredicateItemDeserializer() {
|
||||||
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,
|
super(ApiPredicateItem.class);
|
||||||
false);
|
}
|
||||||
|
|
||||||
|
void registerApiPredicateItem(String uniqueAttribute,
|
||||||
|
Class<? extends ApiPredicateItem> apiPredicateItemClass) {
|
||||||
|
registry.put(uniqueAttribute, apiPredicateItemClass);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ApiPredicateItem deserialize(JsonParser jp,
|
||||||
|
DeserializationContext ctxt) throws IOException {
|
||||||
|
ObjectMapper mapper = (ObjectMapper) jp.getCodec();
|
||||||
|
ObjectNode root = mapper.readTree(jp);
|
||||||
|
Class<? extends ApiPredicateItem> apiPredicateItemClass = null;
|
||||||
|
Iterator<Entry<String, JsonNode>> elementsIterator = root.fields();
|
||||||
|
while (elementsIterator.hasNext()) {
|
||||||
|
Entry<String, JsonNode> element = elementsIterator.next();
|
||||||
|
String name = element.getKey();
|
||||||
|
if (registry.containsKey(name)) {
|
||||||
|
apiPredicateItemClass = registry.get(name);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (apiPredicateItemClass == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return mapper.readValue(root.toString(), apiPredicateItemClass);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean("sentinel-json-flow-converter")
|
@Configuration
|
||||||
public JsonConverter jsonFlowConverter() {
|
protected static class SentinelJsonConfiguration {
|
||||||
return new JsonConverter(objectMapper, FlowRule.class);
|
|
||||||
|
private ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
|
||||||
|
public SentinelJsonConfiguration() {
|
||||||
|
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,
|
||||||
|
false);
|
||||||
|
|
||||||
|
ApiPredicateItemDeserializer deserializer = new ApiPredicateItemDeserializer();
|
||||||
|
deserializer.registerApiPredicateItem("pattern",
|
||||||
|
ApiPathPredicateItem.class);
|
||||||
|
deserializer.registerApiPredicateItem("items",
|
||||||
|
ApiPredicateGroupItem.class);
|
||||||
|
SimpleModule module = new SimpleModule(
|
||||||
|
"PolymorphicApiPredicateItemDeserializerModule",
|
||||||
|
new Version(1, 0, 0, null));
|
||||||
|
module.addDeserializer(ApiPredicateItem.class, deserializer);
|
||||||
|
objectMapper.registerModule(module);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean("sentinel-json-flow-converter")
|
||||||
|
public JsonConverter jsonFlowConverter() {
|
||||||
|
return new JsonConverter(objectMapper, FlowRule.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean("sentinel-json-degrade-converter")
|
||||||
|
public JsonConverter jsonDegradeConverter() {
|
||||||
|
return new JsonConverter(objectMapper, DegradeRule.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean("sentinel-json-system-converter")
|
||||||
|
public JsonConverter jsonSystemConverter() {
|
||||||
|
return new JsonConverter(objectMapper, SystemRule.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean("sentinel-json-authority-converter")
|
||||||
|
public JsonConverter jsonAuthorityConverter() {
|
||||||
|
return new JsonConverter(objectMapper, AuthorityRule.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean("sentinel-json-param-flow-converter")
|
||||||
|
public JsonConverter jsonParamFlowConverter() {
|
||||||
|
return new JsonConverter(objectMapper, ParamFlowRule.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean("sentinel-json-gw-flow-converter")
|
||||||
|
public JsonConverter jsonGatewayFlowConverter() {
|
||||||
|
return new JsonConverter(objectMapper, GatewayFlowRule.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean("sentinel-json-gw-api-group-converter")
|
||||||
|
public JsonConverter jsonApiConverter() {
|
||||||
|
return new JsonConverter(objectMapper, ApiDefinition.class);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean("sentinel-json-degrade-converter")
|
@ConditionalOnClass(XmlMapper.class)
|
||||||
public JsonConverter jsonDegradeConverter() {
|
@Configuration
|
||||||
return new JsonConverter(objectMapper, DegradeRule.class);
|
protected static class SentinelXmlConfiguration {
|
||||||
|
|
||||||
|
private XmlMapper xmlMapper = new XmlMapper();
|
||||||
|
|
||||||
|
public SentinelXmlConfiguration() {
|
||||||
|
xmlMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,
|
||||||
|
false);
|
||||||
|
ApiPredicateItemDeserializer deserializer = new ApiPredicateItemDeserializer();
|
||||||
|
deserializer.registerApiPredicateItem("pattern",
|
||||||
|
ApiPathPredicateItem.class);
|
||||||
|
deserializer.registerApiPredicateItem("items",
|
||||||
|
ApiPredicateGroupItem.class);
|
||||||
|
SimpleModule module = new SimpleModule(
|
||||||
|
"PolymorphicGatewayDeserializerModule",
|
||||||
|
new Version(1, 0, 0, null));
|
||||||
|
module.addDeserializer(ApiPredicateItem.class, deserializer);
|
||||||
|
xmlMapper.registerModule(module);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean("sentinel-xml-flow-converter")
|
||||||
|
public XmlConverter xmlFlowConverter() {
|
||||||
|
return new XmlConverter(xmlMapper, FlowRule.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean("sentinel-xml-degrade-converter")
|
||||||
|
public XmlConverter xmlDegradeConverter() {
|
||||||
|
return new XmlConverter(xmlMapper, DegradeRule.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean("sentinel-xml-system-converter")
|
||||||
|
public XmlConverter xmlSystemConverter() {
|
||||||
|
return new XmlConverter(xmlMapper, SystemRule.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean("sentinel-xml-authority-converter")
|
||||||
|
public XmlConverter xmlAuthorityConverter() {
|
||||||
|
return new XmlConverter(xmlMapper, AuthorityRule.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean("sentinel-xml-param-flow-converter")
|
||||||
|
public XmlConverter xmlParamFlowConverter() {
|
||||||
|
return new XmlConverter(xmlMapper, ParamFlowRule.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean("sentinel-xml-gw-flow-converter")
|
||||||
|
public XmlConverter xmlGatewayFlowConverter() {
|
||||||
|
return new XmlConverter(xmlMapper, GatewayFlowRule.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean("sentinel-xml-gw-api-group-converter")
|
||||||
|
public XmlConverter xmlApiConverter() {
|
||||||
|
return new XmlConverter(xmlMapper, ApiDefinition.class);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean("sentinel-json-system-converter")
|
|
||||||
public JsonConverter jsonSystemConverter() {
|
|
||||||
return new JsonConverter(objectMapper, SystemRule.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean("sentinel-json-authority-converter")
|
|
||||||
public JsonConverter jsonAuthorityConverter() {
|
|
||||||
return new JsonConverter(objectMapper, AuthorityRule.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean("sentinel-json-param-flow-converter")
|
|
||||||
public JsonConverter jsonParamFlowConverter() {
|
|
||||||
return new JsonConverter(objectMapper, ParamFlowRule.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@ConditionalOnClass(XmlMapper.class)
|
|
||||||
protected static class SentinelXmlConfiguration {
|
|
||||||
|
|
||||||
private XmlMapper xmlMapper = new XmlMapper();
|
|
||||||
|
|
||||||
public SentinelXmlConfiguration() {
|
|
||||||
xmlMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean("sentinel-xml-flow-converter")
|
|
||||||
public XmlConverter xmlFlowConverter() {
|
|
||||||
return new XmlConverter(xmlMapper, FlowRule.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean("sentinel-xml-degrade-converter")
|
|
||||||
public XmlConverter xmlDegradeConverter() {
|
|
||||||
return new XmlConverter(xmlMapper, DegradeRule.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean("sentinel-xml-system-converter")
|
|
||||||
public XmlConverter xmlSystemConverter() {
|
|
||||||
return new XmlConverter(xmlMapper, SystemRule.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean("sentinel-xml-authority-converter")
|
|
||||||
public XmlConverter xmlAuthorityConverter() {
|
|
||||||
return new XmlConverter(xmlMapper, AuthorityRule.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean("sentinel-xml-param-flow-converter")
|
|
||||||
public XmlConverter xmlParamFlowConverter() {
|
|
||||||
return new XmlConverter(xmlMapper, ParamFlowRule.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,29 +1,30 @@
|
|||||||
package org.springframework.cloud.alibaba.sentinel.custom;
|
package org.springframework.cloud.alibaba.sentinel.custom;
|
||||||
|
|
||||||
import com.alibaba.csp.sentinel.datasource.AbstractDataSource;
|
import java.lang.reflect.Field;
|
||||||
import com.alibaba.csp.sentinel.datasource.ReadableDataSource;
|
import java.util.Arrays;
|
||||||
import com.alibaba.csp.sentinel.slots.block.AbstractRule;
|
import java.util.Collection;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.SmartInitializingSingleton;
|
import org.springframework.beans.factory.SmartInitializingSingleton;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||||
import org.springframework.cloud.alibaba.sentinel.SentinelProperties;
|
import org.springframework.cloud.alibaba.sentinel.SentinelProperties;
|
||||||
import org.springframework.cloud.alibaba.sentinel.datasource.config.AbstractDataSourceProperties;
|
import org.springframework.cloud.alibaba.sentinel.datasource.config.AbstractDataSourceProperties;
|
||||||
import org.springframework.cloud.alibaba.sentinel.datasource.converter.JsonConverter;
|
import org.springframework.cloud.alibaba.sentinel.datasource.converter.JsonConverter;
|
||||||
import org.springframework.cloud.alibaba.sentinel.datasource.converter.XmlConverter;
|
import org.springframework.cloud.alibaba.sentinel.datasource.converter.XmlConverter;
|
||||||
|
import org.springframework.core.env.Environment;
|
||||||
import org.springframework.util.CollectionUtils;
|
import org.springframework.util.CollectionUtils;
|
||||||
import org.springframework.util.ReflectionUtils;
|
import org.springframework.util.ReflectionUtils;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
import com.alibaba.csp.sentinel.datasource.AbstractDataSource;
|
||||||
import java.util.Arrays;
|
import com.alibaba.csp.sentinel.datasource.ReadableDataSource;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sentinel {@link ReadableDataSource} Handler Handle the configurations of
|
* Sentinel {@link ReadableDataSource} Handler Handle the configurations of
|
||||||
@ -47,12 +48,16 @@ public class SentinelDataSourceHandler implements SmartInitializingSingleton {
|
|||||||
|
|
||||||
private final DefaultListableBeanFactory beanFactory;
|
private final DefaultListableBeanFactory beanFactory;
|
||||||
|
|
||||||
public SentinelDataSourceHandler(DefaultListableBeanFactory beanFactory) {
|
private final SentinelProperties sentinelProperties;
|
||||||
this.beanFactory = beanFactory;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Autowired
|
private final Environment env;
|
||||||
private SentinelProperties sentinelProperties;
|
|
||||||
|
public SentinelDataSourceHandler(DefaultListableBeanFactory beanFactory,
|
||||||
|
SentinelProperties sentinelProperties, Environment env) {
|
||||||
|
this.beanFactory = beanFactory;
|
||||||
|
this.sentinelProperties = sentinelProperties;
|
||||||
|
this.env = env;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void afterSingletonsInstantiated() {
|
public void afterSingletonsInstantiated() {
|
||||||
@ -68,6 +73,7 @@ public class SentinelDataSourceHandler implements SmartInitializingSingleton {
|
|||||||
}
|
}
|
||||||
AbstractDataSourceProperties abstractDataSourceProperties = dataSourceProperties
|
AbstractDataSourceProperties abstractDataSourceProperties = dataSourceProperties
|
||||||
.getValidDataSourceProperties();
|
.getValidDataSourceProperties();
|
||||||
|
abstractDataSourceProperties.setEnv(env);
|
||||||
abstractDataSourceProperties.preCheck(dataSourceName);
|
abstractDataSourceProperties.preCheck(dataSourceName);
|
||||||
registerBean(abstractDataSourceProperties, dataSourceName
|
registerBean(abstractDataSourceProperties, dataSourceName
|
||||||
+ "-sentinel-" + validFields.get(0) + "-datasource");
|
+ "-sentinel-" + validFields.get(0) + "-datasource");
|
||||||
@ -189,7 +195,7 @@ public class SentinelDataSourceHandler implements SmartInitializingSingleton {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void logAndCheckRuleType(AbstractDataSource dataSource, String dataSourceName,
|
private void logAndCheckRuleType(AbstractDataSource dataSource, String dataSourceName,
|
||||||
Class<? extends AbstractRule> ruleClass) {
|
Class ruleClass) {
|
||||||
Object ruleConfig;
|
Object ruleConfig;
|
||||||
try {
|
try {
|
||||||
ruleConfig = dataSource.loadConfig();
|
ruleConfig = dataSource.loadConfig();
|
||||||
@ -199,8 +205,8 @@ public class SentinelDataSourceHandler implements SmartInitializingSingleton {
|
|||||||
+ " loadConfig error: " + e.getMessage(), e);
|
+ " loadConfig error: " + e.getMessage(), e);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (ruleConfig instanceof List) {
|
if (ruleConfig instanceof List || ruleConfig instanceof Set) {
|
||||||
List convertedRuleList = (List) ruleConfig;
|
Collection convertedRuleList = (Collection) ruleConfig;
|
||||||
if (CollectionUtils.isEmpty(convertedRuleList)) {
|
if (CollectionUtils.isEmpty(convertedRuleList)) {
|
||||||
log.warn("[Sentinel Starter] DataSource {} rule list is empty.",
|
log.warn("[Sentinel Starter] DataSource {} rule list is empty.",
|
||||||
dataSourceName);
|
dataSourceName);
|
||||||
|
@ -25,7 +25,7 @@ import feign.MethodMetadata;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Using static field {@link SentinelContractHolder#metadataMap} to hold
|
* Using static field {@link SentinelContractHolder#METADATA_MAP} to hold
|
||||||
* {@link MethodMetadata} data
|
* {@link MethodMetadata} data
|
||||||
*
|
*
|
||||||
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
||||||
@ -38,7 +38,7 @@ public class SentinelContractHolder implements Contract {
|
|||||||
* map key is constructed by ClassFullName + configKey. configKey is constructed by
|
* map key is constructed by ClassFullName + configKey. configKey is constructed by
|
||||||
* {@link feign.Feign#configKey}
|
* {@link feign.Feign#configKey}
|
||||||
*/
|
*/
|
||||||
public final static Map<String, MethodMetadata> metadataMap = new HashMap();
|
public final static Map<String, MethodMetadata> METADATA_MAP = new HashMap<>();
|
||||||
|
|
||||||
public SentinelContractHolder(Contract delegate) {
|
public SentinelContractHolder(Contract delegate) {
|
||||||
this.delegate = delegate;
|
this.delegate = delegate;
|
||||||
@ -47,7 +47,7 @@ public class SentinelContractHolder implements Contract {
|
|||||||
@Override
|
@Override
|
||||||
public List<MethodMetadata> parseAndValidatateMetadata(Class<?> targetType) {
|
public List<MethodMetadata> parseAndValidatateMetadata(Class<?> targetType) {
|
||||||
List<MethodMetadata> metadatas = delegate.parseAndValidatateMetadata(targetType);
|
List<MethodMetadata> metadatas = delegate.parseAndValidatateMetadata(targetType);
|
||||||
metadatas.forEach(metadata -> metadataMap
|
metadatas.forEach(metadata -> METADATA_MAP
|
||||||
.put(targetType.getName() + metadata.configKey(), metadata));
|
.put(targetType.getName() + metadata.configKey(), metadata));
|
||||||
return metadatas;
|
return metadatas;
|
||||||
}
|
}
|
||||||
|
@ -90,9 +90,9 @@ public class SentinelInvocationHandler implements InvocationHandler {
|
|||||||
// only handle by HardCodedTarget
|
// only handle by HardCodedTarget
|
||||||
if (target instanceof Target.HardCodedTarget) {
|
if (target instanceof Target.HardCodedTarget) {
|
||||||
Target.HardCodedTarget hardCodedTarget = (Target.HardCodedTarget) target;
|
Target.HardCodedTarget hardCodedTarget = (Target.HardCodedTarget) target;
|
||||||
MethodMetadata methodMetadata = SentinelContractHolder.metadataMap
|
MethodMetadata methodMetadata = SentinelContractHolder.METADATA_MAP
|
||||||
.get(method.getDeclaringClass().getName()
|
.get(hardCodedTarget.type().getName()
|
||||||
+ Feign.configKey(method.getDeclaringClass(), method));
|
+ Feign.configKey(hardCodedTarget.type(), method));
|
||||||
// resource default is HttpMethod:protocol://url
|
// resource default is HttpMethod:protocol://url
|
||||||
String resourceName = methodMetadata.template().method().toUpperCase() + ":"
|
String resourceName = methodMetadata.template().method().toUpperCase() + ":"
|
||||||
+ hardCodedTarget.url() + methodMetadata.template().path();
|
+ hardCodedTarget.url() + methodMetadata.template().path();
|
||||||
|
@ -60,6 +60,9 @@ public class SentinelFeignTests {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private BarService barService;
|
private BarService barService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private BazService bazService;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
FlowRule rule1 = new FlowRule();
|
FlowRule rule1 = new FlowRule();
|
||||||
@ -83,7 +86,14 @@ public class SentinelFeignTests {
|
|||||||
rule3.setLimitApp("default");
|
rule3.setLimitApp("default");
|
||||||
rule3.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_DEFAULT);
|
rule3.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_DEFAULT);
|
||||||
rule3.setStrategy(RuleConstant.STRATEGY_DIRECT);
|
rule3.setStrategy(RuleConstant.STRATEGY_DIRECT);
|
||||||
FlowRuleManager.loadRules(Arrays.asList(rule1, rule2, rule3));
|
FlowRule rule4 = new FlowRule();
|
||||||
|
rule4.setGrade(RuleConstant.FLOW_GRADE_QPS);
|
||||||
|
rule4.setCount(0);
|
||||||
|
rule4.setResource("GET:http://baz-service/baz");
|
||||||
|
rule4.setLimitApp("default");
|
||||||
|
rule4.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_DEFAULT);
|
||||||
|
rule4.setStrategy(RuleConstant.STRATEGY_DIRECT);
|
||||||
|
FlowRuleManager.loadRules(Arrays.asList(rule1, rule2, rule3,rule4));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -101,6 +111,9 @@ public class SentinelFeignTests {
|
|||||||
assertThatExceptionOfType(Exception.class).isThrownBy(() -> {
|
assertThatExceptionOfType(Exception.class).isThrownBy(() -> {
|
||||||
barService.bar();
|
barService.bar();
|
||||||
});
|
});
|
||||||
|
assertThatExceptionOfType(Exception.class).isThrownBy(() -> {
|
||||||
|
bazService.baz();
|
||||||
|
});
|
||||||
|
|
||||||
assertNotEquals("ToString method invoke was not in SentinelInvocationHandler",
|
assertNotEquals("ToString method invoke was not in SentinelInvocationHandler",
|
||||||
echoService.toString(), fooService.toString());
|
echoService.toString(), fooService.toString());
|
||||||
@ -146,6 +159,15 @@ public class SentinelFeignTests {
|
|||||||
String bar();
|
String bar();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public interface BazService {
|
||||||
|
@RequestMapping(path = "baz")
|
||||||
|
String baz();
|
||||||
|
}
|
||||||
|
|
||||||
|
@FeignClient(value = "baz-service")
|
||||||
|
public interface BazClient extends BazService {
|
||||||
|
}
|
||||||
|
|
||||||
public static class EchoServiceFallback implements EchoService {
|
public static class EchoServiceFallback implements EchoService {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -12,12 +12,12 @@ public class SmsProperties {
|
|||||||
/**
|
/**
|
||||||
* Product name.
|
* Product name.
|
||||||
*/
|
*/
|
||||||
public static final String smsProduct = "Dysmsapi";
|
public static final String SMS_PRODUCT = "Dysmsapi";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Product domain.
|
* Product domain.
|
||||||
*/
|
*/
|
||||||
public static final String smsDomain = "dysmsapi.aliyuncs.com";
|
public static final String SMS_DOMAIN = "dysmsapi.aliyuncs.com";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Report queue name.
|
* Report queue name.
|
||||||
|
@ -60,7 +60,7 @@ public class SmsInitializerEventListener
|
|||||||
// 初始化acsClient,暂不支持region化
|
// 初始化acsClient,暂不支持region化
|
||||||
try {
|
try {
|
||||||
DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou",
|
DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou",
|
||||||
SmsProperties.smsProduct, SmsProperties.smsDomain);
|
SmsProperties.SMS_PRODUCT, SmsProperties.SMS_DOMAIN);
|
||||||
Collection<MessageListener> messageListeners = event.getApplicationContext()
|
Collection<MessageListener> messageListeners = event.getApplicationContext()
|
||||||
.getBeansOfType(MessageListener.class).values();
|
.getBeansOfType(MessageListener.class).values();
|
||||||
if (messageListeners.isEmpty()) {
|
if (messageListeners.isEmpty()) {
|
||||||
|
@ -85,8 +85,8 @@ public class DefaultAlicomMessagePuller {
|
|||||||
this.executorService = executorService;
|
this.executorService = executorService;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static final Map<String, Object> sLockObjMap = new HashMap<String, Object>();
|
protected static final Map<String, Object> S_LOCK_OBJ_MAP = new HashMap<>();
|
||||||
protected static Map<String, Boolean> sPollingMap = new ConcurrentHashMap<String, Boolean>();
|
protected static Map<String, Boolean> sPollingMap = new ConcurrentHashMap<>();
|
||||||
protected Object lockObj;
|
protected Object lockObj;
|
||||||
|
|
||||||
public boolean setPolling(String queueName) {
|
public boolean setPolling(String queueName) {
|
||||||
@ -309,11 +309,11 @@ public class DefaultAlicomMessagePuller {
|
|||||||
task.messageType = messageType;
|
task.messageType = messageType;
|
||||||
task.queueName = queueName;
|
task.queueName = queueName;
|
||||||
|
|
||||||
synchronized (sLockObjMap) {
|
synchronized (S_LOCK_OBJ_MAP) {
|
||||||
lockObj = sLockObjMap.get(queueName);
|
lockObj = S_LOCK_OBJ_MAP.get(queueName);
|
||||||
if (lockObj == null) {
|
if (lockObj == null) {
|
||||||
lockObj = new Object();
|
lockObj = new Object();
|
||||||
sLockObjMap.put(queueName, lockObj);
|
S_LOCK_OBJ_MAP.put(queueName, lockObj);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -355,11 +355,11 @@ public class DefaultAlicomMessagePuller {
|
|||||||
task.messageType = messageType;
|
task.messageType = messageType;
|
||||||
task.queueName = queueName;
|
task.queueName = queueName;
|
||||||
|
|
||||||
synchronized (sLockObjMap) {
|
synchronized (S_LOCK_OBJ_MAP) {
|
||||||
lockObj = sLockObjMap.get(queueName);
|
lockObj = S_LOCK_OBJ_MAP.get(queueName);
|
||||||
if (lockObj == null) {
|
if (lockObj == null) {
|
||||||
lockObj = new Object();
|
lockObj = new Object();
|
||||||
sLockObjMap.put(queueName, lockObj);
|
S_LOCK_OBJ_MAP.put(queueName, lockObj);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -402,11 +402,11 @@ public class DefaultAlicomMessagePuller {
|
|||||||
task.messageType = messageType;
|
task.messageType = messageType;
|
||||||
task.queueName = queueName;
|
task.queueName = queueName;
|
||||||
|
|
||||||
synchronized (sLockObjMap) {
|
synchronized (S_LOCK_OBJ_MAP) {
|
||||||
lockObj = sLockObjMap.get(queueName);
|
lockObj = S_LOCK_OBJ_MAP.get(queueName);
|
||||||
if (lockObj == null) {
|
if (lockObj == null) {
|
||||||
lockObj = new Object();
|
lockObj = new Object();
|
||||||
sLockObjMap.put(queueName, lockObj);
|
S_LOCK_OBJ_MAP.put(queueName, lockObj);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -76,6 +76,7 @@ public class QueryTokenForMnsQueueRequest
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public Class<QueryTokenForMnsQueueResponse> getResponseClass() {
|
public Class<QueryTokenForMnsQueueResponse> getResponseClass() {
|
||||||
return QueryTokenForMnsQueueResponse.class;
|
return QueryTokenForMnsQueueResponse.class;
|
||||||
}
|
}
|
||||||
|
@ -60,6 +60,7 @@ public class QueryTokenForMnsQueueResponse extends AcsResponse {
|
|||||||
this.messageTokenDTO = messageTokenDTO;
|
this.messageTokenDTO = messageTokenDTO;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public QueryTokenForMnsQueueResponse getInstance(UnmarshallerContext context) {
|
public QueryTokenForMnsQueueResponse getInstance(UnmarshallerContext context) {
|
||||||
return QueryTokenForMnsQueueResponseUnmarshaller.unmarshall(this, context);
|
return QueryTokenForMnsQueueResponseUnmarshaller.unmarshall(this, context);
|
||||||
}
|
}
|
||||||
|
@ -50,7 +50,7 @@ public class TokenGetterForAlicom {
|
|||||||
private String domainForPop;
|
private String domainForPop;
|
||||||
private IAcsClient iAcsClient;
|
private IAcsClient iAcsClient;
|
||||||
private Long ownerId;
|
private Long ownerId;
|
||||||
private final static String productName = "Dybaseapi";
|
private final static String PRODUCT_NAME = "Dybaseapi";
|
||||||
private long bufferTime = 1000 * 60 * 2;// 过期时间小于2分钟则重新获取,防止服务器时间误差
|
private long bufferTime = 1000 * 60 * 2;// 过期时间小于2分钟则重新获取,防止服务器时间误差
|
||||||
private final Object lock = new Object();
|
private final Object lock = new Object();
|
||||||
private ConcurrentMap<String, TokenForAlicom> tokenMap = new ConcurrentHashMap<String, TokenForAlicom>();
|
private ConcurrentMap<String, TokenForAlicom> tokenMap = new ConcurrentHashMap<String, TokenForAlicom>();
|
||||||
@ -68,7 +68,7 @@ public class TokenGetterForAlicom {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void init() throws ClientException {
|
private void init() throws ClientException {
|
||||||
DefaultProfile.addEndpoint(endpointNameForPop, regionIdForPop, productName,
|
DefaultProfile.addEndpoint(endpointNameForPop, regionIdForPop, PRODUCT_NAME,
|
||||||
domainForPop);
|
domainForPop);
|
||||||
IClientProfile profile = DefaultProfile.getProfile(regionIdForPop, accessKeyId,
|
IClientProfile profile = DefaultProfile.getProfile(regionIdForPop, accessKeyId,
|
||||||
accessKeySecret);
|
accessKeySecret);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user