Skip to content

Commit

Permalink
Merge pull request #45 from LianjiaTech/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
chentianming11 authored Dec 30, 2020
2 parents dbff6d9 + 5d6fc96 commit 4ba44ed
Show file tree
Hide file tree
Showing 17 changed files with 375 additions and 250 deletions.
117 changes: 45 additions & 72 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ github项目地址:[https://github.com/LianjiaTech/retrofit-spring-boot-starte

gitee项目地址:[https://gitee.com/lianjiatech/retrofit-spring-boot-starter](https://gitee.com/lianjiatech/retrofit-spring-boot-starter)

示例demo:[https://github.com/JVMAK/retrofit-spring-boot-demo](https://github.com/JVMAK/retrofit-spring-boot-demo)
示例demo:[https://github.com/ismart-yuxi/retrofit-spring-boot-demo](https://github.com/ismart-yuxi/retrofit-spring-boot-demo)

> 感谢`@JVMAK`为本项目写的示例demo
> 感谢`@ismart-yuxi`为本项目写的示例demo
<!--more-->

Expand All @@ -50,7 +50,7 @@ gitee项目地址:[https://gitee.com/lianjiatech/retrofit-spring-boot-starter]
<dependency>
<groupId>com.github.lianjiatech</groupId>
<artifactId>retrofit-spring-boot-starter</artifactId>
<version>2.2.4</version>
<version>2.2.5</version>
</dependency>
```

Expand Down Expand Up @@ -107,57 +107,7 @@ public class TestService {

## 配置项说明

`retrofit-spring-boot-starter`支持了多个可配置的属性,用来应对不同的业务场景。您可以视情况进行修改,具体说明如下:

| 配置|默认值 | 说明 |
|------------|-----------|--------|
| enable-log | true| 启用日志打印 |
|logging-interceptor | DefaultLoggingInterceptor | 日志打印拦截器 |
| pool | | 连接池配置 |
| disable-void-return-type | false | 禁用java.lang.Void返回类型 |
| retry-interceptor | DefaultRetryInterceptor | 请求重试拦截器 |
| global-converter-factories | JacksonConverterFactory | 全局转换器工厂 |
| global-call-adapter-factories | BodyCallAdapterFactory,ResponseCallAdapterFactory | 全局调用适配器工厂 |
| enable-degrade | false | 是否启用熔断降级 |
| degrade-type | sentinel | 熔断降级实现方式(目前仅支持Sentinel) |
| resource-name-parser | DefaultResourceNameParser | 熔断资源名称解析器,用于解析资源名称 |


`yml`配置方式:

```yaml
retrofit:
enable-response-call-adapter: true
# 启用日志打印
enable-log: true
# 连接池配置
pool:
test1:
max-idle-connections: 3
keep-alive-second: 100
test2:
max-idle-connections: 5
keep-alive-second: 50
# 禁用void返回值类型
disable-void-return-type: false
# 日志打印拦截器
logging-interceptor: com.github.lianjiatech.retrofit.spring.boot.interceptor.DefaultLoggingInterceptor
# 请求重试拦截器
retry-interceptor: com.github.lianjiatech.retrofit.spring.boot.retry.DefaultRetryInterceptor
# 全局转换器工厂
global-converter-factories:
- retrofit2.converter.jackson.JacksonConverterFactory
# 全局调用适配器工厂
global-call-adapter-factories:
- com.github.lianjiatech.retrofit.spring.boot.core.BodyCallAdapterFactory
- com.github.lianjiatech.retrofit.spring.boot.core.ResponseCallAdapterFactory
# 是否启用熔断降级
enable-degrade: true
# 熔断降级实现方式
degrade-type: sentinel
# 熔断资源名称解析器
resource-name-parser: com.github.lianjiatech.retrofit.spring.boot.degrade.DefaultResourceNameParser
```
`retrofit-spring-boot-starter`支持了多个可配置的属性,用来应对不同的业务场景。详细信息可参考[配置项示例](https://github.com/LianjiaTech/retrofit-spring-boot-starter/blob/master/src/test/resources/application.yml)

## 高级功能

Expand Down Expand Up @@ -381,7 +331,7 @@ public interface HttpApi {

### 日志打印

很多情况下,我们希望将http请求日志记录下来。通过`retrofit.enableLog`配置可以全局控制日志是否开启。
很多情况下,我们希望将http请求日志记录下来。通过`retrofit.log.enable`配置可以全局控制日志是否开启。
针对每个接口,可以通过`@RetrofitClient`的`enableLog`控制是否开启,通过`logLevel`和`logStrategy`,可以指定每个接口的日志打印级别以及日志打印策略。`retrofit-spring-boot-starter`支持了5种日志打印级别(`ERROR`, `WARN`, `INFO`, `DEBUG`, `TRACE`),默认`INFO`;支持了4种日志打印策略(`NONE`, `BASIC`, `HEADERS`, `BODY`),默认`BASIC`。4种日志打印策略含义如下:

1. `NONE`:No logs.
Expand All @@ -393,26 +343,49 @@ public interface HttpApi {

```yaml
retrofit:
# 日志打印拦截器
logging-interceptor: com.github.lianjiatech.retrofit.spring.boot.interceptor.DefaultLoggingInterceptor
# 日志打印配置
log:
# 启用日志打印
enable: true
# 日志打印拦截器
logging-interceptor: com.github.lianjiatech.retrofit.spring.boot.interceptor.DefaultLoggingInterceptor
```

### 请求重试

`retrofit-spring-boot-starter`支持请求重试功能,只需要在接口或者方法上加上`@Retry`注解即可。**`@Retry`支持重试次数`maxRetries`、重试时间间隔`intervalMs`以及重试规则`retryRules`配置**。重试规则支持三种配置:
`retrofit-spring-boot-starter`支持支持全局重试和声明式重试。

1. `RESPONSE_STATUS_NOT_2XX`:响应状态码不是`2xx`时执行重试;
2. `OCCUR_IO_EXCEPTION`:发生IO异常时执行重试;
3. `OCCUR_EXCEPTION`:发生任意异常时执行重试;
#### 全局重试

默认响应状态码不是`2xx`或者发生IO异常时自动进行重试。需要的话,你也可以继承`BaseRetryInterceptor`实现自己的请求重试拦截器,然后将其配置上去。
全局重试默认开启,可以通过配置`retrofit.retry.enable-global-retry=false`关闭。开启之后,所有`HTTP`请求都会按照配置参数自动重试,详细配置项如下:

```yaml
retrofit:
# 请求重试拦截器
retry-interceptor: com.github.lianjiatech.retrofit.spring.boot.retry.DefaultRetryInterceptor
# 重试配置
retry:
# 是否启用全局重试
enable-global-retry: true
# 全局重试间隔时间
global-interval-ms: 20
# 全局最大重试次数
global-max-retries: 10
# 全局重试规则
global-retry-rules:
- response_status_not_2xx
# 重试拦截器
retry-interceptor: com.github.lianjiatech.retrofit.spring.boot.retry.DefaultRetryInterceptor
```

**重试规则支持三种配置**:

1. `RESPONSE_STATUS_NOT_2XX`:响应状态码不是`2xx`时执行重试;
2. `OCCUR_IO_EXCEPTION`:发生IO异常时执行重试;
3. `OCCUR_EXCEPTION`:发生任意异常时执行重试;

#### 声明式重试

如果只需要在指定某些请求才执行重试,可以使用声明式重试!具体就是在接口或者方法上声明`@Retry`注解。

### 错误解码器

在`HTTP`发生请求错误(包括发生异常或者响应数据不符合预期)的时候,错误解码器可将`HTTP`相关信息解码到自定义异常中。你可以在`@RetrofitClient`注解的`errorDecoder()`指定当前接口的错误解码器,自定义错误解码器需要实现`ErrorDecoder`接口:
Expand Down Expand Up @@ -499,8 +472,6 @@ public class SourceInterceptor extends BaseGlobalInterceptor {

### 熔断降级

在分布式服务架构中,对不稳定的外部服务进行熔断降级是保证服务高可用的重要措施之一。由于外部服务的稳定性是不能保证的,当外部服务不稳定时,响应时间会变长。相应地,调用方的响应时间也会变长,线程会产生堆积,最终可能耗尽调用方的线程池,导致整个服务不可用。因此我们需要对不稳定的弱依赖服务调用进行熔断降级,暂时切断不稳定调用,避免局部不稳定导致整体服务雪崩。

`retrofit-spring-boot-starter`支持熔断降级功能,底层基于[Sentinel](https://sentinelguard.io/zh-cn/docs/introduction.html)实现。具体来说,支持了**熔断资源自发现**和**注解式降级规则配置**。如需使用熔断降级,只需要进行以下操作即可:

#### 1. 开启熔断降级功能
Expand All @@ -509,12 +480,14 @@ public class SourceInterceptor extends BaseGlobalInterceptor {

```yaml
retrofit:
# 是否启用熔断降级
enable-degrade: true
# 熔断降级实现方式(目前仅支持Sentinel)
degrade-type: sentinel
# 资源名称解析器
resource-name-parser: com.github.lianjiatech.retrofit.spring.boot.degrade.DefaultResourceNameParser
# 熔断降级配置
degrade:
# 是否启用熔断降级
enable: true
# 熔断降级实现方式
degrade-type: sentinel
# 熔断资源名称解析器
resource-name-parser: com.github.lianjiatech.retrofit.spring.boot.degrade.DefaultResourceNameParser
```

资源名称解析器用于实现用户自定义资源名称,默认配置是`DefaultResourceNameParser`,对应的资源名称格式为`HTTP_OUT:GET:http://localhost:8080/api/degrade/test`。用户可以继承`BaseResourceNameParser`类实现自己的资源名称解析器。
Expand Down
88 changes: 27 additions & 61 deletions README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
<dependency>
<groupId>com.github.lianjiatech</groupId>
<artifactId>retrofit-spring-boot-starter</artifactId>
<version>2.2.4</version>
<version>2.2.5</version>
</dependency>
```

Expand Down Expand Up @@ -96,56 +96,7 @@ All of the related annotations of `HTTP` request use native annotations of `retr

## Configuration item description

`Retrofit-spring-boot-starter` supports multiple configurable properties to deal with different business scenarios. You can modify it as appropriate. The specific instructions are as follows:

| Configuration item|Default value | description |
| ------------ | ----------- | -------- |
| enable-log | true| Enable log printing |
|logging-interceptor | DefaultLoggingInterceptor | Log print interceptor |
| pool | | Connection pool configuration |
| disable-void-return-type | false | disable java.lang.Void return type |
| retry-interceptor | DefaultRetryInterceptor | Retry Interceptor |
| retry-interceptor | DefaultRetryInterceptor | Retry Interceptor |
| global-converter-factories | JacksonConverterFactory | global converter factories |
| global-call-adapter-factories | BodyCallAdapterFactory,ResponseCallAdapterFactory | global call adapter factories |
| enable-degrade | false | enable degrade |
| degrade-type | sentinel | the degade type(Currently only Sentinel is supported) |
| resource-name-parser | DefaultResourceNameParser | the resource name parser,Used to resolve resource name |

`yml` Configuration:

```yaml
retrofit:
# Enable log printing
enable-log: true
# Connection pool configuration
pool:
test1:
max-idle-connections: 3
keep-alive-second: 100
test2:
max-idle-connections: 5
keep-alive-second: 50
# Disable java.lang.Void return type
disable-void-return-type: false
# Log print interceptor
logging-interceptor: com.github.lianjiatech.retrofit.spring.boot.interceptor.DefaultLoggingInterceptor
# Retry Interceptor
retry-interceptor: com.github.lianjiatech.retrofit.spring.boot.retry.DefaultRetryInterceptor
# global converter factories
global-converter-factories:
- retrofit2.converter.jackson.JacksonConverterFactory
# global call adapter factories
global-call-adapter-factories:
- com.github.lianjiatech.retrofit.spring.boot.core.BodyCallAdapterFactory
- com.github.lianjiatech.retrofit.spring.boot.core.ResponseCallAdapterFactory
# enable degrade
enable-degrade: true
# the degade type(Currently only Sentinel is supported)
degrade-type: sentinel
# the resource name parser
resource-name-parser: com.github.lianjiatech.retrofit.spring.boot.degrade.DefaultResourceNameParser
```
`Retrofit-spring-boot-starter` supports multiple configurable properties to deal with different business scenarios.For more information, please refer to the [configuration example](https://github.com/LianjiaTech/retrofit-spring-boot-starter/blob/master/src/test/resources/application.yml)

## Advanced feature

Expand Down Expand Up @@ -370,7 +321,7 @@ By default, all HTTP requests sent through `Retrofit` will use the default conne

### Log printing

In many cases, we want to record the http request log. You can global control whether the log is enabled through `retrofit.enableLog` configuration. For each interface, you can control whether to enable it through the `enableLog` of `@RetrofitClient`. You can specify the log printing level and log printing strategy of each interface through `logLevel` and `logStrategy`. `Retrofit-spring-boot-starter` supports five log printing levels( `ERROR`, `WARN`, `INFO`, `DEBUG`, `TRACE`) which default to `INFO` and four log printing strategy( `NONE`, `BASIC`, `HEADERS`, `BODY`) which default to `BASIC`. The meanings of the 4 log printing strategies are as follows:
In many cases, we want to record the http request log. You can global control whether the log is enabled through `retrofit.log.enable` configuration. For each interface, you can control whether to enable it through the `enableLog` of `@RetrofitClient`. You can specify the log printing level and log printing strategy of each interface through `logLevel` and `logStrategy`. `Retrofit-spring-boot-starter` supports five log printing levels( `ERROR`, `WARN`, `INFO`, `DEBUG`, `TRACE`) which default to `INFO` and four log printing strategy( `NONE`, `BASIC`, `HEADERS`, `BODY`) which default to `BASIC`. The meanings of the 4 log printing strategies are as follows:

1. `NONE`:No logs.
2. `BASIC`:Logs request and response lines.
Expand All @@ -380,26 +331,41 @@ In many cases, we want to record the http request log. You can global control wh
By default, `retrofit-spring-boot-starter` uses `DefaultLoggingInterceptor` to perform the real log printing function. The bottom is `okhttp` native `HttpLoggingInterceptor`. Of course, you can also customize and implement your own log printing interceptor by simply inheriting the `baselogginginterceptor`( For details, please refer to the implementation of `defaultlogginginterceptor`), and then configure it in the configuration file.
```yaml
retrofit:
# log printing interceptor
logging-interceptor: com.github.lianjiatech.retrofit.spring.boot.interceptor.DefaultLoggingInterceptor
log:
enable: true
logging-interceptor: com.github.lianjiatech.retrofit.spring.boot.interceptor.DefaultLoggingInterceptor
```

### Request retry

`Retrofit-spring-boot-starter` supports request retry feature by adding `@Retry` annotation to the interface or method. **`@Retry` supports the configuration of `maxRetries`, `intervalMs` and `retryRules`**. The retry rule supports three configurations:
`retrofit-spring-boot-starter` supports global retry and declarative retry.

1. `RESPONSE_STATUS_NOT_2XX`: Retry when the response status code is not `2xx`;
2. `OCCUR_IO_EXCEPTION`: Retry when an IO exception occurs;
3. `OCCUR_EXCEPTION`: Retry when any exception occurs;
#### global retry

The default response status code is not `2xx` or automatically retry when an IO exception occurs. If necessary, you can also inherit `BaseRetryInterceptor` to implement your own request retry interceptor and then configure it.
Global retry is enabled by default and can be disabled by configuring `retrofit.retry.enable-global-retry=false`. After enabling, all `HTTP` requests will be retried automatically according to the configuration parameters. The detailed configuration items are as follows:

```yaml
retrofit:
# request retry interceptor
retry-interceptor: com.github.lianjiatech.retrofit.spring.boot.retry.DefaultRetryInterceptor
# 重试配置
retry:
# 是否启用全局重试
enable-global-retry: true
# 全局重试间隔时间
global-interval-ms: 20
# 全局最大重试次数
global-max-retries: 10
# 全局重试规则
global-retry-rules:
- response_status_not_2xx
# 重试拦截器
retry-interceptor: com.github.lianjiatech.retrofit.spring.boot.retry.DefaultRetryInterceptor
```

#### declarative retry

If you only need to specify certain requests before retrying, you can use declarative retry! Specifically, declare the `@Retry` annotation on the interface or method.


### Error decoder

When an error occurs in the `HTTP` request (including an exception or the response data does not meet expectations), the error decoder can decode `HTTP` related information into a custom exception. You can specify the error decoder of the current interface in the `errorDecoder()` annotated by `@RetrofitClient`. The custom error decoder needs to implement the `ErrorDecoder` interface:
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>2.2.4</version>
<version>2.2.5</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
@@ -0,0 +1,55 @@
package com.github.lianjiatech.retrofit.spring.boot.config;

import com.github.lianjiatech.retrofit.spring.boot.degrade.BaseResourceNameParser;
import com.github.lianjiatech.retrofit.spring.boot.degrade.DefaultResourceNameParser;
import com.github.lianjiatech.retrofit.spring.boot.degrade.DegradeType;

/**
* @author 陈添明
*/
public class DegradeProperty {


/**
* 启用熔断降级
* enable degrade
*/
private boolean enable = false;

/**
* 熔断降级类型,暂时只支持SENTINEL
* degrade type, Only SENTINEL is currently supported
*/
private DegradeType degradeType = DegradeType.SENTINEL;

/**
* 资源名称解析器
* resource name parser
*/
private Class<? extends BaseResourceNameParser> resourceNameParser = DefaultResourceNameParser.class;


public boolean isEnable() {
return enable;
}

public void setEnable(boolean enable) {
this.enable = enable;
}

public DegradeType getDegradeType() {
return degradeType;
}

public void setDegradeType(DegradeType degradeType) {
this.degradeType = degradeType;
}

public Class<? extends BaseResourceNameParser> getResourceNameParser() {
return resourceNameParser;
}

public void setResourceNameParser(Class<? extends BaseResourceNameParser> resourceNameParser) {
this.resourceNameParser = resourceNameParser;
}
}
Loading

0 comments on commit 4ba44ed

Please sign in to comment.