Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
sl-gateway
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
zhouwei
sl-gateway
Commits
fb31b6af
Commit
fb31b6af
authored
May 06, 2025
by
zhouwei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
添加日志
parent
209c40a9
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
29 additions
and
15 deletions
+29
-15
FailoverGatewayFilterFactory.java
...yan/securitylink/filter/FailoverGatewayFilterFactory.java
+29
-15
No files found.
src/main/java/com/nanyan/securitylink/filter/FailoverGatewayFilterFactory.java
View file @
fb31b6af
...
@@ -19,6 +19,7 @@ import org.springframework.http.HttpStatus;
...
@@ -19,6 +19,7 @@ import org.springframework.http.HttpStatus;
import
org.springframework.http.MediaType
;
import
org.springframework.http.MediaType
;
import
org.springframework.http.client.reactive.ReactorClientHttpConnector
;
import
org.springframework.http.client.reactive.ReactorClientHttpConnector
;
import
org.springframework.http.server.reactive.ServerHttpRequest
;
import
org.springframework.http.server.reactive.ServerHttpRequest
;
import
org.springframework.http.server.reactive.ServerHttpResponse
;
import
org.springframework.stereotype.Component
;
import
org.springframework.stereotype.Component
;
import
org.springframework.web.reactive.function.BodyInserters
;
import
org.springframework.web.reactive.function.BodyInserters
;
import
org.springframework.web.reactive.function.client.WebClient
;
import
org.springframework.web.reactive.function.client.WebClient
;
...
@@ -154,11 +155,29 @@ public class FailoverGatewayFilterFactory extends AbstractGatewayFilterFactory<F
...
@@ -154,11 +155,29 @@ public class FailoverGatewayFilterFactory extends AbstractGatewayFilterFactory<F
?
authHeader
.
substring
(
7
)
?
authHeader
.
substring
(
7
)
:
null
;
:
null
;
String
sourceUri
=
exchange
.
getRequest
().
getPath
().
value
();
String
sourceUri
=
exchange
.
getRequest
().
getPath
().
value
();
String
requestId
;
if
(
exchange
.
getAttribute
(
REQUEST_ID
)
==
null
)
{
requestId
=
UUID
.
randomUUID
().
toString
();
exchange
.
getAttributes
().
put
(
REQUEST_ID
,
requestId
);
}
else
{
requestId
=
exchange
.
getAttributes
().
get
(
REQUEST_ID
).
toString
();
}
ServerWebExchange
modifiedExchange
=
exchange
.
mutate
()
.
request
(
builder
->
builder
.
headers
(
headers
->
{
// 添加新header
headers
.
add
(
REQUEST_ID
,
requestId
);
// 修改已有header
headers
.
putAll
(
exchange
.
getRequest
().
getHeaders
());
})
)
.
build
();
modifiedExchange
.
getAttributes
().
put
(
REQUEST_ID
,
requestId
);
if
(
token
!=
null
)
{
if
(
token
!=
null
)
{
return
tokenRouteMappingService
.
findMatchingUriConfig
(
token
,
sourceUri
)
return
tokenRouteMappingService
.
findMatchingUriConfig
(
token
,
sourceUri
)
.
map
(
uriConfig
->
cacheRequestBody
(
e
xchange
).
flatMap
(
cachedBody
->
{
.
map
(
uriConfig
->
cacheRequestBody
(
modifiedE
xchange
).
flatMap
(
cachedBody
->
{
log
.
info
(
"Found matching URI config for token: {}, sourceUri: {}, request id:{}"
,
token
,
sourceUri
,
e
xchange
.
getAttributes
().
get
(
REQUEST_ID
));
log
.
info
(
"Found matching URI config for token: {}, sourceUri: {}, request id:{}"
,
token
,
sourceUri
,
modifiedE
xchange
.
getAttributes
().
get
(
REQUEST_ID
));
String
primaryUrl
=
uriConfig
.
getPrimaryUrl
();
String
primaryUrl
=
uriConfig
.
getPrimaryUrl
();
CircularQueue
errorStats
=
getOrCreateErrorStats
(
primaryUrl
);
CircularQueue
errorStats
=
getOrCreateErrorStats
(
primaryUrl
);
long
startTime
=
System
.
currentTimeMillis
();
long
startTime
=
System
.
currentTimeMillis
();
...
@@ -173,7 +192,7 @@ public class FailoverGatewayFilterFactory extends AbstractGatewayFilterFactory<F
...
@@ -173,7 +192,7 @@ public class FailoverGatewayFilterFactory extends AbstractGatewayFilterFactory<F
uriConfig
.
getFallbackHost
(),
uriConfig
.
getFallbackHost
(),
uriConfig
.
getModel
(),
uriConfig
.
getModel
(),
uriConfig
.
getModelKey
(),
uriConfig
.
getModelKey
(),
e
xchange
,
modifiedE
xchange
,
false
,
false
,
null
null
);
);
...
@@ -185,7 +204,7 @@ public class FailoverGatewayFilterFactory extends AbstractGatewayFilterFactory<F
...
@@ -185,7 +204,7 @@ public class FailoverGatewayFilterFactory extends AbstractGatewayFilterFactory<F
uriConfig
.
getPrimaryHost
(),
uriConfig
.
getPrimaryHost
(),
uriConfig
.
getModel
(),
uriConfig
.
getModel
(),
uriConfig
.
getModelKey
(),
uriConfig
.
getModelKey
(),
e
xchange
,
modifiedE
xchange
,
true
,
true
,
errorStats
errorStats
).
onErrorResume
(
throwable
->
{
).
onErrorResume
(
throwable
->
{
...
@@ -194,7 +213,7 @@ public class FailoverGatewayFilterFactory extends AbstractGatewayFilterFactory<F
...
@@ -194,7 +213,7 @@ public class FailoverGatewayFilterFactory extends AbstractGatewayFilterFactory<F
throwable
.
getMessage
());
throwable
.
getMessage
());
long
duration
=
System
.
currentTimeMillis
()
-
startTime
;
long
duration
=
System
.
currentTimeMillis
()
-
startTime
;
log
.
info
(
"Source request error:{} took:{}ms, switching to target: {} request id:{}"
,
log
.
info
(
"Source request error:{} took:{}ms, switching to target: {} request id:{}"
,
throwable
.
getMessage
(),
duration
,
uriConfig
.
getTargetUri
(),
e
xchange
.
getAttributes
().
get
(
REQUEST_ID
));
throwable
.
getMessage
(),
duration
,
uriConfig
.
getTargetUri
(),
modifiedE
xchange
.
getAttributes
().
get
(
REQUEST_ID
));
// 记录错误
// 记录错误
getOrCreateErrorStats
(
uriConfig
.
getPrimaryUrl
()).
add
(
false
);
getOrCreateErrorStats
(
uriConfig
.
getPrimaryUrl
()).
add
(
false
);
...
@@ -205,20 +224,20 @@ public class FailoverGatewayFilterFactory extends AbstractGatewayFilterFactory<F
...
@@ -205,20 +224,20 @@ public class FailoverGatewayFilterFactory extends AbstractGatewayFilterFactory<F
uriConfig
.
getFallbackHost
(),
uriConfig
.
getFallbackHost
(),
uriConfig
.
getModel
(),
uriConfig
.
getModel
(),
uriConfig
.
getModelKey
(),
uriConfig
.
getModelKey
(),
e
xchange
,
modifiedE
xchange
,
false
,
false
,
null
null
);
);
});
});
}))
}))
.
orElse
(
chain
.
filter
(
e
xchange
));
.
orElse
(
chain
.
filter
(
modifiedE
xchange
));
}
}
return
chain
.
filter
(
e
xchange
);
return
chain
.
filter
(
modifiedE
xchange
);
}
finally
{
}
finally
{
// 确保在请求处理完成后清理 ThreadLocal
// 确保在请求处理完成后清理 ThreadLocal
// 清理缓存的请求体
// 清理缓存的请求体
exchange
.
getAttributes
().
remove
(
CACHED_REQUEST_BODY_KEY
);
//
exchange.getAttributes().remove(CACHED_REQUEST_BODY_KEY);
}
}
};
};
}
}
...
@@ -231,12 +250,6 @@ public class FailoverGatewayFilterFactory extends AbstractGatewayFilterFactory<F
...
@@ -231,12 +250,6 @@ public class FailoverGatewayFilterFactory extends AbstractGatewayFilterFactory<F
return
Mono
.
just
(
""
);
return
Mono
.
just
(
""
);
}
}
if
(
exchange
.
getAttribute
(
REQUEST_ID
)
==
null
)
{
String
requestId
=
UUID
.
randomUUID
().
toString
();
exchange
.
getAttributes
().
put
(
REQUEST_ID
,
requestId
);
exchange
.
getRequest
().
getHeaders
().
add
(
REQUEST_ID
,
requestId
);
}
// 对于不需要请求体的方法,直接返回
// 对于不需要请求体的方法,直接返回
if
(!
requiresRequestBody
(
request
.
getMethod
()))
{
if
(!
requiresRequestBody
(
request
.
getMethod
()))
{
return
Mono
.
just
(
""
);
return
Mono
.
just
(
""
);
...
@@ -374,6 +387,7 @@ public class FailoverGatewayFilterFactory extends AbstractGatewayFilterFactory<F
...
@@ -374,6 +387,7 @@ public class FailoverGatewayFilterFactory extends AbstractGatewayFilterFactory<F
exchange
.
getResponse
().
setStatusCode
(
responseStatus
);
exchange
.
getResponse
().
setStatusCode
(
responseStatus
);
exchange
.
getResponse
().
getHeaders
().
putAll
(
clientResponse
.
headers
().
asHttpHeaders
());
exchange
.
getResponse
().
getHeaders
().
putAll
(
clientResponse
.
headers
().
asHttpHeaders
());
exchange
.
getResponse
().
getHeaders
().
add
(
REQUEST_ID
,
exchange
.
getAttributes
().
get
(
REQUEST_ID
).
toString
());
DataBuffer
buffer
=
exchange
.
getResponse
().
bufferFactory
()
DataBuffer
buffer
=
exchange
.
getResponse
().
bufferFactory
()
.
wrap
(
body
.
getBytes
(
StandardCharsets
.
UTF_8
));
.
wrap
(
body
.
getBytes
(
StandardCharsets
.
UTF_8
));
return
exchange
.
getResponse
().
writeWith
(
Mono
.
just
(
buffer
));
return
exchange
.
getResponse
().
writeWith
(
Mono
.
just
(
buffer
));
...
...
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