Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
architecture
/
glsx-archetype
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 10acc0e4
authored
Apr 18, 2023
by
刘宇峰
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
框架完善
1 parent
8451c5a0
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
66 additions
and
85 deletions
rest-archetype-framework/rest-admin-context/src/main/java/com/glsx/plat/context/utils/AssertUtils.java
rest-archetype-framework/rest-admin-feign/src/main/java/com/glsx/plat/feign/config/OkHttpConfig.java
rest-archetype-framework/rest-admin-feign/src/main/java/com/glsx/plat/feign/config/RestConfig.java
rest-archetype-framework/rest-admin-feign/src/main/java/com/glsx/plat/feign/config/RestTemplateConfig.java
rest-archetype-framework/rest-admin-feign/src/main/java/com/glsx/plat/feign/utils/RestTemplateUtils.java
rest-archetype-framework/rest-admin-context/src/main/java/com/glsx/plat/context/utils/AssertUtils.java
deleted
100644 → 0
View file @
8451c5a
package
com
.
glsx
.
plat
.
context
.
utils
;
import
cn.hutool.core.lang.Assert
;
import
com.glsx.plat.exception.ValidateException
;
import
org.apache.commons.collections4.CollectionUtils
;
import
org.apache.commons.lang.StringUtils
;
import
java.text.MessageFormat
;
import
java.util.Objects
;
/**
* 断言工具类
*/
public
class
AssertUtils
extends
Assert
{
public
static
void
isBlank
(
String
str
,
String
message
,
Object
...
params
)
{
if
(
StringUtils
.
isBlank
(
str
))
{
throw
new
ValidateException
(
MessageFormat
.
format
(
message
,
params
));
}
}
public
static
void
isNull
(
Object
object
,
String
message
,
Object
...
params
)
{
if
(
Objects
.
isNull
(
object
))
{
throw
new
ValidateException
(
MessageFormat
.
format
(
message
,
params
));
}
}
public
static
void
isEmpty
(
Object
object
,
String
message
,
Object
...
params
)
{
if
(
CollectionUtils
.
sizeIsEmpty
(
object
))
{
throw
new
ValidateException
(
MessageFormat
.
format
(
message
,
params
));
}
}
}
rest-archetype-framework/rest-admin-feign/src/main/java/com/glsx/plat/feign/config/OkHttpConfig.java
View file @
10acc0e
package
com
.
glsx
.
plat
.
feign
.
config
;
import
com.glsx.plat.feign.interceptor.OkHttpTokenInterceptor
;
import
feign.Feign
;
import
okhttp3.ConnectionPool
;
import
org.springframework.boot.autoconfigure.AutoConfigureBefore
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnClass
;
import
org.springframework.cloud.openfeign.FeignAutoConfiguration
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
javax.annotation.Resource
;
import
java.util.concurrent.TimeUnit
;
...
...
@@ -43,25 +38,4 @@ public class OkHttpConfig {
.
build
();
}
//feign:
// # feign启用hystrix,才能熔断、降级
// # hystrix:
// # enabled: true
// # 启用 okhttp 关闭默认 httpclient
// httpclient:
// enabled: false #关闭httpclient
// # 配置连接池
// max-connections: 200 #feign的最大连接数
// max-connections-per-route: 50 #fegin单个路径的最大连接数
// okhttp:
// enabled: true
// # 请求与响应的压缩以提高通信效率
// compression:
// request:
// enabled: true
// min-request-size: 2048
// mime-types: text/xml,application/xml,application/json
// response:
// enabled: true
}
\ No newline at end of file
rest-archetype-framework/rest-admin-feign/src/main/java/com/glsx/plat/feign/config/RestConfig.java
deleted
100644 → 0
View file @
8451c5a
package
com
.
glsx
.
plat
.
feign
.
config
;
import
org.springframework.cloud.client.loadbalancer.LoadBalanced
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.web.client.RestTemplate
;
@Configuration
public
class
RestConfig
{
@Bean
@LoadBalanced
public
RestTemplate
restTemplate
()
{
return
new
RestTemplate
();
}
}
rest-archetype-framework/rest-admin-feign/src/main/java/com/glsx/plat/feign/config/RestTemplateConfig.java
0 → 100644
View file @
10acc0e
package
com
.
glsx
.
plat
.
feign
.
config
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.http.client.OkHttp3ClientHttpRequestFactory
;
import
org.springframework.http.converter.HttpMessageConverter
;
import
org.springframework.http.converter.StringHttpMessageConverter
;
import
org.springframework.web.client.RestTemplate
;
import
java.nio.charset.StandardCharsets
;
import
java.util.List
;
@Configuration
public
class
RestTemplateConfig
{
/**
* 创建http请求工厂
* <p>
* 实例化OkHttp3
*
* @return
*/
@Bean
public
OkHttp3ClientHttpRequestFactory
httpRequestFactory
()
{
OkHttp3ClientHttpRequestFactory
requestFactory
=
new
OkHttp3ClientHttpRequestFactory
();
requestFactory
.
setConnectTimeout
(
10000
);
requestFactory
.
setReadTimeout
(
20000
);
return
requestFactory
;
}
/**
* 实例化RestTemplate模板
*
* @return
*/
@Bean
public
RestTemplate
restTemplate
()
{
RestTemplate
restTemplate
=
new
RestTemplate
(
httpRequestFactory
());
// 中文乱码,主要是 StringHttpMessageConverter的默认编码为ISO导致的
List
<
HttpMessageConverter
<?>>
list
=
restTemplate
.
getMessageConverters
();
for
(
HttpMessageConverter
converter
:
list
)
{
if
(
converter
instanceof
StringHttpMessageConverter
)
{
((
StringHttpMessageConverter
)
converter
).
setDefaultCharset
(
StandardCharsets
.
UTF_8
);
break
;
}
}
return
restTemplate
;
}
}
rest-archetype-framework/rest-admin-feign/src/main/java/com/glsx/plat/feign/utils/RestTemplateUtils.java
View file @
10acc0e
...
...
@@ -2,6 +2,7 @@ package com.glsx.plat.feign.utils;
import
org.springframework.core.ParameterizedTypeReference
;
import
org.springframework.http.*
;
import
org.springframework.http.client.OkHttp3ClientHttpRequestFactory
;
import
org.springframework.http.client.SimpleClientHttpRequestFactory
;
import
org.springframework.util.MultiValueMap
;
import
org.springframework.web.client.RequestCallback
;
...
...
@@ -15,7 +16,7 @@ import java.util.Objects;
public
class
RestTemplateUtils
{
private
static
final
int
CONNECT_TIMEOUT
=
20000
;
private
static
final
int
SOCKET_TIMEOUT
=
2
0000
;
private
static
final
int
SOCKET_TIMEOUT
=
6
0000
;
private
static
RestTemplate
getCustomRestTemplate
()
{
SimpleClientHttpRequestFactory
factory
=
new
SimpleClientHttpRequestFactory
();
...
...
@@ -24,6 +25,13 @@ public class RestTemplateUtils {
return
new
RestTemplate
(
factory
);
}
private
static
RestTemplate
getOkHttp3RestTemplate
()
{
OkHttp3ClientHttpRequestFactory
factory
=
new
OkHttp3ClientHttpRequestFactory
();
factory
.
setConnectTimeout
(
CONNECT_TIMEOUT
);
factory
.
setReadTimeout
(
SOCKET_TIMEOUT
);
return
new
RestTemplate
(
factory
);
}
public
static
<
T
>
T
fetch
(
String
url
,
String
method
,
HttpEntity
<?>
requestEntity
,
...
...
@@ -46,7 +54,7 @@ public class RestTemplateUtils {
HttpEntity
<?>
requestEntity
,
ParameterizedTypeReference
<
T
>
responseType
)
{
T
ret
=
null
;
RestTemplate
restTemplate
=
get
Custom
RestTemplate
();
RestTemplate
restTemplate
=
get
OkHttp3
RestTemplate
();
HttpMethod
httpMethod
=
HttpMethod
.
resolve
(
Objects
.
nonNull
(
method
)
?
method
.
toUpperCase
()
:
""
);
if
(
Objects
.
nonNull
(
httpMethod
))
{
ResponseEntity
<
T
>
responseEntity
=
restTemplate
.
exchange
(
url
,
httpMethod
,
requestEntity
,
responseType
);
...
...
@@ -60,7 +68,7 @@ public class RestTemplateUtils {
HttpEntity
<?>
requestEntity
,
ParameterizedTypeReference
<
T
>
responseType
)
{
T
ret
=
null
;
RestTemplate
restTemplate
=
get
Custom
RestTemplate
();
RestTemplate
restTemplate
=
get
OkHttp3
RestTemplate
();
if
(
Objects
.
nonNull
(
httpMethod
))
{
ResponseEntity
<
T
>
responseEntity
=
restTemplate
.
exchange
(
url
,
httpMethod
,
requestEntity
,
responseType
);
ret
=
responseEntity
.
getBody
();
...
...
@@ -85,7 +93,7 @@ public class RestTemplateUtils {
}
public
static
<
T
>
T
fetchGet
(
String
url
,
HttpEntity
<?>
requestEntity
,
ParameterizedTypeReference
<
T
>
responseType
)
{
RestTemplate
restTemplate
=
get
Custom
RestTemplate
();
RestTemplate
restTemplate
=
get
OkHttp3
RestTemplate
();
ResponseEntity
<
T
>
responseEntity
=
restTemplate
.
exchange
(
url
,
HttpMethod
.
GET
,
requestEntity
,
responseType
);
return
responseEntity
.
getBody
();
}
...
...
@@ -93,7 +101,7 @@ public class RestTemplateUtils {
public
static
<
T
>
T
fetchPost
(
String
url
,
HttpEntity
<?>
requestEntity
,
ParameterizedTypeReference
<
T
>
responseType
)
{
RestTemplate
restTemplate
=
get
Custom
RestTemplate
();
RestTemplate
restTemplate
=
get
OkHttp3
RestTemplate
();
ResponseEntity
<
T
>
responseEntity
=
restTemplate
.
exchange
(
url
,
HttpMethod
.
POST
,
requestEntity
,
responseType
);
return
responseEntity
.
getBody
();
}
...
...
@@ -121,7 +129,7 @@ public class RestTemplateUtils {
HttpEntity
<?>
requestEntity
,
Class
<
T
>
clazz
)
{
T
ret
=
null
;
RestTemplate
restTemplate
=
get
Custom
RestTemplate
();
RestTemplate
restTemplate
=
get
OkHttp3
RestTemplate
();
HttpMethod
httpMethod
=
HttpMethod
.
resolve
(
Objects
.
nonNull
(
method
)
?
method
.
toUpperCase
()
:
""
);
if
(
Objects
.
nonNull
(
httpMethod
))
{
ResponseEntity
<
T
>
responseEntity
=
restTemplate
.
exchange
(
url
,
httpMethod
,
requestEntity
,
clazz
);
...
...
@@ -144,7 +152,7 @@ public class RestTemplateUtils {
RequestCallback
callback
,
ResponseExtractor
<
T
>
extractor
)
{
T
ret
=
null
;
RestTemplate
restTemplate
=
get
Custom
RestTemplate
();
RestTemplate
restTemplate
=
get
OkHttp3
RestTemplate
();
HttpMethod
httpMethod
=
HttpMethod
.
resolve
(
Objects
.
nonNull
(
method
)
?
method
.
toUpperCase
()
:
""
);
if
(
Objects
.
nonNull
(
httpMethod
))
{
ret
=
restTemplate
.
execute
(
url
,
httpMethod
,
callback
,
extractor
);
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment