mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-04-05 17:38:05 +08:00
🎨 优化公众号Spring Boot Starter的redisTemplate注入等代码
This commit is contained in:
parent
8428270e67
commit
8929fc41ab
@ -30,6 +30,16 @@
|
|||||||
<version>${spring.boot.version}</version>
|
<version>${spring.boot.version}</version>
|
||||||
<scope>provided</scope>
|
<scope>provided</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.jodd</groupId>
|
||||||
|
<artifactId>jodd-http</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.squareup.okhttp3</groupId>
|
||||||
|
<artifactId>okhttp</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
@ -2,7 +2,7 @@ package com.binarywang.spring.starter.wxjava.mp.config;
|
|||||||
|
|
||||||
import com.binarywang.spring.starter.wxjava.mp.enums.HttpClientType;
|
import com.binarywang.spring.starter.wxjava.mp.enums.HttpClientType;
|
||||||
import com.binarywang.spring.starter.wxjava.mp.properties.WxMpProperties;
|
import com.binarywang.spring.starter.wxjava.mp.properties.WxMpProperties;
|
||||||
import me.chanjar.weixin.mp.api.*;
|
import me.chanjar.weixin.mp.api.WxMpService;
|
||||||
import me.chanjar.weixin.mp.api.impl.WxMpServiceHttpClientImpl;
|
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.WxMpServiceJoddHttpImpl;
|
||||||
|
@ -1,21 +1,11 @@
|
|||||||
package com.binarywang.spring.starter.wxjava.mp.config;
|
package com.binarywang.spring.starter.wxjava.mp.config;
|
||||||
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
import org.apache.commons.lang3.StringUtils;
|
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
|
||||||
import org.springframework.context.ApplicationContext;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
|
||||||
|
|
||||||
import com.binarywang.spring.starter.wxjava.mp.enums.StorageType;
|
import com.binarywang.spring.starter.wxjava.mp.enums.StorageType;
|
||||||
import com.binarywang.spring.starter.wxjava.mp.properties.RedisProperties;
|
import com.binarywang.spring.starter.wxjava.mp.properties.RedisProperties;
|
||||||
import com.binarywang.spring.starter.wxjava.mp.properties.WxMpProperties;
|
import com.binarywang.spring.starter.wxjava.mp.properties.WxMpProperties;
|
||||||
import com.google.common.collect.Sets;
|
import com.google.common.collect.Sets;
|
||||||
|
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import me.chanjar.weixin.common.redis.JedisWxRedisOps;
|
import me.chanjar.weixin.common.redis.JedisWxRedisOps;
|
||||||
import me.chanjar.weixin.common.redis.RedisTemplateWxRedisOps;
|
import me.chanjar.weixin.common.redis.RedisTemplateWxRedisOps;
|
||||||
import me.chanjar.weixin.common.redis.WxRedisOps;
|
import me.chanjar.weixin.common.redis.WxRedisOps;
|
||||||
@ -23,126 +13,154 @@ import me.chanjar.weixin.mp.bean.WxMpHostConfig;
|
|||||||
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 org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
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.JedisPoolAbstract;
|
import redis.clients.jedis.JedisPoolAbstract;
|
||||||
import redis.clients.jedis.JedisPoolConfig;
|
import redis.clients.jedis.JedisPoolConfig;
|
||||||
import redis.clients.jedis.JedisSentinelPool;
|
import redis.clients.jedis.JedisSentinelPool;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 微信公众号存储策略自动配置.
|
* 微信公众号存储策略自动配置.
|
||||||
*
|
*
|
||||||
* @author someone
|
* @author someone
|
||||||
*/
|
*/
|
||||||
|
@Slf4j
|
||||||
@Configuration
|
@Configuration
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class WxMpStorageAutoConfiguration {
|
public class WxMpStorageAutoConfiguration {
|
||||||
private final ApplicationContext applicationContext;
|
private final ApplicationContext applicationContext;
|
||||||
|
|
||||||
private final WxMpProperties wxMpProperties;
|
private final WxMpProperties wxMpProperties;
|
||||||
|
|
||||||
@Value("${wx.mp.config-storage.redis.host:")
|
@Value("${wx.mp.config-storage.redis.host:")
|
||||||
private String redisHost;
|
private String redisHost;
|
||||||
|
|
||||||
@Value("${wx.mp.configStorage.redis.host:")
|
@Value("${wx.mp.configStorage.redis.host:")
|
||||||
private String redisHost2;
|
private String redisHost2;
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@ConditionalOnMissingBean(WxMpConfigStorage.class)
|
@ConditionalOnMissingBean(WxMpConfigStorage.class)
|
||||||
public WxMpConfigStorage wxMpConfigStorage() {
|
public WxMpConfigStorage wxMpConfigStorage() {
|
||||||
StorageType type = wxMpProperties.getConfigStorage().getType();
|
StorageType type = wxMpProperties.getConfigStorage().getType();
|
||||||
WxMpConfigStorage config;
|
WxMpConfigStorage config;
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case Jedis:
|
case Jedis:
|
||||||
config = jedisConfigStorage();
|
config = jedisConfigStorage();
|
||||||
break;
|
break;
|
||||||
case RedisTemplate:
|
case RedisTemplate:
|
||||||
config = redisTemplateConfigStorage();
|
config = redisTemplateConfigStorage();
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
config = defaultConfigStorage();
|
config = defaultConfigStorage();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
// wx host config
|
// wx host config
|
||||||
if (null != wxMpProperties.getHosts() && StringUtils.isNotEmpty(wxMpProperties.getHosts().getApiHost())) {
|
if (null != wxMpProperties.getHosts() && StringUtils.isNotEmpty(wxMpProperties.getHosts().getApiHost())) {
|
||||||
WxMpHostConfig hostConfig = new WxMpHostConfig();
|
WxMpHostConfig hostConfig = new WxMpHostConfig();
|
||||||
hostConfig.setApiHost(wxMpProperties.getHosts().getApiHost());
|
hostConfig.setApiHost(wxMpProperties.getHosts().getApiHost());
|
||||||
hostConfig.setMpHost(wxMpProperties.getHosts().getMpHost());
|
hostConfig.setMpHost(wxMpProperties.getHosts().getMpHost());
|
||||||
hostConfig.setOpenHost(wxMpProperties.getHosts().getOpenHost());
|
hostConfig.setOpenHost(wxMpProperties.getHosts().getOpenHost());
|
||||||
config.setHostConfig(hostConfig);
|
config.setHostConfig(hostConfig);
|
||||||
}
|
}
|
||||||
return config;
|
return config;
|
||||||
}
|
}
|
||||||
|
|
||||||
private WxMpConfigStorage defaultConfigStorage() {
|
private WxMpConfigStorage defaultConfigStorage() {
|
||||||
WxMpDefaultConfigImpl config = new WxMpDefaultConfigImpl();
|
WxMpDefaultConfigImpl config = new WxMpDefaultConfigImpl();
|
||||||
setWxMpInfo(config);
|
setWxMpInfo(config);
|
||||||
return config;
|
return config;
|
||||||
}
|
}
|
||||||
|
|
||||||
private WxMpConfigStorage jedisConfigStorage() {
|
private WxMpConfigStorage jedisConfigStorage() {
|
||||||
JedisPoolAbstract jedisPool;
|
JedisPoolAbstract jedisPool;
|
||||||
if (StringUtils.isNotEmpty(redisHost) || StringUtils.isNotEmpty(redisHost2)) {
|
if (StringUtils.isNotEmpty(redisHost) || StringUtils.isNotEmpty(redisHost2)) {
|
||||||
jedisPool = getJedisPool();
|
jedisPool = getJedisPool();
|
||||||
} else {
|
} else {
|
||||||
jedisPool = applicationContext.getBean(JedisPool.class);
|
jedisPool = applicationContext.getBean(JedisPool.class);
|
||||||
}
|
}
|
||||||
WxRedisOps redisOps = new JedisWxRedisOps(jedisPool);
|
WxRedisOps redisOps = new JedisWxRedisOps(jedisPool);
|
||||||
WxMpRedisConfigImpl wxMpRedisConfig = new WxMpRedisConfigImpl(redisOps,
|
WxMpRedisConfigImpl wxMpRedisConfig = new WxMpRedisConfigImpl(redisOps,
|
||||||
wxMpProperties.getConfigStorage().getKeyPrefix());
|
wxMpProperties.getConfigStorage().getKeyPrefix());
|
||||||
setWxMpInfo(wxMpRedisConfig);
|
setWxMpInfo(wxMpRedisConfig);
|
||||||
return wxMpRedisConfig;
|
return wxMpRedisConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
private WxMpConfigStorage redisTemplateConfigStorage() {
|
private WxMpConfigStorage redisTemplateConfigStorage() {
|
||||||
StringRedisTemplate redisTemplate = applicationContext.getBean(StringRedisTemplate.class);
|
StringRedisTemplate redisTemplate = null;
|
||||||
WxRedisOps redisOps = new RedisTemplateWxRedisOps(redisTemplate);
|
try {
|
||||||
WxMpRedisConfigImpl wxMpRedisConfig = new WxMpRedisConfigImpl(redisOps,
|
redisTemplate = applicationContext.getBean(StringRedisTemplate.class);
|
||||||
wxMpProperties.getConfigStorage().getKeyPrefix());
|
} catch (Exception e) {
|
||||||
setWxMpInfo(wxMpRedisConfig);
|
log.error(e.getMessage(), e);
|
||||||
return wxMpRedisConfig;
|
}
|
||||||
}
|
try {
|
||||||
|
if (null == redisTemplate) {
|
||||||
|
redisTemplate = (StringRedisTemplate) applicationContext.getBean("stringRedisTemplate");
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error(e.getMessage(), e);
|
||||||
|
}
|
||||||
|
|
||||||
private void setWxMpInfo(WxMpDefaultConfigImpl config) {
|
if (null == redisTemplate) {
|
||||||
WxMpProperties properties = wxMpProperties;
|
redisTemplate = (StringRedisTemplate) applicationContext.getBean("redisTemplate");
|
||||||
WxMpProperties.ConfigStorage configStorageProperties = properties.getConfigStorage();
|
}
|
||||||
config.setAppId(properties.getAppId());
|
|
||||||
config.setSecret(properties.getSecret());
|
|
||||||
config.setToken(properties.getToken());
|
|
||||||
config.setAesKey(properties.getAesKey());
|
|
||||||
|
|
||||||
config.setHttpProxyHost(configStorageProperties.getHttpProxyHost());
|
WxRedisOps redisOps = new RedisTemplateWxRedisOps(redisTemplate);
|
||||||
config.setHttpProxyUsername(configStorageProperties.getHttpProxyUsername());
|
WxMpRedisConfigImpl wxMpRedisConfig = new WxMpRedisConfigImpl(redisOps,
|
||||||
config.setHttpProxyPassword(configStorageProperties.getHttpProxyPassword());
|
wxMpProperties.getConfigStorage().getKeyPrefix());
|
||||||
if (configStorageProperties.getHttpProxyPort() != null) {
|
|
||||||
config.setHttpProxyPort(configStorageProperties.getHttpProxyPort());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private JedisPoolAbstract getJedisPool() {
|
setWxMpInfo(wxMpRedisConfig);
|
||||||
WxMpProperties.ConfigStorage storage = wxMpProperties.getConfigStorage();
|
return wxMpRedisConfig;
|
||||||
RedisProperties redis = storage.getRedis();
|
}
|
||||||
|
|
||||||
JedisPoolConfig config = new JedisPoolConfig();
|
private void setWxMpInfo(WxMpDefaultConfigImpl config) {
|
||||||
if (redis.getMaxActive() != null) {
|
WxMpProperties properties = wxMpProperties;
|
||||||
config.setMaxTotal(redis.getMaxActive());
|
WxMpProperties.ConfigStorage configStorageProperties = properties.getConfigStorage();
|
||||||
}
|
config.setAppId(properties.getAppId());
|
||||||
if (redis.getMaxIdle() != null) {
|
config.setSecret(properties.getSecret());
|
||||||
config.setMaxIdle(redis.getMaxIdle());
|
config.setToken(properties.getToken());
|
||||||
}
|
config.setAesKey(properties.getAesKey());
|
||||||
if (redis.getMaxWaitMillis() != null) {
|
|
||||||
config.setMaxWaitMillis(redis.getMaxWaitMillis());
|
|
||||||
}
|
|
||||||
if (redis.getMinIdle() != null) {
|
|
||||||
config.setMinIdle(redis.getMinIdle());
|
|
||||||
}
|
|
||||||
config.setTestOnBorrow(true);
|
|
||||||
config.setTestWhileIdle(true);
|
|
||||||
if (StringUtils.isNotEmpty(redis.getSentinelIps())) {
|
|
||||||
Set<String> sentinels = Sets.newHashSet(redis.getSentinelIps().split(","));
|
|
||||||
return new JedisSentinelPool(redis.getSentinelName(), sentinels);
|
|
||||||
}
|
|
||||||
|
|
||||||
return new JedisPool(config, redis.getHost(), redis.getPort(), redis.getTimeout(), redis.getPassword(),
|
config.setHttpProxyHost(configStorageProperties.getHttpProxyHost());
|
||||||
redis.getDatabase());
|
config.setHttpProxyUsername(configStorageProperties.getHttpProxyUsername());
|
||||||
}
|
config.setHttpProxyPassword(configStorageProperties.getHttpProxyPassword());
|
||||||
|
if (configStorageProperties.getHttpProxyPort() != null) {
|
||||||
|
config.setHttpProxyPort(configStorageProperties.getHttpProxyPort());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private JedisPoolAbstract getJedisPool() {
|
||||||
|
WxMpProperties.ConfigStorage storage = wxMpProperties.getConfigStorage();
|
||||||
|
RedisProperties redis = storage.getRedis();
|
||||||
|
|
||||||
|
JedisPoolConfig config = new JedisPoolConfig();
|
||||||
|
if (redis.getMaxActive() != null) {
|
||||||
|
config.setMaxTotal(redis.getMaxActive());
|
||||||
|
}
|
||||||
|
if (redis.getMaxIdle() != null) {
|
||||||
|
config.setMaxIdle(redis.getMaxIdle());
|
||||||
|
}
|
||||||
|
if (redis.getMaxWaitMillis() != null) {
|
||||||
|
config.setMaxWaitMillis(redis.getMaxWaitMillis());
|
||||||
|
}
|
||||||
|
if (redis.getMinIdle() != null) {
|
||||||
|
config.setMinIdle(redis.getMinIdle());
|
||||||
|
}
|
||||||
|
config.setTestOnBorrow(true);
|
||||||
|
config.setTestWhileIdle(true);
|
||||||
|
if (StringUtils.isNotEmpty(redis.getSentinelIps())) {
|
||||||
|
Set<String> sentinels = Sets.newHashSet(redis.getSentinelIps().split(","));
|
||||||
|
return new JedisSentinelPool(redis.getSentinelName(), sentinels);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new JedisPool(config, redis.getHost(), redis.getPort(), redis.getTimeout(), redis.getPassword(),
|
||||||
|
redis.getDatabase());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,18 +1,18 @@
|
|||||||
package com.binarywang.spring.starter.wxjava.mp.properties;
|
package com.binarywang.spring.starter.wxjava.mp.properties;
|
||||||
|
|
||||||
import java.io.Serializable;
|
|
||||||
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
public class HostConfig implements Serializable {
|
public class HostConfig implements Serializable {
|
||||||
|
|
||||||
private static final long serialVersionUID = -4172767630740346001L;
|
|
||||||
|
|
||||||
private String apiHost;
|
private static final long serialVersionUID = -4172767630740346001L;
|
||||||
|
|
||||||
private String openHost;
|
private String apiHost;
|
||||||
|
|
||||||
private String mpHost;
|
private String openHost;
|
||||||
|
|
||||||
|
private String mpHost;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -38,12 +38,12 @@ public class RedisProperties implements Serializable {
|
|||||||
* 数据库.
|
* 数据库.
|
||||||
*/
|
*/
|
||||||
private int database = 0;
|
private int database = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* sentinel ips
|
* sentinel ips
|
||||||
*/
|
*/
|
||||||
private String sentinelIps;
|
private String sentinelIps;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* sentinel name
|
* sentinel name
|
||||||
*/
|
*/
|
||||||
|
@ -40,7 +40,7 @@ public class WxMpProperties {
|
|||||||
* 设置微信公众号的EncodingAESKey.
|
* 设置微信公众号的EncodingAESKey.
|
||||||
*/
|
*/
|
||||||
private String aesKey;
|
private String aesKey;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 自定义host配置
|
* 自定义host配置
|
||||||
*/
|
*/
|
||||||
|
@ -41,7 +41,7 @@ public class WxCpRedissonConfigImpl extends WxCpDefaultConfigImpl {
|
|||||||
this(redissonClient, null);
|
this(redissonClient, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
private WxCpRedissonConfigImpl(@NonNull WxRedisOps redisOps, String keyPrefix) {
|
public WxCpRedissonConfigImpl(@NonNull WxRedisOps redisOps, String keyPrefix) {
|
||||||
this.redisOps = redisOps;
|
this.redisOps = redisOps;
|
||||||
this.keyPrefix = keyPrefix;
|
this.keyPrefix = keyPrefix;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user