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

weixin-java-open优化redis存储, 集成spring-boot增加新特性 #1522

Merged
merged 3 commits into from
Apr 19, 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
49 changes: 29 additions & 20 deletions spring-boot-starters/wx-java-miniapp-spring-boot-starter/README.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,35 @@
# 使用说明
1. 在自己的Spring Boot项目里,引入maven依赖
```xml
# wx-java-miniapp-spring-boot-starter
## 快速开始
1. 引入依赖
```xml
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>wx-java-miniapp-spring-boot-starter</artifactId>
<version>${version}</version>
</dependency>
```
2. 添加配置(application.yml)
```yml
wx:
miniapp:
appid: 111
secret: 111
token: 111
aesKey: 111
msgDataFormat: JSON
```






```
2. 添加配置(application.properties)
```properties
# 公众号配置(必填)
wx.miniapp.appid = appId
wx.miniapp.secret = @secret
wx.miniapp.token = @token
wx.miniapp.aesKey = @aesKey
wx.miniapp.msgDataFormat = @msgDataFormat # 消息格式,XML或者JSON.
# 存储配置redis(可选)
# 注意: 指定redis.host值后不会使用容器注入的redis连接(JedisPool)
wx.miniapp.config-storage.type = jedis # 配置类型: memory(默认), jedis, redistemplate
wx.miniapp.config-storage.key-prefix = wa # 相关redis前缀配置: wa(默认)
wx.miniapp.config-storage.redis.host = 127.0.0.1
wx.miniapp.config-storage.redis.port = 6379
# http客户端配置
wx.miniapp.config-storage.http-client-type=httpclient # http客户端类型: httpclient(默认)
wx.miniapp.config-storage.http-proxy-host=
wx.miniapp.config-storage.http-proxy-port=
wx.miniapp.config-storage.http-proxy-username=
wx.miniapp.config-storage.http-proxy-password=
```
3. 自动注入的类型
- `WxMaService`
- `WxMaConfig`

8 changes: 4 additions & 4 deletions spring-boot-starters/wx-java-mp-spring-boot-starter/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# wx-java-mp-starter
# wx-java-mp-spring-boot-starter
## 快速开始
1. 引入依赖
```xml
Expand Down Expand Up @@ -27,9 +27,9 @@
wx.mp.config-storage.http-proxy-username=
wx.mp.config-storage.http-proxy-password=
```
3. 支持自动注入的类型

`WxMpService`以及~~相关的服务类, 比如: `wxMpService.getXxxService`。~~
3. 自动注入的类型
- `WxMpService`以及~~相关的服务类, 比如: `wxMpService.getXxxService`。~~
- `WxMpConfigStorage`



Expand Down
35 changes: 18 additions & 17 deletions spring-boot-starters/wx-java-open-spring-boot-starter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,27 @@
</dependency>
```
2. 添加配置(application.properties)
```
# 开放平台配置(必填)
wx.open.appId = @appId
wx.open.secret = @secret
wx.open.token = @token
wx.open.aesKey = @aesKey
# 存储配置redis(可选), 优先使用(wx.open.config-storage.redis)配置的redis, 支持自定注入的JedisPool
wx.open.config-storage.type = redis # 可选值, memory(默认), redis
wx.open.config-storage.redis.host = 127.0.0.1
wx.open.config-storage.redis.port = 6379
```properties
# 公众号配置(必填)
wx.open.appId = appId
wx.open.secret = @secret
wx.open.token = @token
wx.open.aesKey = @aesKey
# 存储配置redis(可选)
# 优先注入容器的(JedisPool, RedissonClient), 当配置了wx.open.config-storage.redis.host, 不会使用容器注入redis连接配置
wx.open.config-storage.type = redis # 配置类型: memory(默认), redis(jedis), jedis, redisson, redistemplate
wx.open.config-storage.key-prefix = wx # 相关redis前缀配置: wx(默认)
wx.open.config-storage.redis.host = 127.0.0.1
wx.open.config-storage.redis.port = 6379
# http客户端配置
wx.open.config-storage.http-client-type=httpclient # http客户端类型: httpclient(默认)
wx.open.config-storage.http-proxy-host=
wx.open.config-storage.http-proxy-port=
wx.open.config-storage.http-proxy-username=
wx.open.config-storage.http-proxy-password=
```
3. 支持自动注入的类型: `WxOpenService, WxOpenMessageRouter, WxOpenComponentService`

4. 覆盖自动配置: 自定义注入的bean会覆盖自动注入的
- WxOpenConfigStorage
- WxOpenService







10 changes: 8 additions & 2 deletions spring-boot-starters/wx-java-open-spring-boot-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,18 @@
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<scope>compile</scope>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<scope>compile</scope>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>${spring.boot.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,23 @@
import com.binarywang.spring.starter.wxjava.open.properties.RedisProperties;
import com.binarywang.spring.starter.wxjava.open.properties.WxOpenProperties;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.common.redis.JedisWxRedisOps;
import me.chanjar.weixin.common.redis.RedisTemplateWxRedisOps;
import me.chanjar.weixin.common.redis.RedissonWxRedisOps;
import me.chanjar.weixin.common.redis.WxRedisOps;
import me.chanjar.weixin.open.api.WxOpenConfigStorage;
import me.chanjar.weixin.open.api.impl.WxOpenInMemoryConfigStorage;
import me.chanjar.weixin.open.api.impl.WxOpenInRedisConfigStorage;
import me.chanjar.weixin.open.api.impl.WxOpenInRedissonConfigStorage;
import org.apache.commons.lang3.StringUtils;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.TransportMode;
import org.springframework.beans.factory.annotation.Autowired;
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.JedisPoolConfig;

Expand All @@ -29,59 +32,71 @@
@RequiredArgsConstructor
public class WxOpenStorageAutoConfiguration {
private final WxOpenProperties properties;

@Autowired(required = false)
private JedisPool jedisPool;

@Autowired(required = false)
private RedissonClient redissonClient;

@Value("${wx.open.config-storage.redis.host:}")
private String redisHost;
private final ApplicationContext applicationContext;

@Bean
@ConditionalOnMissingBean(WxOpenConfigStorage.class)
public WxOpenConfigStorage wxOpenConfigStorage() {
WxOpenProperties.ConfigStorage storage = properties.getConfigStorage();
WxOpenProperties.StorageType type = storage.getType();

if (type == WxOpenProperties.StorageType.redis) {
return getWxOpenInRedisConfigStorage();
WxOpenInMemoryConfigStorage config;
if (type == WxOpenProperties.StorageType.redis || type == WxOpenProperties.StorageType.jedis) {
config = getWxOpenInRedisConfigStorage();
} else if (type == WxOpenProperties.StorageType.redisson) {
config = getWxOpenInRedissonConfigStorage();
} else if (type == WxOpenProperties.StorageType.redistemplate) {
config = getWxOpenInRedisTemplateConfigStorage();
} else {
config = getWxOpenInMemoryConfigStorage();
}

if (type == WxOpenProperties.StorageType.jedis) {
return getWxOpenInRedisConfigStorage();
}

if (type == WxOpenProperties.StorageType.redisson) {
return 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 getWxOpenInMemoryConfigStorage();
return config;
}

private WxOpenInMemoryConfigStorage getWxOpenInMemoryConfigStorage() {
WxOpenInMemoryConfigStorage config = new WxOpenInMemoryConfigStorage();
config.setWxOpenInfo(properties.getAppId(), properties.getSecret(), properties.getToken(), properties.getAesKey());
return config;
}

private WxOpenInRedisConfigStorage getWxOpenInRedisConfigStorage() {
JedisPool poolToUse = jedisPool;
if (jedisPool == null || StringUtils.isNotEmpty(redisHost)) {
poolToUse = getJedisPool();
RedisProperties redisProperties = properties.getConfigStorage().getRedis();
JedisPool jedisPool;
if (redisProperties != null && StringUtils.isNotEmpty(redisProperties.getHost())) {
jedisPool = getJedisPool();
} else {
jedisPool = applicationContext.getBean(JedisPool.class);
}
WxOpenInRedisConfigStorage config = new WxOpenInRedisConfigStorage(poolToUse, properties.getConfigStorage().getKeyPrefix());
config.setWxOpenInfo(properties.getAppId(), properties.getSecret(), properties.getToken(), properties.getAesKey());
WxRedisOps redisOps = new JedisWxRedisOps(jedisPool);
WxOpenInRedisConfigStorage config = new WxOpenInRedisConfigStorage(redisOps, properties.getConfigStorage().getKeyPrefix());
return config;
}

private WxOpenInRedissonConfigStorage getWxOpenInRedissonConfigStorage() {
RedissonClient redissonClientToUse = this.redissonClient;
if (redissonClient == null) {
redissonClientToUse = getRedissonClient();
private WxOpenInRedisConfigStorage getWxOpenInRedissonConfigStorage() {
RedisProperties redisProperties = properties.getConfigStorage().getRedis();
RedissonClient redissonClient;
if (redisProperties != null && StringUtils.isNotEmpty(redisProperties.getHost())) {
redissonClient = getRedissonClient();
} else {
redissonClient = applicationContext.getBean(RedissonClient.class);
}
WxOpenInRedissonConfigStorage config = new WxOpenInRedissonConfigStorage(redissonClientToUse, properties.getConfigStorage().getKeyPrefix());
config.setWxOpenInfo(properties.getAppId(), properties.getSecret(), properties.getToken(), properties.getAesKey());
WxRedisOps redisOps = new RedissonWxRedisOps(redissonClient);
WxOpenInRedisConfigStorage config = new WxOpenInRedisConfigStorage(redisOps, properties.getConfigStorage().getKeyPrefix());
return config;
}

private WxOpenInRedisConfigStorage getWxOpenInRedisTemplateConfigStorage() {
StringRedisTemplate redisTemplate = applicationContext.getBean(StringRedisTemplate.class);
WxRedisOps redisOps = new RedisTemplateWxRedisOps(redisTemplate);
WxOpenInRedisConfigStorage config = new WxOpenInRedisConfigStorage(redisOps, properties.getConfigStorage().getKeyPrefix());
return config;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class RedisProperties implements Serializable {
/**
* 主机地址.
*/
private String host = "127.0.0.1";
private String host;

/**
* 端口号.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class WxOpenProperties {
private String aesKey;

/**
* 存储策略, memory, redis.
* 存储策略.
*/
private ConfigStorage configStorage = new ConfigStorage();

Expand All @@ -49,11 +49,45 @@ public class WxOpenProperties {
public static class ConfigStorage implements Serializable {
private static final long serialVersionUID = 4815731027000065434L;

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

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

/**
* redis连接配置.
*/
private RedisProperties redis = new RedisProperties();

private String keyPrefix;
/**
* http客户端类型.
*/
private HttpClientType httpClientType = HttpClientType.httpclient;

/**
* http代理主机.
*/
private String httpProxyHost;

/**
* http代理端口.
*/
private Integer httpProxyPort;

/**
* http代理用户名.
*/
private String httpProxyUsername;

/**
* http代理密码.
*/
private String httpProxyPassword;

}

Expand All @@ -73,6 +107,17 @@ public enum StorageType {
/**
* redisson.
*/
redisson
redisson,
/**
* redistemplate
*/
redistemplate
}

public enum HttpClientType {
/**
* HttpClient.
*/
httpclient
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.common.util.locks.JedisDistributedLock;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.util.Pool;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;

@RequiredArgsConstructor
public class JedisWxRedisOps implements WxRedisOps {

private final JedisPool jedisPool;
private final Pool<Jedis> jedisPool;

@Override
public String getValue(String key) {
Expand Down
Loading