com.github.chengtengfei:okhttp3-fast-spring-boot-starter

The test uploads the jar to the maven central repository

License

License

Categories

Categories

Spring Boot Container Microservices
GroupId

GroupId

com.github.chengtengfei
ArtifactId

ArtifactId

okhttp3-fast-spring-boot-starter
Last Version

Last Version

0.0.1
Release Date

Release Date

Type

Type

jar
Description

Description

com.github.chengtengfei:okhttp3-fast-spring-boot-starter
The test uploads the jar to the maven central repository
Project URL

Project URL

https://github.com/chengtengfei/okhttp3-fast-spring-boot-starter
Source Code Management

Source Code Management

https://github.com/chengtengfei/okhttp3-fast-spring-boot-starter/tree/master

Download okhttp3-fast-spring-boot-starter

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.chengtengfei/okhttp3-fast-spring-boot-starter/ -->
<dependency>
    <groupId>com.github.chengtengfei</groupId>
    <artifactId>okhttp3-fast-spring-boot-starter</artifactId>
    <version>0.0.1</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.chengtengfei/okhttp3-fast-spring-boot-starter/
implementation 'com.github.chengtengfei:okhttp3-fast-spring-boot-starter:0.0.1'
// https://jarcasting.com/artifacts/com.github.chengtengfei/okhttp3-fast-spring-boot-starter/
implementation ("com.github.chengtengfei:okhttp3-fast-spring-boot-starter:0.0.1")
'com.github.chengtengfei:okhttp3-fast-spring-boot-starter:jar:0.0.1'
<dependency org="com.github.chengtengfei" name="okhttp3-fast-spring-boot-starter" rev="0.0.1">
  <artifact name="okhttp3-fast-spring-boot-starter" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.chengtengfei', module='okhttp3-fast-spring-boot-starter', version='0.0.1')
)
libraryDependencies += "com.github.chengtengfei" % "okhttp3-fast-spring-boot-starter" % "0.0.1"
[com.github.chengtengfei/okhttp3-fast-spring-boot-starter "0.0.1"]

Dependencies

compile (6)

Group / Artifact Type Version
org.springframework.boot : spring-boot-autoconfigure jar
org.springframework.boot : spring-boot-configuration-processor Optional jar
org.springframework.boot : spring-boot-starter-actuator jar
com.squareup.okhttp3 : okhttp jar 3.14.9
org.slf4j : slf4j-api jar 1.7.30
io.dropwizard.metrics : metrics-core jar 4.1.9

Project Modules

There are no modules declared in this project.

English

okhttp3-fast-spring-boot-starter

Spring Boot项目中方便的使用okhttp3,并提供了简单易用的POSTGETPUTDELETE方法帮助你连接HTTP请求,本项目依赖OkHttp的版本为3.14.9,适用于Spring Boot 2.X版本。 本项目不仅可助力快速使用okhttp3,还可以作为学习okhttp3使用方式的入门示例。

Getting Started

目前项目处于测试中,若是需要使用则可以添加snapshot版本的jar包进行试用,发现问题或者对本项目有更多想法的话,请通过[email protected]联系我。

<dependency>
    <groupId>com.github.chengtengfei</groupId>
    <artifactId>okhttp3-fast-spring-boot-starter</artifactId>
    <version>0.0.2-SNAPSHOT</version>
</dependency>

How to use it

Spring Boot项目中可以很方便的使用,只需要简单的注入即可。

import org.springframework.stereotype.Component;
import org.springframework.beans.factory.annotation.Autowired;

import com.github.chengtengfei.bean.OkHttp3Fast;
import com.github.chengtengfei.constant.RawMediaType;
import okhttp3.*;

@Component
public class Okhttp3FastTest {

    @Autowired
    private OkHttp3Fast okHttp3Fast;

    public void run() throws Exception {
        
        // get method
        Response response = okHttp3Fast.get("http://www.baidu.com/");
        if (response.isSuccessful()) {
            System.out.println(response.body() == null ? "" : response.body().string());
        }
        
        // post method
        String body = "{\"name\":\"Alice\", \"age\":18}";
        Response postResponse = okHttp3Fast.putRaw("http://www.space.test.com/search", body, RawMediaType.APPLICATION_JSON);
        if (postResponse.isSuccessful()) {
            System.out.println(postResponse.body() == null ? "" : postResponse.body().string());
        }
    }
}

同时okhttp3-fast-spring-boot-starter提供了几个用于配置请求的参数,可以在application.properties文件中配置以修改请求的方式。

# 用来控制OkHttp3Fast是否支持对HTTPS接口的请求,默认是true
okhttp3.fast.https
# 用来控制OkHttp3Fast请求接口时的连接超时时间,默认3秒
okhttp3.fast.connectTimeout
# 用来控制OkHttp3Fast请求接口时的读取超时时间,默认3秒
okhttp3.fast.readTimeout
# 用来控制OkHttp3Fast请求接口时的写入超时时间,默认3秒
okhttp3.fast.writeTimeout
# 用来控制OkHttp3Fast请求失败的时候是否执行重连,默认false
okhttp3.fast.retryOnConnectionFailure

上述参数中okhttp3.fast.retryOnConnectionFailure对请求失败执行重连的时候,要保证该请求失败的情况是是以下情况之一。

  • 无法访问的IP地址。如果网址的主机有多个IP地址,则无法访问任何单个IP地址不会使整个请求失败。这可以增加多宿主服务的可用性。
  • 连接池中失效的连接。 ConnectionPool重用连接以减少请求延迟,但是这些连接有时会超时。
  • 无法访问的代理服务器。

关于OkHttp3Fast发送请求时连接、读取、写入超时时间的说明,可参考TBD

Features

OkHttpClient复用

okhttp3-fast-spring-boot-starter不仅暴露出名为okHttp3FastSpring Bean,还暴露出okHttpClientSpring Bean,而okHttp3Fast又是依赖于okHttpClient。若是项目中已存在名为okHttpClientSpring Bean,则 okHttp3Fast则直接依赖于已经存在的okHttpClient而不会使用okhttp3-fast-spring-boot-starter提供的okHttpClient

所以如果想改写okHttp3Fast的一些功能,则可以直接提供一个自己喜欢的okHttpClientokHttp3Fast使用。

若是想增强okHttp3Fast的功能,也可以通过对okhttp3-fast-spring-boot-starter提供的okHttpClient添加更多的功能来完成。

易用的认证方式

okhttp3.fast.retryOnConnectionFailure基于OkHttp3Authenticator机制提供了更方便易用的认证使用方式。若是请求的服务开启了Baisc Auth或者 Digest Auth认证方式,在使用OkHttp3Fast访问接口时,只需要在访问前设置如下代码。

// Baisc Auth认证
AuthConfig.basicAuth("admin", "123456");
AuthConfig.on();
// okHttp3Fast method sent
AuthConfig.off();

// Digest Auth认证
AuthConfig.digestAuth("admin", "123456");
AuthConfig.on();
// okHttp3Fast method sent
AuthConfig.off();

监控功能

基于Spring Boot Actuator的监控功能提供metrics统计,监控暴露端点为okhttp3fast,在Spring Boot 2.X项目开启Actuator并使用默认配置时,通过访问 http://localhost:8080/actuator/okhttp3fast即可获取如下的统计信息。

{
    "okhttp3-fast": "https://github.com/chengtengfei/okhttp3-fast-spring-boot-starter",
    "author": "[email protected]",
    "metrics": {
        "okhttp3.fast.client.www.baidu.com.GET.meanRate": "1.88",
        "okhttp3.fast.client.www.baidu.com.GET.fifteenMinuteRate": "18.93",
        "okhttp3.fast.client.www.baidu.com.GET.count": 107,
        "okhttp3.fast.client.www.baidu.com.GET.oneMinuteRate": "8.76",
        "okhttp3.fast.client.www.baidu.com.GET.fiveMinuteRate": "16.95"
    }
}

其中okhttp3.fast.client标识请求客户端,www.baidu.com标识请求的域名,GET标识请求使用的HTTP Method。 从五个指标统计对某个地址的请求。

  • count表示应用启动后对总的请求次数
  • oneMinuteRate表示最近1分钟平均请求数
  • fiveMinuteRate表示最近5分钟平均请求数
  • fifteenMinuteRate表示最近15分钟平均请求数
  • meanRate表示应用启动后总的平均请求数(单位为秒)

详细的调用日志

使用okhttp3-fast-spring-boot-starter时把日志级别调到DEBUG模式,okhttp3-fast-spring-boot-starter会把所有HTTP请求的的详细信息打印出来,方便追踪和查找问题。

License

This project is licensed under the Apache 2.0 License.

Acknowledgments

Versions

Version
0.0.1