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

微信小程序集成spring-boot增加一些新特性 #1521

Merged
merged 3 commits into from
Apr 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
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,14 @@
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>3.12.0</version>
<optional>true</optional>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.8.23.RELEASE</version>
<optional>true</optional>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down
11 changes: 11 additions & 0 deletions spring-boot-starters/wx-java-miniapp-spring-boot-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@
<artifactId>weixin-java-miniapp</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<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>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,23 @@
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.WxMaConfig;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import cn.binarywang.wx.miniapp.config.impl.WxMaRedisBetterConfigImpl;
import com.binarywang.spring.starter.wxjava.miniapp.properties.WxMaProperties;
import lombok.AllArgsConstructor;
import me.chanjar.weixin.common.redis.JedisWxRedisOps;
import me.chanjar.weixin.common.redis.RedisTemplateWxRedisOps;
import me.chanjar.weixin.common.redis.WxRedisOps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
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 @@ -26,7 +34,9 @@
@EnableConfigurationProperties(WxMaProperties.class)
@ConditionalOnProperty(prefix = "wx.miniapp", value = "enabled", matchIfMissing = true)
public class WxMaAutoConfiguration {
private WxMaProperties properties;

private final WxMaProperties wxMaProperties;
private final ApplicationContext applicationContext;

/**
* 小程序service.
Expand All @@ -35,16 +45,87 @@ public class WxMaAutoConfiguration {
*/
@Bean
@ConditionalOnMissingBean(WxMaService.class)
public WxMaService service() {
WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
config.setAppid(StringUtils.trimToNull(this.properties.getAppid()));
config.setSecret(StringUtils.trimToNull(this.properties.getSecret()));
config.setToken(StringUtils.trimToNull(this.properties.getToken()));
config.setAesKey(StringUtils.trimToNull(this.properties.getAesKey()));
config.setMsgDataFormat(StringUtils.trimToNull(this.properties.getMsgDataFormat()));

public WxMaService service(WxMaConfig wxMaConfig) {
final WxMaServiceImpl service = new WxMaServiceImpl();
service.setWxMaConfig(config);
service.setWxMaConfig(wxMaConfig);
return service;
}

@Bean
@ConditionalOnMissingBean(WxMaConfig.class)
public WxMaConfig wxMaConfig() {
WxMaProperties.StorageType type = wxMaProperties.getConfigStorage().getType();
WxMaDefaultConfigImpl config;
if (type == WxMaProperties.StorageType.jedis) {
config = wxMaInJedisConfigStorage();
} else if (type == WxMaProperties.StorageType.redistemplate) {
config = wxMaInRedisTemplateConfigStorage();
} else {
config = wxMaInMemoryConfigStorage();
}

config.setAppid(StringUtils.trimToNull(this.wxMaProperties.getAppid()));
config.setSecret(StringUtils.trimToNull(this.wxMaProperties.getSecret()));
config.setToken(StringUtils.trimToNull(this.wxMaProperties.getToken()));
config.setAesKey(StringUtils.trimToNull(this.wxMaProperties.getAesKey()));
config.setMsgDataFormat(StringUtils.trimToNull(this.wxMaProperties.getMsgDataFormat()));

WxMaProperties.ConfigStorage configStorageProperties = wxMaProperties.getConfigStorage();
config.setHttpProxyHost(configStorageProperties.getHttpProxyHost());
config.setHttpProxyUsername(configStorageProperties.getHttpProxyUsername());
config.setHttpProxyPassword(configStorageProperties.getHttpProxyPassword());
if (configStorageProperties.getHttpProxyPort() != null) {
config.setHttpProxyPort(configStorageProperties.getHttpProxyPort());
}
return config;
}

private WxMaDefaultConfigImpl wxMaInMemoryConfigStorage() {
WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
return config;
}

private WxMaDefaultConfigImpl wxMaInJedisConfigStorage() {
WxMaProperties.RedisProperties redisProperties = wxMaProperties.getConfigStorage().getRedis();
JedisPool jedisPool;
if (redisProperties != null && StringUtils.isNotEmpty(redisProperties.getHost())) {
jedisPool = getJedisPool();
} else {
jedisPool = applicationContext.getBean(JedisPool.class);
}
WxRedisOps redisOps = new JedisWxRedisOps(jedisPool);
WxMaRedisBetterConfigImpl wxMaRedisConfig = new WxMaRedisBetterConfigImpl(redisOps, wxMaProperties.getConfigStorage().getKeyPrefix());
return wxMaRedisConfig;
}

private WxMaDefaultConfigImpl wxMaInRedisTemplateConfigStorage() {
StringRedisTemplate redisTemplate = applicationContext.getBean(StringRedisTemplate.class);
WxRedisOps redisOps = new RedisTemplateWxRedisOps(redisTemplate);
WxMaRedisBetterConfigImpl wxMaRedisConfig = new WxMaRedisBetterConfigImpl(redisOps, wxMaProperties.getConfigStorage().getKeyPrefix());
return wxMaRedisConfig;
}

private JedisPool getJedisPool() {
WxMaProperties.ConfigStorage storage = wxMaProperties.getConfigStorage();
WxMaProperties.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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.io.Serializable;

/**
* 属性配置类.
*
Expand Down Expand Up @@ -36,4 +38,112 @@ public class WxMaProperties {
* 消息格式,XML或者JSON.
*/
private String msgDataFormat;

/**
* 存储策略
*/
private ConfigStorage configStorage = new ConfigStorage();

@Data
public static class ConfigStorage implements Serializable {
private static final long serialVersionUID = 4815731027000065434L;

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

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

/**
* redis连接配置.
*/
private RedisProperties redis;

/**
* 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 {
/**
* 内存.
*/
memory,
/**
* redis(JedisClient).
*/
jedis,
/**
* redis(RedisTemplate).
*/
redistemplate
}

public enum HttpClientType {
/**
* HttpClient.
*/
httpclient
}

@Data
public static class RedisProperties implements Serializable {
private static final long serialVersionUID = -5924815351660074401L;

/**
* 主机地址.
*/
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;
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.binarywang.spring.starter.wxjava.mp.config;

import com.binarywang.spring.starter.wxjava.mp.extend.RedisTemplateWxMpRedisOps;
import com.binarywang.spring.starter.wxjava.mp.properties.WxMpProperties;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.common.redis.JedisWxRedisOps;
import me.chanjar.weixin.common.redis.RedisTemplateWxRedisOps;
import me.chanjar.weixin.common.redis.WxRedisOps;
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
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;
Expand Down Expand Up @@ -65,15 +65,15 @@ private WxMpConfigStorage wxMpInJedisConfigStorage() {
} else {
jedisPool = applicationContext.getBean(JedisPool.class);
}
WxMpRedisOps redisOps = new JedisWxMpRedisOps(jedisPool);
WxRedisOps redisOps = new JedisWxRedisOps(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);
WxRedisOps redisOps = new RedisTemplateWxRedisOps(redisTemplate);
WxMpRedisConfigImpl wxMpRedisConfig = new WxMpRedisConfigImpl(redisOps, wxMpProperties.getConfigStorage().getKeyPrefix());
setWxMpInfo(wxMpRedisConfig);
return wxMpRedisConfig;
Expand Down
9 changes: 8 additions & 1 deletion weixin-java-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,18 @@
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>

<dependency>
<groupId>com.github.jedis-lock</groupId>
<artifactId>jedis-lock</artifactId>
</dependency>
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package me.chanjar.weixin.mp.config.redis;
package me.chanjar.weixin.common.redis;

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

/**
* 微信公众号redis操作基本类
* 微信redis操作基本类
* <p>
* 非内置实现redis相关操作, 请实现该类
*/
public class BaseWxMpRedisOps implements WxMpRedisOps {
public abstract class BaseWxRedisOps implements WxRedisOps {

@Override
public String getValue(String key) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
package me.chanjar.weixin.mp.config.redis;
package me.chanjar.weixin.common.redis;

import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.common.util.locks.JedisDistributedLock;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

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

/**
* Jedis实现相关操作
*/
@AllArgsConstructor
public class JedisWxMpRedisOps implements WxMpRedisOps {
@RequiredArgsConstructor
public class JedisWxRedisOps implements WxRedisOps {

private JedisPool jedisPool;
private final JedisPool jedisPool;

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