🎨 wx-java-open-spring-boot-starter 增加重试次数配置

This commit is contained in:
Forever杨 2021-04-21 13:27:59 +08:00 committed by GitHub
parent 0a75b0e902
commit 3eabfedd9c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 201 additions and 59 deletions

View File

@ -27,6 +27,10 @@
wx.open.config-storage.http-proxy-port=
wx.open.config-storage.http-proxy-username=
wx.open.config-storage.http-proxy-password=
# 最大重试次数默认5 次,如果小于 0则为 0
wx.open.config-storage.max-retry-times=5
# 重试时间间隔步进默认1000 毫秒,如果小于 0则为 1000
wx.open.config-storage.retry-sleep-millis=1000
```
3. 支持自动注入的类型: `WxOpenService, WxOpenMessageRouter, WxOpenComponentService`

View File

@ -1,7 +1,7 @@
package com.binarywang.spring.starter.wxjava.open.config;
import com.binarywang.spring.starter.wxjava.open.config.storage.WxOpenInJedisConfigStorageConfiguration;
import com.binarywang.spring.starter.wxjava.open.config.storage.WxOpenInMemoryConfigStorageConfiguration;
import com.binarywang.spring.starter.wxjava.open.config.storage.WxOpenInRedisConfigStorageConfiguration;
import com.binarywang.spring.starter.wxjava.open.config.storage.WxOpenInRedisTemplateConfigStorageConfiguration;
import com.binarywang.spring.starter.wxjava.open.config.storage.WxOpenInRedissonConfigStorageConfiguration;
import org.springframework.context.annotation.Configuration;
@ -16,7 +16,7 @@ import org.springframework.context.annotation.Import;
@Import({
WxOpenInMemoryConfigStorageConfiguration.class,
WxOpenInRedisTemplateConfigStorageConfiguration.class,
WxOpenInRedisConfigStorageConfiguration.class,
WxOpenInJedisConfigStorageConfiguration.class,
WxOpenInRedissonConfigStorageConfiguration.class
})
public class WxOpenStorageAutoConfiguration {

View File

@ -0,0 +1,32 @@
package com.binarywang.spring.starter.wxjava.open.config.storage;
import com.binarywang.spring.starter.wxjava.open.properties.WxOpenProperties;
import me.chanjar.weixin.open.api.impl.WxOpenInMemoryConfigStorage;
/**
* @author yl
*/
public abstract class AbstractWxOpenConfigStorageConfiguration {
protected WxOpenInMemoryConfigStorage config(WxOpenInMemoryConfigStorage config, WxOpenProperties properties) {
WxOpenProperties.ConfigStorage configStorageProperties = properties.getConfigStorage();
config.setWxOpenInfo(properties.getAppId(), properties.getSecret(), properties.getToken(), properties.getAesKey());
config.setHttpProxyHost(configStorageProperties.getHttpProxyHost());
config.setHttpProxyUsername(configStorageProperties.getHttpProxyUsername());
config.setHttpProxyPassword(configStorageProperties.getHttpProxyPassword());
if (configStorageProperties.getHttpProxyPort() != null) {
config.setHttpProxyPort(configStorageProperties.getHttpProxyPort());
}
int maxRetryTimes = configStorageProperties.getMaxRetryTimes();
if (configStorageProperties.getMaxRetryTimes() < 0) {
maxRetryTimes = 0;
}
int retrySleepMillis = configStorageProperties.getRetrySleepMillis();
if (retrySleepMillis < 0) {
retrySleepMillis = 1000;
}
config.setRetrySleepMillis(retrySleepMillis);
config.setMaxRetryTimes(maxRetryTimes);
return config;
}
}

View File

@ -27,7 +27,7 @@ import redis.clients.jedis.JedisPoolConfig;
)
@ConditionalOnClass({JedisPool.class, JedisPoolConfig.class})
@RequiredArgsConstructor
public class WxOpenInRedisConfigStorageConfiguration {
public class WxOpenInJedisConfigStorageConfiguration extends AbstractWxOpenConfigStorageConfiguration {
private final WxOpenProperties properties;
private final ApplicationContext applicationContext;
@ -35,16 +35,7 @@ public class WxOpenInRedisConfigStorageConfiguration {
@ConditionalOnMissingBean(WxOpenConfigStorage.class)
public WxOpenConfigStorage wxOpenConfigStorage() {
WxOpenInMemoryConfigStorage config = getWxOpenInRedisConfigStorage();
WxOpenProperties.ConfigStorage configStorageProperties = properties.getConfigStorage();
config.setWxOpenInfo(properties.getAppId(), properties.getSecret(), properties.getToken(), properties.getAesKey());
config.setHttpProxyHost(configStorageProperties.getHttpProxyHost());
config.setHttpProxyUsername(configStorageProperties.getHttpProxyUsername());
config.setHttpProxyPassword(configStorageProperties.getHttpProxyPassword());
if (configStorageProperties.getHttpProxyPort() != null) {
config.setHttpProxyPort(configStorageProperties.getHttpProxyPort());
}
return config;
return this.config(config, properties);
}
private WxOpenInRedisConfigStorage getWxOpenInRedisConfigStorage() {

View File

@ -18,22 +18,13 @@ import org.springframework.context.annotation.Configuration;
matchIfMissing = true, havingValue = "memory"
)
@RequiredArgsConstructor
public class WxOpenInMemoryConfigStorageConfiguration {
public class WxOpenInMemoryConfigStorageConfiguration extends AbstractWxOpenConfigStorageConfiguration {
private final WxOpenProperties properties;
@Bean
@ConditionalOnMissingBean(WxOpenConfigStorage.class)
public WxOpenConfigStorage wxOpenConfigStorage() {
WxOpenInMemoryConfigStorage config = new WxOpenInMemoryConfigStorage();
WxOpenProperties.ConfigStorage configStorageProperties = properties.getConfigStorage();
config.setWxOpenInfo(properties.getAppId(), properties.getSecret(), properties.getToken(), properties.getAesKey());
config.setHttpProxyHost(configStorageProperties.getHttpProxyHost());
config.setHttpProxyUsername(configStorageProperties.getHttpProxyUsername());
config.setHttpProxyPassword(configStorageProperties.getHttpProxyPassword());
if (configStorageProperties.getHttpProxyPort() != null) {
config.setHttpProxyPort(configStorageProperties.getHttpProxyPort());
}
return config;
return this.config(config, properties);
}
}

View File

@ -24,7 +24,7 @@ import org.springframework.data.redis.core.StringRedisTemplate;
)
@ConditionalOnClass(StringRedisTemplate.class)
@RequiredArgsConstructor
public class WxOpenInRedisTemplateConfigStorageConfiguration {
public class WxOpenInRedisTemplateConfigStorageConfiguration extends AbstractWxOpenConfigStorageConfiguration {
private final WxOpenProperties properties;
private final ApplicationContext applicationContext;
@ -32,16 +32,7 @@ public class WxOpenInRedisTemplateConfigStorageConfiguration {
@ConditionalOnMissingBean(WxOpenConfigStorage.class)
public WxOpenConfigStorage wxOpenConfigStorage() {
WxOpenInMemoryConfigStorage config = getWxOpenInRedisTemplateConfigStorage();
WxOpenProperties.ConfigStorage configStorageProperties = properties.getConfigStorage();
config.setWxOpenInfo(properties.getAppId(), properties.getSecret(), properties.getToken(), properties.getAesKey());
config.setHttpProxyHost(configStorageProperties.getHttpProxyHost());
config.setHttpProxyUsername(configStorageProperties.getHttpProxyUsername());
config.setHttpProxyPassword(configStorageProperties.getHttpProxyPassword());
if (configStorageProperties.getHttpProxyPort() != null) {
config.setHttpProxyPort(configStorageProperties.getHttpProxyPort());
}
return config;
return this.config(config, properties);
}
private WxOpenInRedisConfigStorage getWxOpenInRedisTemplateConfigStorage() {

View File

@ -29,7 +29,7 @@ import org.springframework.context.annotation.Configuration;
)
@ConditionalOnClass({Redisson.class, RedissonClient.class})
@RequiredArgsConstructor
public class WxOpenInRedissonConfigStorageConfiguration {
public class WxOpenInRedissonConfigStorageConfiguration extends AbstractWxOpenConfigStorageConfiguration {
private final WxOpenProperties properties;
private final ApplicationContext applicationContext;
@ -37,16 +37,7 @@ public class WxOpenInRedissonConfigStorageConfiguration {
@ConditionalOnMissingBean(WxOpenConfigStorage.class)
public WxOpenConfigStorage wxOpenConfigStorage() {
WxOpenInMemoryConfigStorage config = getWxOpenInRedissonConfigStorage();
WxOpenProperties.ConfigStorage configStorageProperties = properties.getConfigStorage();
config.setWxOpenInfo(properties.getAppId(), properties.getSecret(), properties.getToken(), properties.getAesKey());
config.setHttpProxyHost(configStorageProperties.getHttpProxyHost());
config.setHttpProxyUsername(configStorageProperties.getHttpProxyUsername());
config.setHttpProxyPassword(configStorageProperties.getHttpProxyPassword());
if (configStorageProperties.getHttpProxyPort() != null) {
config.setHttpProxyPort(configStorageProperties.getHttpProxyPort());
}
return config;
return this.config(config, properties);
}
private WxOpenInRedisConfigStorage getWxOpenInRedissonConfigStorage() {

View File

@ -91,6 +91,23 @@ public class WxOpenProperties {
*/
private String httpProxyPassword;
/**
* http 请求重试间隔
* <pre>
* {@link me.chanjar.weixin.mp.api.impl.BaseWxMpServiceImpl#setRetrySleepMillis(int)}
* {@link cn.binarywang.wx.miniapp.api.impl.BaseWxMaServiceImpl#setRetrySleepMillis(int)}
* </pre>
*/
private int retrySleepMillis = 1000;
/**
* http 请求最大重试次数
* <pre>
* {@link me.chanjar.weixin.mp.api.impl.BaseWxMpServiceImpl#setMaxRetryTimes(int)}
* {@link cn.binarywang.wx.miniapp.api.impl.BaseWxMaServiceImpl#setMaxRetryTimes(int)}
* </pre>
*/
private int maxRetryTimes = 5;
}
public enum StorageType {

View File

@ -205,6 +205,22 @@ public interface WxMaConfig {
*/
String getHttpProxyPassword();
/**
* http 请求重试间隔
* <pre>
* {@link cn.binarywang.wx.miniapp.api.impl.BaseWxMaServiceImpl#setRetrySleepMillis(int)}
* </pre>
*/
int getRetrySleepMillis();
/**
* http 请求最大重试次数
* <pre>
* {@link cn.binarywang.wx.miniapp.api.impl.BaseWxMaServiceImpl#setMaxRetryTimes(int)}
* </pre>
*/
int getMaxRetryTimes();
/**
* http client builder
*
@ -231,7 +247,7 @@ public interface WxMaConfig {
* 获取自定义的apiHost地址用于替换原请求中的https://api.weixin.qq.com
* 具体取值可以参考https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Interface_field_description.html
*
* @return 自定义的api域名地址
* @return 自定义的api域名地址
*/
String getApiHostUrl();
}

View File

@ -41,6 +41,10 @@ public class WxMaDefaultConfigImpl implements WxMaConfig {
private volatile int httpProxyPort;
private volatile String httpProxyUsername;
private volatile String httpProxyPassword;
private volatile int retrySleepMillis = 1000;
private volatile int maxRetryTimes = 5;
private volatile String jsapiTicket;
private volatile long jsapiTicketExpiresTime;
/**
@ -257,6 +261,24 @@ public class WxMaDefaultConfigImpl implements WxMaConfig {
this.httpProxyPassword = httpProxyPassword;
}
@Override
public int getRetrySleepMillis() {
return this.retrySleepMillis;
}
public void setRetrySleepMillis(int retrySleepMillis) {
this.retrySleepMillis = retrySleepMillis;
}
@Override
public int getMaxRetryTimes() {
return this.maxRetryTimes;
}
public void setMaxRetryTimes(int maxRetryTimes) {
this.maxRetryTimes = maxRetryTimes;
}
@Override
public String toString() {
return WxMaGsonBuilder.create().toJson(this);

View File

@ -172,6 +172,22 @@ public interface WxMpConfigStorage {
*/
String getHttpProxyPassword();
/**
* http 请求重试间隔
* <pre>
* {@link me.chanjar.weixin.mp.api.impl.BaseWxMpServiceImpl#setRetrySleepMillis(int)}
* </pre>
*/
int getRetrySleepMillis();
/**
* http 请求最大重试次数
* <pre>
* {@link me.chanjar.weixin.mp.api.impl.BaseWxMpServiceImpl#setMaxRetryTimes(int)}
* </pre>
*/
int getMaxRetryTimes();
/**
* Gets tmp dir file.
*

View File

@ -4,8 +4,8 @@ import lombok.Data;
import me.chanjar.weixin.common.bean.WxAccessToken;
import me.chanjar.weixin.common.enums.TicketType;
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
import me.chanjar.weixin.mp.config.WxMpHostConfig;
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
import me.chanjar.weixin.mp.config.WxMpHostConfig;
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
import java.io.File;
@ -37,6 +37,9 @@ public class WxMpDefaultConfigImpl implements WxMpConfigStorage, Serializable {
protected volatile String httpProxyUsername;
protected volatile String httpProxyPassword;
protected volatile int retrySleepMillis = 1000;
protected volatile int maxRetryTimes = 5;
protected volatile String jsapiTicket;
protected volatile long jsapiTicketExpiresTime;

View File

@ -139,6 +139,24 @@ public interface WxOpenConfigStorage {
*/
String getHttpProxyPassword();
/**
* http 请求重试间隔
* <pre>
* {@link me.chanjar.weixin.mp.api.impl.BaseWxMpServiceImpl#setRetrySleepMillis(int)}
* {@link cn.binarywang.wx.miniapp.api.impl.BaseWxMaServiceImpl#setRetrySleepMillis(int)}
* </pre>
*/
int getRetrySleepMillis();
/**
* http 请求最大重试次数
* <pre>
* {@link me.chanjar.weixin.mp.api.impl.BaseWxMpServiceImpl#setMaxRetryTimes(int)}
* {@link cn.binarywang.wx.miniapp.api.impl.BaseWxMaServiceImpl#setMaxRetryTimes(int)}
* </pre>
*/
int getMaxRetryTimes();
/**
* Gets apache http client builder.
*
@ -210,6 +228,7 @@ public interface WxOpenConfigStorage {
/**
* setAuthorizerRefreshToken(String appId, String authorizerRefreshToken) 方法重载方法
*
* @param appId the app id
* @param authorizerRefreshToken the authorizer refresh token
*/

View File

@ -6,6 +6,7 @@ import com.google.gson.reflect.TypeToken;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken;
import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.error.WxRuntimeException;
@ -14,12 +15,24 @@ import me.chanjar.weixin.common.util.http.URIUtil;
import me.chanjar.weixin.common.util.json.GsonParser;
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken;
import me.chanjar.weixin.open.api.*;
import me.chanjar.weixin.open.bean.*;
import me.chanjar.weixin.open.api.WxOpenComponentService;
import me.chanjar.weixin.open.api.WxOpenConfigStorage;
import me.chanjar.weixin.open.api.WxOpenFastMaService;
import me.chanjar.weixin.open.api.WxOpenMaService;
import me.chanjar.weixin.open.api.WxOpenMpService;
import me.chanjar.weixin.open.api.WxOpenService;
import me.chanjar.weixin.open.bean.WxOpenAuthorizerAccessToken;
import me.chanjar.weixin.open.bean.WxOpenComponentAccessToken;
import me.chanjar.weixin.open.bean.WxOpenCreateResult;
import me.chanjar.weixin.open.bean.WxOpenGetResult;
import me.chanjar.weixin.open.bean.WxOpenMaCodeTemplate;
import me.chanjar.weixin.open.bean.auth.WxOpenAuthorizationInfo;
import me.chanjar.weixin.open.bean.message.WxOpenXmlMessage;
import me.chanjar.weixin.open.bean.result.*;
import me.chanjar.weixin.open.bean.result.WxOpenAuthorizerInfoResult;
import me.chanjar.weixin.open.bean.result.WxOpenAuthorizerListResult;
import me.chanjar.weixin.open.bean.result.WxOpenAuthorizerOptionResult;
import me.chanjar.weixin.open.bean.result.WxOpenQueryAuthResult;
import me.chanjar.weixin.open.bean.result.WxOpenResult;
import me.chanjar.weixin.open.util.json.WxOpenGsonBuilder;
import org.apache.commons.lang3.StringUtils;
@ -49,8 +62,11 @@ public class WxOpenComponentServiceImpl implements WxOpenComponentService {
synchronized (WX_OPEN_MP_SERVICE_MAP) {
wxMpService = WX_OPEN_MP_SERVICE_MAP.get(appId);
if (wxMpService == null) {
wxMpService = new WxOpenMpServiceImpl(this, appId, getWxOpenConfigStorage().getWxMpConfigStorage(appId));
WxOpenConfigStorage storage = this.getWxOpenConfigStorage();
wxMpService = new WxOpenMpServiceImpl(this, appId, storage.getWxMpConfigStorage(appId));
// 配置重试次数和重试间隔
wxMpService.setMaxRetryTimes(storage.getMaxRetryTimes());
wxMpService.setRetrySleepMillis(storage.getRetrySleepMillis());
WX_OPEN_MP_SERVICE_MAP.put(appId, wxMpService);
}
}
@ -65,7 +81,11 @@ public class WxOpenComponentServiceImpl implements WxOpenComponentService {
synchronized (WX_OPEN_MA_SERVICE_MAP) {
wxOpenMaService = WX_OPEN_MA_SERVICE_MAP.get(appId);
if (wxOpenMaService == null) {
wxOpenMaService = new WxOpenMaServiceImpl(this, appId, getWxOpenConfigStorage().getWxMaConfig(appId));
WxOpenConfigStorage storage = this.getWxOpenConfigStorage();
wxOpenMaService = new WxOpenMaServiceImpl(this, appId, storage.getWxMaConfig(appId));
// 配置重试次数和重试间隔
wxOpenMaService.setMaxRetryTimes(storage.getMaxRetryTimes());
wxOpenMaService.setRetrySleepMillis(storage.getRetrySleepMillis());
WX_OPEN_MA_SERVICE_MAP.put(appId, wxOpenMaService);
}
}
@ -80,7 +100,11 @@ public class WxOpenComponentServiceImpl implements WxOpenComponentService {
synchronized (WX_OPEN_FAST_MA_SERVICE_MAP) {
fastMaService = WX_OPEN_FAST_MA_SERVICE_MAP.get(appId);
if (fastMaService == null) {
fastMaService = new WxOpenFastMaServiceImpl(this, appId, getWxOpenConfigStorage().getWxMaConfig(appId));
WxOpenConfigStorage storage = this.getWxOpenConfigStorage();
fastMaService = new WxOpenFastMaServiceImpl(this, appId, storage.getWxMaConfig(appId));
// 配置重试次数和重试间隔
fastMaService.setMaxRetryTimes(storage.getMaxRetryTimes());
fastMaService.setRetrySleepMillis(storage.getRetrySleepMillis());
WX_OPEN_FAST_MA_SERVICE_MAP.put(appId, fastMaService);
}
}
@ -103,7 +127,7 @@ public class WxOpenComponentServiceImpl implements WxOpenComponentService {
return SHA1.gen(getWxOpenConfigStorage().getComponentToken(), timestamp, nonce)
.equals(signature);
} catch (Exception e) {
this.log.error("Checking signature failed, and the reason is :" + e.getMessage());
log.error("Checking signature failed, and the reason is :" + e.getMessage());
return false;
}
}

View File

@ -3,12 +3,11 @@ package me.chanjar.weixin.open.api.impl;
import cn.binarywang.wx.miniapp.config.WxMaConfig;
import lombok.Data;
import lombok.Getter;
import me.chanjar.weixin.common.bean.WxAccessToken;
import me.chanjar.weixin.common.enums.TicketType;
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
import me.chanjar.weixin.mp.config.WxMpHostConfig;
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
import me.chanjar.weixin.mp.config.WxMpHostConfig;
import me.chanjar.weixin.open.api.WxOpenConfigStorage;
import me.chanjar.weixin.open.bean.WxOpenAuthorizerAccessToken;
import me.chanjar.weixin.open.bean.WxOpenComponentAccessToken;
@ -39,6 +38,22 @@ public class WxOpenInMemoryConfigStorage implements WxOpenConfigStorage {
private int httpProxyPort;
private String httpProxyUsername;
private String httpProxyPassword;
/**
* http 请求重试间隔
* <pre>
* {@link me.chanjar.weixin.mp.api.impl.BaseWxMpServiceImpl#setRetrySleepMillis(int)}
* {@link cn.binarywang.wx.miniapp.api.impl.BaseWxMaServiceImpl#setRetrySleepMillis(int)}
* </pre>
*/
private int retrySleepMillis = 1000;
/**
* http 请求最大重试次数
* <pre>
* {@link me.chanjar.weixin.mp.api.impl.BaseWxMpServiceImpl#setMaxRetryTimes(int)}
* {@link cn.binarywang.wx.miniapp.api.impl.BaseWxMaServiceImpl#setMaxRetryTimes(int)}
* </pre>
*/
private int maxRetryTimes = 5;
private ApacheHttpClientBuilder apacheHttpClientBuilder;
private Map<String, Token> authorizerRefreshTokens = new ConcurrentHashMap<>();
@ -511,6 +526,16 @@ public class WxOpenInMemoryConfigStorage implements WxOpenConfigStorage {
return this.wxOpenConfigStorage.getHttpProxyPassword();
}
@Override
public int getRetrySleepMillis() {
return this.wxOpenConfigStorage.getRetrySleepMillis();
}
@Override
public int getMaxRetryTimes() {
return this.wxOpenConfigStorage.getMaxRetryTimes();
}
@Override
public String toString() {
return WxOpenGsonBuilder.create().toJson(this);