🎨 #1516 公众号spring-boot-starter 优化代码,增加http客户端和代理等配置

This commit is contained in:
Mario Luo 2020-04-17 15:45:43 +08:00 committed by GitHub
parent 2112db791d
commit dc01e0b1ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 269 additions and 125 deletions

View File

@ -11,18 +11,25 @@
2. 添加配置(application.properties) 2. 添加配置(application.properties)
```properties ```properties
# 公众号配置(必填) # 公众号配置(必填)
wx.mp.appId = @appId wx.mp.appId = appId
wx.mp.secret = @secret wx.mp.secret = @secret
wx.mp.token = @token wx.mp.token = @token
wx.mp.aesKey = @aesKey wx.mp.aesKey = @aesKey
# 存储配置redis(可选) # 存储配置redis(可选)
wx.mp.config-storage.type = redis wx.mp.config-storage.type = redis # 配置类型: memory(默认), redis, jedis, redistemplate
wx.mp.config-storage.redis.host = 127.0.0.1 wx.mp.config-storage.key-prefix = wx # 相关redis前缀配置: wx(默认)
wx.mp.config-storage.redis.port = 6379 wx.mp.config-storage.redis.host = 127.0.0.1
wx.mp.config-storage.redis.port = 6379
# http客户端配置
wx.mp.config-storage.http-client-type=httpclient # http客户端类型: httpclient(默认), okhttp, joddhttp
wx.mp.config-storage.http-proxy-host=
wx.mp.config-storage.http-proxy-port=
wx.mp.config-storage.http-proxy-username=
wx.mp.config-storage.http-proxy-password=
``` ```
3. 支持自动注入的类型 3. 支持自动注入的类型
`WxMpService`以及相关的服务类, 比如: `wxMpService.getXxxService` `WxMpService`以及~~相关的服务类, 比如: `wxMpService.getXxxService`~~

View File

@ -25,10 +25,10 @@
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.redisson</groupId> <groupId>org.springframework.data</groupId>
<artifactId>redisson</artifactId> <artifactId>spring-data-redis</artifactId>
<scope>compile</scope> <version>${spring.boot.version}</version>
<optional>true</optional> <scope>provided</scope>
</dependency> </dependency>
</dependencies> </dependencies>

View File

@ -1,7 +1,11 @@
package com.binarywang.spring.starter.wxjava.mp.config; package com.binarywang.spring.starter.wxjava.mp.config;
import com.binarywang.spring.starter.wxjava.mp.properties.WxMpProperties;
import me.chanjar.weixin.mp.api.*; import me.chanjar.weixin.mp.api.*;
import me.chanjar.weixin.mp.api.impl.WxMpServiceHttpClientImpl;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl; import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.api.impl.WxMpServiceJoddHttpImpl;
import me.chanjar.weixin.mp.api.impl.WxMpServiceOkHttpImpl;
import me.chanjar.weixin.mp.config.WxMpConfigStorage; import me.chanjar.weixin.mp.config.WxMpConfigStorage;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
@ -17,113 +21,161 @@ public class WxMpServiceAutoConfiguration {
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public WxMpService wxMpService(WxMpConfigStorage configStorage) { public WxMpService wxMpService(WxMpConfigStorage configStorage, WxMpProperties wxMpProperties) {
WxMpService wxMpService = new WxMpServiceImpl(); WxMpProperties.HttpClientType httpClientType = wxMpProperties.getConfigStorage().getHttpClientType();
WxMpService wxMpService;
if (httpClientType == WxMpProperties.HttpClientType.okhttp) {
wxMpService = newWxMpServiceJoddHttpImpl();
} else if (httpClientType == WxMpProperties.HttpClientType.joddhttp) {
wxMpService = newWxMpServiceOkHttpImpl();
} else if (httpClientType == WxMpProperties.HttpClientType.httpclient) {
wxMpService = newWxMpServiceHttpClientImpl();
} else {
wxMpService = newWxMpServiceImpl();
}
wxMpService.setWxMpConfigStorage(configStorage); wxMpService.setWxMpConfigStorage(configStorage);
return wxMpService; return wxMpService;
} }
private WxMpService newWxMpServiceImpl() {
return new WxMpServiceImpl();
}
private WxMpService newWxMpServiceHttpClientImpl() {
return new WxMpServiceHttpClientImpl();
}
private WxMpService newWxMpServiceOkHttpImpl() {
return new WxMpServiceOkHttpImpl();
}
private WxMpService newWxMpServiceJoddHttpImpl() {
return new WxMpServiceJoddHttpImpl();
}
@Bean @Bean
@Deprecated
public WxMpKefuService wxMpKefuService(WxMpService wxMpService) { public WxMpKefuService wxMpKefuService(WxMpService wxMpService) {
return wxMpService.getKefuService(); return wxMpService.getKefuService();
} }
@Bean @Bean
@Deprecated
public WxMpMaterialService wxMpMaterialService(WxMpService wxMpService) { public WxMpMaterialService wxMpMaterialService(WxMpService wxMpService) {
return wxMpService.getMaterialService(); return wxMpService.getMaterialService();
} }
@Bean @Bean
@Deprecated
public WxMpMenuService wxMpMenuService(WxMpService wxMpService) { public WxMpMenuService wxMpMenuService(WxMpService wxMpService) {
return wxMpService.getMenuService(); return wxMpService.getMenuService();
} }
@Bean @Bean
@Deprecated
public WxMpUserService wxMpUserService(WxMpService wxMpService) { public WxMpUserService wxMpUserService(WxMpService wxMpService) {
return wxMpService.getUserService(); return wxMpService.getUserService();
} }
@Bean @Bean
@Deprecated
public WxMpUserTagService wxMpUserTagService(WxMpService wxMpService) { public WxMpUserTagService wxMpUserTagService(WxMpService wxMpService) {
return wxMpService.getUserTagService(); return wxMpService.getUserTagService();
} }
@Bean @Bean
@Deprecated
public WxMpQrcodeService wxMpQrcodeService(WxMpService wxMpService) { public WxMpQrcodeService wxMpQrcodeService(WxMpService wxMpService) {
return wxMpService.getQrcodeService(); return wxMpService.getQrcodeService();
} }
@Bean @Bean
@Deprecated
public WxMpCardService wxMpCardService(WxMpService wxMpService) { public WxMpCardService wxMpCardService(WxMpService wxMpService) {
return wxMpService.getCardService(); return wxMpService.getCardService();
} }
@Bean @Bean
@Deprecated
public WxMpDataCubeService wxMpDataCubeService(WxMpService wxMpService) { public WxMpDataCubeService wxMpDataCubeService(WxMpService wxMpService) {
return wxMpService.getDataCubeService(); return wxMpService.getDataCubeService();
} }
@Bean @Bean
@Deprecated
public WxMpUserBlacklistService wxMpUserBlacklistService(WxMpService wxMpService) { public WxMpUserBlacklistService wxMpUserBlacklistService(WxMpService wxMpService) {
return wxMpService.getBlackListService(); return wxMpService.getBlackListService();
} }
@Bean @Bean
@Deprecated
public WxMpStoreService wxMpStoreService(WxMpService wxMpService) { public WxMpStoreService wxMpStoreService(WxMpService wxMpService) {
return wxMpService.getStoreService(); return wxMpService.getStoreService();
} }
@Bean @Bean
@Deprecated
public WxMpTemplateMsgService wxMpTemplateMsgService(WxMpService wxMpService) { public WxMpTemplateMsgService wxMpTemplateMsgService(WxMpService wxMpService) {
return wxMpService.getTemplateMsgService(); return wxMpService.getTemplateMsgService();
} }
@Bean @Bean
@Deprecated
public WxMpSubscribeMsgService wxMpSubscribeMsgService(WxMpService wxMpService) { public WxMpSubscribeMsgService wxMpSubscribeMsgService(WxMpService wxMpService) {
return wxMpService.getSubscribeMsgService(); return wxMpService.getSubscribeMsgService();
} }
@Bean @Bean
@Deprecated
public WxMpDeviceService wxMpDeviceService(WxMpService wxMpService) { public WxMpDeviceService wxMpDeviceService(WxMpService wxMpService) {
return wxMpService.getDeviceService(); return wxMpService.getDeviceService();
} }
@Bean @Bean
@Deprecated
public WxMpShakeService wxMpShakeService(WxMpService wxMpService) { public WxMpShakeService wxMpShakeService(WxMpService wxMpService) {
return wxMpService.getShakeService(); return wxMpService.getShakeService();
} }
@Bean @Bean
@Deprecated
public WxMpMemberCardService wxMpMemberCardService(WxMpService wxMpService) { public WxMpMemberCardService wxMpMemberCardService(WxMpService wxMpService) {
return wxMpService.getMemberCardService(); return wxMpService.getMemberCardService();
} }
@Bean @Bean
@Deprecated
public WxMpMassMessageService wxMpMassMessageService(WxMpService wxMpService) { public WxMpMassMessageService wxMpMassMessageService(WxMpService wxMpService) {
return wxMpService.getMassMessageService(); return wxMpService.getMassMessageService();
} }
@Bean @Bean
@Deprecated
public WxMpAiOpenService wxMpAiOpenService(WxMpService wxMpService) { public WxMpAiOpenService wxMpAiOpenService(WxMpService wxMpService) {
return wxMpService.getAiOpenService(); return wxMpService.getAiOpenService();
} }
@Bean @Bean
@Deprecated
public WxMpWifiService wxMpWifiService(WxMpService wxMpService) { public WxMpWifiService wxMpWifiService(WxMpService wxMpService) {
return wxMpService.getWifiService(); return wxMpService.getWifiService();
} }
@Bean @Bean
@Deprecated
public WxMpMarketingService wxMpMarketingService(WxMpService wxMpService) { public WxMpMarketingService wxMpMarketingService(WxMpService wxMpService) {
return wxMpService.getMarketingService(); return wxMpService.getMarketingService();
} }
@Bean @Bean
@Deprecated
public WxMpCommentService wxMpCommentService(WxMpService wxMpService) { public WxMpCommentService wxMpCommentService(WxMpService wxMpService) {
return wxMpService.getCommentService(); return wxMpService.getCommentService();
} }
@Bean @Bean
@Deprecated
public WxMpOcrService wxMpOcrService(WxMpService wxMpService) { public WxMpOcrService wxMpOcrService(WxMpService wxMpService) {
return wxMpService.getOcrService(); return wxMpService.getOcrService();
} }

View File

@ -1,15 +1,20 @@
package com.binarywang.spring.starter.wxjava.mp.config; package com.binarywang.spring.starter.wxjava.mp.config;
import com.binarywang.spring.starter.wxjava.mp.properties.RedisProperties; import com.binarywang.spring.starter.wxjava.mp.extend.RedisTemplateWxMpRedisOps;
import com.binarywang.spring.starter.wxjava.mp.properties.WxMpProperties; import com.binarywang.spring.starter.wxjava.mp.properties.WxMpProperties;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.mp.config.WxMpConfigStorage; import me.chanjar.weixin.mp.config.WxMpConfigStorage;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl; import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import me.chanjar.weixin.mp.config.impl.WxMpRedisConfigImpl; import me.chanjar.weixin.mp.config.impl.WxMpRedisConfigImpl;
import me.chanjar.weixin.mp.config.redis.JedisWxMpRedisOps;
import me.chanjar.weixin.mp.config.redis.WxMpRedisOps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig; import redis.clients.jedis.JedisPoolConfig;
@ -22,55 +27,77 @@ import redis.clients.jedis.JedisPoolConfig;
@RequiredArgsConstructor @RequiredArgsConstructor
public class WxMpStorageAutoConfiguration { public class WxMpStorageAutoConfiguration {
private final WxMpProperties properties;
private final ApplicationContext applicationContext; private final ApplicationContext applicationContext;
private final WxMpProperties wxMpProperties;
@Value("${wx.mp.config-storage.redis.host:")
private String redisHost;
@Value("${wx.mp.configStorage.redis.host:")
private String redisHost2;
@Bean @Bean
@ConditionalOnMissingBean(WxMpConfigStorage.class) @ConditionalOnMissingBean(WxMpConfigStorage.class)
public WxMpConfigStorage wxMpInMemoryConfigStorage() { public WxMpConfigStorage wxMpConfigStorage() {
WxMpProperties.ConfigStorage storage = properties.getConfigStorage(); WxMpProperties.StorageType type = wxMpProperties.getConfigStorage().getType();
WxMpProperties.StorageType type = storage.getType(); WxMpConfigStorage config;
if (type == WxMpProperties.StorageType.redis || type == WxMpProperties.StorageType.jedis) {
if (type == WxMpProperties.StorageType.redis) { config = wxMpInJedisConfigStorage();
return getWxMpInRedisConfigStorage(); } else if (type == WxMpProperties.StorageType.redistemplate) {
config = wxMpInRedisTemplateConfigStorage();
} else {
config = wxMpInMemoryConfigStorage();
} }
return getWxMpInMemoryConfigStorage(); return config;
} }
private WxMpDefaultConfigImpl getWxMpInMemoryConfigStorage() { private WxMpConfigStorage wxMpInMemoryConfigStorage() {
WxMpDefaultConfigImpl config = new WxMpDefaultConfigImpl(); WxMpDefaultConfigImpl config = new WxMpDefaultConfigImpl();
setWxMpInfo(config); setWxMpInfo(config);
return config; return config;
} }
private WxMpRedisConfigImpl getWxMpInRedisConfigStorage() { private WxMpConfigStorage wxMpInJedisConfigStorage() {
RedisProperties.ImplType implType = properties.getConfigStorage().getRedis().getImpl(); JedisPool jedisPool;
boolean reuseBean = properties.getConfigStorage().getRedis().isReuseBean(); if (StringUtils.isNotEmpty(redisHost) || StringUtils.isNotEmpty(redisHost2)) {
if (implType == RedisProperties.ImplType.jedis) { jedisPool = getJedisPool();
JedisPool pool = null; } else {
if (reuseBean) { jedisPool = applicationContext.getBean(JedisPool.class);
pool = getBean(JedisPool.class);
}
if (pool == null) {
pool = getJedisPool();
}
WxMpRedisConfigImpl config = new WxMpRedisConfigImpl(pool);
setWxMpInfo(config);
return config;
} }
throw new UnsupportedOperationException(); WxMpRedisOps redisOps = new JedisWxMpRedisOps(jedisPool);
WxMpRedisConfigImpl wxMpRedisConfig = new WxMpRedisConfigImpl(redisOps, wxMpProperties.getConfigStorage().getKeyPrefix());
setWxMpInfo(wxMpRedisConfig);
return wxMpRedisConfig;
}
private WxMpConfigStorage wxMpInRedisTemplateConfigStorage() {
StringRedisTemplate redisTemplate = applicationContext.getBean(StringRedisTemplate.class);
WxMpRedisOps redisOps = new RedisTemplateWxMpRedisOps(redisTemplate);
WxMpRedisConfigImpl wxMpRedisConfig = new WxMpRedisConfigImpl(redisOps, wxMpProperties.getConfigStorage().getKeyPrefix());
setWxMpInfo(wxMpRedisConfig);
return wxMpRedisConfig;
} }
private void setWxMpInfo(WxMpDefaultConfigImpl config) { private void setWxMpInfo(WxMpDefaultConfigImpl config) {
WxMpProperties properties = wxMpProperties;
WxMpProperties.ConfigStorage configStorageProperties = properties.getConfigStorage();
config.setAppId(properties.getAppId()); config.setAppId(properties.getAppId());
config.setSecret(properties.getSecret()); config.setSecret(properties.getSecret());
config.setToken(properties.getToken()); config.setToken(properties.getToken());
config.setAesKey(properties.getAesKey()); config.setAesKey(properties.getAesKey());
config.setHttpProxyHost(configStorageProperties.getHttpProxyHost());
config.setHttpProxyUsername(configStorageProperties.getHttpProxyUsername());
config.setHttpProxyPassword(configStorageProperties.getHttpProxyPassword());
if (configStorageProperties.getHttpProxyPort() != null) {
config.setHttpProxyPort(configStorageProperties.getHttpProxyPort());
}
} }
private JedisPool getJedisPool() { private JedisPool getJedisPool() {
WxMpProperties.ConfigStorage storage = properties.getConfigStorage(); WxMpProperties.ConfigStorage storage = wxMpProperties.getConfigStorage();
RedisProperties redis = storage.getRedis(); WxMpProperties.RedisProperties redis = storage.getRedis();
JedisPoolConfig config = new JedisPoolConfig(); JedisPoolConfig config = new JedisPoolConfig();
if (redis.getMaxActive() != null) { if (redis.getMaxActive() != null) {
@ -91,11 +118,4 @@ public class WxMpStorageAutoConfiguration {
return new JedisPool(config, redis.getHost(), redis.getPort(), redis.getTimeout(), redis.getPassword(), return new JedisPool(config, redis.getHost(), redis.getPort(), redis.getTimeout(), redis.getPassword(),
redis.getDatabase()); redis.getDatabase());
} }
private <T> T getBean(Class<T> clazz) {
if (this.applicationContext.getBeanNamesForType(clazz, false, false).length > 0) {
return this.applicationContext.getBean(clazz);
}
return null;
}
} }

View File

@ -0,0 +1,40 @@
package com.binarywang.spring.starter.wxjava.mp.extend;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.mp.config.redis.BaseWxMpRedisOps;
import org.springframework.data.redis.core.StringRedisTemplate;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
@RequiredArgsConstructor
public class RedisTemplateWxMpRedisOps extends BaseWxMpRedisOps {
private final StringRedisTemplate redisTemplate;
@Override
public String getValue(String key) {
return redisTemplate.opsForValue().get(key);
}
@Override
public void setValue(String key, String value, int expire, TimeUnit timeUnit) {
redisTemplate.opsForValue().set(key, value, expire, timeUnit);
}
@Override
public Long getExpire(String key) {
return redisTemplate.getExpire(key);
}
@Override
public void expire(String key, int expire, TimeUnit timeUnit) {
redisTemplate.expire(key, expire, timeUnit);
}
@Override
public Lock getLock(String key) {
return new ReentrantLock();
}
}

View File

@ -1,66 +0,0 @@
package com.binarywang.spring.starter.wxjava.mp.properties;
import lombok.Data;
import java.io.Serializable;
/**
* Redis配置.
*
* @author someone
*/
@Data
public class RedisProperties implements Serializable {
private static final long serialVersionUID = -5924815351660074401L;
/**
* 操作Redis的实现
*/
private ImplType impl = ImplType.jedis;
/**
* 操作Redis的实现如果在spring容器里是否直接使用
*/
private boolean reuseBean = true;
/**
* 主机地址.
*/
private String host = "127.0.0.1";
/**
* 端口号.
*/
private int port = 6379;
/**
* 密码.
*/
private String password;
/**
* 超时.
*/
private int timeout = 2000;
/**
* 数据库.
*/
private int database = 0;
private Integer maxActive;
private Integer maxIdle;
private Integer maxWaitMillis;
private Integer minIdle;
public enum ImplType {
/**
* jedis.
*/
jedis,
/**
* redisson.
*/
// redisson
}
}

View File

@ -2,7 +2,6 @@ package com.binarywang.spring.starter.wxjava.mp.properties;
import lombok.Data; import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;
import java.io.Serializable; import java.io.Serializable;
@ -41,19 +40,54 @@ public class WxMpProperties {
private String aesKey; private String aesKey;
/** /**
* 存储策略, memory, redis. * 存储策略
*/ */
private final ConfigStorage configStorage = new ConfigStorage(); private ConfigStorage configStorage = new ConfigStorage();
@Data @Data
public static class ConfigStorage implements Serializable { public static class ConfigStorage implements Serializable {
private static final long serialVersionUID = 4815731027000065434L; private static final long serialVersionUID = 4815731027000065434L;
/**
* 存储类型.
*/
private StorageType type = memory; private StorageType type = memory;
@NestedConfigurationProperty /**
private final RedisProperties redis = new RedisProperties(); * 指定key前缀.
*/
private String keyPrefix = "wx";
/**
* redis连接配置.
*/
private RedisProperties redis = new RedisProperties();
/**
* http客户端类型.
*/
private HttpClientType httpClientType = HttpClientType.httpclient;
/**
* http代理主机.
*/
private String httpProxyHost;
/**
* http代理端口.
*/
private Integer httpProxyPort;
/**
* http代理用户名.
*/
private String httpProxyUsername;
/**
* http代理密码.
*/
private String httpProxyPassword;
} }
public enum StorageType { public enum StorageType {
@ -62,8 +96,67 @@ public class WxMpProperties {
*/ */
memory, memory,
/** /**
* redis. * jedis.
*/ */
redis redis,
/**
* redis(JedisClient).
*/
jedis,
/**
* redis(RedisTemplate).
*/
redistemplate
} }
public enum HttpClientType {
/**
* HttpClient.
*/
httpclient,
/**
* OkHttp.
*/
okhttp,
/**
* JoddHttp.
*/
joddhttp
}
@Data
public static class RedisProperties implements Serializable {
private static final long serialVersionUID = -5924815351660074401L;
/**
* 主机地址.
*/
private String host = "127.0.0.1";
/**
* 端口号.
*/
private int port = 6379;
/**
* 密码.
*/
private String password;
/**
* 超时.
*/
private int timeout = 2000;
/**
* 数据库.
*/
private int database = 0;
private Integer maxActive;
private Integer maxIdle;
private Integer maxWaitMillis;
private Integer minIdle;
}
} }

View File

@ -14,8 +14,6 @@ import okio.BufferedSink;
import okio.Okio; import okio.Okio;
import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;