Skip to content

Commit

Permalink
autoSetPrototypeScopeForPathMathInterceptor config
Browse files Browse the repository at this point in the history
  • Loading branch information
chentianming11 committed Mar 8, 2023
1 parent 74f6a90 commit 30894f3
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 15 deletions.
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ gitee项目地址:[https://gitee.com/lianjiatech/retrofit-spring-boot-starter]
<dependency>
<groupId>com.github.lianjiatech</groupId>
<artifactId>retrofit-spring-boot-starter</artifactId>
<version>3.0.0</version>
<version>3.0.1</version>
</dependency>
```

Expand Down Expand Up @@ -177,6 +177,8 @@ retrofit:
enable: false
# 根据该名称从#{@link CircuitBreakerConfigRegistry}获取CircuitBreakerConfig,作为全局熔断配置
circuit-breaker-config-name: defaultCircuitBreakerConfig
# 自动设置PathMathInterceptor的scope为prototype
auto-set-prototype-scope-for-path-math-interceptor: true
```

## 高级功能
Expand Down Expand Up @@ -260,7 +262,21 @@ public class TimeStampInterceptor extends BasePathMatchInterceptor {
return chain.proceed(newRequest);
}
}
```

默认情况下,**组件会自动将`BasePathMatchInterceptor``scope`设置为`prototype`**
可通过`retrofit.auto-set-prototype-scope-for-path-math-interceptor=false`关闭该功能。关闭之后,需要手动将`scope`设置为`prototype`

```java
@Component
@Scope("prototype")
public class TimeStampInterceptor extends BasePathMatchInterceptor {

@Override
public Response doIntercept(Chain chain) throws IOException {
// ...
}
}
```

#### 接口上使用`@Intercept`进行标注
Expand All @@ -281,6 +297,8 @@ public interface HttpApi {

上面的`@Intercept`配置表示:拦截`HttpApi`接口下`/api/**`路径下(排除`/api/test/savePerson`)的请求,拦截处理器使用`TimeStampInterceptor`



### 自定义拦截注解

有的时候,我们需要在"拦截注解"动态传入一些参数,然后在拦截的时候使用这些参数。 这时候,我们可以使用"自定义拦截注解",步骤如下:
Expand Down
17 changes: 16 additions & 1 deletion README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<dependency>
<groupId>com.github.lianjiatech</groupId>
<artifactId>retrofit-spring-boot-starter</artifactId>
<version>3.0.0</version>
<version>3.0.1</version>
</dependency>
```

Expand Down Expand Up @@ -141,6 +141,7 @@ retrofit:
enable: false
# Get CircuitBreakerConfig from {@link CircuitBreakerConfigRegistry} based on this name as a global circuit breaker configuration
circuit-breaker-config-name: defaultCircuitBreakerConfig
auto-set-prototype-scope-for-path-math-interceptor: true
```

## Advanced Features
Expand Down Expand Up @@ -234,7 +235,21 @@ public class TimeStampInterceptor extends BasePathMatchInterceptor {
return chain.proceed(newRequest);
}
}
```

By default, **component will automatically set `scope` of `BasePathMatchInterceptor` to `prototype`**.
This feature can be turned off by `retrofit.auto-set-prototype-scope-for-path-math-interceptor=false`. After closing, you need to manually set `scope` to `prototype`.

```java
@Component
@Scope("prototype")
public class TimeStampInterceptor extends BasePathMatchInterceptor {

@Override
public Response doIntercept(Chain chain) throws IOException {
// ...
}
}
```

#### Use the `@Intercept` annotation to specify the interceptor to use
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.github.lianjiatech</groupId>
<artifactId>retrofit-spring-boot-starter</artifactId>
<version>3.0.0</version>
<version>3.0.1</version>

<name>retrofit-spring-boot-starter</name>
<description>retrofit-spring-boot-starter</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ public RetrofitAutoConfiguration(RetrofitProperties retrofitProperties) {
public static class RetrofitAdvanceConfiguration {

@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(prefix = "retrofit", name = "auto-set-prototype-scope-for-path-math-interceptor",
matchIfMissing = true)
public static PathMatchInterceptorBdfProcessor pathMatchInterceptorBdfProcessor() {
return new PathMatchInterceptorBdfProcessor();
}
Expand Down Expand Up @@ -106,7 +107,8 @@ public ServiceInstanceChooser retrofitServiceInstanceChooser() {

@Bean
@ConditionalOnMissingBean
public ServiceChooseInterceptor retrofitServiceChooseInterceptor(@Autowired ServiceInstanceChooser serviceInstanceChooser) {
public ServiceChooseInterceptor
retrofitServiceChooseInterceptor(@Autowired ServiceInstanceChooser serviceInstanceChooser) {
return new ServiceChooseInterceptor(serviceInstanceChooser);
}

Expand Down Expand Up @@ -168,7 +170,8 @@ public CircuitBreakerConfigRegistry retrofitCircuitBreakerConfigRegistry(

@Bean
@ConditionalOnMissingBean
public RetrofitDegrade retrofitResilience4jRetrofitDegrade(CircuitBreakerConfigRegistry circuitBreakerConfigRegistry) {
public RetrofitDegrade
retrofitResilience4jRetrofitDegrade(CircuitBreakerConfigRegistry circuitBreakerConfigRegistry) {
return new Resilience4jRetrofitDegrade(CircuitBreakerRegistry.ofDefaults(),
properties.getDegrade().getGlobalResilience4jDegrade(), circuitBreakerConfigRegistry);
}
Expand All @@ -179,17 +182,17 @@ public RetrofitDegrade retrofitResilience4jRetrofitDegrade(CircuitBreakerConfigR
@EnableConfigurationProperties(RetrofitProperties.class)
public static class SentinelConfiguration {

private final RetrofitProperties properties;
private final RetrofitProperties properties;

public SentinelConfiguration(RetrofitProperties properties) {
this.properties = properties;
}
public SentinelConfiguration(RetrofitProperties properties) {
this.properties = properties;
}

@Bean
@ConditionalOnMissingBean
public RetrofitDegrade retrofitSentinelRetrofitDegrade() {
return new SentinelRetrofitDegrade(properties.getDegrade().getGlobalSentinelDegrade());
}
@Bean
@ConditionalOnMissingBean
public RetrofitDegrade retrofitSentinelRetrofitDegrade() {
return new SentinelRetrofitDegrade(properties.getDegrade().getGlobalSentinelDegrade());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
@Data
public class RetrofitProperties {

/**
* 自动设置PathMathInterceptor的scope为prototype
*/
private boolean autoSetPrototypeScopeForPathMathInterceptor = true;

/**
* 全局重试配置
* <p>
Expand Down
2 changes: 2 additions & 0 deletions src/test/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ retrofit:
enable: false
# 根据该名称从#{@link CircuitBreakerConfigRegistry}获取CircuitBreakerConfig,作为全局熔断配置
circuit-breaker-config-name: defaultCircuitBreakerConfig
# 自动设置PathMathInterceptor的scope为prototype
auto-set-prototype-scope-for-path-math-interceptor: true

test:
baseUrl: http://localhost:8080/api/test/
Expand Down
2 changes: 2 additions & 0 deletions src/test/resources/default-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,5 @@ retrofit:
enable: false
# 根据该名称从#{@link CircuitBreakerConfigRegistry}获取CircuitBreakerConfig,作为全局熔断配置
circuit-breaker-config-name: defaultCircuitBreakerConfig
# 自动设置PathMathInterceptor的scope为prototype
auto-set-prototype-scope-for-path-math-interceptor: true

0 comments on commit 30894f3

Please sign in to comment.