1
0
mirror of synced 2025-12-17 04:43:15 +08:00

🆕 #3217 增加 solon-plugins 适配

This commit is contained in:
西东
2024-09-02 17:18:56 +08:00
committed by GitHub
parent 8ceca63f28
commit 41bb5e44cc
103 changed files with 5069 additions and 0 deletions

View File

@@ -0,0 +1,39 @@
# wx-java-open-solon-plugin
## 快速开始
1. 引入依赖
```xml
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>wx-java-open-solon-plugin</artifactId>
<version>${version}</version>
</dependency>
```
2. 添加配置(app.properties)
```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=
# 最大重试次数默认5 次,如果小于 0则为 0
wx.open.config-storage.max-retry-times=5
# 重试时间间隔步进默认1000 毫秒,如果小于 0则为 1000
wx.open.config-storage.retry-sleep-millis=1000
```
3. 支持自动注入的类型: `WxOpenService, WxOpenMessageRouter, WxOpenComponentService`
4. 覆盖自动配置: 自定义注入的bean会覆盖自动注入的
- WxOpenConfigStorage
- WxOpenService

View File

@@ -0,0 +1,32 @@
<?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-solon-plugins</artifactId>
<groupId>com.github.binarywang</groupId>
<version>4.6.4.B</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>wx-java-open-solon-plugin</artifactId>
<name>WxJava - Solon Plugin for WxOpen</name>
<description>微信开放平台开发的 Solon Plugin</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>
</dependency>
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,37 @@
package com.binarywang.solon.wxjava.open.config;
import me.chanjar.weixin.open.api.WxOpenComponentService;
import me.chanjar.weixin.open.api.WxOpenConfigStorage;
import me.chanjar.weixin.open.api.WxOpenService;
import me.chanjar.weixin.open.api.impl.WxOpenMessageRouter;
import me.chanjar.weixin.open.api.impl.WxOpenServiceImpl;
import org.noear.solon.annotation.Bean;
import org.noear.solon.annotation.Condition;
import org.noear.solon.annotation.Configuration;
/**
* 微信开放平台相关服务自动注册.
*
* @author someone
*/
@Configuration
public class WxOpenServiceAutoConfiguration {
@Bean
@Condition(onMissingBean = WxOpenService.class, onBean = WxOpenConfigStorage.class)
public WxOpenService wxOpenService(WxOpenConfigStorage wxOpenConfigStorage) {
WxOpenService wxOpenService = new WxOpenServiceImpl();
wxOpenService.setWxOpenConfigStorage(wxOpenConfigStorage);
return wxOpenService;
}
@Bean
public WxOpenMessageRouter wxOpenMessageRouter(WxOpenService wxOpenService) {
return new WxOpenMessageRouter(wxOpenService);
}
@Bean
public WxOpenComponentService wxOpenComponentService(WxOpenService wxOpenService) {
return wxOpenService.getWxOpenComponentService();
}
}

View File

@@ -0,0 +1,33 @@
package com.binarywang.solon.wxjava.open.config.storage;
import com.binarywang.solon.wxjava.open.properties.WxOpenProperties;
import me.chanjar.weixin.open.api.impl.WxOpenInMemoryConfigStorage;
/**
* @author yl
*/
public abstract class AbstractWxOpenConfigStorageConfiguration {
protected WxOpenInMemoryConfigStorage config(WxOpenInMemoryConfigStorage config, WxOpenProperties properties) {
WxOpenProperties.ConfigStorage storage = properties.getConfigStorage();
config.setWxOpenInfo(properties.getAppId(), properties.getSecret(), properties.getToken(), properties.getAesKey());
config.setHttpProxyHost(storage.getHttpProxyHost());
config.setHttpProxyUsername(storage.getHttpProxyUsername());
config.setHttpProxyPassword(storage.getHttpProxyPassword());
Integer httpProxyPort = storage.getHttpProxyPort();
if (httpProxyPort != null) {
config.setHttpProxyPort(httpProxyPort);
}
int maxRetryTimes = storage.getMaxRetryTimes();
if (maxRetryTimes < 0) {
maxRetryTimes = 0;
}
int retrySleepMillis = storage.getRetrySleepMillis();
if (retrySleepMillis < 0) {
retrySleepMillis = 1000;
}
config.setRetrySleepMillis(retrySleepMillis);
config.setMaxRetryTimes(maxRetryTimes);
return config;
}
}

View File

@@ -0,0 +1,71 @@
package com.binarywang.solon.wxjava.open.config.storage;
import com.binarywang.solon.wxjava.open.properties.WxOpenProperties;
import com.binarywang.solon.wxjava.open.properties.WxOpenRedisProperties;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.open.api.WxOpenConfigStorage;
import me.chanjar.weixin.open.api.impl.WxOpenInMemoryConfigStorage;
import me.chanjar.weixin.open.api.impl.WxOpenInRedisConfigStorage;
import org.apache.commons.lang3.StringUtils;
import org.noear.solon.annotation.Bean;
import org.noear.solon.annotation.Condition;
import org.noear.solon.annotation.Configuration;
import org.noear.solon.core.AppContext;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
/**
* @author yl
*/
@Configuration
@Condition(
onProperty = "${"+WxOpenProperties.PREFIX + ".configStorage.type} = jedis",
onClass = JedisPool.class
)
@RequiredArgsConstructor
public class WxOpenInJedisConfigStorageConfiguration extends AbstractWxOpenConfigStorageConfiguration {
private final WxOpenProperties properties;
private final AppContext applicationContext;
@Bean
@Condition(onMissingBean=WxOpenConfigStorage.class)
public WxOpenConfigStorage wxOpenConfigStorage() {
WxOpenInMemoryConfigStorage config = getWxOpenInRedisConfigStorage();
return this.config(config, properties);
}
private WxOpenInRedisConfigStorage getWxOpenInRedisConfigStorage() {
WxOpenRedisProperties wxOpenRedisProperties = properties.getConfigStorage().getRedis();
JedisPool jedisPool;
if (wxOpenRedisProperties != null && StringUtils.isNotEmpty(wxOpenRedisProperties.getHost())) {
jedisPool = getJedisPool();
} else {
jedisPool = applicationContext.getBean(JedisPool.class);
}
return new WxOpenInRedisConfigStorage(jedisPool, properties.getConfigStorage().getKeyPrefix());
}
private JedisPool getJedisPool() {
WxOpenProperties.ConfigStorage storage = properties.getConfigStorage();
WxOpenRedisProperties 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());
}
}

View File

@@ -0,0 +1,28 @@
package com.binarywang.solon.wxjava.open.config.storage;
import com.binarywang.solon.wxjava.open.properties.WxOpenProperties;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.open.api.WxOpenConfigStorage;
import me.chanjar.weixin.open.api.impl.WxOpenInMemoryConfigStorage;
import org.noear.solon.annotation.Bean;
import org.noear.solon.annotation.Condition;
import org.noear.solon.annotation.Configuration;
/**
* @author yl
*/
@Configuration
@Condition(
onProperty = "${"+WxOpenProperties.PREFIX + ".configStorage.type:memory} = memory"
)
@RequiredArgsConstructor
public class WxOpenInMemoryConfigStorageConfiguration extends AbstractWxOpenConfigStorageConfiguration {
private final WxOpenProperties properties;
@Bean
@Condition(onMissingBean=WxOpenConfigStorage.class)
public WxOpenConfigStorage wxOpenConfigStorage() {
WxOpenInMemoryConfigStorage config = new WxOpenInMemoryConfigStorage();
return this.config(config, properties);
}
}

View File

@@ -0,0 +1,62 @@
package com.binarywang.solon.wxjava.open.config.storage;
import com.binarywang.solon.wxjava.open.properties.WxOpenProperties;
import com.binarywang.solon.wxjava.open.properties.WxOpenRedisProperties;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.open.api.WxOpenConfigStorage;
import me.chanjar.weixin.open.api.impl.WxOpenInMemoryConfigStorage;
import me.chanjar.weixin.open.api.impl.WxOpenInRedissonConfigStorage;
import org.apache.commons.lang3.StringUtils;
import org.noear.solon.annotation.Bean;
import org.noear.solon.annotation.Condition;
import org.noear.solon.annotation.Configuration;
import org.noear.solon.core.AppContext;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.TransportMode;
/**
* @author yl
*/
@Configuration
@Condition(
onProperty = "${"+WxOpenProperties.PREFIX + ".configStorage.type} = redisson",
onClass = Redisson.class
)
@RequiredArgsConstructor
public class WxOpenInRedissonConfigStorageConfiguration extends AbstractWxOpenConfigStorageConfiguration {
private final WxOpenProperties properties;
private final AppContext applicationContext;
@Bean
@Condition(onMissingBean=WxOpenConfigStorage.class)
public WxOpenConfigStorage wxOpenConfigStorage() {
WxOpenInMemoryConfigStorage config = getWxOpenInRedissonConfigStorage();
return this.config(config, properties);
}
private WxOpenInRedissonConfigStorage getWxOpenInRedissonConfigStorage() {
WxOpenRedisProperties wxOpenRedisProperties = properties.getConfigStorage().getRedis();
RedissonClient redissonClient;
if (wxOpenRedisProperties != null && StringUtils.isNotEmpty(wxOpenRedisProperties.getHost())) {
redissonClient = getRedissonClient();
} else {
redissonClient = applicationContext.getBean(RedissonClient.class);
}
return new WxOpenInRedissonConfigStorage(redissonClient, properties.getConfigStorage().getKeyPrefix());
}
private RedissonClient getRedissonClient() {
WxOpenProperties.ConfigStorage storage = properties.getConfigStorage();
WxOpenRedisProperties 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);
}
}

View File

@@ -0,0 +1,25 @@
package com.binarywang.solon.wxjava.open.integration;
import com.binarywang.solon.wxjava.open.config.WxOpenServiceAutoConfiguration;
import com.binarywang.solon.wxjava.open.config.storage.WxOpenInJedisConfigStorageConfiguration;
import com.binarywang.solon.wxjava.open.config.storage.WxOpenInMemoryConfigStorageConfiguration;
import com.binarywang.solon.wxjava.open.config.storage.WxOpenInRedissonConfigStorageConfiguration;
import com.binarywang.solon.wxjava.open.properties.WxOpenProperties;
import org.noear.solon.core.AppContext;
import org.noear.solon.core.Plugin;
/**
* @author noear 2024/9/2 created
*/
public class WxOpenPluginImpl implements Plugin {
@Override
public void start(AppContext context) throws Throwable {
context.beanMake(WxOpenProperties.class);
context.beanMake(WxOpenServiceAutoConfiguration.class);
context.beanMake(WxOpenInMemoryConfigStorageConfiguration.class);
context.beanMake(WxOpenInJedisConfigStorageConfiguration.class);
context.beanMake(WxOpenInRedissonConfigStorageConfiguration.class);
}
}

View File

@@ -0,0 +1,138 @@
package com.binarywang.solon.wxjava.open.properties;
import lombok.Data;
import org.noear.solon.annotation.Configuration;
import org.noear.solon.annotation.Inject;
import java.io.Serializable;
import static com.binarywang.solon.wxjava.open.properties.WxOpenProperties.PREFIX;
import static com.binarywang.solon.wxjava.open.properties.WxOpenProperties.StorageType.memory;
/**
* 微信接入相关配置属性.
*
* @author someone
*/
@Data
@Configuration
@Inject("${"+PREFIX+"}")
public class WxOpenProperties {
public static final String PREFIX = "wx.open";
/**
* 设置微信开放平台的appid.
*/
private String appId;
/**
* 设置微信开放平台的app secret.
*/
private String secret;
/**
* 设置微信开放平台的token.
*/
private String token;
/**
* 设置微信开放平台的EncodingAESKey.
*/
private String aesKey;
/**
* 存储策略.
*/
private ConfigStorage configStorage = new ConfigStorage();
@Data
public static class ConfigStorage implements Serializable {
private static final long serialVersionUID = 4815731027000065434L;
/**
* 存储类型.
*/
private StorageType type = memory;
/**
* 指定key前缀.
*/
private String keyPrefix = "wx:open";
/**
* redis连接配置.
*/
private WxOpenRedisProperties redis = new WxOpenRedisProperties();
/**
* http客户端类型.
*/
private HttpClientType httpClientType = HttpClientType.httpclient;
/**
* 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#setRetrySleepMillis(int)}
* {@link cn.binarywang.wx.miniapp.api.impl.BaseWxMaServiceImpl#setRetrySleepMillis(int)}
* </pre>
*/
private int retrySleepMillis = 1000;
/**
* 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;
}
public enum StorageType {
/**
* 内存.
*/
memory,
/**
* jedis.
*/
jedis,
/**
* redisson.
*/
redisson,
/**
* redistemplate
*/
redistemplate
}
public enum HttpClientType {
/**
* HttpClient.
*/
httpclient
}
}

View File

@@ -0,0 +1,45 @@
package com.binarywang.solon.wxjava.open.properties;
import lombok.Data;
import java.io.Serializable;
/**
* Redis配置.
*
* @author someone
*/
@Data
public class WxOpenRedisProperties 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;
}

View File

@@ -0,0 +1,2 @@
solon.plugin=com.binarywang.solon.wxjava.open.integration.WxOpenPluginImpl
solon.plugin.priority=10