Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
smart_gov_platform
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
赵啸非
smart_gov_platform
Commits
dc1d564c
Commit
dc1d564c
authored
Aug 18, 2022
by
赵啸非
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
修改网关日志
parent
e9d82692
Changes
10
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
4201 additions
and
81 deletions
+4201
-81
base-manager/src/test/java/com/mortals/httpclient/http-client.env.json
...src/test/java/com/mortals/httpclient/http-client.env.json
+1
-1
common-lib/src/main/java/com/mortals/xhx/common/pdu/access/AccessLogPdu.java
.../java/com/mortals/xhx/common/pdu/access/AccessLogPdu.java
+1130
-0
common-lib/src/main/java/com/mortals/xhx/common/pdu/biz/BizLogPdu.java
...c/main/java/com/mortals/xhx/common/pdu/biz/BizLogPdu.java
+932
-0
common-lib/src/main/java/com/mortals/xhx/common/pdu/error/ErrorLogPdu.java
...in/java/com/mortals/xhx/common/pdu/error/ErrorLogPdu.java
+875
-0
common-lib/src/main/java/com/mortals/xhx/common/pdu/operate/OperateLogPdu.java
...ava/com/mortals/xhx/common/pdu/operate/OperateLogPdu.java
+1043
-0
smart-gateway/pom.xml
smart-gateway/pom.xml
+4
-1
smart-gateway/src/main/java/com/mortals/xhx/base/framework/filter/GatewayResponseFilter.java
...tals/xhx/base/framework/filter/GatewayResponseFilter.java
+90
-0
smart-gateway/src/main/java/com/mortals/xhx/base/framework/filter/GlobalLogFilter.java
...om/mortals/xhx/base/framework/filter/GlobalLogFilter.java
+40
-79
smart-gateway/src/main/java/com/mortals/xhx/base/framework/service/MessageProducer.java
...m/mortals/xhx/base/framework/service/MessageProducer.java
+19
-0
smart-gateway/src/main/java/com/mortals/xhx/common/utils/IpUtils.java
...y/src/main/java/com/mortals/xhx/common/utils/IpUtils.java
+67
-0
No files found.
base-manager/src/test/java/com/mortals/httpclient/http-client.env.json
View file @
dc1d564c
{
"base-local"
:
{
"baseUrl"
:
"http://127.0.0.1:172
31
/base"
"baseUrl"
:
"http://127.0.0.1:172
14
/base"
},
"base-dev"
:
{
"baseUrl"
:
"http://192.168.0.217:17211/base"
...
...
common-lib/src/main/java/com/mortals/xhx/common/pdu/access/AccessLogPdu.java
0 → 100644
View file @
dc1d564c
This diff is collapsed.
Click to expand it.
common-lib/src/main/java/com/mortals/xhx/common/pdu/biz/BizLogPdu.java
0 → 100644
View file @
dc1d564c
This diff is collapsed.
Click to expand it.
common-lib/src/main/java/com/mortals/xhx/common/pdu/error/ErrorLogPdu.java
0 → 100644
View file @
dc1d564c
This diff is collapsed.
Click to expand it.
common-lib/src/main/java/com/mortals/xhx/common/pdu/operate/OperateLogPdu.java
0 → 100644
View file @
dc1d564c
This diff is collapsed.
Click to expand it.
smart-gateway/pom.xml
View file @
dc1d564c
...
...
@@ -27,9 +27,12 @@
<profiles.kafka.brokers>
192.168.0.251:9092
</profiles.kafka.brokers>
<profiles.rabbitmq.host>
192.168.0.98
</profiles.rabbitmq.host>
<profiles.rabbitmq.port>
5672
</profiles.rabbitmq.port>
<profiles.rabbitmq.username>
taxi_mq
</profiles.rabbitmq.username>
<profiles.rabbitmq.password>
admin@2020
</profiles.rabbitmq.password>
<profiles.rabbitmq.virtualhost>
/test
</profiles.rabbitmq.virtualhost>
<profiles.nacos.server-addr>
192.168.0.252:8848
</profiles.nacos.server-addr>
<profiles.nacos.group>
DEFAULT_GROUP
</profiles.nacos.group>
<profiles.nacos.namespace>
smart-gov
-dev
</profiles.nacos.namespace>
<profiles.nacos.namespace>
smart-gov
</profiles.nacos.namespace>
<profiles.log.path>
/mortals/app/logs
</profiles.log.path>
</properties>
</profile>
...
...
smart-gateway/src/main/java/com/mortals/xhx/base/framework/filter/GatewayResponseFilter.java
0 → 100644
View file @
dc1d564c
package
com.mortals.xhx.base.framework.filter
;
import
cn.hutool.extra.servlet.ServletUtil
;
import
com.alibaba.fastjson.JSON
;
import
com.mortals.framework.util.StringUtils
;
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.commons.codec.Charsets
;
import
org.reactivestreams.Publisher
;
import
org.slf4j.MDC
;
import
org.springframework.cloud.gateway.filter.GatewayFilterChain
;
import
org.springframework.cloud.gateway.filter.GlobalFilter
;
import
org.springframework.core.Ordered
;
import
org.springframework.core.io.buffer.DataBuffer
;
import
org.springframework.core.io.buffer.DataBufferFactory
;
import
org.springframework.core.io.buffer.DataBufferUtils
;
import
org.springframework.core.io.buffer.DefaultDataBufferFactory
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.server.reactive.ServerHttpResponse
;
import
org.springframework.http.server.reactive.ServerHttpResponseDecorator
;
import
org.springframework.stereotype.Component
;
import
org.springframework.util.ObjectUtils
;
import
org.springframework.web.server.ServerWebExchange
;
import
reactor.core.publisher.Flux
;
import
reactor.core.publisher.Mono
;
import
java.nio.charset.Charset
;
import
java.util.Map
;
import
static
org
.
springframework
.
cloud
.
gateway
.
support
.
ServerWebExchangeUtils
.
ORIGINAL_RESPONSE_CONTENT_TYPE_ATTR
;
/**
* 响应拦截
*
* @author: zxfei
* @date: 2022/8/18 13:48
* @description:
**/
//@Component
@Slf4j
public
class
GatewayResponseFilter
implements
GlobalFilter
,
Ordered
{
@Override
public
Mono
<
Void
>
filter
(
ServerWebExchange
exchange
,
GatewayFilterChain
chain
)
{
ServerHttpResponse
originalResponse
=
exchange
.
getResponse
();
DataBufferFactory
bufferFactory
=
originalResponse
.
bufferFactory
();
ServerHttpResponseDecorator
response
=
new
ServerHttpResponseDecorator
(
originalResponse
)
{
@Override
public
Mono
<
Void
>
writeWith
(
Publisher
<?
extends
DataBuffer
>
body
)
{
if
(
getStatusCode
().
equals
(
HttpStatus
.
OK
)
&&
body
instanceof
Flux
)
{
// 获取ContentType,判断是否返回JSON格式数据
String
originalResponseContentType
=
exchange
.
getAttribute
(
ORIGINAL_RESPONSE_CONTENT_TYPE_ATTR
);
if
(
StringUtils
.
isNotBlank
(
originalResponseContentType
)
&&
originalResponseContentType
.
contains
(
"application/json"
))
{
Flux
<?
extends
DataBuffer
>
fluxBody
=
Flux
.
from
(
body
);
return
super
.
writeWith
(
fluxBody
.
buffer
().
map
(
dataBuffers
->
{
DataBufferFactory
dataBufferFactory
=
new
DefaultDataBufferFactory
();
DataBuffer
join
=
dataBufferFactory
.
join
(
dataBuffers
);
byte
[]
content
=
new
byte
[
join
.
readableByteCount
()];
join
.
read
(
content
);
DataBufferUtils
.
release
(
join
);
String
responseData
=
new
String
(
content
,
Charsets
.
UTF_8
);
// responseData = beforeBodyWriteInternal(responseData, exchange.getRequest());
// responseData = "\"" + beforeBodyWriteInternal(responseData, exchange.getRequest()) + "\"";
byte
[]
uppedContent
=
new
String
(
responseData
.
getBytes
(),
Charset
.
forName
(
"UTF-8"
)).
getBytes
();
originalResponse
.
getHeaders
().
setContentLength
(
uppedContent
.
length
);
// 设置加密头标识
originalResponse
.
getHeaders
().
set
(
"encrypt"
,
"true"
);
return
bufferFactory
.
wrap
(
uppedContent
);
}));
}
}
return
super
.
writeWith
(
body
);
}
@Override
public
Mono
<
Void
>
writeAndFlushWith
(
Publisher
<?
extends
Publisher
<?
extends
DataBuffer
>>
body
)
{
return
writeWith
(
Flux
.
from
(
body
)
.
flatMapSequential
(
p
->
p
));
}
};
return
chain
.
filter
(
exchange
.
mutate
().
response
(
response
).
build
());
}
@Override
public
int
getOrder
()
{
return
-
10
;
}
}
smart-gateway/src/main/java/com/mortals/xhx/base/framework/filter/GlobalLogFilter.java
View file @
dc1d564c
package
com.mortals.xhx.base.framework.filter
;
import
cn.hutool.core.map.MapUtil
;
import
cn.hutool.core.net.NetUtil
;
import
cn.hutool.core.util.IdUtil
;
import
cn.hutool.extra.servlet.ServletUtil
;
import
cn.hutool.http.HttpUtil
;
import
com.alibaba.fastjson.JSON
;
import
com.mortals.xhx.base.framework.config.CustomGatewayProperties
;
import
com.mortals.xhx.base.framework.service.MessageProducer
;
import
com.mortals.xhx.common.pdu.access.AccessLogPdu
;
import
com.mortals.xhx.common.utils.IpUtils
;
import
com.sun.jndi.toolkit.url.UrlUtil
;
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.commons.lang3.StringUtils
;
import
org.slf4j.MDC
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.cloud.gateway.filter.GatewayFilterChain
;
import
org.springframework.cloud.gateway.filter.GlobalFilter
;
import
org.springframework.core.Ordered
;
...
...
@@ -22,9 +31,11 @@ import reactor.core.publisher.Flux;
import
reactor.core.publisher.Mono
;
import
javax.sound.midi.Track
;
import
java.net.InetAddress
;
import
java.net.URI
;
import
java.nio.CharBuffer
;
import
java.nio.charset.StandardCharsets
;
import
java.util.Date
;
import
java.util.LinkedHashSet
;
import
java.util.concurrent.atomic.AtomicReference
;
...
...
@@ -37,9 +48,14 @@ import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.G
* @date: 2022/6/20 17:00
*/
@Slf4j
//
@Component
@Component
public
class
GlobalLogFilter
implements
GlobalFilter
,
Ordered
{
@Value
(
"${spring.application.name:gateway}"
)
private
String
appName
;
@Autowired
private
MessageProducer
messageProducer
;
@Autowired
private
CustomGatewayProperties
customGatewayProperties
;
...
...
@@ -49,106 +65,51 @@ public class GlobalLogFilter implements GlobalFilter, Ordered {
@Override
public
Mono
<
Void
>
filter
(
ServerWebExchange
exchange
,
GatewayFilterChain
chain
)
{
log
.
info
(
"GlobalLogFilter:{}"
,
getOrder
());
MDC
.
put
(
"startTime"
,
String
.
valueOf
(
System
.
currentTimeMillis
()));
//保存请求链路,设置数据追踪id到header中
if
(!
customGatewayProperties
.
getRequestLog
())
{
return
chain
.
filter
(
exchange
);
}
ServerHttpRequest
request
=
exchange
.
getRequest
();
String
path
=
getOriginalRequestUrl
(
exchange
);
String
url
=
request
.
getMethod
().
name
()
+
" "
+
path
;
String
path
=
request
.
getPath
().
toString
(
);
String
method
=
request
.
getMethod
().
name
()
;
//判断请求类型
String
paramStr
=
""
;
if
(
isJsonRequest
(
request
))
{
//读取请求体后 内容需要重新设置进去
String
jsonParam
=
resolveBodyFromRequest
(
request
);
log
.
info
(
"开始请求 => URL[{}],参数类型[json],参数:[{}]"
,
url
,
jsonParam
);
paramStr
=
resolveBodyFromRequest
(
request
);
}
else
{
//非json类型,
MultiValueMap
<
String
,
String
>
parameterMap
=
request
.
getQueryParams
();
if
(
MapUtil
.
isNotEmpty
(
parameterMap
))
{
log
.
info
(
"开始请求 => URL[{}],参数类型[param],参数:[{}]"
,
url
,
JSON
.
toJSONString
(
parameterMap
));
}
else
{
log
.
info
(
"开始请求 => URL[{}],无参数"
,
url
);
}
}
exchange
.
getAttributes
().
put
(
START_TIME
,
System
.
currentTimeMillis
());
exchange
.
getRequest
().
getHeaders
().
add
(
TRACE_ID
,
IdUtil
.
fastSimpleUUID
());
return
chain
.
filter
(
exchange
).
then
(
Mono
.
fromRunnable
(()
->
{
Long
startTime
=
exchange
.
getAttribute
(
START_TIME
);
if
(
startTime
!=
null
)
{
long
executeTime
=
(
System
.
currentTimeMillis
()
-
startTime
);
log
.
info
(
"结束请求 => URL[{}],耗时:[{}]毫秒"
,
url
,
executeTime
);
paramStr
=
JSON
.
toJSONString
(
parameterMap
);
}
}));
//return chain.filter(exchange);
/* if (!customGatewayProperties.getRequestLog()) {
return chain.filter(exchange);
}*/
/* ServerHttpRequest request = exchange.getRequest();
String path = getOriginalRequestUrl(exchange);
String url = request.getMethod().name() + " " + path;
// 打印请求参数
if (isJsonRequest(request)) {
String jsonParam = resolveBodyFromRequest(request);
log.info("开始请求 => URL[{}],参数类型[json],参数:[{}]", url, jsonParam);
} else {
MultiValueMap<String, String> parameterMap = request.getQueryParams();
if (MapUtil.isNotEmpty(parameterMap)) {
log.info("开始请求 => URL[{}],参数类型[param],参数:[{}]", url, JSON.toJSONString(parameterMap));
} else {
log.info("开始请求 => URL[{}],无参数", url);
}
}
String
traceId
=
IdUtil
.
fastSimpleUUID
();
AccessLogPdu
accessLogPdu
=
new
AccessLogPdu
();
accessLogPdu
.
initAttrValue
();
accessLogPdu
.
setAppName
(
appName
);
accessLogPdu
.
setHostName
(
NetUtil
.
getLocalHostName
());
accessLogPdu
.
setLogLevel
(
"INFO"
);
accessLogPdu
.
setRequestIp
(
IpUtils
.
getRealIpAddress
(
exchange
.
getRequest
()));
accessLogPdu
.
setRequestParam
(
paramStr
);
accessLogPdu
.
setLogTime
(
new
Date
());
accessLogPdu
.
setMethod
(
method
);
messageProducer
.
syncAccessSend
(
accessLogPdu
);
exchange
.
getAttributes
().
put
(
START_TIME
,
System
.
currentTimeMillis
());
return chain.filter(exchange).then(Mono.fromRunnable(() -> {
Long startTime = exchange.getAttribute(START_TIME);
if (startTime != null) {
long executeTime = (System.currentTimeMillis() - startTime);
log.info("结束请求 => URL[{}],耗时:[{}]毫秒", url, executeTime);
}
}));*/
ServerHttpRequest
newRequest
=
exchange
.
getRequest
().
mutate
()
.
header
(
TRACE_ID
,
traceId
)
.
build
();
/*ServerHttpRequest request = exchange.getRequest();
String path = getOriginalRequestUrl(exchange);
String url = request.getMethod().name() + " " + path;
return
chain
.
filter
(
exchange
.
mutate
().
request
(
newRequest
).
build
());
if (!customGatewayProperties.getRequestLog()) {
return chain.filter(exchange);
}
// 打印请求参数
if (isJsonRequest(request)) {
String jsonParam = resolveBodyFromRequest(request);
log.debug("开始请求 => URL[{}],参数类型[json],参数:[{}]", url, jsonParam);
} else {
MultiValueMap<String, String> parameterMap = request.getQueryParams();
if (MapUtil.isNotEmpty(parameterMap)) {
log.debug("开始请求 => URL[{}],参数类型[param],参数:[{}]", url, JSON.toJSONString(parameterMap));
} else {
log.debug("开始请求 => URL[{}],无参数", url);
}
}
exchange.getAttributes().put(START_TIME, System.currentTimeMillis());
return chain.filter(exchange).then(Mono.fromRunnable(() -> {
Long startTime = exchange.getAttribute(START_TIME);
if (startTime != null) {
long executeTime = (System.currentTimeMillis() - startTime);
log.debug("结束请求 => URL[{}],耗时:[{}]毫秒", url, executeTime);
}
}));*/
}
@Override
public
int
getOrder
()
{
return
Ordered
.
LOWEST_PRECEDENCE
;
return
0
;
}
/**
...
...
smart-gateway/src/main/java/com/mortals/xhx/base/framework/service/MessageProducer.java
0 → 100644
View file @
dc1d564c
package
com.mortals.xhx.base.framework.service
;
import
com.mortals.xhx.common.key.QueueKey
;
import
com.mortals.xhx.common.pdu.access.AccessLogPdu
;
import
org.springframework.amqp.rabbit.core.RabbitTemplate
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Component
;
@Component
public
class
MessageProducer
{
@Autowired
private
RabbitTemplate
rabbitTemplate
;
public
void
syncAccessSend
(
AccessLogPdu
accessLogPdu
)
{
rabbitTemplate
.
convertAndSend
(
QueueKey
.
EXCHANGE
,
QueueKey
.
BIZ_LOG_QUEUE
,
accessLogPdu
);
}
}
smart-gateway/src/main/java/com/mortals/xhx/common/utils/IpUtils.java
0 → 100644
View file @
dc1d564c
package
com.mortals.xhx.common.utils
;
import
org.apache.commons.lang3.StringUtils
;
import
org.springframework.http.server.reactive.ServerHttpRequest
;
import
java.net.InetAddress
;
import
java.net.InetSocketAddress
;
/**
* @author: zxfei
* @date: 2022/8/18 14:42
* @description:
**/
public
class
IpUtils
{
private
static
final
String
UNKNOWN
=
"unknown"
;
private
static
final
String
LOCALHOST
=
"127.0.0.1"
;
private
static
final
String
SEPARATOR
=
","
;
private
static
final
String
HEADER_X_FORWARDED_FOR
=
"x-forwarded-for"
;
private
static
final
String
HEADER_PROXY_CLIENT_IP
=
"Proxy-Client-IP"
;
private
static
final
String
HEADER_WL_PROXY_CLIENT_IP
=
"WL-Proxy-Client-IP"
;
/**
* 获取真实客户端IP
*
* @param serverHttpRequest
* @return
*/
public
static
String
getRealIpAddress
(
ServerHttpRequest
serverHttpRequest
)
{
String
ipAddress
;
try
{
// 1.根据常见的代理服务器转发的请求ip存放协议,从请求头获取原始请求ip。值类似于203.98.182.163, 203.98.182.163
ipAddress
=
serverHttpRequest
.
getHeaders
().
getFirst
(
HEADER_X_FORWARDED_FOR
);
if
(
StringUtils
.
isEmpty
(
ipAddress
)
||
UNKNOWN
.
equalsIgnoreCase
(
ipAddress
))
{
ipAddress
=
serverHttpRequest
.
getHeaders
().
getFirst
(
HEADER_PROXY_CLIENT_IP
);
}
if
(
StringUtils
.
isEmpty
(
ipAddress
)
||
UNKNOWN
.
equalsIgnoreCase
(
ipAddress
))
{
ipAddress
=
serverHttpRequest
.
getHeaders
().
getFirst
(
HEADER_WL_PROXY_CLIENT_IP
);
}
// 2.如果没有转发的ip,则取当前通信的请求端的ip
if
(
StringUtils
.
isEmpty
(
ipAddress
)
||
UNKNOWN
.
equalsIgnoreCase
(
ipAddress
))
{
InetSocketAddress
inetSocketAddress
=
serverHttpRequest
.
getRemoteAddress
();
if
(
inetSocketAddress
!=
null
)
{
ipAddress
=
inetSocketAddress
.
getAddress
().
getHostAddress
();
}
// 如果是127.0.0.1,则取本地真实ip
if
(
LOCALHOST
.
equals
(
ipAddress
))
{
InetAddress
localAddress
=
InetAddress
.
getLocalHost
();
if
(
localAddress
.
getHostAddress
()
!=
null
)
{
ipAddress
=
localAddress
.
getHostAddress
();
}
}
}
// 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
// "***.***.***.***"
if
(
ipAddress
!=
null
)
{
ipAddress
=
ipAddress
.
split
(
SEPARATOR
)[
0
].
trim
();
}
}
catch
(
Exception
e
)
{
ipAddress
=
""
;
}
return
ipAddress
==
null
?
""
:
ipAddress
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
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 comment