Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
提交反馈
为 GitLab 提交贡献
登录
切换导航
iBiz4j Spring R7
项目
项目
详情
动态
版本
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
iBiz-R7后台标准模板
iBiz4j Spring R7
提交
f39f7a9e
提交
f39f7a9e
编写于
5月 08, 2020
作者:
sq3536
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
通用feign客户端
上级
99c555fc
变更
1
显示空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
168 行增加
和
0 行删除
+168
-0
RemoteService.java.ftl
...in/java/%SYS_PKGPATH%/util/service/RemoteService.java.ftl
+168
-0
未找到文件。
SLN/%PUBPRJ%-util/src/main/java/%SYS_PKGPATH%/util/service/RemoteService.java.ftl
0 → 100644
浏览文件 @
f39f7a9e
<#
ibiztemplate
>
TARGET
=
PSSYSTEM
</#
ibiztemplate
>
package
${
pub
.
getPKGCodeName
()}.
util
.
service
;
import
com
.
alibaba
.
fastjson
.
JSON
;
import
com
.
alibaba
.
fastjson
.
JSONObject
;
import
com
.
alibaba
.
fastjson
.
serializer
.
SerializerFeature
;
import
com
.
alibaba
.
fastjson
.
support
.
config
.
FastJsonConfig
;
import
com
.
alibaba
.
fastjson
.
support
.
spring
.
FastJsonHttpMessageConverter
;
import
com
.
alibaba
.
fastjson
.
support
.
springfox
.
SwaggerJsonSerializer
;
import
feign
.*;
import
feign
.
codec
.
Decoder
;
import
org
.
springframework
.
beans
.
factory
.
ObjectFactory
;
import
org
.
springframework
.
beans
.
factory
.
annotation
.
Autowired
;
import
org
.
springframework
.
boot
.
autoconfigure
.
http
.
HttpMessageConverters
;
import
org
.
springframework
.
cloud
.
openfeign
.
FeignContext
;
import
org
.
springframework
.
cloud
.
openfeign
.
support
.
SpringDecoder
;
import
org
.
springframework
.
context
.
annotation
.
Configuration
;
import
org
.
springframework
.
http
.
MediaType
;
import
org
.
springframework
.
stereotype
.
Service
;
import
org
.
springframework
.
web
.
bind
.
annotation
.*;
import
org
.
springframework
.
web
.
context
.
request
.
RequestContextHolder
;
import
org
.
springframework
.
web
.
context
.
request
.
ServletRequestAttributes
;
import
javax
.
servlet
.
http
.
HttpServletRequest
;
import
java
.
util
.
ArrayList
;
import
java
.
util
.
List
;
import
java
.
util
.
Map
;
import
java
.
util
.
Objects
;
import
java
.
util
.
concurrent
.
ConcurrentHashMap
;
/**
*
自定义
feigen
客户端配置
*
*
@
author
*/
@
Service
public
class
RemoteService
{
/**
*
FeignClientFactoryBean
该工厂类中
设置
builder
属性时就是通过该对象,源码中可看到
*/
@
Autowired
protected
FeignContext
feignContext
;
/**
*
FeignClient
默认
LoadBalancerFeignClient
*/
@
Autowired
private
Client
feignClient
;
private
static
final
Map
<
String
,
Object
>
FEIGN_CLIENTS
=
new
ConcurrentHashMap
<>();
/**
*
定义远程通用接口
*/
public
interface
RemoteFeignClient
{
@
RequestMapping
(
method
=
RequestMethod
.
POST
,
value
=
"/{path}"
)
JSONObject
post
(
@
PathVariable
(
"path"
)
String
path
,
@
RequestHeader
(
"Authorization"
)
String
token
,@
RequestBody
Map
param
);
@
RequestMapping
(
method
=
RequestMethod
.
GET
,
value
=
"/{path}"
)
JSONObject
request
(
@
PathVariable
(
"path"
)
String
path
,
@
RequestHeader
(
"Authorization"
)
String
token
,
Map
param
);
@
RequestMapping
(
method
=
RequestMethod
.
GET
,
value
=
"/{path}"
)
JSONObject
get
(
@
PathVariable
(
"path"
)
String
path
,
@
RequestHeader
(
"Authorization"
)
String
token
);
@
RequestMapping
(
method
=
RequestMethod
.
PUT
,
value
=
"/{path}"
)
JSONObject
put
(
@
PathVariable
(
"path"
)
String
path
,
@
RequestHeader
(
"Authorization"
)
String
token
,@
RequestBody
Map
param
);
@
RequestMapping
(
method
=
RequestMethod
.
DELETE
,
value
=
"/{path}"
)
JSONObject
delete
(
@
PathVariable
(
"path"
)
String
path
,
@
RequestHeader
(
"Authorization"
)
String
token
);
}
/**
*
@
param
serverId
*
@
return
*/
public
RemoteFeignClient
getClient
(
String
serverId
)
{
return
this
.
create
(
RemoteFeignClient
.
class
,
serverId
);
}
/**
*
设置编码解码器为
FastJson
*
*
@
param
clazz
*
@
param
serviceId
*
@
param
<
T
>
*
@
return
*/
private
<
T
>
T
create
(
Class
<
T
>
clazz
,
String
serviceId
)
{
Object
object
=
FEIGN_CLIENTS
.
get
(
serviceId
);
if
(
Objects
.
isNull
(
object
))
{
object
=
Feign
.
builder
()
//
decoder
指定对象解码方式
.
decoder
(
this
.
feignDecoder
())
.
client
(
feignClient
)
//
options
方法指定连接超时时长及响应超时时长
.
options
(
new
Request
.
Options
(
5000
,
5000
))
//
retryer
方法指定重试策略
//.
retryer
(
new
Retryer
.
Default
(
5000
,
5000
,
3
))
.
contract
(
feignContext
.
getInstance
(
serviceId
,
Contract
.
class
))
//
target
方法绑定接口与服务端地址。返回类型为绑定的接口类型。
.
target
(
clazz
,
"http://"
+
serviceId
);
FEIGN_CLIENTS
.
put
(
serviceId
,
object
);
}
return
(
T
)
object
;
}
private
Decoder
feignDecoder
()
{
return
new
SpringDecoder
(
feignHttpMessageConverter
());
}
/**
*
设置解码器为
fastjson
*
*
@
return
*/
private
ObjectFactory
<
HttpMessageConverters
>
feignHttpMessageConverter
()
{
final
HttpMessageConverters
httpMessageConverters
=
new
HttpMessageConverters
(
this
.
getFastJsonConverter
());
return
()
->
httpMessageConverters
;
}
private
FastJsonHttpMessageConverter
getFastJsonConverter
()
{
FastJsonHttpMessageConverter
converter
=
new
FastJsonHttpMessageConverter
();
List
<
MediaType
>
supportedMediaTypes
=
new
ArrayList
<>();
MediaType
mediaTypeJson
=
MediaType
.
valueOf
(
MediaType
.
APPLICATION_JSON_UTF8_VALUE
);
supportedMediaTypes
.
add
(
mediaTypeJson
);
converter
.
setSupportedMediaTypes
(
supportedMediaTypes
);
FastJsonConfig
config
=
new
FastJsonConfig
();
config
.
getSerializeConfig
().
put
(
JSON
.
class
,
new
SwaggerJsonSerializer
());
config
.
setSerializerFeatures
(
SerializerFeature
.
DisableCircularReferenceDetect
);
converter
.
setFastJsonConfig
(
config
);
return
converter
;
}
@
Configuration
public
class
FeignTokenInterceptor
implements
RequestInterceptor
{
@
Override
public
void
apply
(
RequestTemplate
template
)
{
HttpServletRequest
request
=
getServletRequest
();
if
(
null
==
request
){
return
;
}
template
.
header
(
"uid"
,
getHeaders
(
request
));
}
private
HttpServletRequest
getServletRequest
()
{
return
((
ServletRequestAttributes
)
RequestContextHolder
.
getRequestAttributes
()).
getRequest
();
}
private
String
getHeaders
(
HttpServletRequest
request
){
return
request
.
getHeader
(
"uid"
);
}
}
}
\ No newline at end of file
编辑
预览
Markdown
格式
0%
请重试
or
添加新附件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
先完成此消息的编辑!
取消
想要评论请
注册
或
登录