Browse Source

:art: #1516 公众号spring-boot-starter 优化代码,增加http客户端和代理等配置

Mario Luo 5 years ago
parent
commit
dc01e0b1ed

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

@@ -11,18 +11,25 @@
 2. 添加配置(application.properties)
     ```properties
     # 公众号配置(必填)
-    wx.mp.appId = @appId
-	wx.mp.secret = @secret
-	wx.mp.token = @token
-	wx.mp.aesKey = @aesKey
-	# 存储配置redis(可选)
-	wx.mp.config-storage.type = redis
-	wx.mp.config-storage.redis.host = 127.0.0.1
-	wx.mp.config-storage.redis.port = 6379
+    wx.mp.appId = appId
+    wx.mp.secret = @secret
+    wx.mp.token = @token
+    wx.mp.aesKey = @aesKey
+    # 存储配置redis(可选)
+    wx.mp.config-storage.type = redis                     # 配置类型: memory(默认), redis, jedis, redistemplate
+    wx.mp.config-storage.key-prefix = wx                  # 相关redis前缀配置: wx(默认)
+    wx.mp.config-storage.redis.host = 127.0.0.1
+    wx.mp.config-storage.redis.port = 6379
+    # http客户端配置
+    wx.mp.config-storage.http-client-type=httpclient      # http客户端类型: httpclient(默认), okhttp, joddhttp
+    wx.mp.config-storage.http-proxy-host=
+    wx.mp.config-storage.http-proxy-port=
+    wx.mp.config-storage.http-proxy-username=
+    wx.mp.config-storage.http-proxy-password=
     ```
 3. 支持自动注入的类型
 
-`WxMpService`以及相关的服务类, 比如: `wxMpService.getXxxService`。
+`WxMpService`以及~~相关的服务类, 比如: `wxMpService.getXxxService`。~~
 
 
 

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

@@ -25,10 +25,10 @@
       <scope>compile</scope>
     </dependency>
     <dependency>
-      <groupId>org.redisson</groupId>
-      <artifactId>redisson</artifactId>
-      <scope>compile</scope>
-      <optional>true</optional>
+      <groupId>org.springframework.data</groupId>
+      <artifactId>spring-data-redis</artifactId>
+      <version>${spring.boot.version}</version>
+      <scope>provided</scope>
     </dependency>
   </dependencies>
 

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

@@ -1,7 +1,11 @@
 package com.binarywang.spring.starter.wxjava.mp.config;
 
+import com.binarywang.spring.starter.wxjava.mp.properties.WxMpProperties;
 import me.chanjar.weixin.mp.api.*;
+import me.chanjar.weixin.mp.api.impl.WxMpServiceHttpClientImpl;
 import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
+import me.chanjar.weixin.mp.api.impl.WxMpServiceJoddHttpImpl;
+import me.chanjar.weixin.mp.api.impl.WxMpServiceOkHttpImpl;
 import me.chanjar.weixin.mp.config.WxMpConfigStorage;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
 import org.springframework.context.annotation.Bean;
@@ -17,113 +21,161 @@ public class WxMpServiceAutoConfiguration {
 
   @Bean
   @ConditionalOnMissingBean
-  public WxMpService wxMpService(WxMpConfigStorage configStorage) {
-    WxMpService wxMpService = new WxMpServiceImpl();
+  public WxMpService wxMpService(WxMpConfigStorage configStorage, WxMpProperties wxMpProperties) {
+    WxMpProperties.HttpClientType httpClientType = wxMpProperties.getConfigStorage().getHttpClientType();
+    WxMpService wxMpService;
+    if (httpClientType == WxMpProperties.HttpClientType.okhttp) {
+      wxMpService = newWxMpServiceJoddHttpImpl();
+    } else if (httpClientType == WxMpProperties.HttpClientType.joddhttp) {
+      wxMpService = newWxMpServiceOkHttpImpl();
+    } else if (httpClientType == WxMpProperties.HttpClientType.httpclient) {
+      wxMpService = newWxMpServiceHttpClientImpl();
+    } else {
+      wxMpService = newWxMpServiceImpl();
+    }
+
     wxMpService.setWxMpConfigStorage(configStorage);
     return wxMpService;
   }
 
+  private WxMpService newWxMpServiceImpl() {
+    return new WxMpServiceImpl();
+  }
+
+  private WxMpService newWxMpServiceHttpClientImpl() {
+    return new WxMpServiceHttpClientImpl();
+  }
+
+  private WxMpService newWxMpServiceOkHttpImpl() {
+    return new WxMpServiceOkHttpImpl();
+  }
+
+  private WxMpService newWxMpServiceJoddHttpImpl() {
+    return new WxMpServiceJoddHttpImpl();
+  }
+
   @Bean
+  @Deprecated
   public WxMpKefuService wxMpKefuService(WxMpService wxMpService) {
     return wxMpService.getKefuService();
   }
 
   @Bean
+  @Deprecated
   public WxMpMaterialService wxMpMaterialService(WxMpService wxMpService) {
     return wxMpService.getMaterialService();
   }
 
   @Bean
+  @Deprecated
   public WxMpMenuService wxMpMenuService(WxMpService wxMpService) {
     return wxMpService.getMenuService();
   }
 
   @Bean
+  @Deprecated
   public WxMpUserService wxMpUserService(WxMpService wxMpService) {
     return wxMpService.getUserService();
   }
 
   @Bean
+  @Deprecated
   public WxMpUserTagService wxMpUserTagService(WxMpService wxMpService) {
     return wxMpService.getUserTagService();
   }
 
   @Bean
+  @Deprecated
   public WxMpQrcodeService wxMpQrcodeService(WxMpService wxMpService) {
     return wxMpService.getQrcodeService();
   }
 
   @Bean
+  @Deprecated
   public WxMpCardService wxMpCardService(WxMpService wxMpService) {
     return wxMpService.getCardService();
   }
 
   @Bean
+  @Deprecated
   public WxMpDataCubeService wxMpDataCubeService(WxMpService wxMpService) {
     return wxMpService.getDataCubeService();
   }
 
   @Bean
+  @Deprecated
   public WxMpUserBlacklistService wxMpUserBlacklistService(WxMpService wxMpService) {
     return wxMpService.getBlackListService();
   }
 
   @Bean
+  @Deprecated
   public WxMpStoreService wxMpStoreService(WxMpService wxMpService) {
     return wxMpService.getStoreService();
   }
 
   @Bean
+  @Deprecated
   public WxMpTemplateMsgService wxMpTemplateMsgService(WxMpService wxMpService) {
     return wxMpService.getTemplateMsgService();
   }
 
   @Bean
+  @Deprecated
   public WxMpSubscribeMsgService wxMpSubscribeMsgService(WxMpService wxMpService) {
     return wxMpService.getSubscribeMsgService();
   }
 
   @Bean
+  @Deprecated
   public WxMpDeviceService wxMpDeviceService(WxMpService wxMpService) {
     return wxMpService.getDeviceService();
   }
 
   @Bean
+  @Deprecated
   public WxMpShakeService wxMpShakeService(WxMpService wxMpService) {
     return wxMpService.getShakeService();
   }
 
   @Bean
+  @Deprecated
   public WxMpMemberCardService wxMpMemberCardService(WxMpService wxMpService) {
     return wxMpService.getMemberCardService();
   }
 
   @Bean
+  @Deprecated
   public WxMpMassMessageService wxMpMassMessageService(WxMpService wxMpService) {
     return wxMpService.getMassMessageService();
   }
 
   @Bean
+  @Deprecated
   public WxMpAiOpenService wxMpAiOpenService(WxMpService wxMpService) {
     return wxMpService.getAiOpenService();
   }
 
   @Bean
+  @Deprecated
   public WxMpWifiService wxMpWifiService(WxMpService wxMpService) {
     return wxMpService.getWifiService();
   }
 
   @Bean
+  @Deprecated
   public WxMpMarketingService wxMpMarketingService(WxMpService wxMpService) {
     return wxMpService.getMarketingService();
   }
 
   @Bean
+  @Deprecated
   public WxMpCommentService wxMpCommentService(WxMpService wxMpService) {
     return wxMpService.getCommentService();
   }
 
   @Bean
+  @Deprecated
   public WxMpOcrService wxMpOcrService(WxMpService wxMpService) {
     return wxMpService.getOcrService();
   }

+ 54 - 34
spring-boot-starters/wx-java-mp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/mp/config/WxMpStorageAutoConfiguration.java

@@ -1,15 +1,20 @@
 package com.binarywang.spring.starter.wxjava.mp.config;
 
-import com.binarywang.spring.starter.wxjava.mp.properties.RedisProperties;
+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.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;
 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;
 
@@ -22,55 +27,77 @@ import redis.clients.jedis.JedisPoolConfig;
 @RequiredArgsConstructor
 public class WxMpStorageAutoConfiguration {
 
-  private final WxMpProperties properties;
   private final ApplicationContext applicationContext;
 
+  private final WxMpProperties wxMpProperties;
+
+  @Value("${wx.mp.config-storage.redis.host:")
+  private String redisHost;
+
+  @Value("${wx.mp.configStorage.redis.host:")
+  private String redisHost2;
+
   @Bean
   @ConditionalOnMissingBean(WxMpConfigStorage.class)
-  public WxMpConfigStorage wxMpInMemoryConfigStorage() {
-    WxMpProperties.ConfigStorage storage = properties.getConfigStorage();
-    WxMpProperties.StorageType type = storage.getType();
-
-    if (type == WxMpProperties.StorageType.redis) {
-      return getWxMpInRedisConfigStorage();
+  public WxMpConfigStorage wxMpConfigStorage() {
+    WxMpProperties.StorageType type = wxMpProperties.getConfigStorage().getType();
+    WxMpConfigStorage config;
+    if (type == WxMpProperties.StorageType.redis || type == WxMpProperties.StorageType.jedis) {
+      config = wxMpInJedisConfigStorage();
+    } else if (type == WxMpProperties.StorageType.redistemplate) {
+      config = wxMpInRedisTemplateConfigStorage();
+    } else {
+      config = wxMpInMemoryConfigStorage();
     }
-    return getWxMpInMemoryConfigStorage();
+    return config;
   }
 
-  private WxMpDefaultConfigImpl getWxMpInMemoryConfigStorage() {
+  private WxMpConfigStorage wxMpInMemoryConfigStorage() {
     WxMpDefaultConfigImpl config = new WxMpDefaultConfigImpl();
     setWxMpInfo(config);
     return config;
   }
 
-  private WxMpRedisConfigImpl getWxMpInRedisConfigStorage() {
-    RedisProperties.ImplType implType = properties.getConfigStorage().getRedis().getImpl();
-    boolean reuseBean = properties.getConfigStorage().getRedis().isReuseBean();
-    if (implType == RedisProperties.ImplType.jedis) {
-      JedisPool pool = null;
-      if (reuseBean) {
-        pool = getBean(JedisPool.class);
-      }
-      if (pool == null) {
-        pool = getJedisPool();
-      }
-      WxMpRedisConfigImpl config = new WxMpRedisConfigImpl(pool);
-      setWxMpInfo(config);
-      return config;
+  private WxMpConfigStorage wxMpInJedisConfigStorage() {
+    JedisPool jedisPool;
+    if (StringUtils.isNotEmpty(redisHost) || StringUtils.isNotEmpty(redisHost2)) {
+      jedisPool = getJedisPool();
+    } else {
+      jedisPool = applicationContext.getBean(JedisPool.class);
     }
-    throw new UnsupportedOperationException();
+    WxMpRedisOps redisOps = new JedisWxMpRedisOps(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);
+    WxMpRedisConfigImpl wxMpRedisConfig = new WxMpRedisConfigImpl(redisOps, wxMpProperties.getConfigStorage().getKeyPrefix());
+    setWxMpInfo(wxMpRedisConfig);
+    return wxMpRedisConfig;
   }
 
   private void setWxMpInfo(WxMpDefaultConfigImpl config) {
+    WxMpProperties properties = wxMpProperties;
+    WxMpProperties.ConfigStorage configStorageProperties = properties.getConfigStorage();
     config.setAppId(properties.getAppId());
     config.setSecret(properties.getSecret());
     config.setToken(properties.getToken());
     config.setAesKey(properties.getAesKey());
+
+    config.setHttpProxyHost(configStorageProperties.getHttpProxyHost());
+    config.setHttpProxyUsername(configStorageProperties.getHttpProxyUsername());
+    config.setHttpProxyPassword(configStorageProperties.getHttpProxyPassword());
+    if (configStorageProperties.getHttpProxyPort() != null) {
+      config.setHttpProxyPort(configStorageProperties.getHttpProxyPort());
+    }
   }
 
   private JedisPool getJedisPool() {
-    WxMpProperties.ConfigStorage storage = properties.getConfigStorage();
-    RedisProperties redis = storage.getRedis();
+    WxMpProperties.ConfigStorage storage = wxMpProperties.getConfigStorage();
+    WxMpProperties.RedisProperties redis = storage.getRedis();
 
     JedisPoolConfig config = new JedisPoolConfig();
     if (redis.getMaxActive() != null) {
@@ -91,11 +118,4 @@ public class WxMpStorageAutoConfiguration {
     return new JedisPool(config, redis.getHost(), redis.getPort(), redis.getTimeout(), redis.getPassword(),
       redis.getDatabase());
   }
-
-  private <T> T getBean(Class<T> clazz) {
-    if (this.applicationContext.getBeanNamesForType(clazz, false, false).length > 0) {
-      return this.applicationContext.getBean(clazz);
-    }
-    return null;
-  }
 }

+ 40 - 0
spring-boot-starters/wx-java-mp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/mp/extend/RedisTemplateWxMpRedisOps.java

@@ -0,0 +1,40 @@
+package com.binarywang.spring.starter.wxjava.mp.extend;
+
+import lombok.RequiredArgsConstructor;
+import me.chanjar.weixin.mp.config.redis.BaseWxMpRedisOps;
+import org.springframework.data.redis.core.StringRedisTemplate;
+
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
+
+@RequiredArgsConstructor
+public class RedisTemplateWxMpRedisOps extends BaseWxMpRedisOps {
+
+  private final StringRedisTemplate redisTemplate;
+
+  @Override
+  public String getValue(String key) {
+    return redisTemplate.opsForValue().get(key);
+  }
+
+  @Override
+  public void setValue(String key, String value, int expire, TimeUnit timeUnit) {
+    redisTemplate.opsForValue().set(key, value, expire, timeUnit);
+  }
+
+  @Override
+  public Long getExpire(String key) {
+    return redisTemplate.getExpire(key);
+  }
+
+  @Override
+  public void expire(String key, int expire, TimeUnit timeUnit) {
+    redisTemplate.expire(key, expire, timeUnit);
+  }
+
+  @Override
+  public Lock getLock(String key) {
+    return new ReentrantLock();
+  }
+}

+ 0 - 66
spring-boot-starters/wx-java-mp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/mp/properties/RedisProperties.java

@@ -1,66 +0,0 @@
-package com.binarywang.spring.starter.wxjava.mp.properties;
-
-import lombok.Data;
-
-import java.io.Serializable;
-
-/**
- * Redis配置.
- *
- * @author someone
- */
-@Data
-public class RedisProperties implements Serializable {
-  private static final long serialVersionUID = -5924815351660074401L;
-
-  /**
-   * 操作Redis的实现
-   */
-  private ImplType impl = ImplType.jedis;
-
-  /**
-   * 操作Redis的实现如果在spring容器里是否直接使用
-   */
-  private boolean reuseBean = true;
-
-  /**
-   * 主机地址.
-   */
-  private String host = "127.0.0.1";
-
-  /**
-   * 端口号.
-   */
-  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;
-
-  public enum ImplType {
-    /**
-     * jedis.
-     */
-    jedis,
-    /**
-     * redisson.
-     */
-//    redisson
-  }
-}

+ 101 - 8
spring-boot-starters/wx-java-mp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/mp/properties/WxMpProperties.java

@@ -2,7 +2,6 @@ package com.binarywang.spring.starter.wxjava.mp.properties;
 
 import lombok.Data;
 import org.springframework.boot.context.properties.ConfigurationProperties;
-import org.springframework.boot.context.properties.NestedConfigurationProperty;
 
 import java.io.Serializable;
 
@@ -41,19 +40,54 @@ public class WxMpProperties {
   private String aesKey;
 
   /**
-   * 存储策略, memory, redis.
+   * 存储策略
    */
-  private final ConfigStorage configStorage = new ConfigStorage();
-
+  private ConfigStorage configStorage = new ConfigStorage();
 
   @Data
   public static class ConfigStorage implements Serializable {
     private static final long serialVersionUID = 4815731027000065434L;
 
+    /**
+     * 存储类型.
+     */
     private StorageType type = memory;
 
-    @NestedConfigurationProperty
-    private final RedisProperties redis = new RedisProperties();
+    /**
+     * 指定key前缀.
+     */
+    private String keyPrefix = "wx";
+
+    /**
+     * redis连接配置.
+     */
+    private RedisProperties redis = new RedisProperties();
+
+    /**
+     * 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 {
@@ -62,8 +96,67 @@ public class WxMpProperties {
      */
     memory,
     /**
-     * redis.
+     * jedis.
+     */
+    redis,
+    /**
+     * redis(JedisClient).
+     */
+    jedis,
+    /**
+     * redis(RedisTemplate).
      */
-    redis
+    redistemplate
   }
+
+  public enum HttpClientType {
+    /**
+     * HttpClient.
+     */
+    httpclient,
+    /**
+     * OkHttp.
+     */
+    okhttp,
+    /**
+     * JoddHttp.
+     */
+    joddhttp
+  }
+
+  @Data
+  public static class RedisProperties 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;
+
+    private Integer maxActive;
+    private Integer maxIdle;
+    private Integer maxWaitMillis;
+    private Integer minIdle;
+  }
+
 }

+ 0 - 2
weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/okhttp/OkHttpMediaDownloadRequestExecutor.java

@@ -14,8 +14,6 @@ import okio.BufferedSink;
 import okio.Okio;
 import org.apache.commons.io.FilenameUtils;
 import org.apache.commons.lang3.StringUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 import java.io.File;
 import java.io.IOException;