跳至主要內容

公用工具类使用手册

大约 2 分钟

公用工具类使用手册

一、Sequence No生成工具

1. 工具说明

  此工具类支持基于snowflake算法、redis、db三种方式生成唯一序列号。工具类自动引入了common-redis-starter包,如果需要用到redis工具的,不需另外引用redis包。

使用db方式时,需要额外配置一个 DbSequenceHelper 的接口实现类。

2. 引用依赖包

在pom.xml文件中增加配置

<dependency>
   <groupId>com.pcitc.si</groupId>
   <artifactId>common-utils-sequence</artifactId>
   <version>1.0.0-SNAPSHOT</version>
</dependency>

3. 参数配置

  四个开关配置参数,分别为:sequence.db.enable、sequence.redis.enable、sequence.snowflake.redis.enable、sequence.snowflake.enable。
  sequence.db.enable: 是否开户数据库序列生成工具开关。
  sequence.redis.enable: 是否开户redis序列生成工具开关。
  sequence.snowflake.redis.enable: 是否开户基于redis的snowflake序列生成工具开关。   snowflake.enable: 是否开户snowflake序列生成工具开关(不配置也默认开启)

示例:

properties方式:
snowflake.enable=true

yml方式
sequence:
  snowflake:
    enable: true

四个开关参数可同此开启。

sequence.db.enable的额外配置:

//数据库操作实现
sequence.db.dbSequenceHelper=xxx.xxx.DbSequenceHelperImpl
//重试次数
sequence.db.retryTimes=3

4.使用

对应的工具开头开启后,可通过其util类直接使用,如snowflake.enable=true时,可直接使用SeqNoSnowflakeUtil工具类。

String seqNo = SeqNoSnowflakeUtil.getId();
或
long seqNo = SeqNoSnowflakeUtil.getNext();

其中四个工具类分类为:
SeqNoDbUtil、SeqNoRedisUtil、SeqNoSnowflakeRedisUtil、SeqNoSnowflakeUtil

二、HttpClient工具

1.引用依赖包

在pom.xml文件中增加:

<dependency>
    <groupId>com.pcitc.si</groupId>
    <artifactId>common-utils-httpclient</artifactId>
    <version>1.0.0-SNAPSHOT</version>
</dependency>

2.参数配置

可选配置参数(如不配置,都用默认配置):

//最大连接数 默认200
httpclient.pool.maxTotal=200
//单个路由数量 默认50
httpclient.pool.defaultMaxPerRoute=50
//连接超时(毫秒) 默认10000
httpclient.pool.connectTimeout=10000
//应答超时(毫秒) 默认300000
httpclient.pool.socketTimeout=300000
//从连接池获取连接超时(毫秒) 默认2000
httpclient.pool.connectionRequestTimeout=200
//可用空闲连接过期时间,重用空闲连接时会先检查是否空闲时间超过这个时间,如果超过,释放socket重新建立 (毫秒) 默认5000
httpclient.pool.validateAfterInactivity=5000
//默认编码 默认"UTF-8"
httpclient.pool.charEncoding="UTF-8"
//最大连接空闲时长(秒) 默认20
httpclient.pool.maxIdleTime=20

3.使用

使用示例:

@Autowired
HttpClientCompent httpClientCompent;

Map<String,String> params = new HashMap();
params.put("content", "基本面");
Map<String,String> header = new HashMap<>();
header.put(HttpHeaders.USER_AGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
header.put(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString());
header.put(HttpHeaders.ACCEPT, ContentType
        .create(ContentType.APPLICATION_XML.getMimeType(), Consts.UTF_8).toString());
        
String resp = httpClientCompent.doPost("uri",header, JSONUtil.toJsonStr(params), "UTF-8");

4.提供的可使用方法列表

  • public void doGet(String requestUrl) throws Exception;
  • public String doGet(String requestUrl, String rspEncoding) throws Exception;
  • public String doGet(String requestUrl, Map<String, String> headerMap) throws Exception;
  • public String doGet(String requestUrl, Map<String, String> paramsMap, Map<String, String> headerMap) throws Exception;
  • public String doGet(String requestUrl, Map<String, String> paramsMap, Map<String, String> headerMap, String rspEncoding) throws Exception;
  • public String doPost(String requestUrl, Map<String, String> headerMap, String requestMessage, String msgEncoding) throws Exception;
  • public String doPost(String requestUrl, Map<String, String> headerMap, String requestMessage) throws Exception;
  • public String doPost(String requestUrl, String requestMessage) throws Exception;
  • public String doPostNoBody(String requestUrl, String rspEncoding) throws Exception;
  • public String doPost(String requestUrl, Map<String, String> paramsMap, Map<String, String> headerMap, HttpEntity requestEntity, String msgEncoding, String... reqs) throws Exception;
  • public int doTest(String requestUrl, Map<String, String> paramsMap, RequestConfig config) throws Exception