Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

properties不需要搞那么多,使用内部类即可;auto类优化一下,因为sb会去代理method导致必须引jedis的包才能启动 #1627

Merged
merged 1 commit into from
Jun 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import cn.binarywang.wx.miniapp.config.impl.WxMaRedisBetterConfigImpl;
import com.binarywang.spring.starter.wxjava.miniapp.enums.HttpClientType;
import com.binarywang.spring.starter.wxjava.miniapp.properties.ConfigStorage;
import com.binarywang.spring.starter.wxjava.miniapp.properties.RedisProperties;
import com.binarywang.spring.starter.wxjava.miniapp.properties.WxMaProperties;
import lombok.AllArgsConstructor;
import me.chanjar.weixin.common.redis.JedisWxRedisOps;
Expand Down Expand Up @@ -89,7 +87,7 @@ public WxMaConfig wxMaConfig() {
config.setAesKey(StringUtils.trimToNull(this.wxMaProperties.getAesKey()));
config.setMsgDataFormat(StringUtils.trimToNull(this.wxMaProperties.getMsgDataFormat()));

ConfigStorage configStorageProperties = wxMaProperties.getConfigStorage();
WxMaProperties.ConfigStorage configStorageProperties = wxMaProperties.getConfigStorage();
config.setHttpProxyHost(configStorageProperties.getHttpProxyHost());
config.setHttpProxyUsername(configStorageProperties.getHttpProxyUsername());
config.setHttpProxyPassword(configStorageProperties.getHttpProxyPassword());
Expand All @@ -104,10 +102,27 @@ private WxMaDefaultConfigImpl wxMaDefaultConfigStorage() {
}

private WxMaDefaultConfigImpl wxMaJedisConfigStorage() {
RedisProperties redisProperties = wxMaProperties.getConfigStorage().getRedis();
WxMaProperties.RedisProperties redisProperties = wxMaProperties.getConfigStorage().getRedis();
JedisPool jedisPool;
if (redisProperties != null && StringUtils.isNotEmpty(redisProperties.getHost())) {
jedisPool = getJedisPool();
if (StringUtils.isNotEmpty(redisProperties.getHost())) {
JedisPoolConfig config = new JedisPoolConfig();
if (redisProperties.getMaxActive() != null) {
config.setMaxTotal(redisProperties.getMaxActive());
}
if (redisProperties.getMaxIdle() != null) {
config.setMaxIdle(redisProperties.getMaxIdle());
}
if (redisProperties.getMaxWaitMillis() != null) {
config.setMaxWaitMillis(redisProperties.getMaxWaitMillis());
}
if (redisProperties.getMinIdle() != null) {
config.setMinIdle(redisProperties.getMinIdle());
}
config.setTestOnBorrow(true);
config.setTestWhileIdle(true);

jedisPool = new JedisPool(config, redisProperties.getHost(), redisProperties.getPort(),
redisProperties.getTimeout(), redisProperties.getPassword(), redisProperties.getDatabase());
} else {
jedisPool = applicationContext.getBean(JedisPool.class);
}
Expand All @@ -120,28 +135,4 @@ private WxMaDefaultConfigImpl wxMaRedisTemplateConfigStorage() {
WxRedisOps redisOps = new RedisTemplateWxRedisOps(redisTemplate);
return new WxMaRedisBetterConfigImpl(redisOps, wxMaProperties.getConfigStorage().getKeyPrefix());
}

private JedisPool getJedisPool() {
ConfigStorage storage = wxMaProperties.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);

return new JedisPool(config, redis.getHost(), redis.getPort(), redis.getTimeout(), redis.getPassword(),
redis.getDatabase());
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.binarywang.spring.starter.wxjava.miniapp.properties;

import com.binarywang.spring.starter.wxjava.miniapp.enums.HttpClientType;
import com.binarywang.spring.starter.wxjava.miniapp.enums.StorageType;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;

Expand Down Expand Up @@ -40,6 +42,83 @@ public class WxMaProperties {
/**
* 存储策略
*/
private ConfigStorage configStorage = new ConfigStorage();
private final ConfigStorage configStorage = new ConfigStorage();

@Data
public static class ConfigStorage {

/**
* 存储类型.
*/
private StorageType type = StorageType.Memory;

/**
* 指定key前缀.
*/
private String keyPrefix = "wa";

/**
* redis连接配置.
*/
private final 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;
}

@Data
public static class RedisProperties {

/**
* 主机地址.不填则从spring容器内获取JedisPool
*/
private String host;

/**
* 端口号.
*/
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;
}
}