Skip to content

Commit dc01e0b

Browse files
authored
🎨 #1516 公众号spring-boot-starter 优化代码,增加http客户端和代理等配置
1 parent 2112db7 commit dc01e0b

File tree

8 files changed

+269
-125
lines changed

8 files changed

+269
-125
lines changed

spring-boot-starters/wx-java-mp-spring-boot-starter/README.md

+16-9
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,25 @@
1111
2. 添加配置(application.properties)
1212
```properties
1313
# 公众号配置(必填)
14-
wx.mp.appId = @appId
15-
wx.mp.secret = @secret
16-
wx.mp.token = @token
17-
wx.mp.aesKey = @aesKey
18-
# 存储配置redis(可选)
19-
wx.mp.config-storage.type = redis
20-
wx.mp.config-storage.redis.host = 127.0.0.1
21-
wx.mp.config-storage.redis.port = 6379
14+
wx.mp.appId = appId
15+
wx.mp.secret = @secret
16+
wx.mp.token = @token
17+
wx.mp.aesKey = @aesKey
18+
# 存储配置redis(可选)
19+
wx.mp.config-storage.type = redis # 配置类型: memory(默认), redis, jedis, redistemplate
20+
wx.mp.config-storage.key-prefix = wx # 相关redis前缀配置: wx(默认)
21+
wx.mp.config-storage.redis.host = 127.0.0.1
22+
wx.mp.config-storage.redis.port = 6379
23+
# http客户端配置
24+
wx.mp.config-storage.http-client-type=httpclient # http客户端类型: httpclient(默认), okhttp, joddhttp
25+
wx.mp.config-storage.http-proxy-host=
26+
wx.mp.config-storage.http-proxy-port=
27+
wx.mp.config-storage.http-proxy-username=
28+
wx.mp.config-storage.http-proxy-password=
2229
```
2330
3. 支持自动注入的类型
2431

25-
`WxMpService`以及相关的服务类, 比如: `wxMpService.getXxxService`。
32+
`WxMpService`以及~~相关的服务类, 比如: `wxMpService.getXxxService`。~~
2633

2734

2835

spring-boot-starters/wx-java-mp-spring-boot-starter/pom.xml

+4-4
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525
<scope>compile</scope>
2626
</dependency>
2727
<dependency>
28-
<groupId>org.redisson</groupId>
29-
<artifactId>redisson</artifactId>
30-
<scope>compile</scope>
31-
<optional>true</optional>
28+
<groupId>org.springframework.data</groupId>
29+
<artifactId>spring-data-redis</artifactId>
30+
<version>${spring.boot.version}</version>
31+
<scope>provided</scope>
3232
</dependency>
3333
</dependencies>
3434

spring-boot-starters/wx-java-mp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/mp/config/WxMpServiceAutoConfiguration.java

+54-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package com.binarywang.spring.starter.wxjava.mp.config;
22

3+
import com.binarywang.spring.starter.wxjava.mp.properties.WxMpProperties;
34
import me.chanjar.weixin.mp.api.*;
5+
import me.chanjar.weixin.mp.api.impl.WxMpServiceHttpClientImpl;
46
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
7+
import me.chanjar.weixin.mp.api.impl.WxMpServiceJoddHttpImpl;
8+
import me.chanjar.weixin.mp.api.impl.WxMpServiceOkHttpImpl;
59
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
610
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
711
import org.springframework.context.annotation.Bean;
@@ -17,113 +21,161 @@ public class WxMpServiceAutoConfiguration {
1721

1822
@Bean
1923
@ConditionalOnMissingBean
20-
public WxMpService wxMpService(WxMpConfigStorage configStorage) {
21-
WxMpService wxMpService = new WxMpServiceImpl();
24+
public WxMpService wxMpService(WxMpConfigStorage configStorage, WxMpProperties wxMpProperties) {
25+
WxMpProperties.HttpClientType httpClientType = wxMpProperties.getConfigStorage().getHttpClientType();
26+
WxMpService wxMpService;
27+
if (httpClientType == WxMpProperties.HttpClientType.okhttp) {
28+
wxMpService = newWxMpServiceJoddHttpImpl();
29+
} else if (httpClientType == WxMpProperties.HttpClientType.joddhttp) {
30+
wxMpService = newWxMpServiceOkHttpImpl();
31+
} else if (httpClientType == WxMpProperties.HttpClientType.httpclient) {
32+
wxMpService = newWxMpServiceHttpClientImpl();
33+
} else {
34+
wxMpService = newWxMpServiceImpl();
35+
}
36+
2237
wxMpService.setWxMpConfigStorage(configStorage);
2338
return wxMpService;
2439
}
2540

41+
private WxMpService newWxMpServiceImpl() {
42+
return new WxMpServiceImpl();
43+
}
44+
45+
private WxMpService newWxMpServiceHttpClientImpl() {
46+
return new WxMpServiceHttpClientImpl();
47+
}
48+
49+
private WxMpService newWxMpServiceOkHttpImpl() {
50+
return new WxMpServiceOkHttpImpl();
51+
}
52+
53+
private WxMpService newWxMpServiceJoddHttpImpl() {
54+
return new WxMpServiceJoddHttpImpl();
55+
}
56+
2657
@Bean
58+
@Deprecated
2759
public WxMpKefuService wxMpKefuService(WxMpService wxMpService) {
2860
return wxMpService.getKefuService();
2961
}
3062

3163
@Bean
64+
@Deprecated
3265
public WxMpMaterialService wxMpMaterialService(WxMpService wxMpService) {
3366
return wxMpService.getMaterialService();
3467
}
3568

3669
@Bean
70+
@Deprecated
3771
public WxMpMenuService wxMpMenuService(WxMpService wxMpService) {
3872
return wxMpService.getMenuService();
3973
}
4074

4175
@Bean
76+
@Deprecated
4277
public WxMpUserService wxMpUserService(WxMpService wxMpService) {
4378
return wxMpService.getUserService();
4479
}
4580

4681
@Bean
82+
@Deprecated
4783
public WxMpUserTagService wxMpUserTagService(WxMpService wxMpService) {
4884
return wxMpService.getUserTagService();
4985
}
5086

5187
@Bean
88+
@Deprecated
5289
public WxMpQrcodeService wxMpQrcodeService(WxMpService wxMpService) {
5390
return wxMpService.getQrcodeService();
5491
}
5592

5693
@Bean
94+
@Deprecated
5795
public WxMpCardService wxMpCardService(WxMpService wxMpService) {
5896
return wxMpService.getCardService();
5997
}
6098

6199
@Bean
100+
@Deprecated
62101
public WxMpDataCubeService wxMpDataCubeService(WxMpService wxMpService) {
63102
return wxMpService.getDataCubeService();
64103
}
65104

66105
@Bean
106+
@Deprecated
67107
public WxMpUserBlacklistService wxMpUserBlacklistService(WxMpService wxMpService) {
68108
return wxMpService.getBlackListService();
69109
}
70110

71111
@Bean
112+
@Deprecated
72113
public WxMpStoreService wxMpStoreService(WxMpService wxMpService) {
73114
return wxMpService.getStoreService();
74115
}
75116

76117
@Bean
118+
@Deprecated
77119
public WxMpTemplateMsgService wxMpTemplateMsgService(WxMpService wxMpService) {
78120
return wxMpService.getTemplateMsgService();
79121
}
80122

81123
@Bean
124+
@Deprecated
82125
public WxMpSubscribeMsgService wxMpSubscribeMsgService(WxMpService wxMpService) {
83126
return wxMpService.getSubscribeMsgService();
84127
}
85128

86129
@Bean
130+
@Deprecated
87131
public WxMpDeviceService wxMpDeviceService(WxMpService wxMpService) {
88132
return wxMpService.getDeviceService();
89133
}
90134

91135
@Bean
136+
@Deprecated
92137
public WxMpShakeService wxMpShakeService(WxMpService wxMpService) {
93138
return wxMpService.getShakeService();
94139
}
95140

96141
@Bean
142+
@Deprecated
97143
public WxMpMemberCardService wxMpMemberCardService(WxMpService wxMpService) {
98144
return wxMpService.getMemberCardService();
99145
}
100146

101147
@Bean
148+
@Deprecated
102149
public WxMpMassMessageService wxMpMassMessageService(WxMpService wxMpService) {
103150
return wxMpService.getMassMessageService();
104151
}
105152

106153
@Bean
154+
@Deprecated
107155
public WxMpAiOpenService wxMpAiOpenService(WxMpService wxMpService) {
108156
return wxMpService.getAiOpenService();
109157
}
110158

111159
@Bean
160+
@Deprecated
112161
public WxMpWifiService wxMpWifiService(WxMpService wxMpService) {
113162
return wxMpService.getWifiService();
114163
}
115164

116165
@Bean
166+
@Deprecated
117167
public WxMpMarketingService wxMpMarketingService(WxMpService wxMpService) {
118168
return wxMpService.getMarketingService();
119169
}
120170

121171
@Bean
172+
@Deprecated
122173
public WxMpCommentService wxMpCommentService(WxMpService wxMpService) {
123174
return wxMpService.getCommentService();
124175
}
125176

126177
@Bean
178+
@Deprecated
127179
public WxMpOcrService wxMpOcrService(WxMpService wxMpService) {
128180
return wxMpService.getOcrService();
129181
}
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
package com.binarywang.spring.starter.wxjava.mp.config;
22

3-
import com.binarywang.spring.starter.wxjava.mp.properties.RedisProperties;
3+
import com.binarywang.spring.starter.wxjava.mp.extend.RedisTemplateWxMpRedisOps;
44
import com.binarywang.spring.starter.wxjava.mp.properties.WxMpProperties;
55
import lombok.RequiredArgsConstructor;
66
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
77
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
88
import me.chanjar.weixin.mp.config.impl.WxMpRedisConfigImpl;
9+
import me.chanjar.weixin.mp.config.redis.JedisWxMpRedisOps;
10+
import me.chanjar.weixin.mp.config.redis.WxMpRedisOps;
11+
import org.apache.commons.lang3.StringUtils;
12+
import org.springframework.beans.factory.annotation.Value;
913
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
1014
import org.springframework.context.ApplicationContext;
1115
import org.springframework.context.annotation.Bean;
1216
import org.springframework.context.annotation.Configuration;
17+
import org.springframework.data.redis.core.StringRedisTemplate;
1318
import redis.clients.jedis.JedisPool;
1419
import redis.clients.jedis.JedisPoolConfig;
1520

@@ -22,55 +27,77 @@
2227
@RequiredArgsConstructor
2328
public class WxMpStorageAutoConfiguration {
2429

25-
private final WxMpProperties properties;
2630
private final ApplicationContext applicationContext;
2731

32+
private final WxMpProperties wxMpProperties;
33+
34+
@Value("${wx.mp.config-storage.redis.host:")
35+
private String redisHost;
36+
37+
@Value("${wx.mp.configStorage.redis.host:")
38+
private String redisHost2;
39+
2840
@Bean
2941
@ConditionalOnMissingBean(WxMpConfigStorage.class)
30-
public WxMpConfigStorage wxMpInMemoryConfigStorage() {
31-
WxMpProperties.ConfigStorage storage = properties.getConfigStorage();
32-
WxMpProperties.StorageType type = storage.getType();
33-
34-
if (type == WxMpProperties.StorageType.redis) {
35-
return getWxMpInRedisConfigStorage();
42+
public WxMpConfigStorage wxMpConfigStorage() {
43+
WxMpProperties.StorageType type = wxMpProperties.getConfigStorage().getType();
44+
WxMpConfigStorage config;
45+
if (type == WxMpProperties.StorageType.redis || type == WxMpProperties.StorageType.jedis) {
46+
config = wxMpInJedisConfigStorage();
47+
} else if (type == WxMpProperties.StorageType.redistemplate) {
48+
config = wxMpInRedisTemplateConfigStorage();
49+
} else {
50+
config = wxMpInMemoryConfigStorage();
3651
}
37-
return getWxMpInMemoryConfigStorage();
52+
return config;
3853
}
3954

40-
private WxMpDefaultConfigImpl getWxMpInMemoryConfigStorage() {
55+
private WxMpConfigStorage wxMpInMemoryConfigStorage() {
4156
WxMpDefaultConfigImpl config = new WxMpDefaultConfigImpl();
4257
setWxMpInfo(config);
4358
return config;
4459
}
4560

46-
private WxMpRedisConfigImpl getWxMpInRedisConfigStorage() {
47-
RedisProperties.ImplType implType = properties.getConfigStorage().getRedis().getImpl();
48-
boolean reuseBean = properties.getConfigStorage().getRedis().isReuseBean();
49-
if (implType == RedisProperties.ImplType.jedis) {
50-
JedisPool pool = null;
51-
if (reuseBean) {
52-
pool = getBean(JedisPool.class);
53-
}
54-
if (pool == null) {
55-
pool = getJedisPool();
56-
}
57-
WxMpRedisConfigImpl config = new WxMpRedisConfigImpl(pool);
58-
setWxMpInfo(config);
59-
return config;
61+
private WxMpConfigStorage wxMpInJedisConfigStorage() {
62+
JedisPool jedisPool;
63+
if (StringUtils.isNotEmpty(redisHost) || StringUtils.isNotEmpty(redisHost2)) {
64+
jedisPool = getJedisPool();
65+
} else {
66+
jedisPool = applicationContext.getBean(JedisPool.class);
6067
}
61-
throw new UnsupportedOperationException();
68+
WxMpRedisOps redisOps = new JedisWxMpRedisOps(jedisPool);
69+
WxMpRedisConfigImpl wxMpRedisConfig = new WxMpRedisConfigImpl(redisOps, wxMpProperties.getConfigStorage().getKeyPrefix());
70+
setWxMpInfo(wxMpRedisConfig);
71+
return wxMpRedisConfig;
72+
}
73+
74+
private WxMpConfigStorage wxMpInRedisTemplateConfigStorage() {
75+
StringRedisTemplate redisTemplate = applicationContext.getBean(StringRedisTemplate.class);
76+
WxMpRedisOps redisOps = new RedisTemplateWxMpRedisOps(redisTemplate);
77+
WxMpRedisConfigImpl wxMpRedisConfig = new WxMpRedisConfigImpl(redisOps, wxMpProperties.getConfigStorage().getKeyPrefix());
78+
setWxMpInfo(wxMpRedisConfig);
79+
return wxMpRedisConfig;
6280
}
6381

6482
private void setWxMpInfo(WxMpDefaultConfigImpl config) {
83+
WxMpProperties properties = wxMpProperties;
84+
WxMpProperties.ConfigStorage configStorageProperties = properties.getConfigStorage();
6585
config.setAppId(properties.getAppId());
6686
config.setSecret(properties.getSecret());
6787
config.setToken(properties.getToken());
6888
config.setAesKey(properties.getAesKey());
89+
90+
config.setHttpProxyHost(configStorageProperties.getHttpProxyHost());
91+
config.setHttpProxyUsername(configStorageProperties.getHttpProxyUsername());
92+
config.setHttpProxyPassword(configStorageProperties.getHttpProxyPassword());
93+
if (configStorageProperties.getHttpProxyPort() != null) {
94+
config.setHttpProxyPort(configStorageProperties.getHttpProxyPort());
95+
}
6996
}
7097

7198
private JedisPool getJedisPool() {
72-
WxMpProperties.ConfigStorage storage = properties.getConfigStorage();
73-
RedisProperties redis = storage.getRedis();
99+
WxMpProperties.ConfigStorage storage = wxMpProperties.getConfigStorage();
100+
WxMpProperties.RedisProperties redis = storage.getRedis();
74101

75102
JedisPoolConfig config = new JedisPoolConfig();
76103
if (redis.getMaxActive() != null) {
@@ -91,11 +118,4 @@ private JedisPool getJedisPool() {
91118
return new JedisPool(config, redis.getHost(), redis.getPort(), redis.getTimeout(), redis.getPassword(),
92119
redis.getDatabase());
93120
}
94-
95-
private <T> T getBean(Class<T> clazz) {
96-
if (this.applicationContext.getBeanNamesForType(clazz, false, false).length > 0) {
97-
return this.applicationContext.getBean(clazz);
98-
}
99-
return null;
100-
}
101121
}

0 commit comments

Comments
 (0)