|
1 | 1 | package com.binarywang.spring.starter.wxjava.mp.config; |
2 | 2 |
|
3 | | -import com.binarywang.spring.starter.wxjava.mp.properties.RedisProperties; |
| 3 | +import java.util.Set; |
| 4 | + |
| 5 | +import org.apache.commons.lang3.StringUtils; |
| 6 | +import org.springframework.beans.factory.annotation.Value; |
| 7 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
| 8 | +import org.springframework.context.ApplicationContext; |
| 9 | +import org.springframework.context.annotation.Bean; |
| 10 | +import org.springframework.context.annotation.Configuration; |
| 11 | +import org.springframework.data.redis.core.StringRedisTemplate; |
| 12 | + |
4 | 13 | import com.binarywang.spring.starter.wxjava.mp.enums.StorageType; |
| 14 | +import com.binarywang.spring.starter.wxjava.mp.properties.RedisProperties; |
5 | 15 | import com.binarywang.spring.starter.wxjava.mp.properties.WxMpProperties; |
| 16 | +import com.google.common.collect.Sets; |
| 17 | + |
6 | 18 | import lombok.RequiredArgsConstructor; |
7 | 19 | import me.chanjar.weixin.common.redis.JedisWxRedisOps; |
8 | 20 | import me.chanjar.weixin.common.redis.RedisTemplateWxRedisOps; |
9 | 21 | import me.chanjar.weixin.common.redis.WxRedisOps; |
| 22 | +import me.chanjar.weixin.mp.bean.WxMpHostConfig; |
10 | 23 | import me.chanjar.weixin.mp.config.WxMpConfigStorage; |
11 | 24 | import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl; |
12 | 25 | import me.chanjar.weixin.mp.config.impl.WxMpRedisConfigImpl; |
13 | | -import org.apache.commons.lang3.StringUtils; |
14 | | -import org.springframework.beans.factory.annotation.Value; |
15 | | -import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
16 | | -import org.springframework.context.ApplicationContext; |
17 | | -import org.springframework.context.annotation.Bean; |
18 | | -import org.springframework.context.annotation.Configuration; |
19 | | -import org.springframework.data.redis.core.StringRedisTemplate; |
20 | 26 | import redis.clients.jedis.JedisPool; |
| 27 | +import redis.clients.jedis.JedisPoolAbstract; |
21 | 28 | import redis.clients.jedis.JedisPoolConfig; |
| 29 | +import redis.clients.jedis.JedisSentinelPool; |
22 | 30 |
|
23 | 31 | /** |
24 | 32 | * 微信公众号存储策略自动配置. |
|
28 | 36 | @Configuration |
29 | 37 | @RequiredArgsConstructor |
30 | 38 | public class WxMpStorageAutoConfiguration { |
31 | | - private final ApplicationContext applicationContext; |
32 | | - |
33 | | - private final WxMpProperties wxMpProperties; |
34 | | - |
35 | | - @Value("${wx.mp.config-storage.redis.host:") |
36 | | - private String redisHost; |
37 | | - |
38 | | - @Value("${wx.mp.configStorage.redis.host:") |
39 | | - private String redisHost2; |
40 | | - |
41 | | - @Bean |
42 | | - @ConditionalOnMissingBean(WxMpConfigStorage.class) |
43 | | - public WxMpConfigStorage wxMpConfigStorage() { |
44 | | - StorageType type = wxMpProperties.getConfigStorage().getType(); |
45 | | - WxMpConfigStorage config; |
46 | | - switch (type) { |
47 | | - case Jedis: |
48 | | - config = jedisConfigStorage(); |
49 | | - break; |
50 | | - case RedisTemplate: |
51 | | - config = redisTemplateConfigStorage(); |
52 | | - break; |
53 | | - default: |
54 | | - config = defaultConfigStorage(); |
55 | | - break; |
56 | | - } |
57 | | - return config; |
58 | | - } |
59 | | - |
60 | | - private WxMpConfigStorage defaultConfigStorage() { |
61 | | - WxMpDefaultConfigImpl config = new WxMpDefaultConfigImpl(); |
62 | | - setWxMpInfo(config); |
63 | | - return config; |
64 | | - } |
65 | | - |
66 | | - private WxMpConfigStorage jedisConfigStorage() { |
67 | | - JedisPool jedisPool; |
68 | | - if (StringUtils.isNotEmpty(redisHost) || StringUtils.isNotEmpty(redisHost2)) { |
69 | | - jedisPool = getJedisPool(); |
70 | | - } else { |
71 | | - jedisPool = applicationContext.getBean(JedisPool.class); |
72 | | - } |
73 | | - WxRedisOps redisOps = new JedisWxRedisOps(jedisPool); |
74 | | - WxMpRedisConfigImpl wxMpRedisConfig = new WxMpRedisConfigImpl(redisOps, wxMpProperties.getConfigStorage().getKeyPrefix()); |
75 | | - setWxMpInfo(wxMpRedisConfig); |
76 | | - return wxMpRedisConfig; |
77 | | - } |
78 | | - |
79 | | - private WxMpConfigStorage redisTemplateConfigStorage() { |
80 | | - StringRedisTemplate redisTemplate = applicationContext.getBean(StringRedisTemplate.class); |
81 | | - WxRedisOps redisOps = new RedisTemplateWxRedisOps(redisTemplate); |
82 | | - WxMpRedisConfigImpl wxMpRedisConfig = new WxMpRedisConfigImpl(redisOps, wxMpProperties.getConfigStorage().getKeyPrefix()); |
83 | | - setWxMpInfo(wxMpRedisConfig); |
84 | | - return wxMpRedisConfig; |
85 | | - } |
86 | | - |
87 | | - private void setWxMpInfo(WxMpDefaultConfigImpl config) { |
88 | | - WxMpProperties properties = wxMpProperties; |
89 | | - WxMpProperties.ConfigStorage configStorageProperties = properties.getConfigStorage(); |
90 | | - config.setAppId(properties.getAppId()); |
91 | | - config.setSecret(properties.getSecret()); |
92 | | - config.setToken(properties.getToken()); |
93 | | - config.setAesKey(properties.getAesKey()); |
94 | | - |
95 | | - config.setHttpProxyHost(configStorageProperties.getHttpProxyHost()); |
96 | | - config.setHttpProxyUsername(configStorageProperties.getHttpProxyUsername()); |
97 | | - config.setHttpProxyPassword(configStorageProperties.getHttpProxyPassword()); |
98 | | - if (configStorageProperties.getHttpProxyPort() != null) { |
99 | | - config.setHttpProxyPort(configStorageProperties.getHttpProxyPort()); |
100 | | - } |
101 | | - } |
102 | | - |
103 | | - private JedisPool getJedisPool() { |
104 | | - WxMpProperties.ConfigStorage storage = wxMpProperties.getConfigStorage(); |
105 | | - RedisProperties redis = storage.getRedis(); |
106 | | - |
107 | | - JedisPoolConfig config = new JedisPoolConfig(); |
108 | | - if (redis.getMaxActive() != null) { |
109 | | - config.setMaxTotal(redis.getMaxActive()); |
110 | | - } |
111 | | - if (redis.getMaxIdle() != null) { |
112 | | - config.setMaxIdle(redis.getMaxIdle()); |
113 | | - } |
114 | | - if (redis.getMaxWaitMillis() != null) { |
115 | | - config.setMaxWaitMillis(redis.getMaxWaitMillis()); |
116 | | - } |
117 | | - if (redis.getMinIdle() != null) { |
118 | | - config.setMinIdle(redis.getMinIdle()); |
119 | | - } |
120 | | - config.setTestOnBorrow(true); |
121 | | - config.setTestWhileIdle(true); |
122 | | - |
123 | | - return new JedisPool(config, redis.getHost(), redis.getPort(), redis.getTimeout(), redis.getPassword(), |
124 | | - redis.getDatabase()); |
125 | | - } |
| 39 | + private final ApplicationContext applicationContext; |
| 40 | + |
| 41 | + private final WxMpProperties wxMpProperties; |
| 42 | + |
| 43 | + @Value("${wx.mp.config-storage.redis.host:") |
| 44 | + private String redisHost; |
| 45 | + |
| 46 | + @Value("${wx.mp.configStorage.redis.host:") |
| 47 | + private String redisHost2; |
| 48 | + |
| 49 | + @Bean |
| 50 | + @ConditionalOnMissingBean(WxMpConfigStorage.class) |
| 51 | + public WxMpConfigStorage wxMpConfigStorage() { |
| 52 | + StorageType type = wxMpProperties.getConfigStorage().getType(); |
| 53 | + WxMpConfigStorage config; |
| 54 | + switch (type) { |
| 55 | + case Jedis: |
| 56 | + config = jedisConfigStorage(); |
| 57 | + break; |
| 58 | + case RedisTemplate: |
| 59 | + config = redisTemplateConfigStorage(); |
| 60 | + break; |
| 61 | + default: |
| 62 | + config = defaultConfigStorage(); |
| 63 | + break; |
| 64 | + } |
| 65 | + // wx host config |
| 66 | + if (null != wxMpProperties.getHosts() && StringUtils.isNotEmpty(wxMpProperties.getHosts().getApiHost())) { |
| 67 | + WxMpHostConfig hostConfig = new WxMpHostConfig(); |
| 68 | + hostConfig.setApiHost(wxMpProperties.getHosts().getApiHost()); |
| 69 | + hostConfig.setMpHost(wxMpProperties.getHosts().getMpHost()); |
| 70 | + hostConfig.setOpenHost(wxMpProperties.getHosts().getOpenHost()); |
| 71 | + config.setHostConfig(hostConfig); |
| 72 | + } |
| 73 | + return config; |
| 74 | + } |
| 75 | + |
| 76 | + private WxMpConfigStorage defaultConfigStorage() { |
| 77 | + WxMpDefaultConfigImpl config = new WxMpDefaultConfigImpl(); |
| 78 | + setWxMpInfo(config); |
| 79 | + return config; |
| 80 | + } |
| 81 | + |
| 82 | + private WxMpConfigStorage jedisConfigStorage() { |
| 83 | + JedisPoolAbstract jedisPool; |
| 84 | + if (StringUtils.isNotEmpty(redisHost) || StringUtils.isNotEmpty(redisHost2)) { |
| 85 | + jedisPool = getJedisPool(); |
| 86 | + } else { |
| 87 | + jedisPool = applicationContext.getBean(JedisPool.class); |
| 88 | + } |
| 89 | + WxRedisOps redisOps = new JedisWxRedisOps(jedisPool); |
| 90 | + WxMpRedisConfigImpl wxMpRedisConfig = new WxMpRedisConfigImpl(redisOps, |
| 91 | + wxMpProperties.getConfigStorage().getKeyPrefix()); |
| 92 | + setWxMpInfo(wxMpRedisConfig); |
| 93 | + return wxMpRedisConfig; |
| 94 | + } |
| 95 | + |
| 96 | + private WxMpConfigStorage redisTemplateConfigStorage() { |
| 97 | + StringRedisTemplate redisTemplate = applicationContext.getBean(StringRedisTemplate.class); |
| 98 | + WxRedisOps redisOps = new RedisTemplateWxRedisOps(redisTemplate); |
| 99 | + WxMpRedisConfigImpl wxMpRedisConfig = new WxMpRedisConfigImpl(redisOps, |
| 100 | + wxMpProperties.getConfigStorage().getKeyPrefix()); |
| 101 | + setWxMpInfo(wxMpRedisConfig); |
| 102 | + return wxMpRedisConfig; |
| 103 | + } |
| 104 | + |
| 105 | + private void setWxMpInfo(WxMpDefaultConfigImpl config) { |
| 106 | + WxMpProperties properties = wxMpProperties; |
| 107 | + WxMpProperties.ConfigStorage configStorageProperties = properties.getConfigStorage(); |
| 108 | + config.setAppId(properties.getAppId()); |
| 109 | + config.setSecret(properties.getSecret()); |
| 110 | + config.setToken(properties.getToken()); |
| 111 | + config.setAesKey(properties.getAesKey()); |
| 112 | + |
| 113 | + config.setHttpProxyHost(configStorageProperties.getHttpProxyHost()); |
| 114 | + config.setHttpProxyUsername(configStorageProperties.getHttpProxyUsername()); |
| 115 | + config.setHttpProxyPassword(configStorageProperties.getHttpProxyPassword()); |
| 116 | + if (configStorageProperties.getHttpProxyPort() != null) { |
| 117 | + config.setHttpProxyPort(configStorageProperties.getHttpProxyPort()); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + private JedisPoolAbstract getJedisPool() { |
| 122 | + WxMpProperties.ConfigStorage storage = wxMpProperties.getConfigStorage(); |
| 123 | + RedisProperties redis = storage.getRedis(); |
| 124 | + |
| 125 | + JedisPoolConfig config = new JedisPoolConfig(); |
| 126 | + if (redis.getMaxActive() != null) { |
| 127 | + config.setMaxTotal(redis.getMaxActive()); |
| 128 | + } |
| 129 | + if (redis.getMaxIdle() != null) { |
| 130 | + config.setMaxIdle(redis.getMaxIdle()); |
| 131 | + } |
| 132 | + if (redis.getMaxWaitMillis() != null) { |
| 133 | + config.setMaxWaitMillis(redis.getMaxWaitMillis()); |
| 134 | + } |
| 135 | + if (redis.getMinIdle() != null) { |
| 136 | + config.setMinIdle(redis.getMinIdle()); |
| 137 | + } |
| 138 | + config.setTestOnBorrow(true); |
| 139 | + config.setTestWhileIdle(true); |
| 140 | + if (StringUtils.isNotEmpty(redis.getSentinelIps())) { |
| 141 | + Set<String> sentinels = Sets.newHashSet(redis.getSentinelIps().split(",")); |
| 142 | + return new JedisSentinelPool(redis.getSentinelName(), sentinels); |
| 143 | + } |
| 144 | + |
| 145 | + return new JedisPool(config, redis.getHost(), redis.getPort(), redis.getTimeout(), redis.getPassword(), |
| 146 | + redis.getDatabase()); |
| 147 | + } |
126 | 148 | } |
0 commit comments