com.github.duanxinyuan:library-network-netty

网络工具类,包含OKHttp、TCP、Netty、AKKA的封装

License

License

Categories

Categories

Net Netty Networking
GroupId

GroupId

com.github.duanxinyuan
ArtifactId

ArtifactId

library-network-netty
Last Version

Last Version

1.0.0
Release Date

Release Date

Type

Type

jar
Description

Description

网络工具类,包含OKHttp、TCP、Netty、AKKA的封装

Download library-network-netty

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.duanxinyuan/library-network-netty/ -->
<dependency>
    <groupId>com.github.duanxinyuan</groupId>
    <artifactId>library-network-netty</artifactId>
    <version>1.0.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.duanxinyuan/library-network-netty/
implementation 'com.github.duanxinyuan:library-network-netty:1.0.0'
// https://jarcasting.com/artifacts/com.github.duanxinyuan/library-network-netty/
implementation ("com.github.duanxinyuan:library-network-netty:1.0.0")
'com.github.duanxinyuan:library-network-netty:jar:1.0.0'
<dependency org="com.github.duanxinyuan" name="library-network-netty" rev="1.0.0">
  <artifact name="library-network-netty" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.duanxinyuan', module='library-network-netty', version='1.0.0')
)
libraryDependencies += "com.github.duanxinyuan" % "library-network-netty" % "1.0.0"
[com.github.duanxinyuan/library-network-netty "1.0.0"]

Dependencies

compile (4)

Group / Artifact Type Version
com.github.duanxinyuan : library-json jar 1.2.0
org.apache.commons : commons-lang3 jar 3.6
org.projectlombok : lombok jar 1.16.20
com.google.guava : guava jar 25.0-jre

Project Modules

There are no modules declared in this project.

util-http

  • Http 协议网络交互 API
  • 极简 API
  • 支持 GET、POST、PATCH、PUT、DELETE、DOWNLOAD
  • 支持配置 请求日志、请求超时时间、失败重试次数、重试间隔毫秒数
  • Http工具类:Http

Maven 依赖

<dependency>
    <groupId>com.github.duanxinyuan</groupId>
    <artifactId>util-http</artifactId>
</dependency>

配置示例

properties 配置示例

#是否默认开启请求日志,默认为true
http.defaultRequestLogEnable=true
#默认请求超时时间,单位为秒,默认为60秒
http.defaultTimoutMillis=60000
#默认请求失败重试次数,默认为0
http.defaultRetries=0
#请求失败重试间隔毫秒数,默认为0
http.retryIntervalMillis=0
#每个地址的最大连接数,默认为5
http.maxIdleConnections=5
#连接的存活时间,单位为分钟,默认5分钟
http.keepAliveDuration=5
#异步请求的最大并发请求数,默认为64
http.async.maxRequests=64
#异步请求的单个域名最大并发请求数,默认为5
http.async.maxRequestsPerHost=5

yaml 配置示例

http:
  #是否默认开启请求日志,默认为true
  defaultRequestLogEnable: true
  #默认请求超时时间,单位为秒,默认为60秒
  defaultTimoutMillis: 60000
  #默认请求失败重试次数,默认为0
  defaultRetries: 0
  #请求失败重试间隔毫秒数,默认为0
  retryIntervalMillis: 0
  #每个地址的最大连接数,默认为5
  maxIdleConnections: 5
  #连接的存活时间,单位为分钟,默认5分钟
  keepAliveDuration: 5
  async:
    #异步请求的最大并发请求数,默认为64
    maxRequests: 64
    #异步请求的单个域名最大并发请求数,默认为5
    maxRequestsPerHost: 5

Http 使用示例

public class HttpTest{

    private String url = "http://test.com/api";
    private Headers headers;
    private Params params;

    @Before
    public void headersAndParams() {
        headers = new Headers("token", "").add("key", "").add("k1", "v1", "k2", "v2", "k3", "v3");
        params = new Params("name", "").add("key", "").add("k1", "v1", "k2", "v2", "k3", "v3");
    }

    /**
     * GET
     */
    public void get() {
        //同步请求
        String s = Http.get(url, headers, params);
        Result result = Http.get(url, headers, params, Result.class);

        Response response = Http.getForNative(url, headers, params);

        //异步请求
        Http.getAsync(url, headers, params, callback);
    }

    /**
     * POST
     */
    public void post() {
        //同步请求:
        String s = Http.post(url, headers, params);
        Result result = Http.post(url, headers, params, Result.class);

        Response response = Http.postForNative(url, headers, params);

        //异步请求:
        Http.postAsync(url, headers, params, callback);

        //POST同步提交JSON:
        String s = Http.postJson(url, headers, params, testBean);
        Result result = Http.postJson(url, testBean, Result.class);

        Response response = Http.postJsonForNative(url, testBean);

        //POST异步提交JSON:
        Http.postJsonAsync(url, testBean, callback);

        //POST同步上传文件:
        String s = Http.postFile(url, headers, params, fileparam);
        Result result = Http.postFile(url, fileparam, Result.class);

        Response response = Http.postFileForNative(url, fileparam);

        //POST同步上传文件:
        Http.postFileAsync(url, testBean, fileparam);

        //POST异步上传文件:
        Http.postFileAsync(url, testBean, fileparam, callback);
    }

    /**
     * PUT
     */
    public void put() {
        //同步请求:
        String s = Http.put(url, headers, params);
        Result result = Http.put(url, headers, params, Result.class);

        Response response = Http.putForNative(url, headers, params);

        //异步请求:
        Http.putAsync(url, headers, params, callback);

        //PUT同步提交JSON:
        String s = Http.putJson(url, headers, params, testBean);
        Result result = Http.putJson(url, testBean, Result.class);

        Response response = Http.putJsonForNative(url, testBean);

        //PUT异步提交JSON:
        Http.putJsonAsync(url, testBean, callback);
    }

    /**
     * PATCH
     */
    public void patch() {
        //同步请求
        String s = Http.patch(url, headers, params);
        Result result = Http.patch(url, headers, params, Result.class);

        Response response = Http.patchForNative(url, headers, params);

        //异步请求
        Http.patchAsync(url, headers, params, callback);

    }

    /**
     * DELETE
     */
    public void delete() {
        //同步请求
        String s = Http.delete(url, headers, params);
        Result result = Http.delete(url, headers, params, Result.class);

        Response response = Http.deleteForNative(url, headers, params);

        //异步请求
        Http.deleteAsync(url, headers, params, callback);
    }

    /**
     * DOWNLOAD
     */
    public void download() {
        //同步下载,targetPath为本地文件存储地址
        Http.download(url, targetPath);

        //异步下载,targetPath为本地文件存储地址
        Http.downloadAsync(url, targetPath);

        //以byte[]、InputStream或者Reader的形式接收数据
        InputStream inputStream = Http.get(url, InputStream.class);
        Reader reader = Http.get(url, Reader.class);

    }

    /**
     * 请求配置(请求日志开关、请求超时时间、失败重试次数、重试间隔毫秒数)
     */
    public void config() {

        //完整示例
        Http.requestLogEnable(false).timeout(300).retries(2).get(url, headers, params);
        Http.requestLogEnable(false).timeout(300).retries(3, 30000).get(url, headers, params);

        //设置请求日志开关
        Http.requestLogEnable(false).get(url, headers, params);

        //关闭日志请求:
        Http.disableRequestLog().get(url, headers, params);

        //开启日志请求:
        Http.enableRequestLog().get(url, headers, params);

        //设置请求超时时间,单位为秒:
        Http.timeout(300).get(url, headers, params);
        Http.timeout(5, TimeUnit.MINUTES).get(url, headers, params);
        Http.timeoutMillis(3000).get(url, headers, params);

        //设置失败重试次数
        Http.retries(100).get(url, headers, params);

        //设置全局请求日志开关:
        Http.setDefaultRequestLogEnable(false);

        //设置全局超时时间,单位为秒:
        Http.setDefaultTimeout(120);

        //设置全局重试次数
        Http.setDefaultRetries(3);
    }

}

Versions

Version
1.0.0