mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2026-01-23 13:22:04 +08:00
🆕 #3847 【开放平台】新增 wx-java-open-multi-spring-boot-starter 支持多开放平台配置
This commit is contained in:
@@ -23,6 +23,7 @@
|
||||
<module>wx-java-mp-multi-spring-boot-starter</module>
|
||||
<module>wx-java-mp-spring-boot-starter</module>
|
||||
<module>wx-java-pay-spring-boot-starter</module>
|
||||
<module>wx-java-open-multi-spring-boot-starter</module>
|
||||
<module>wx-java-open-spring-boot-starter</module>
|
||||
<module>wx-java-qidian-spring-boot-starter</module>
|
||||
<module>wx-java-cp-multi-spring-boot-starter</module>
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
# wx-java-open-multi-spring-boot-starter
|
||||
|
||||
## 快速开始
|
||||
|
||||
1. 引入依赖
|
||||
```xml
|
||||
<dependency>
|
||||
<groupId>com.github.binarywang</groupId>
|
||||
<artifactId>wx-java-open-multi-spring-boot-starter</artifactId>
|
||||
<version>${version}</version>
|
||||
</dependency>
|
||||
```
|
||||
2. 添加配置(application.properties)
|
||||
```properties
|
||||
# 开放平台配置
|
||||
## 应用 1 配置(必填)
|
||||
wx.open.apps.tenantId1.app-id=appId
|
||||
wx.open.apps.tenantId1.secret=@secret
|
||||
## 选填
|
||||
wx.open.apps.tenantId1.token=@token
|
||||
wx.open.apps.tenantId1.aes-key=@aesKey
|
||||
wx.open.apps.tenantId1.api-host-url=@apiHostUrl
|
||||
wx.open.apps.tenantId1.access-token-url=@accessTokenUrl
|
||||
## 应用 2 配置(必填)
|
||||
wx.open.apps.tenantId2.app-id=@appId
|
||||
wx.open.apps.tenantId2.secret=@secret
|
||||
## 选填
|
||||
wx.open.apps.tenantId2.token=@token
|
||||
wx.open.apps.tenantId2.aes-key=@aesKey
|
||||
wx.open.apps.tenantId2.api-host-url=@apiHostUrl
|
||||
wx.open.apps.tenantId2.access-token-url=@accessTokenUrl
|
||||
|
||||
# ConfigStorage 配置(选填)
|
||||
## 配置类型: memory(默认), jedis, redisson, redistemplate
|
||||
wx.open.config-storage.type=memory
|
||||
## 相关redis前缀配置: wx:open:multi(默认)
|
||||
wx.open.config-storage.key-prefix=wx:open:multi
|
||||
wx.open.config-storage.redis.host=127.0.0.1
|
||||
wx.open.config-storage.redis.port=6379
|
||||
## 注意:当前版本暂不支持 sentinel 配置,以下配置仅作为预留
|
||||
# wx.open.config-storage.redis.sentinel-ips=127.0.0.1:16379,127.0.0.1:26379
|
||||
# wx.open.config-storage.redis.sentinel-name=mymaster
|
||||
|
||||
# http 客户端配置(选填)
|
||||
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=
|
||||
## 最大重试次数,默认:5 次,如果小于 0,则为 0
|
||||
wx.open.config-storage.max-retry-times=5
|
||||
## 重试时间间隔步进,默认:1000 毫秒,如果小于 0,则为 1000
|
||||
wx.open.config-storage.retry-sleep-millis=1000
|
||||
## 连接超时时间,单位毫秒,默认:5000
|
||||
wx.open.config-storage.connection-timeout=5000
|
||||
## 读数据超时时间,即socketTimeout,单位毫秒,默认:5000
|
||||
wx.open.config-storage.so-timeout=5000
|
||||
## 从连接池获取链接的超时时间,单位毫秒,默认:5000
|
||||
wx.open.config-storage.connection-request-timeout=5000
|
||||
```
|
||||
3. 自动注入的类型:`WxOpenMultiServices`
|
||||
|
||||
4. 使用样例
|
||||
|
||||
```java
|
||||
import com.binarywang.spring.starter.wxjava.open.service.WxOpenMultiServices;
|
||||
import me.chanjar.weixin.open.api.WxOpenService;
|
||||
import me.chanjar.weixin.open.api.WxOpenComponentService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class DemoService {
|
||||
@Autowired
|
||||
private WxOpenMultiServices wxOpenMultiServices;
|
||||
|
||||
public void test() {
|
||||
// 应用 1 的 WxOpenService
|
||||
WxOpenService wxOpenService1 = wxOpenMultiServices.getWxOpenService("tenantId1");
|
||||
WxOpenComponentService componentService1 = wxOpenService1.getWxOpenComponentService();
|
||||
// todo ...
|
||||
|
||||
// 应用 2 的 WxOpenService
|
||||
WxOpenService wxOpenService2 = wxOpenMultiServices.getWxOpenService("tenantId2");
|
||||
WxOpenComponentService componentService2 = wxOpenService2.getWxOpenComponentService();
|
||||
// todo ...
|
||||
|
||||
// 应用 3 的 WxOpenService
|
||||
WxOpenService wxOpenService3 = wxOpenMultiServices.getWxOpenService("tenantId3");
|
||||
// 判断是否为空
|
||||
if (wxOpenService3 == null) {
|
||||
// todo wxOpenService3 为空,请先配置 tenantId3 微信开放平台应用参数
|
||||
return;
|
||||
}
|
||||
WxOpenComponentService componentService3 = wxOpenService3.getWxOpenComponentService();
|
||||
// todo ...
|
||||
}
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,62 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>wx-java-spring-boot-starters</artifactId>
|
||||
<groupId>com.github.binarywang</groupId>
|
||||
<version>4.8.0</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>wx-java-open-multi-spring-boot-starter</artifactId>
|
||||
<name>WxJava - Spring Boot Starter for WxOpen::支持多账号配置</name>
|
||||
<description>微信开放平台开发的 Spring Boot Starter::支持多账号配置</description>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.github.binarywang</groupId>
|
||||
<artifactId>weixin-java-open</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>redis.clients</groupId>
|
||||
<artifactId>jedis</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.redisson</groupId>
|
||||
<artifactId>redisson</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-redis</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<version>${spring.boot.version}</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-source-plugin</artifactId>
|
||||
<version>2.2.1</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>attach-sources</id>
|
||||
<goals>
|
||||
<goal>jar-no-fork</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.binarywang.spring.starter.wxjava.open.autoconfigure;
|
||||
|
||||
import com.binarywang.spring.starter.wxjava.open.configuration.WxOpenMultiServiceConfiguration;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
/**
|
||||
* 微信开放平台多账号自动配置
|
||||
*
|
||||
* @author Binary Wang
|
||||
*/
|
||||
@Configuration
|
||||
@Import(WxOpenMultiServiceConfiguration.class)
|
||||
public class WxOpenMultiAutoConfiguration {
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.binarywang.spring.starter.wxjava.open.configuration;
|
||||
|
||||
import com.binarywang.spring.starter.wxjava.open.configuration.services.WxOpenInJedisConfiguration;
|
||||
import com.binarywang.spring.starter.wxjava.open.configuration.services.WxOpenInMemoryConfiguration;
|
||||
import com.binarywang.spring.starter.wxjava.open.configuration.services.WxOpenInRedisTemplateConfiguration;
|
||||
import com.binarywang.spring.starter.wxjava.open.configuration.services.WxOpenInRedissonConfiguration;
|
||||
import com.binarywang.spring.starter.wxjava.open.properties.WxOpenMultiProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
/**
|
||||
* 微信开放平台相关服务自动注册
|
||||
*
|
||||
* @author Binary Wang
|
||||
*/
|
||||
@Configuration
|
||||
@EnableConfigurationProperties(WxOpenMultiProperties.class)
|
||||
@Import({
|
||||
WxOpenInJedisConfiguration.class,
|
||||
WxOpenInMemoryConfiguration.class,
|
||||
WxOpenInRedissonConfiguration.class,
|
||||
WxOpenInRedisTemplateConfiguration.class
|
||||
})
|
||||
public class WxOpenMultiServiceConfiguration {
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
package com.binarywang.spring.starter.wxjava.open.configuration.services;
|
||||
|
||||
import com.binarywang.spring.starter.wxjava.open.properties.WxOpenMultiProperties;
|
||||
import com.binarywang.spring.starter.wxjava.open.properties.WxOpenSingleProperties;
|
||||
import com.binarywang.spring.starter.wxjava.open.service.WxOpenMultiServices;
|
||||
import com.binarywang.spring.starter.wxjava.open.service.WxOpenMultiServicesImpl;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
|
||||
import me.chanjar.weixin.common.util.http.apache.DefaultApacheHttpClientBuilder;
|
||||
import me.chanjar.weixin.open.api.WxOpenConfigStorage;
|
||||
import me.chanjar.weixin.open.api.WxOpenService;
|
||||
import me.chanjar.weixin.open.api.impl.WxOpenInMemoryConfigStorage;
|
||||
import me.chanjar.weixin.open.api.impl.WxOpenServiceImpl;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* WxOpenConfigStorage 抽象配置类
|
||||
*
|
||||
* @author Binary Wang
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public abstract class AbstractWxOpenConfiguration {
|
||||
|
||||
protected WxOpenMultiServices wxOpenMultiServices(WxOpenMultiProperties wxOpenMultiProperties) {
|
||||
Map<String, WxOpenSingleProperties> appsMap = wxOpenMultiProperties.getApps();
|
||||
if (appsMap == null || appsMap.isEmpty()) {
|
||||
log.warn("微信开放平台应用参数未配置,通过 WxOpenMultiServices#getWxOpenService(\"tenantId\")获取实例将返回空");
|
||||
return new WxOpenMultiServicesImpl();
|
||||
}
|
||||
/**
|
||||
* 校验 appId 是否唯一,避免使用 redis 缓存 token、ticket 时错乱。
|
||||
*/
|
||||
Collection<WxOpenSingleProperties> apps = appsMap.values();
|
||||
if (apps.size() > 1) {
|
||||
// 校验 appId 是否唯一
|
||||
String nullAppIdPlaceholder = "__NULL_APP_ID__";
|
||||
boolean multi = apps.stream()
|
||||
// 没有 appId,如果不判断是否为空,这里会报 NPE 异常
|
||||
.collect(Collectors.groupingBy(c -> c.getAppId() == null ? nullAppIdPlaceholder : c.getAppId(), Collectors.counting()))
|
||||
.entrySet().stream().anyMatch(e -> e.getValue() > 1);
|
||||
if (multi) {
|
||||
throw new RuntimeException("请确保微信开放平台配置 appId 的唯一性");
|
||||
}
|
||||
}
|
||||
WxOpenMultiServicesImpl services = new WxOpenMultiServicesImpl();
|
||||
|
||||
Set<Map.Entry<String, WxOpenSingleProperties>> entries = appsMap.entrySet();
|
||||
for (Map.Entry<String, WxOpenSingleProperties> entry : entries) {
|
||||
String tenantId = entry.getKey();
|
||||
WxOpenSingleProperties wxOpenSingleProperties = entry.getValue();
|
||||
WxOpenInMemoryConfigStorage storage = this.wxOpenConfigStorage(wxOpenMultiProperties);
|
||||
this.configApp(storage, wxOpenSingleProperties);
|
||||
this.configHttp(storage, wxOpenMultiProperties.getConfigStorage());
|
||||
WxOpenService wxOpenService = this.wxOpenService(storage, wxOpenMultiProperties);
|
||||
services.addWxOpenService(tenantId, wxOpenService);
|
||||
}
|
||||
return services;
|
||||
}
|
||||
|
||||
/**
|
||||
* 配置 WxOpenInMemoryConfigStorage
|
||||
*
|
||||
* @param wxOpenMultiProperties 参数
|
||||
* @return WxOpenInMemoryConfigStorage
|
||||
*/
|
||||
protected abstract WxOpenInMemoryConfigStorage wxOpenConfigStorage(WxOpenMultiProperties wxOpenMultiProperties);
|
||||
|
||||
public WxOpenService wxOpenService(WxOpenConfigStorage configStorage, WxOpenMultiProperties wxOpenMultiProperties) {
|
||||
WxOpenService wxOpenService = new WxOpenServiceImpl();
|
||||
wxOpenService.setWxOpenConfigStorage(configStorage);
|
||||
return wxOpenService;
|
||||
}
|
||||
|
||||
private void configApp(WxOpenInMemoryConfigStorage config, WxOpenSingleProperties appProperties) {
|
||||
String appId = appProperties.getAppId();
|
||||
String secret = appProperties.getSecret();
|
||||
String token = appProperties.getToken();
|
||||
String aesKey = appProperties.getAesKey();
|
||||
String apiHostUrl = appProperties.getApiHostUrl();
|
||||
String accessTokenUrl = appProperties.getAccessTokenUrl();
|
||||
|
||||
// appId 和 secret 是必需的
|
||||
if (StringUtils.isBlank(appId)) {
|
||||
throw new IllegalArgumentException("微信开放平台 appId 不能为空");
|
||||
}
|
||||
if (StringUtils.isBlank(secret)) {
|
||||
throw new IllegalArgumentException("微信开放平台 secret 不能为空");
|
||||
}
|
||||
|
||||
config.setComponentAppId(appId);
|
||||
config.setComponentAppSecret(secret);
|
||||
if (StringUtils.isNotBlank(token)) {
|
||||
config.setComponentToken(token);
|
||||
}
|
||||
if (StringUtils.isNotBlank(aesKey)) {
|
||||
config.setComponentAesKey(aesKey);
|
||||
}
|
||||
// 设置URL配置
|
||||
config.setApiHostUrl(StringUtils.trimToNull(apiHostUrl));
|
||||
config.setAccessTokenUrl(StringUtils.trimToNull(accessTokenUrl));
|
||||
}
|
||||
|
||||
private void configHttp(WxOpenInMemoryConfigStorage config, WxOpenMultiProperties.ConfigStorage storage) {
|
||||
String httpProxyHost = storage.getHttpProxyHost();
|
||||
Integer httpProxyPort = storage.getHttpProxyPort();
|
||||
String httpProxyUsername = storage.getHttpProxyUsername();
|
||||
String httpProxyPassword = storage.getHttpProxyPassword();
|
||||
if (StringUtils.isNotBlank(httpProxyHost)) {
|
||||
config.setHttpProxyHost(httpProxyHost);
|
||||
if (httpProxyPort != null) {
|
||||
config.setHttpProxyPort(httpProxyPort);
|
||||
}
|
||||
if (StringUtils.isNotBlank(httpProxyUsername)) {
|
||||
config.setHttpProxyUsername(httpProxyUsername);
|
||||
}
|
||||
if (StringUtils.isNotBlank(httpProxyPassword)) {
|
||||
config.setHttpProxyPassword(httpProxyPassword);
|
||||
}
|
||||
}
|
||||
|
||||
// 设置重试配置
|
||||
int maxRetryTimes = storage.getMaxRetryTimes();
|
||||
if (maxRetryTimes < 0) {
|
||||
maxRetryTimes = 0;
|
||||
}
|
||||
int retrySleepMillis = storage.getRetrySleepMillis();
|
||||
if (retrySleepMillis < 0) {
|
||||
retrySleepMillis = 1000;
|
||||
}
|
||||
config.setRetrySleepMillis(retrySleepMillis);
|
||||
config.setMaxRetryTimes(maxRetryTimes);
|
||||
|
||||
// 设置自定义的HttpClient超时配置
|
||||
ApacheHttpClientBuilder clientBuilder = config.getApacheHttpClientBuilder();
|
||||
if (clientBuilder == null) {
|
||||
clientBuilder = DefaultApacheHttpClientBuilder.get();
|
||||
}
|
||||
if (clientBuilder instanceof DefaultApacheHttpClientBuilder) {
|
||||
DefaultApacheHttpClientBuilder defaultBuilder = (DefaultApacheHttpClientBuilder) clientBuilder;
|
||||
defaultBuilder.setConnectionTimeout(storage.getConnectionTimeout());
|
||||
defaultBuilder.setSoTimeout(storage.getSoTimeout());
|
||||
defaultBuilder.setConnectionRequestTimeout(storage.getConnectionRequestTimeout());
|
||||
config.setApacheHttpClientBuilder(defaultBuilder);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package com.binarywang.spring.starter.wxjava.open.configuration.services;
|
||||
|
||||
import com.binarywang.spring.starter.wxjava.open.properties.WxOpenMultiProperties;
|
||||
import com.binarywang.spring.starter.wxjava.open.properties.WxOpenMultiRedisProperties;
|
||||
import com.binarywang.spring.starter.wxjava.open.service.WxOpenMultiServices;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import me.chanjar.weixin.open.api.impl.WxOpenInMemoryConfigStorage;
|
||||
import me.chanjar.weixin.open.api.impl.WxOpenInRedisConfigStorage;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import redis.clients.jedis.JedisPool;
|
||||
import redis.clients.jedis.JedisPoolConfig;
|
||||
|
||||
|
||||
/**
|
||||
* 自动装配基于 jedis 策略配置
|
||||
*
|
||||
* @author Binary Wang
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnProperty(
|
||||
prefix = WxOpenMultiProperties.PREFIX + ".config-storage", name = "type", havingValue = "JEDIS"
|
||||
)
|
||||
@ConditionalOnClass({JedisPool.class, JedisPoolConfig.class})
|
||||
@RequiredArgsConstructor
|
||||
public class WxOpenInJedisConfiguration extends AbstractWxOpenConfiguration {
|
||||
private final WxOpenMultiProperties wxOpenMultiProperties;
|
||||
private final ApplicationContext applicationContext;
|
||||
|
||||
@Bean
|
||||
public WxOpenMultiServices wxOpenMultiServices() {
|
||||
return this.wxOpenMultiServices(wxOpenMultiProperties);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected WxOpenInMemoryConfigStorage wxOpenConfigStorage(WxOpenMultiProperties wxOpenMultiProperties) {
|
||||
return this.configJedis(wxOpenMultiProperties);
|
||||
}
|
||||
|
||||
private WxOpenInRedisConfigStorage configJedis(WxOpenMultiProperties wxOpenMultiProperties) {
|
||||
WxOpenMultiRedisProperties redisProperties = wxOpenMultiProperties.getConfigStorage().getRedis();
|
||||
JedisPool jedisPool;
|
||||
if (redisProperties != null && StringUtils.isNotEmpty(redisProperties.getHost())) {
|
||||
jedisPool = getJedisPool(wxOpenMultiProperties);
|
||||
} else {
|
||||
jedisPool = applicationContext.getBean(JedisPool.class);
|
||||
}
|
||||
return new WxOpenInRedisConfigStorage(jedisPool, wxOpenMultiProperties.getConfigStorage().getKeyPrefix());
|
||||
}
|
||||
|
||||
private JedisPool getJedisPool(WxOpenMultiProperties wxOpenMultiProperties) {
|
||||
WxOpenMultiProperties.ConfigStorage storage = wxOpenMultiProperties.getConfigStorage();
|
||||
WxOpenMultiRedisProperties 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());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.binarywang.spring.starter.wxjava.open.configuration.services;
|
||||
|
||||
import com.binarywang.spring.starter.wxjava.open.properties.WxOpenMultiProperties;
|
||||
import com.binarywang.spring.starter.wxjava.open.service.WxOpenMultiServices;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import me.chanjar.weixin.open.api.impl.WxOpenInMemoryConfigStorage;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* 自动装配基于内存策略配置
|
||||
*
|
||||
* @author someone
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnProperty(
|
||||
prefix = WxOpenMultiProperties.PREFIX + ".config-storage", name = "type", havingValue = "MEMORY", matchIfMissing = true
|
||||
)
|
||||
@RequiredArgsConstructor
|
||||
public class WxOpenInMemoryConfiguration extends AbstractWxOpenConfiguration {
|
||||
private final WxOpenMultiProperties wxOpenMultiProperties;
|
||||
|
||||
@Bean
|
||||
public WxOpenMultiServices wxOpenMultiServices() {
|
||||
return this.wxOpenMultiServices(wxOpenMultiProperties);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected WxOpenInMemoryConfigStorage wxOpenConfigStorage(WxOpenMultiProperties wxOpenMultiProperties) {
|
||||
return new WxOpenInMemoryConfigStorage();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.binarywang.spring.starter.wxjava.open.configuration.services;
|
||||
|
||||
import com.binarywang.spring.starter.wxjava.open.properties.WxOpenMultiProperties;
|
||||
import com.binarywang.spring.starter.wxjava.open.service.WxOpenMultiServices;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import me.chanjar.weixin.open.api.impl.WxOpenInMemoryConfigStorage;
|
||||
import me.chanjar.weixin.open.api.impl.WxOpenInRedisTemplateConfigStorage;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
|
||||
/**
|
||||
* 自动装配基于 redis template 策略配置
|
||||
*
|
||||
* @author Binary Wang
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnProperty(
|
||||
prefix = WxOpenMultiProperties.PREFIX + ".config-storage", name = "type", havingValue = "redistemplate"
|
||||
)
|
||||
@ConditionalOnClass(StringRedisTemplate.class)
|
||||
@RequiredArgsConstructor
|
||||
public class WxOpenInRedisTemplateConfiguration extends AbstractWxOpenConfiguration {
|
||||
private final WxOpenMultiProperties wxOpenMultiProperties;
|
||||
private final ApplicationContext applicationContext;
|
||||
|
||||
@Bean
|
||||
public WxOpenMultiServices wxOpenMultiServices() {
|
||||
return this.wxOpenMultiServices(wxOpenMultiProperties);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected WxOpenInMemoryConfigStorage wxOpenConfigStorage(WxOpenMultiProperties wxOpenMultiProperties) {
|
||||
return this.configRedisTemplate();
|
||||
}
|
||||
|
||||
private WxOpenInRedisTemplateConfigStorage configRedisTemplate() {
|
||||
StringRedisTemplate redisTemplate = applicationContext.getBean(StringRedisTemplate.class);
|
||||
return new WxOpenInRedisTemplateConfigStorage(redisTemplate, wxOpenMultiProperties.getConfigStorage().getKeyPrefix());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package com.binarywang.spring.starter.wxjava.open.configuration.services;
|
||||
|
||||
import com.binarywang.spring.starter.wxjava.open.properties.WxOpenMultiProperties;
|
||||
import com.binarywang.spring.starter.wxjava.open.properties.WxOpenMultiRedisProperties;
|
||||
import com.binarywang.spring.starter.wxjava.open.service.WxOpenMultiServices;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import me.chanjar.weixin.open.api.impl.WxOpenInMemoryConfigStorage;
|
||||
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.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* 自动装配基于 redisson 策略配置
|
||||
*
|
||||
* @author Binary Wang
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnProperty(
|
||||
prefix = WxOpenMultiProperties.PREFIX + ".config-storage", name = "type", havingValue = "redisson"
|
||||
)
|
||||
@ConditionalOnClass({Redisson.class, RedissonClient.class})
|
||||
@RequiredArgsConstructor
|
||||
public class WxOpenInRedissonConfiguration extends AbstractWxOpenConfiguration {
|
||||
private final WxOpenMultiProperties wxOpenMultiProperties;
|
||||
private final ApplicationContext applicationContext;
|
||||
|
||||
@Bean
|
||||
public WxOpenMultiServices wxOpenMultiServices() {
|
||||
return this.wxOpenMultiServices(wxOpenMultiProperties);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected WxOpenInMemoryConfigStorage wxOpenConfigStorage(WxOpenMultiProperties wxOpenMultiProperties) {
|
||||
return this.configRedisson(wxOpenMultiProperties);
|
||||
}
|
||||
|
||||
private WxOpenInRedissonConfigStorage configRedisson(WxOpenMultiProperties wxOpenMultiProperties) {
|
||||
WxOpenMultiRedisProperties redisProperties = wxOpenMultiProperties.getConfigStorage().getRedis();
|
||||
RedissonClient redissonClient;
|
||||
if (redisProperties != null && StringUtils.isNotEmpty(redisProperties.getHost())) {
|
||||
redissonClient = getRedissonClient(wxOpenMultiProperties);
|
||||
} else {
|
||||
redissonClient = applicationContext.getBean(RedissonClient.class);
|
||||
}
|
||||
return new WxOpenInRedissonConfigStorage(redissonClient, wxOpenMultiProperties.getConfigStorage().getKeyPrefix());
|
||||
}
|
||||
|
||||
private RedissonClient getRedissonClient(WxOpenMultiProperties wxOpenMultiProperties) {
|
||||
WxOpenMultiProperties.ConfigStorage storage = wxOpenMultiProperties.getConfigStorage();
|
||||
WxOpenMultiRedisProperties redis = storage.getRedis();
|
||||
|
||||
Config config = new Config();
|
||||
config.useSingleServer()
|
||||
.setAddress("redis://" + redis.getHost() + ":" + redis.getPort())
|
||||
.setDatabase(redis.getDatabase())
|
||||
.setPassword(redis.getPassword());
|
||||
config.setTransportMode(TransportMode.NIO);
|
||||
return Redisson.create(config);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
package com.binarywang.spring.starter.wxjava.open.properties;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.context.properties.NestedConfigurationProperty;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 微信开放平台多账号配置属性.
|
||||
*
|
||||
* @author Binary Wang
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@ConfigurationProperties(WxOpenMultiProperties.PREFIX)
|
||||
public class WxOpenMultiProperties implements Serializable {
|
||||
private static final long serialVersionUID = -5358245184407791011L;
|
||||
public static final String PREFIX = "wx.open";
|
||||
|
||||
private Map<String, WxOpenSingleProperties> apps = new HashMap<>();
|
||||
|
||||
/**
|
||||
* 存储策略
|
||||
*/
|
||||
private final ConfigStorage configStorage = new ConfigStorage();
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
public static class ConfigStorage implements Serializable {
|
||||
private static final long serialVersionUID = 4815731027000065434L;
|
||||
|
||||
/**
|
||||
* 存储类型.
|
||||
*/
|
||||
private StorageType type = StorageType.memory;
|
||||
|
||||
/**
|
||||
* 指定key前缀.
|
||||
*/
|
||||
private String keyPrefix = "wx:open:multi";
|
||||
|
||||
/**
|
||||
* redis连接配置.
|
||||
*/
|
||||
@NestedConfigurationProperty
|
||||
private final WxOpenMultiRedisProperties redis = new WxOpenMultiRedisProperties();
|
||||
|
||||
/**
|
||||
* http代理主机.
|
||||
*/
|
||||
private String httpProxyHost;
|
||||
|
||||
/**
|
||||
* http代理端口.
|
||||
*/
|
||||
private Integer httpProxyPort;
|
||||
|
||||
/**
|
||||
* http代理用户名.
|
||||
*/
|
||||
private String httpProxyUsername;
|
||||
|
||||
/**
|
||||
* http代理密码.
|
||||
*/
|
||||
private String httpProxyPassword;
|
||||
|
||||
/**
|
||||
* http 请求最大重试次数
|
||||
* <pre>
|
||||
* {@link me.chanjar.weixin.mp.api.impl.BaseWxMpServiceImpl#setMaxRetryTimes(int)}
|
||||
* {@link cn.binarywang.wx.miniapp.api.impl.BaseWxMaServiceImpl#setMaxRetryTimes(int)}
|
||||
* </pre>
|
||||
*/
|
||||
private int maxRetryTimes = 5;
|
||||
|
||||
/**
|
||||
* http 请求重试间隔
|
||||
* <pre>
|
||||
* {@link me.chanjar.weixin.mp.api.impl.BaseWxMpServiceImpl#setRetrySleepMillis(int)}
|
||||
* {@link cn.binarywang.wx.miniapp.api.impl.BaseWxMaServiceImpl#setRetrySleepMillis(int)}
|
||||
* </pre>
|
||||
*/
|
||||
private int retrySleepMillis = 1000;
|
||||
|
||||
/**
|
||||
* 连接超时时间,单位毫秒
|
||||
*/
|
||||
private int connectionTimeout = 5000;
|
||||
|
||||
/**
|
||||
* 读数据超时时间,即socketTimeout,单位毫秒
|
||||
*/
|
||||
private int soTimeout = 5000;
|
||||
|
||||
/**
|
||||
* 从连接池获取链接的超时时间,单位毫秒
|
||||
*/
|
||||
private int connectionRequestTimeout = 5000;
|
||||
}
|
||||
|
||||
public enum StorageType {
|
||||
/**
|
||||
* 内存
|
||||
*/
|
||||
memory,
|
||||
/**
|
||||
* jedis
|
||||
*/
|
||||
jedis,
|
||||
/**
|
||||
* redisson
|
||||
*/
|
||||
redisson,
|
||||
/**
|
||||
* redisTemplate
|
||||
*/
|
||||
redistemplate
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package com.binarywang.spring.starter.wxjava.open.properties;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 微信开放平台多账号Redis配置.
|
||||
*
|
||||
* @author Binary Wang
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
public class WxOpenMultiRedisProperties 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;
|
||||
|
||||
/**
|
||||
* sentinel ips
|
||||
*/
|
||||
private String sentinelIps;
|
||||
|
||||
/**
|
||||
* sentinel name
|
||||
*/
|
||||
private String sentinelName;
|
||||
|
||||
private Integer maxActive;
|
||||
private Integer maxIdle;
|
||||
private Integer maxWaitMillis;
|
||||
private Integer minIdle;
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.binarywang.spring.starter.wxjava.open.properties;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 微信开放平台单个应用配置.
|
||||
*
|
||||
* @author Binary Wang
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
public class WxOpenSingleProperties implements Serializable {
|
||||
private static final long serialVersionUID = 1980986361098922525L;
|
||||
|
||||
/**
|
||||
* 设置微信开放平台的appid.
|
||||
*/
|
||||
private String appId;
|
||||
|
||||
/**
|
||||
* 设置微信开放平台的app secret.
|
||||
*/
|
||||
private String secret;
|
||||
|
||||
/**
|
||||
* 设置微信开放平台的token.
|
||||
*/
|
||||
private String token;
|
||||
|
||||
/**
|
||||
* 设置微信开放平台的EncodingAESKey.
|
||||
*/
|
||||
private String aesKey;
|
||||
|
||||
/**
|
||||
* 自定义API主机地址,用于替换默认的 https://api.weixin.qq.com
|
||||
* 例如:http://proxy.company.com:8080
|
||||
*/
|
||||
private String apiHostUrl;
|
||||
|
||||
/**
|
||||
* 自定义获取AccessToken地址,用于向自定义统一服务获取AccessToken
|
||||
* 例如:http://proxy.company.com:8080/oauth/token
|
||||
*/
|
||||
private String accessTokenUrl;
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.binarywang.spring.starter.wxjava.open.service;
|
||||
|
||||
|
||||
import me.chanjar.weixin.open.api.WxOpenService;
|
||||
|
||||
/**
|
||||
* 微信开放平台 {@link WxOpenService} 所有实例存放类.
|
||||
*
|
||||
* @author binarywang
|
||||
*/
|
||||
public interface WxOpenMultiServices {
|
||||
/**
|
||||
* 通过租户 Id 获取 WxOpenService
|
||||
*
|
||||
* @param tenantId 租户 Id
|
||||
* @return WxOpenService
|
||||
*/
|
||||
WxOpenService getWxOpenService(String tenantId);
|
||||
|
||||
/**
|
||||
* 根据租户 Id,从列表中移除一个 WxOpenService 实例
|
||||
*
|
||||
* @param tenantId 租户 Id
|
||||
*/
|
||||
void removeWxOpenService(String tenantId);
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.binarywang.spring.starter.wxjava.open.service;
|
||||
|
||||
import me.chanjar.weixin.open.api.WxOpenService;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* 微信开放平台 {@link WxOpenMultiServices} 默认实现
|
||||
*
|
||||
* @author Binary Wang
|
||||
*/
|
||||
public class WxOpenMultiServicesImpl implements WxOpenMultiServices {
|
||||
private final Map<String, WxOpenService> services = new ConcurrentHashMap<>();
|
||||
|
||||
@Override
|
||||
public WxOpenService getWxOpenService(String tenantId) {
|
||||
return this.services.get(tenantId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据租户 Id,添加一个 WxOpenService 到列表
|
||||
*
|
||||
* @param tenantId 租户 Id
|
||||
* @param wxOpenService WxOpenService 实例
|
||||
*/
|
||||
public void addWxOpenService(String tenantId, WxOpenService wxOpenService) {
|
||||
this.services.put(tenantId, wxOpenService);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeWxOpenService(String tenantId) {
|
||||
this.services.remove(tenantId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
com.binarywang.spring.starter.wxjava.open.autoconfigure.WxOpenMultiAutoConfiguration
|
||||
@@ -0,0 +1 @@
|
||||
com.binarywang.spring.starter.wxjava.open.autoconfigure.WxOpenMultiAutoConfiguration
|
||||
Reference in New Issue
Block a user