seasidesky GitHub пре 4 година
родитељ
комит
cfea7ae338
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
29 измењених фајлова са 1844 додато и 0 уклоњено
  1. +1
    -0
      sentinel-adapter/pom.xml
  2. +79
    -0
      sentinel-adapter/sentinel-jax-rs-adapter/README.md
  3. +74
    -0
      sentinel-adapter/sentinel-jax-rs-adapter/pom.xml
  4. +79
    -0
      sentinel-adapter/sentinel-jax-rs-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/jaxrs/SentinelJaxRsClientTemplate.java
  5. +88
    -0
      sentinel-adapter/sentinel-jax-rs-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/jaxrs/SentinelJaxRsProviderFilter.java
  6. +62
    -0
      sentinel-adapter/sentinel-jax-rs-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/jaxrs/config/SentinelJaxRsConfig.java
  7. +39
    -0
      sentinel-adapter/sentinel-jax-rs-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/jaxrs/exception/DefaultExceptionMapper.java
  8. +45
    -0
      sentinel-adapter/sentinel-jax-rs-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/jaxrs/fallback/DefaultSentinelJaxRsFallback.java
  9. +43
    -0
      sentinel-adapter/sentinel-jax-rs-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/jaxrs/fallback/SentinelJaxRsFallback.java
  10. +81
    -0
      sentinel-adapter/sentinel-jax-rs-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/jaxrs/future/FutureWrapper.java
  11. +31
    -0
      sentinel-adapter/sentinel-jax-rs-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/jaxrs/request/DefaultRequestOriginParser.java
  12. +33
    -0
      sentinel-adapter/sentinel-jax-rs-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/jaxrs/request/DefaultResourceNameParser.java
  13. +34
    -0
      sentinel-adapter/sentinel-jax-rs-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/jaxrs/request/RequestOriginParser.java
  14. +27
    -0
      sentinel-adapter/sentinel-jax-rs-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/jaxrs/request/ResourceNameParser.java
  15. +413
    -0
      sentinel-adapter/sentinel-jax-rs-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/jaxrs/ClientFilterTest.java
  16. +257
    -0
      sentinel-adapter/sentinel-jax-rs-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/jaxrs/ProviderFilterTest.java
  17. +30
    -0
      sentinel-adapter/sentinel-jax-rs-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/jaxrs/TestApplication.java
  18. +92
    -0
      sentinel-adapter/sentinel-jax-rs-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/jaxrs/TestResource.java
  19. +3
    -0
      sentinel-adapter/sentinel-jax-rs-adapter/src/test/resources/application-client.yml
  20. +3
    -0
      sentinel-adapter/sentinel-jax-rs-adapter/src/test/resources/application-provider.yml
  21. +1
    -0
      sentinel-demo/pom.xml
  22. +49
    -0
      sentinel-demo/sentinel-demo-jax-rs/pom.xml
  23. +38
    -0
      sentinel-demo/sentinel-demo-jax-rs/src/main/java/com/alibaba/csp/sentinel/demo/jaxrs/CustomExceptionMapper.java
  24. +62
    -0
      sentinel-demo/sentinel-demo-jax-rs/src/main/java/com/alibaba/csp/sentinel/demo/jaxrs/HelloEntity.java
  25. +63
    -0
      sentinel-demo/sentinel-demo-jax-rs/src/main/java/com/alibaba/csp/sentinel/demo/jaxrs/HelloResource.java
  26. +51
    -0
      sentinel-demo/sentinel-demo-jax-rs/src/main/java/com/alibaba/csp/sentinel/demo/jaxrs/JaxRsClientDemo.java
  27. +33
    -0
      sentinel-demo/sentinel-demo-jax-rs/src/main/java/com/alibaba/csp/sentinel/demo/jaxrs/JaxRsDemoApplication.java
  28. +32
    -0
      sentinel-demo/sentinel-demo-jax-rs/src/main/java/com/alibaba/csp/sentinel/demo/jaxrs/SentinelJaxRsConfig.java
  29. +1
    -0
      sentinel-demo/sentinel-demo-jax-rs/src/main/resources/application.properties

+ 1
- 0
sentinel-adapter/pom.xml Прегледај датотеку

@@ -27,6 +27,7 @@
<module>sentinel-spring-cloud-gateway-adapter</module>
<module>sentinel-spring-webmvc-adapter</module>
<module>sentinel-zuul2-adapter</module>
<module>sentinel-jax-rs-adapter</module>
</modules>

<dependencyManagement>


+ 79
- 0
sentinel-adapter/sentinel-jax-rs-adapter/README.md Прегледај датотеку

@@ -0,0 +1,79 @@
# Sentinel for jax-rs

Sentinel provides filter integration to enable flow control for web requests.
Add the following dependency in `pom.xml` (if you are using Maven):

```xml
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-jax-rs-adapter</artifactId>
<version>x.y.z</version>
</dependency>
```

## SentinelJaxRsProviderFilter

the `SentinelJaxRsProviderFilter` is auto activated in pure jax-rs application.

For Spring web applications you can configure with Spring bean:

```java
@Configuration
public class FilterConfig {

@Bean
public SentinelJaxRsProviderFilter sentinelJaxRsProviderFilter() {
return new SentinelJaxRsProviderFilter();
}
}
```

## DefaultExceptionMapper

Sentinel provides DefaultExceptionMapper to map Throwable to Response(with Status.INTERNAL_SERVER_ERROR), in order to let SentinelJaxRsProviderFilter to be called and exit the sentinel entry.

according to `3.3.4 Exceptions` of [jaxrs-2_1-final-spec](https://download.oracle.com/otn-pub/jcp/jaxrs-2_1-final-eval-spec/jaxrs-2_1-final-spec.pdf):
>Checked exceptions and throwables that have not been mapped and cannot be thrown directly MUSTbe wrapped in a container-specific exception that is then thrown and allowed to propagate to the un-derlying container.

if WebApplicationException or its subclasses is thrown, there are automated converted to Response and can enter response filter.

if throw exception which is not WebApplicationException or its subclass, and not matched by custom exception mapper, then the response filter cannot be called. for this case, I thank a default exception mapper maybe introduced.

according to `4.4 Exception Mapping Providers` of [jaxrs-2_1-final-spec](https://download.oracle.com/otn-pub/jcp/jaxrs-2_1-final-eval-spec/jaxrs-2_1-final-spec.pdf):
>When choosing an exception mapping provider to map an exception, an implementation MUST use theprovider whose generic type is the nearest superclass of the exception. If two or more exception providers are applicable, the one with the highest priority MUST be chosen as described in Section 4.1.3.

if user also provide custom exception mapper of `Throwable`, then user has the responsibility to convert it to response and then the response filter can be called.

as describe in `6.7.1 exceptions` of [jaxrs-2_1-final-spec](https://download.oracle.com/otn-pub/jcp/jaxrs-2_1-final-eval-spec/jaxrs-2_1-final-spec.pdf):
>A response mapped from an exception MUST be processed using the ContainerResponsefilter chain and theWriteTointerceptor chain (if an entity is present in the mapped response).

## SentinelJaxRsClientTemplate

For jax-rs client, we provide `SentinelJaxRsClientTemplate` you can use it like this:

```
Response response = SentinelJaxRsClientTemplate.execute(resourceName, new Supplier<Response>() {
@Override
public Response get() {
return client.target(host).path(url).request()
.get();
}
});
```

or executeAsync like this:

```
Future<Response> future = SentinelJaxRsClientTemplate.executeAsync(resourceName, new Supplier<Future<Response>>() {
@Override
public Future<Response> get() {
return client.target(host).path(url).request()
.async()
.get();
}
});
```

When a request is blocked, Sentinel jax-rs filter will return Response with status of TOO_MANY_REQUESTS indicating the request is rejected.

You can customize it by implement your own `SentinelJaxRsFallback` and register to `SentinelJaxRsConfig`.

+ 74
- 0
sentinel-adapter/sentinel-jax-rs-adapter/pom.xml Прегледај датотеку

@@ -0,0 +1,74 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-adapter</artifactId>
<version>1.8.0-SNAPSHOT</version>
</parent>

<artifactId>sentinel-jax-rs-adapter</artifactId>
<packaging>jar</packaging>

<properties>
<javax.ws.rs-api.version>2.1.1</javax.ws.rs-api.version>
</properties>

<dependencies>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-core</artifactId>
</dependency>

<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>${javax.ws.rs-api.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.2.6.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.2.6.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>4.3.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-spring-boot-starter</artifactId>
<version>4.5.1.Final</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<forkMode>always</forkMode>
</configuration>
</plugin>
</plugins>
</build>
</project>

+ 79
- 0
sentinel-adapter/sentinel-jax-rs-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/jaxrs/SentinelJaxRsClientTemplate.java Прегледај датотеку

@@ -0,0 +1,79 @@
/*
* Copyright 1999-2020 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
*
* 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 com.alibaba.csp.sentinel.adapter.jaxrs;

import com.alibaba.csp.sentinel.*;
import com.alibaba.csp.sentinel.adapter.jaxrs.config.SentinelJaxRsConfig;
import com.alibaba.csp.sentinel.adapter.jaxrs.future.FutureWrapper;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.util.function.Supplier;

import javax.ws.rs.core.Response;
import java.util.concurrent.Future;


/**
* wrap jax-rs client execution with sentinel
* <pre>
* Response response = SentinelJaxRsClientTemplate.execute(resourceName, new Supplier<Response>() {
*
* @Override
* public Response get() {
* return client.target(host).path(url).request()
* .get();
* }
* });
* </pre>
* @author sea
*/
public class SentinelJaxRsClientTemplate {

/**
* execute supplier with sentinel
* @param resourceName
* @param supplier
* @return
*/
public static Response execute(String resourceName, Supplier<Response> supplier) {
Entry entry = null;
try {
entry = SphU.entry(resourceName, ResourceTypeConstants.COMMON_WEB, EntryType.OUT);
return supplier.get();
} catch (BlockException ex) {
return SentinelJaxRsConfig.getJaxRsFallback().fallbackResponse(resourceName, ex);
} finally {
System.out.println("entry exit");
if (entry != null) {
entry.exit();
}
}
}

/**
* execute supplier with sentinel
* @param resourceName
* @param supplier
* @return
*/
public static Future<Response> executeAsync(String resourceName, Supplier<Future<Response>> supplier) {
try {
AsyncEntry entry = SphU.asyncEntry(resourceName, ResourceTypeConstants.COMMON_WEB, EntryType.OUT);
return new FutureWrapper<>(entry, supplier.get());
} catch (BlockException ex) {
return SentinelJaxRsConfig.getJaxRsFallback().fallbackFutureResponse(resourceName, ex);
}
}
}

+ 88
- 0
sentinel-adapter/sentinel-jax-rs-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/jaxrs/SentinelJaxRsProviderFilter.java Прегледај датотеку

@@ -0,0 +1,88 @@
/*
* Copyright 1999-2020 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
*
* 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 com.alibaba.csp.sentinel.adapter.jaxrs;

import com.alibaba.csp.sentinel.*;
import com.alibaba.csp.sentinel.adapter.jaxrs.config.SentinelJaxRsConfig;
import com.alibaba.csp.sentinel.context.ContextUtil;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.util.StringUtil;

import javax.ws.rs.container.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.ext.Provider;
import java.io.IOException;

/**
* @author sea
*/
@Provider
public class SentinelJaxRsProviderFilter implements ContainerRequestFilter, ContainerResponseFilter {

private static final String SENTINEL_JAX_RS_PROVIDER_CONTEXT_NAME = "sentinel_jax_rs_provider_context";


private static final String SENTINEL_JAX_RS_PROVIDER_ENTRY_PROPERTY = "sentinel_jax_rs_provider_entry_property";

@Context
private ResourceInfo resourceInfo;

@Override
public void filter(ContainerRequestContext containerRequestContext) throws IOException {

try {
String resourceName = getResourceName(containerRequestContext, resourceInfo);

if (StringUtil.isNotEmpty(resourceName)) {
// Parse the request origin using registered origin parser.
String origin = parseOrigin(containerRequestContext);
String contextName = getContextName(containerRequestContext);
ContextUtil.enter(contextName, origin);
Entry entry = SphU.entry(resourceName, ResourceTypeConstants.COMMON_WEB, EntryType.IN);

containerRequestContext.setProperty(SENTINEL_JAX_RS_PROVIDER_ENTRY_PROPERTY, entry);
}
} catch (BlockException e) {
try {
containerRequestContext.abortWith(SentinelJaxRsConfig.getJaxRsFallback().fallbackResponse(containerRequestContext.getUriInfo().getPath(), e));
} finally {
ContextUtil.exit();
}
}
}

@Override
public void filter(ContainerRequestContext containerRequestContext, ContainerResponseContext containerResponseContext) throws IOException {
Entry entry = (Entry) containerRequestContext.getProperty(SENTINEL_JAX_RS_PROVIDER_ENTRY_PROPERTY);
if (entry != null) {
entry.exit();
}
containerRequestContext.removeProperty(SENTINEL_JAX_RS_PROVIDER_ENTRY_PROPERTY);
ContextUtil.exit();
}

public String getResourceName(ContainerRequestContext containerRequestContext, ResourceInfo resourceInfo) {
return SentinelJaxRsConfig.getResourceNameParser().parse(containerRequestContext, resourceInfo);
}

protected String getContextName(ContainerRequestContext request) {
return SENTINEL_JAX_RS_PROVIDER_CONTEXT_NAME;
}

protected String parseOrigin(ContainerRequestContext request) {
return SentinelJaxRsConfig.getRequestOriginParser().parseOrigin(request);
}
}

+ 62
- 0
sentinel-adapter/sentinel-jax-rs-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/jaxrs/config/SentinelJaxRsConfig.java Прегледај датотеку

@@ -0,0 +1,62 @@
/*
* Copyright 1999-2020 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
*
* 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 com.alibaba.csp.sentinel.adapter.jaxrs.config;

import com.alibaba.csp.sentinel.adapter.jaxrs.fallback.DefaultSentinelJaxRsFallback;
import com.alibaba.csp.sentinel.adapter.jaxrs.fallback.SentinelJaxRsFallback;
import com.alibaba.csp.sentinel.adapter.jaxrs.request.DefaultRequestOriginParser;
import com.alibaba.csp.sentinel.adapter.jaxrs.request.DefaultResourceNameParser;
import com.alibaba.csp.sentinel.adapter.jaxrs.request.RequestOriginParser;
import com.alibaba.csp.sentinel.adapter.jaxrs.request.ResourceNameParser;

/**
* @author sea
*/
public class SentinelJaxRsConfig {

private static volatile ResourceNameParser resourceNameParser = new DefaultResourceNameParser();

private static volatile RequestOriginParser requestOriginParser = new DefaultRequestOriginParser();

private static volatile SentinelJaxRsFallback jaxRsFallback = new DefaultSentinelJaxRsFallback();

public static ResourceNameParser getResourceNameParser() {
return resourceNameParser;
}

public static void setResourceNameParser(ResourceNameParser resourceNameParser) {
SentinelJaxRsConfig.resourceNameParser = resourceNameParser;
}

public static RequestOriginParser getRequestOriginParser() {
return requestOriginParser;
}

public static void setRequestOriginParser(RequestOriginParser originParser) {
SentinelJaxRsConfig.requestOriginParser = originParser;
}

public static SentinelJaxRsFallback getJaxRsFallback() {
return jaxRsFallback;
}

public static void setJaxRsFallback(SentinelJaxRsFallback jaxRsFallback) {
SentinelJaxRsConfig.jaxRsFallback = jaxRsFallback;
}

private SentinelJaxRsConfig() {
}
}

+ 39
- 0
sentinel-adapter/sentinel-jax-rs-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/jaxrs/exception/DefaultExceptionMapper.java Прегледај датотеку

@@ -0,0 +1,39 @@
/*
* Copyright 1999-2020 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
*
* 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 com.alibaba.csp.sentinel.adapter.jaxrs.exception;


import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

/**
* sentinel jax-rs adapter provide this exception mapper
* in case of user throw exception which is not {@link javax.ws.rs.WebApplicationException} and not matched by any ExceptionMapper
* this exception mapper convert exception to Response let ContainerResponseFilter to be called to exit sentinel entry
* user can add custom ExceptionMapper and config with {@link javax.annotation.Priority} with lower value
* @author sea
*/
@Provider
public class DefaultExceptionMapper implements ExceptionMapper<Throwable> {

@Override
public Response toResponse(Throwable exception) {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity(exception.getMessage())
.build();
}
}

+ 45
- 0
sentinel-adapter/sentinel-jax-rs-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/jaxrs/fallback/DefaultSentinelJaxRsFallback.java Прегледај датотеку

@@ -0,0 +1,45 @@
/*
* Copyright 1999-2020 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
*
* 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 com.alibaba.csp.sentinel.adapter.jaxrs.fallback;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;

/**
* @author sea
*/
public class DefaultSentinelJaxRsFallback implements SentinelJaxRsFallback {
@Override
public Response fallbackResponse(String route, Throwable cause) {
return Response.status(Response.Status.TOO_MANY_REQUESTS)
.entity("Blocked by Sentinel (flow limiting)")
.type(MediaType.APPLICATION_JSON_TYPE)
.build();
}

@Override
public Future<Response> fallbackFutureResponse(final String route, final Throwable cause) {
return new FutureTask<>(new Callable<Response>() {
@Override
public Response call() throws Exception {
return fallbackResponse(route, cause);
}
});
}
}

+ 43
- 0
sentinel-adapter/sentinel-jax-rs-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/jaxrs/fallback/SentinelJaxRsFallback.java Прегледај датотеку

@@ -0,0 +1,43 @@
/*
* Copyright 1999-2020 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
*
* 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 com.alibaba.csp.sentinel.adapter.jaxrs.fallback;

import javax.ws.rs.core.Response;
import java.util.concurrent.Future;

/**
* @author sea
*/
public interface SentinelJaxRsFallback {

/**
* Provides a fallback response based on the cause of the failed execution.
*
* @param route The route the fallback is for
* @param cause cause of the main method failure, may be <code>null</code>
* @return the fallback response
*/
Response fallbackResponse(String route, Throwable cause);

/**
* Provides a fallback response future based on the cause of the failed execution.
*
* @param route The route the fallback is for
* @param cause cause of the main method failure, may be <code>null</code>
* @return the fallback response future
*/
Future<Response> fallbackFutureResponse(String route, Throwable cause);
}

+ 81
- 0
sentinel-adapter/sentinel-jax-rs-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/jaxrs/future/FutureWrapper.java Прегледај датотеку

@@ -0,0 +1,81 @@
/*
* Copyright 1999-2020 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
*
* 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 com.alibaba.csp.sentinel.adapter.jaxrs.future;

import com.alibaba.csp.sentinel.AsyncEntry;

import java.util.concurrent.*;

/**
* wrap Future to ensure entry exit
* @author sea
*/
public class FutureWrapper<V> implements Future<V> {

AsyncEntry entry;

Future<V> future;

public FutureWrapper(AsyncEntry entry, Future<V> future) {
this.entry = entry;
this.future = future;
}

@Override
public boolean cancel(boolean mayInterruptIfRunning) {
try {
return future.cancel(mayInterruptIfRunning);
} finally {
exitEntry();
}

}

@Override
public boolean isCancelled() {
return future.isCancelled();
}

@Override
public boolean isDone() {
return future.isDone();
}

@Override
public V get() throws InterruptedException, ExecutionException {
try {
return future.get();
} finally {
exitEntry();
}
}

@Override
public V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
try {
return future.get(timeout, unit);
} finally {
exitEntry();
}
}

private void exitEntry() {
if (entry != null) {
entry.exit();
}
}

}

+ 31
- 0
sentinel-adapter/sentinel-jax-rs-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/jaxrs/request/DefaultRequestOriginParser.java Прегледај датотеку

@@ -0,0 +1,31 @@
/*
* Copyright 1999-2020 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
*
* 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 com.alibaba.csp.sentinel.adapter.jaxrs.request;

import javax.ws.rs.container.ContainerRequestContext;

/**
* @author sea
*/
public class DefaultRequestOriginParser implements RequestOriginParser {

private static final String EMPTY_ORIGIN = "";

@Override
public String parseOrigin(ContainerRequestContext request) {
return EMPTY_ORIGIN;
}
}

+ 33
- 0
sentinel-adapter/sentinel-jax-rs-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/jaxrs/request/DefaultResourceNameParser.java Прегледај датотеку

@@ -0,0 +1,33 @@
/*
* Copyright 1999-2020 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
*
* 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 com.alibaba.csp.sentinel.adapter.jaxrs.request;

import javax.ws.rs.Path;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ResourceInfo;

/**
* @author sea
*/
public class DefaultResourceNameParser implements ResourceNameParser {
@Override
public String parse(ContainerRequestContext containerRequestContext, ResourceInfo resourceInfo) {
Path classPath = resourceInfo.getResourceClass().getAnnotation(Path.class);
Path methodPath = resourceInfo.getResourceMethod().getAnnotation(Path.class);
return containerRequestContext.getRequest().getMethod() + ":" + (classPath != null ? classPath.value() : "") + (methodPath != null ? methodPath.value() : "");
}

}

+ 34
- 0
sentinel-adapter/sentinel-jax-rs-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/jaxrs/request/RequestOriginParser.java Прегледај датотеку

@@ -0,0 +1,34 @@
/*
* Copyright 1999-2020 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
*
* 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 com.alibaba.csp.sentinel.adapter.jaxrs.request;

import javax.ws.rs.container.ContainerRequestContext;

/**
* The origin parser parses request origin (e.g. IP, user, appName) from HTTP request.
*
* @author sea
*/
public interface RequestOriginParser {

/**
* Parse the origin from given HTTP request.
*
* @param request HTTP request
* @return parsed origin
*/
String parseOrigin(ContainerRequestContext request);
}

+ 27
- 0
sentinel-adapter/sentinel-jax-rs-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/jaxrs/request/ResourceNameParser.java Прегледај датотеку

@@ -0,0 +1,27 @@
/*
* Copyright 1999-2020 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
*
* 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 com.alibaba.csp.sentinel.adapter.jaxrs.request;

import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ResourceInfo;

/**
* @author sea
*/
public interface ResourceNameParser {

String parse(ContainerRequestContext containerRequestContext, ResourceInfo resourceInfo);
}

+ 413
- 0
sentinel-adapter/sentinel-jax-rs-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/jaxrs/ClientFilterTest.java Прегледај датотеку

@@ -0,0 +1,413 @@
/*
* Copyright 1999-2020 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
*
* 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 com.alibaba.csp.sentinel.adapter.jaxrs;

import com.alibaba.csp.sentinel.Constants;
import com.alibaba.csp.sentinel.CtSph;
import com.alibaba.csp.sentinel.adapter.jaxrs.config.SentinelJaxRsConfig;
import com.alibaba.csp.sentinel.adapter.jaxrs.fallback.SentinelJaxRsFallback;
import com.alibaba.csp.sentinel.context.Context;
import com.alibaba.csp.sentinel.context.ContextUtil;
import com.alibaba.csp.sentinel.node.ClusterNode;
import com.alibaba.csp.sentinel.node.EntranceNode;
import com.alibaba.csp.sentinel.node.Node;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.alibaba.csp.sentinel.slots.clusterbuilder.ClusterBuilderSlot;
import com.alibaba.csp.sentinel.util.StringUtil;
import com.alibaba.csp.sentinel.util.function.Supplier;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.util.SocketUtils;

import javax.ws.rs.ProcessingException;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import java.lang.reflect.Method;
import java.util.Collections;
import java.util.concurrent.*;

import static org.junit.Assert.*;
import static org.junit.Assert.assertNull;

/**
* @author sea
*/
public class ClientFilterTest {

private static final String HELLO_STR = "Hello!";

static int port;

static String host;

static Client client;

static ConfigurableApplicationContext ctx;

@BeforeClass
public static void startApplication() {
client = ClientBuilder.newBuilder()
.connectTimeout(3, TimeUnit.SECONDS)
.readTimeout(3, TimeUnit.SECONDS)
.build();

port = SocketUtils.findAvailableTcpPort();
host = "http://127.0.0.1:" + port;
SpringApplication springApplication = new SpringApplication(TestApplication.class);
ctx = springApplication.run("--spring.profiles.active=client", "--server.port=" + port);
}

@AfterClass
public static void shutdown() {
ctx.close();

Context context = ContextUtil.getContext();
if (context != null) {
context.setCurEntry(null);
ContextUtil.exit();
}

Constants.ROOT.removeChildList();

ClusterBuilderSlot.getClusterNodeMap().clear();

// Clear chainMap in CtSph
try {
Method resetChainMapMethod = CtSph.class.getDeclaredMethod("resetChainMap");
resetChainMapMethod.setAccessible(true);
resetChainMapMethod.invoke(null);
} catch (Exception e) {
// Empty
}
}

@After
public void cleanUp() {
FlowRuleManager.loadRules(null);
ClusterBuilderSlot.resetClusterNodes();
}

@Test
public void testClientGetHello() {
final String url = "/test/hello";
String resourceName = "GET:" + url;
Response response = SentinelJaxRsClientTemplate.execute(resourceName, new Supplier<Response>() {

@Override
public Response get() {
return client.target(host).path(url).request()
.get();
}
});
assertEquals(200, response.getStatus());
assertEquals(HELLO_STR, response.readEntity(String.class));

ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
assertNotNull(cn);
assertEquals(1, cn.passQps(), 0.01);

String context = "";
for (Node n : Constants.ROOT.getChildList()) {
if (n instanceof EntranceNode) {
String id = ((EntranceNode) n).getId().getName();
if (url.equals(id)) {
context = ((EntranceNode) n).getId().getName();
}
}
}
assertEquals("", context);
}

@Test
public void testClientAsyncGetHello() throws InterruptedException, ExecutionException {
final String url = "/test/async-hello";
final String resourceName = "GET:" + url;

Future<Response> future = SentinelJaxRsClientTemplate.executeAsync(resourceName, new Supplier<Future<Response>>() {
@Override
public Future<Response> get() {
return client.target(host).path(url).request()
.async()
.get();
}
});

ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
assertNotNull(cn);
assertEquals(HELLO_STR, future.get().readEntity(String.class));

cn = ClusterBuilderSlot.getClusterNode(resourceName);
assertNotNull(cn);
assertEquals(1, cn.passQps(), 0.01);
}

@Test
public void testCustomResourceName() {
final String url = "/test/hello/{name}";
final String resourceName = "GET:" + url;

Response response1 = SentinelJaxRsClientTemplate.execute(resourceName, new Supplier<Response>() {
@Override
public Response get() {
return client.target(host)
.path(url)
.resolveTemplate("name", "abc")
.request()
.get();
}
});
assertEquals(200, response1.getStatus());
assertEquals("Hello abc !", response1.readEntity(String.class));

Response response2 = SentinelJaxRsClientTemplate.execute(resourceName, new Supplier<Response>() {
@Override
public Response get() {
return client.target(host)
.path(url)
.resolveTemplate("name", "def")
.request()
.get();
}
});
assertEquals(javax.ws.rs.core.Response.Status.OK.getStatusCode(), response2.getStatus());
assertEquals("Hello def !", response2.readEntity(String.class));

ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
assertNotNull(cn);
assertEquals(2, cn.passQps(), 0.01);

assertNull(ClusterBuilderSlot.getClusterNode("/test/hello/abc"));
assertNull(ClusterBuilderSlot.getClusterNode("/test/hello/def"));
}

@Test
public void testClientFallback() {
final String url = "/test/hello";
final String resourceName = "GET:" + url;
configureRulesFor(resourceName, 0);

Response response = SentinelJaxRsClientTemplate.execute(resourceName, new Supplier<Response>() {
@Override
public Response get() {
return client.target(host).path(url).request()
.get();
}
});
assertEquals(javax.ws.rs.core.Response.Status.TOO_MANY_REQUESTS.getStatusCode(), response.getStatus());
assertEquals("Blocked by Sentinel (flow limiting)", response.readEntity(String.class));


ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
assertNotNull(cn);
assertEquals(0, cn.passQps(), 0.01);
}

@Test
public void testClientCustomFallback() {
final String url = "/test/hello";
final String resourceName = "GET:" + url;
configureRulesFor(resourceName, 0);

SentinelJaxRsConfig.setJaxRsFallback(new SentinelJaxRsFallback() {
@Override
public javax.ws.rs.core.Response fallbackResponse(String route, Throwable cause) {
return javax.ws.rs.core.Response.status(javax.ws.rs.core.Response.Status.OK)
.entity("Blocked by Sentinel (flow limiting)")
.type(MediaType.APPLICATION_JSON_TYPE)
.build();
}

@Override
public Future<Response> fallbackFutureResponse(final String route, final Throwable cause) {
return new FutureTask<>(new Callable<Response>() {
@Override
public Response call() throws Exception {
return fallbackResponse(route, cause);
}
});
}
});

Response response = SentinelJaxRsClientTemplate.execute(resourceName, new Supplier<Response>() {
@Override
public Response get() {
return client.target(host).path(url).request()
.get();
}
});
assertEquals(javax.ws.rs.core.Response.Status.OK.getStatusCode(), response.getStatus());
assertEquals("Blocked by Sentinel (flow limiting)", response.readEntity(String.class));


ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
assertNotNull(cn);
assertEquals(0, cn.passQps(), 0.01);
}

@Test
public void testServerReturn400() {
final String url = "/test/400";
final String resourceName = "GET:" + url;
Response response = SentinelJaxRsClientTemplate.execute(resourceName, new Supplier<Response>() {
@Override
public Response get() {
return client.target(host).path(url).request()
.get();
}
});
assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus());
assertEquals("test return 400", response.readEntity(String.class));


ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
assertNotNull(cn);
assertEquals(1, cn.passQps(), 0.01);
}

@Test
public void testServerReturn500() {
final String url = "/test/ex";
final String resourceName = "GET:" + url;
Response response = SentinelJaxRsClientTemplate.execute(resourceName, new Supplier<Response>() {
@Override
public Response get() {
return client.target(host).path(url).request()
.get();
}
});
assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus());
assertEquals("test exception mapper", response.readEntity(String.class));


ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
assertNotNull(cn);
assertEquals(1, cn.passQps(), 0.01);
}

@Test
public void testServerTimeout() {
final String url = "/test/delay/10";
final String resourceName = "GET:/test/delay/{seconds}";
try {
SentinelJaxRsClientTemplate.execute(resourceName, new Supplier<Response>() {
@Override
public Response get() {
return client.target(host).path(url).request()
.get();
}
});
} catch (ProcessingException e) {
//ignore
}

ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
assertNotNull(cn);
assertEquals(0, cn.passQps(), 0.01);
}

@Test
public void testFutureGetServerTimeout() {
final String url = "/test/delay/10";
final String resourceName = "GET:/test/delay/{seconds}";
try {
Future<Response> future = SentinelJaxRsClientTemplate.executeAsync(resourceName, new Supplier<Future<Response>>() {
@Override
public Future<Response> get() {
return client.target(host).path(url).request()
.async()
.get();
}
});
future.get();
} catch (Exception e) {
//ignore
}

ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
assertNotNull(cn);
assertEquals(0, cn.passQps(), 0.01);
}

@Test
public void testFutureGetTimeout() {
final String url = "/test/delay/10";
final String resourceName = "GET:/test/delay/{seconds}";
try {
Future<Response> future = SentinelJaxRsClientTemplate.executeAsync(resourceName, new Supplier<Future<Response>>() {
@Override
public Future<Response> get() {
return client.target(host).path(url).request()
.async()
.get();
}
});
future.get(1, TimeUnit.SECONDS);
} catch (Exception e) {
//ignore
}

ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
assertNotNull(cn);
assertEquals(0, cn.passQps(), 0.01);
}

@Test
public void testCancelFuture() {
final String url = "/test/delay/10";
final String resourceName = "GET:/test/delay/{seconds}";
try {
Future<Response> future = SentinelJaxRsClientTemplate.executeAsync(resourceName, new Supplier<Future<Response>>() {
@Override
public Future<Response> get() {
return client.target(host).path(url).request()
.async()
.get();
}
});
future.cancel(false);
} catch (Exception e) {
//ignore
}

ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
assertNotNull(cn);
assertEquals(1, cn.passQps(), 0.01);
}

private void configureRulesFor(String resource, int count) {
configureRulesFor(resource, count, "default");
}

private void configureRulesFor(String resource, int count, String limitApp) {
FlowRule rule = new FlowRule()
.setCount(count)
.setGrade(RuleConstant.FLOW_GRADE_QPS);
rule.setResource(resource);
if (StringUtil.isNotBlank(limitApp)) {
rule.setLimitApp(limitApp);
}
FlowRuleManager.loadRules(Collections.singletonList(rule));
}
}

+ 257
- 0
sentinel-adapter/sentinel-jax-rs-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/jaxrs/ProviderFilterTest.java Прегледај датотеку

@@ -0,0 +1,257 @@
/*
* Copyright 1999-2020 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
*
* 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 com.alibaba.csp.sentinel.adapter.jaxrs;

import java.util.Collections;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;

import com.alibaba.csp.sentinel.Constants;
import com.alibaba.csp.sentinel.adapter.jaxrs.config.SentinelJaxRsConfig;
import com.alibaba.csp.sentinel.adapter.jaxrs.fallback.SentinelJaxRsFallback;
import com.alibaba.csp.sentinel.adapter.jaxrs.request.RequestOriginParser;
import com.alibaba.csp.sentinel.node.ClusterNode;
import com.alibaba.csp.sentinel.node.EntranceNode;
import com.alibaba.csp.sentinel.node.Node;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.alibaba.csp.sentinel.slots.clusterbuilder.ClusterBuilderSlot;
import com.alibaba.csp.sentinel.util.StringUtil;

import io.restassured.RestAssured;
import io.restassured.response.Response;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.util.SocketUtils;

import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.core.MediaType;

import static io.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.*;

/**
* @author sea
*/
public class ProviderFilterTest {

private static final String HELLO_STR = "Hello!";

static ConfigurableApplicationContext ctx;

@BeforeClass
public static void startApplication() {
RestAssured.basePath = "";
int port = SocketUtils.findAvailableTcpPort();
RestAssured.port = port;
SpringApplication springApplication = new SpringApplication(TestApplication.class);
ctx = springApplication.run("--spring.profiles.active=provider", "--server.port=" + port);
}

@AfterClass
public static void shutdown() {
ctx.close();
}

@After
public void cleanUp() {
FlowRuleManager.loadRules(null);
ClusterBuilderSlot.resetClusterNodes();
}


@Test
public void testGetHello() {
String url = "/test/hello";
String resourceName = "GET:" + url;
Response response = given().get(url);
response.then().statusCode(200).body(equalTo(HELLO_STR));

ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
assertNotNull(cn);
assertEquals(1, cn.passQps(), 0.01);

String context = "";
for (Node n : Constants.ROOT.getChildList()) {
if (n instanceof EntranceNode) {
String id = ((EntranceNode) n).getId().getName();
if (url.equals(id)) {
context = ((EntranceNode) n).getId().getName();
}
}
}
assertEquals("", context);
}

@Test
public void testAsyncGetHello() {
String url = "/test/async-hello";
String resourceName = "GET:" + url;
Response response = given().get(url);
response.then().statusCode(200).body(equalTo(HELLO_STR));

ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
assertNotNull(cn);
assertEquals(1, cn.passQps(), 0.01);

String context = "";
for (Node n : Constants.ROOT.getChildList()) {
if (n instanceof EntranceNode) {
String id = ((EntranceNode) n).getId().getName();
if (url.equals(id)) {
context = ((EntranceNode) n).getId().getName();
}
}
}
assertEquals("", context);
}

@Test
public void testUrlPathParam() {
String url = "/test/hello/{name}";
String resourceName = "GET:" + url;

String url1 = "/test/hello/abc";
Response response1 = given().get(url1);
response1.then().statusCode(200).body(equalTo("Hello abc !"));

String url2 = "/test/hello/def";
Response response2 = given().get(url2);
response2.then().statusCode(200).body(equalTo("Hello def !"));

ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
assertNotNull(cn);
assertEquals(2, cn.passQps(), 0.01);

assertNull(ClusterBuilderSlot.getClusterNode("GET:" + url1));
assertNull(ClusterBuilderSlot.getClusterNode("GET:" + url2));
}

@Test
public void testDefaultFallback() {
String url = "/test/hello";
String resourceName = "GET:" + url;
configureRulesFor(resourceName, 0);
Response response = given().get(url);
response.then().statusCode(javax.ws.rs.core.Response.Status.TOO_MANY_REQUESTS.getStatusCode())
.body(equalTo("Blocked by Sentinel (flow limiting)"));

ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
assertNotNull(cn);
assertEquals(0, cn.passQps(), 0.01);
}

@Test
public void testCustomFallback() {
String url = "/test/hello";
String resourceName = "GET:" + url;
SentinelJaxRsConfig.setJaxRsFallback(new SentinelJaxRsFallback() {
@Override
public javax.ws.rs.core.Response fallbackResponse(String route, Throwable cause) {
return javax.ws.rs.core.Response.status(javax.ws.rs.core.Response.Status.OK)
.entity("Blocked by Sentinel (flow limiting)")
.type(MediaType.APPLICATION_JSON_TYPE)
.build();
}

@Override
public Future<javax.ws.rs.core.Response> fallbackFutureResponse(final String route, final Throwable cause) {
return new FutureTask<>(new Callable<javax.ws.rs.core.Response>() {
@Override
public javax.ws.rs.core.Response call() throws Exception {
return fallbackResponse(route, cause);
}
});
}
});


configureRulesFor(resourceName, 0);
Response response = given().get(url);
response.then().statusCode(javax.ws.rs.core.Response.Status.OK.getStatusCode())
.body(equalTo("Blocked by Sentinel (flow limiting)"));

ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
assertNotNull(cn);
assertEquals(0, cn.passQps(), 0.01);
}

@Test
public void testCustomRequestOriginParser() {
String url = "/test/hello";
String resourceName = "GET:" + url;

String limitOrigin = "appB";
final String headerName = "X-APP";
configureRulesFor(resourceName, 0, limitOrigin);

SentinelJaxRsConfig.setRequestOriginParser(new RequestOriginParser() {
@Override
public String parseOrigin(ContainerRequestContext request) {
String origin = request.getHeaderString(headerName);
return origin != null ? origin : "";
}
});

Response response = given()
.header(headerName, "appA").get(url);
response.then().statusCode(200).body(equalTo(HELLO_STR));

Response blockedResp = given()
.header(headerName, "appB")
.get(url);
blockedResp.then().statusCode(javax.ws.rs.core.Response.Status.TOO_MANY_REQUESTS.getStatusCode())
.body(equalTo("Blocked by Sentinel (flow limiting)"));

ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
assertNotNull(cn);
assertEquals(1, cn.passQps(), 0.01);
assertEquals(1, cn.blockQps(), 0.01);
}

@Test
public void testExceptionMapper() {
String url = "/test/ex";
String resourceName = "GET:" + url;
Response response = given().get(url);
response.then().statusCode(javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).body(equalTo("test exception mapper"));

ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
assertNotNull(cn);
}

private void configureRulesFor(String resource, int count) {
configureRulesFor(resource, count, "default");
}

private void configureRulesFor(String resource, int count, String limitApp) {
FlowRule rule = new FlowRule()
.setCount(count)
.setGrade(RuleConstant.FLOW_GRADE_QPS);
rule.setResource(resource);
if (StringUtil.isNotBlank(limitApp)) {
rule.setLimitApp(limitApp);
}
FlowRuleManager.loadRules(Collections.singletonList(rule));
}
}

+ 30
- 0
sentinel-adapter/sentinel-jax-rs-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/jaxrs/TestApplication.java Прегледај датотеку

@@ -0,0 +1,30 @@
/*
* Copyright 1999-2020 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
*
* 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 com.alibaba.csp.sentinel.adapter.jaxrs;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
* @author sea
*/
@SpringBootApplication
public class TestApplication {

public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
}

+ 92
- 0
sentinel-adapter/sentinel-jax-rs-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/jaxrs/TestResource.java Прегледај датотеку

@@ -0,0 +1,92 @@
/*
* Copyright 1999-2020 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
*
* 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 com.alibaba.csp.sentinel.adapter.jaxrs;

import org.springframework.stereotype.Component;

import javax.ws.rs.*;
import javax.ws.rs.container.AsyncResponse;
import javax.ws.rs.container.Suspended;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
* @author sea
*/
@Path("/test")
@Component
public class TestResource {

ExecutorService executor = Executors.newFixedThreadPool(5);

@Path("/hello")
@GET
@Produces({ MediaType.APPLICATION_JSON })
public String sayHello() {
return "Hello!";
}

@Path("/async-hello")
@GET
@Produces({ MediaType.APPLICATION_JSON })
public void asyncSayHello(@Suspended final AsyncResponse asyncResponse) {
executor.submit(new Runnable() {
@Override
public void run() {
try {
TimeUnit.MILLISECONDS.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
asyncResponse.resume("Hello!");
}
});
}

@Path("/hello/{name}")
@GET
@Produces({ MediaType.APPLICATION_JSON })
public String sayHelloWithName(@PathParam(value = "name") String name) {
return "Hello " + name + " !";
}

@Path("/ex")
@GET
@Produces({ MediaType.APPLICATION_JSON })
public String exception() {
throw new RuntimeException("test exception mapper");
}

@Path("/400")
@GET
@Produces({ MediaType.APPLICATION_JSON })
public String badRequest() {
throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST)
.entity("test return 400")
.build());
}

@Path("/delay/{seconds}")
@GET
@Produces({ MediaType.APPLICATION_JSON })
public String delay(@PathParam(value = "seconds") long seconds) throws InterruptedException {
TimeUnit.SECONDS.sleep(seconds);
return "finish";
}
}

+ 3
- 0
sentinel-adapter/sentinel-jax-rs-adapter/src/test/resources/application-client.yml Прегледај датотеку

@@ -0,0 +1,3 @@
resteasy:
jaxrs:
scan-packages: com.alibaba.csp.sentinel.adapter.jaxrs.exception

+ 3
- 0
sentinel-adapter/sentinel-jax-rs-adapter/src/test/resources/application-provider.yml Прегледај датотеку

@@ -0,0 +1,3 @@
resteasy:
jaxrs:
scan-packages: com.alibaba.csp.sentinel.adapter.jaxrs

+ 1
- 0
sentinel-demo/pom.xml Прегледај датотеку

@@ -39,6 +39,7 @@
<module>sentinel-demo-spring-webmvc</module>
<module>sentinel-demo-zuul2-gateway</module>
<module>sentinel-demo-log-logback</module>
<module>sentinel-demo-jax-rs</module>
</modules>

<dependencies>


+ 49
- 0
sentinel-demo/sentinel-demo-jax-rs/pom.xml Прегледај датотеку

@@ -0,0 +1,49 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>sentinel-demo</artifactId>
<groupId>com.alibaba.csp</groupId>
<version>1.8.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>sentinel-demo-jax-rs</artifactId>

<properties>
<spring.boot.version>2.2.6.RELEASE</spring.boot.version>
</properties>

<dependencies>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-core</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-transport-simple-http</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-jax-rs-adapter</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-spring-boot-starter</artifactId>
<version>4.5.1.Final</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${spring.boot.version}</version>
</dependency>
</dependencies>

</project>

+ 38
- 0
sentinel-demo/sentinel-demo-jax-rs/src/main/java/com/alibaba/csp/sentinel/demo/jaxrs/CustomExceptionMapper.java Прегледај датотеку

@@ -0,0 +1,38 @@
/*
* Copyright 1999-2020 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
*
* 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 com.alibaba.csp.sentinel.demo.jaxrs;

import org.springframework.stereotype.Component;

import javax.annotation.Priority;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

/**
* @author sea
*/
@Component
@Provider
@Priority(javax.ws.rs.Priorities.USER - 1)
public class CustomExceptionMapper implements ExceptionMapper<Throwable> {
@Override
public Response toResponse(Throwable exception) {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity("Unknown Server Error")
.build();
}
}

+ 62
- 0
sentinel-demo/sentinel-demo-jax-rs/src/main/java/com/alibaba/csp/sentinel/demo/jaxrs/HelloEntity.java Прегледај датотеку

@@ -0,0 +1,62 @@
/*
* Copyright 1999-2020 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
*
* 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 com.alibaba.csp.sentinel.demo.jaxrs;

/**
* @author sea
*/
public class HelloEntity {

Long id;

String msg;

public HelloEntity() {
}

public HelloEntity(String msg) {
this.msg = msg;
}

public HelloEntity(Long id, String msg) {
this.id = id;
this.msg = msg;
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getMsg() {
return msg;
}

public void setMsg(String msg) {
this.msg = msg;
}

@Override
public String toString() {
return "HelloEntity{" +
"id=" + id +
", msg='" + msg + '\'' +
'}';
}
}

+ 63
- 0
sentinel-demo/sentinel-demo-jax-rs/src/main/java/com/alibaba/csp/sentinel/demo/jaxrs/HelloResource.java Прегледај датотеку

@@ -0,0 +1,63 @@
/*
* Copyright 1999-2020 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
*
* 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 com.alibaba.csp.sentinel.demo.jaxrs;

import org.springframework.stereotype.Component;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
* HelloResource
* @author sea
*/
@Path("/hello")
@Produces(MediaType.APPLICATION_JSON)
@Component
public class HelloResource {

@GET
public HelloEntity sayHello() {
return new HelloEntity("hello");
}

@GET
@Path("/{id}")
public HelloEntity get(@PathParam(value = "id") Long id) {
return new HelloEntity(id, "hello");
}

@GET
@Path("/list")
public List<HelloEntity> getAll() {
return IntStream.rangeClosed(1, 1000)
.mapToObj(i -> new HelloEntity((long)i, "hello"))
.collect(Collectors.toList());
}

@Path("/ex")
@GET
@Produces({ MediaType.APPLICATION_JSON })
public String exception() {
throw new RuntimeException("test exception mapper");
}
}

+ 51
- 0
sentinel-demo/sentinel-demo-jax-rs/src/main/java/com/alibaba/csp/sentinel/demo/jaxrs/JaxRsClientDemo.java Прегледај датотеку

@@ -0,0 +1,51 @@
/*
* Copyright 1999-2020 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
*
* 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 com.alibaba.csp.sentinel.demo.jaxrs;

import com.alibaba.csp.sentinel.adapter.jaxrs.SentinelJaxRsClientTemplate;
import com.alibaba.csp.sentinel.util.function.Supplier;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.Response;
import java.util.concurrent.TimeUnit;

/**
* @author sea
*/
public class JaxRsClientDemo {

public static void main(String[] args) {
Client client = ClientBuilder.newBuilder()
.connectTimeout(3, TimeUnit.SECONDS)
.readTimeout(3, TimeUnit.SECONDS)
.build();

final String host = "http://127.0.0.1:8181";
final String url = "/hello/1";
String resourceName = "GET:" + url;
Response response = SentinelJaxRsClientTemplate.execute(resourceName, new Supplier<Response>() {

@Override
public Response get() {
return client.target(host).path(url).request()
.get();
}
});
System.out.println(response.readEntity(HelloEntity.class));
System.exit(0);
}
}

+ 33
- 0
sentinel-demo/sentinel-demo-jax-rs/src/main/java/com/alibaba/csp/sentinel/demo/jaxrs/JaxRsDemoApplication.java Прегледај датотеку

@@ -0,0 +1,33 @@
/*
* Copyright 1999-2020 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
*
* 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 com.alibaba.csp.sentinel.demo.jaxrs;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
* <p>Add the JVM parameter to connect to the dashboard:</p>
* {@code -Dcsp.sentinel.dashboard.server=127.0.0.1:8080 -Dproject.name=sentinel-demo-jax-rs}
*
* @author sea
*/
@SpringBootApplication
public class JaxRsDemoApplication {

public static void main(String[] args) {
SpringApplication.run(JaxRsDemoApplication.class);
}
}

+ 32
- 0
sentinel-demo/sentinel-demo-jax-rs/src/main/java/com/alibaba/csp/sentinel/demo/jaxrs/SentinelJaxRsConfig.java Прегледај датотеку

@@ -0,0 +1,32 @@
/*
* Copyright 1999-2020 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
*
* 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 com.alibaba.csp.sentinel.demo.jaxrs;

import com.alibaba.csp.sentinel.adapter.jaxrs.SentinelJaxRsProviderFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
* @author sea
*/
@Configuration(proxyBeanMethods = false)
public class SentinelJaxRsConfig {

@Bean
public SentinelJaxRsProviderFilter sentinelJaxRsProviderFilter() {
return new SentinelJaxRsProviderFilter();
}
}

+ 1
- 0
sentinel-demo/sentinel-demo-jax-rs/src/main/resources/application.properties Прегледај датотеку

@@ -0,0 +1 @@
server.port=8181

Loading…
Откажи
Сачувај