增加 resttemplate 代理配置,支持 http , socks5 两种,通过配置 resttemplate.proxy.{host, port, type} 定义
authorTang Cheng <cheng.tang@supwisdom.com>
Mon, 22 Jul 2019 06:04:40 +0000 (14:04 +0800)
committerTang Cheng <cheng.tang@supwisdom.com>
Thu, 25 Jul 2019 01:37:28 +0000 (09:37 +0800)
config/application-devel-pg-local.properties
payapi/src/main/kotlin/com/supwisdom/dlpay/PayApiApplication.kt

index 6fa530b..3b2dd4a 100644 (file)
@@ -29,3 +29,6 @@ shopbalance.updater.cron=-
 spring.cloud.consul.enabled=false
 spring.cloud.consul.host=172.28.201.70
 spring.cloud.consul.port=8500
+resttemplate.proxy.type=http
+resttemplate.proxy.host=127.0.0.1
+resttemplate.proxy.port=1087
index 7e2fb15..e256b06 100644 (file)
@@ -5,10 +5,12 @@ import io.lettuce.core.ReadFrom
 import net.javacrumbs.shedlock.core.LockProvider
 import net.javacrumbs.shedlock.provider.redis.spring.RedisLockProvider
 import org.springframework.beans.factory.annotation.Autowired
+import org.springframework.beans.factory.annotation.Value
 import org.springframework.boot.SpringApplication
 import org.springframework.boot.autoconfigure.SpringBootApplication
 import org.springframework.boot.autoconfigure.data.redis.RedisProperties
 import org.springframework.boot.builder.SpringApplicationBuilder
+import org.springframework.boot.context.properties.ConfigurationProperties
 import org.springframework.boot.web.servlet.ServletComponentScan
 import org.springframework.boot.web.servlet.support.SpringBootServletInitializer
 import org.springframework.cache.annotation.EnableCaching
@@ -28,7 +30,10 @@ import org.springframework.data.redis.serializer.StringRedisSerializer
 import org.springframework.http.client.ClientHttpRequestFactory
 import org.springframework.http.client.SimpleClientHttpRequestFactory
 import org.springframework.scheduling.annotation.EnableScheduling
+import org.springframework.stereotype.Component
 import org.springframework.web.client.RestTemplate
+import java.net.InetSocketAddress
+import java.net.Proxy
 
 
 @Configuration
@@ -79,11 +84,30 @@ class HttpSessionConfig {
 
 @Configuration
 class RestTemplateConfig {
+    @Component
+    @ConfigurationProperties("resttemplate.proxy")
+    class RestTemplateProxyConfig {
+        @Value("\${type:}")
+        lateinit var type: String
+        @Value("\${host:}")
+        lateinit var host: String
+        @Value("\${port:0}")
+        var port: Int = 0
+    }
+
     @Bean
-    fun simpleClientHttpRequestFactory(): SimpleClientHttpRequestFactory {
+    fun simpleClientHttpRequestFactory(proxyConfig: RestTemplateProxyConfig): SimpleClientHttpRequestFactory {
         val factory = SimpleClientHttpRequestFactory()
         factory.setConnectTimeout(15000)
         factory.setReadTimeout(5000)
+        if (proxyConfig.type.isNotEmpty()) {
+            val proxyType = when (proxyConfig.type) {
+                "http" -> Proxy.Type.HTTP
+                "socks5" -> Proxy.Type.SOCKS
+                else -> Proxy.Type.DIRECT
+            }
+            factory.setProxy(Proxy(proxyType, InetSocketAddress(proxyConfig.host, proxyConfig.port)))
+        }
         return factory
     }