Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
喜欢杯子
microservices-platform
提交
c0412257
microservices-platform
项目概览
喜欢杯子
/
microservices-platform
与 Fork 源项目一致
Fork自
whatyn / microservices-platform
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
microservices-platform
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
c0412257
编写于
9月 08, 2019
作者:
zlt2000
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
优化ip获取
上级
4d7dbb7b
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
77 addition
and
37 deletion
+77
-37
zlt-commons/zlt-common-spring-boot-starter/src/main/java/com/central/common/utils/AddrUtil.java
...rter/src/main/java/com/central/common/utils/AddrUtil.java
+71
-0
zlt-gateway/zuul-gateway/src/main/java/com/central/gateway/filter/pre/RequestStatisticsFilter.java
...m/central/gateway/filter/pre/RequestStatisticsFilter.java
+6
-37
未找到文件。
zlt-commons/zlt-common-spring-boot-starter/src/main/java/com/central/common/utils/AddrUtil.java
0 → 100644
浏览文件 @
c0412257
package
com.central.common.utils
;
import
cn.hutool.core.util.StrUtil
;
import
lombok.extern.slf4j.Slf4j
;
import
javax.servlet.http.HttpServletRequest
;
import
java.net.InetAddress
;
import
java.net.UnknownHostException
;
/**
* @author zlt
* @date 2019/9/8
*/
@Slf4j
public
class
AddrUtil
{
private
final
static
String
UNKNOWN_STR
=
"unknown"
;
/**
* 获取客户端IP地址
*/
public
static
String
getRemoteAddr
(
HttpServletRequest
request
)
{
String
ip
=
request
.
getHeader
(
"X-Forwarded-For"
);
if
(
isEmptyIP
(
ip
))
{
ip
=
request
.
getHeader
(
"Proxy-Client-IP"
);
if
(
isEmptyIP
(
ip
))
{
ip
=
request
.
getHeader
(
"WL-Proxy-Client-IP"
);
if
(
isEmptyIP
(
ip
))
{
ip
=
request
.
getHeader
(
"HTTP_CLIENT_IP"
);
if
(
isEmptyIP
(
ip
))
{
ip
=
request
.
getHeader
(
"HTTP_X_FORWARDED_FOR"
);
if
(
isEmptyIP
(
ip
))
{
ip
=
request
.
getRemoteAddr
();
if
(
"127.0.0.1"
.
equals
(
ip
)
||
"0:0:0:0:0:0:0:1"
.
equals
(
ip
))
{
// 根据网卡取本机配置的IP
ip
=
getLocalAddr
();
}
}
}
}
}
}
else
if
(
ip
.
length
()
>
15
)
{
String
[]
ips
=
ip
.
split
(
","
);
for
(
int
index
=
0
;
index
<
ips
.
length
;
index
++)
{
String
strIp
=
ips
[
index
];
if
(!
isEmptyIP
(
ip
))
{
ip
=
strIp
;
break
;
}
}
}
return
ip
;
}
private
static
boolean
isEmptyIP
(
String
ip
)
{
if
(
StrUtil
.
isEmpty
(
ip
)
||
UNKNOWN_STR
.
equalsIgnoreCase
(
ip
))
{
return
true
;
}
return
false
;
}
/**
* 获取本机的IP地址
*/
public
static
String
getLocalAddr
()
{
try
{
return
InetAddress
.
getLocalHost
().
getHostAddress
();
}
catch
(
UnknownHostException
e
)
{
log
.
error
(
"InetAddress.getLocalHost()-error"
,
e
);
}
return
""
;
}
}
zlt-gateway/zuul-gateway/src/main/java/com/central/gateway/filter/pre/RequestStatisticsFilter.java
浏览文件 @
c0412257
package
com.central.gateway.filter.pre
;
package
com.central.gateway.filter.pre
;
import
c
n.hutool.core.util.St
rUtil
;
import
c
om.central.common.utils.Add
rUtil
;
import
com.central.log.monitor.PointUtil
;
import
com.central.log.monitor.PointUtil
;
import
com.netflix.zuul.ZuulFilter
;
import
com.netflix.zuul.ZuulFilter
;
import
com.netflix.zuul.context.RequestContext
;
import
com.netflix.zuul.context.RequestContext
;
...
@@ -20,8 +20,6 @@ import javax.servlet.http.HttpServletRequest;
...
@@ -20,8 +20,6 @@ import javax.servlet.http.HttpServletRequest;
@Slf4j
@Slf4j
@Component
@Component
public
class
RequestStatisticsFilter
extends
ZuulFilter
{
public
class
RequestStatisticsFilter
extends
ZuulFilter
{
private
final
static
String
UNKNOWN_STR
=
"unknown"
;
@Override
@Override
public
String
filterType
()
{
public
String
filterType
()
{
return
FilterConstants
.
PRE_TYPE
;
return
FilterConstants
.
PRE_TYPE
;
...
@@ -44,40 +42,11 @@ public class RequestStatisticsFilter extends ZuulFilter {
...
@@ -44,40 +42,11 @@ public class RequestStatisticsFilter extends ZuulFilter {
UserAgent
userAgent
=
UserAgent
.
parseUserAgentString
(
req
.
getHeader
(
"User-Agent"
));
UserAgent
userAgent
=
UserAgent
.
parseUserAgentString
(
req
.
getHeader
(
"User-Agent"
));
//埋点
//埋点
PointUtil
.
debug
(
"0"
,
"request-statistics"
,
PointUtil
.
debug
(
"0"
,
"request-statistics"
,
"ip="
+
this
.
getIpAddr
(
req
)
"ip="
+
AddrUtil
.
getRemoteAddr
(
req
)
+
"&browser="
+
userAgent
.
getBrowser
()
+
"&browser="
+
userAgent
.
getBrowser
()
+
"&operatingSystem="
+
userAgent
.
getOperatingSystem
());
+
"&operatingSystem="
+
userAgent
.
getOperatingSystem
());
return
null
;
}
/**
return
null
;
* 获取Ip地址
*/
public
String
getIpAddr
(
HttpServletRequest
request
){
String
ip
=
request
.
getHeader
(
"X-Forwarded-For"
);
if
(
isEmptyIP
(
ip
))
{
ip
=
request
.
getHeader
(
"Proxy-Client-IP"
);
if
(
isEmptyIP
(
ip
))
{
ip
=
request
.
getHeader
(
"WL-Proxy-Client-IP"
);
if
(
isEmptyIP
(
ip
))
{
ip
=
request
.
getHeader
(
"HTTP_CLIENT_IP"
);
if
(
isEmptyIP
(
ip
))
{
ip
=
request
.
getHeader
(
"HTTP_X_FORWARDED_FOR"
);
if
(
isEmptyIP
(
ip
))
{
ip
=
request
.
getRemoteAddr
();
}
}
}
}
}
return
ip
;
}
private
boolean
isEmptyIP
(
String
ip
)
{
if
(
StrUtil
.
isEmpty
(
ip
)
||
UNKNOWN_STR
.
equalsIgnoreCase
(
ip
))
{
return
true
;
}
return
false
;
}
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录