Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
D
device-new-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
赵啸非
device-new-platform
Commits
b8ab7faa
Commit
b8ab7faa
authored
Jan 13, 2023
by
赵啸非
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
修改udp获取ip方式
parent
d0cc8dad
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
70 additions
and
27 deletions
+70
-27
device-manager/pom.xml
device-manager/pom.xml
+1
-16
device-manager/src/main/java/com/mortals/xhx/daemon/netty/server/controlserver/handler/NettyUDPServerHandler.java
...y/server/controlserver/handler/NettyUDPServerHandler.java
+69
-11
No files found.
device-manager/pom.xml
View file @
b8ab7faa
...
...
@@ -60,10 +60,6 @@
<id>
test
</id>
<properties>
<profiles.active>
test
</profiles.active>
<profiles.server.ip>
192.168.0.98
</profiles.server.ip>
<profiles.server.port>
18222
</profiles.server.port>
<profiles.nginx.port>
11091
</profiles.nginx.port>
<profiles.server.gatewayport>
11078
</profiles.server.gatewayport>
<profiles.server.path>
/m
</profiles.server.path>
<profiles.publish.path>
/home/publish
</profiles.publish.path>
<profiles.platform.type>
cloud
</profiles.platform.type>
...
...
@@ -87,7 +83,6 @@
<profiles.sms.type>
3
</profiles.sms.type>
<profiles.filepath>
/mortals/app/data
</profiles.filepath>
<profiles.log.level>
INFO
</profiles.log.level>
<profiles.data.path>
/data
</profiles.data.path>
<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
</profiles.nacos.namespace>
...
...
@@ -100,10 +95,6 @@
<id>
product
</id>
<properties>
<profiles.active>
product
</profiles.active>
<profiles.server.ip>
192.168.0.251
</profiles.server.ip>
<profiles.server.port>
17002
</profiles.server.port>
<profiles.nginx.port>
11099
</profiles.nginx.port>
<profiles.server.gatewayport>
11078
</profiles.server.gatewayport>
<profiles.server.path>
/sampleform
</profiles.server.path>
<profiles.publish.path>
/home/publish
</profiles.publish.path>
<profiles.server.port>
18222
</profiles.server.port>
...
...
@@ -128,7 +119,6 @@
<profiles.sms.type>
3
</profiles.sms.type>
<profiles.filepath>
/mortals/app/data
</profiles.filepath>
<profiles.log.level>
INFO
</profiles.log.level>
<profiles.data.path>
/data
</profiles.data.path>
<profiles.nacos.server-addr>
127.0.0.1:8848
</profiles.nacos.server-addr>
<profiles.nacos.group>
DEFAULT_GROUP
</profiles.nacos.group>
<profiles.nacos.namespace>
smart-gov
</profiles.nacos.namespace>
...
...
@@ -142,10 +132,6 @@
<id>
yibin
</id>
<properties>
<profiles.active>
yibin
</profiles.active>
<profiles.server.ip>
192.168.2.144
</profiles.server.ip>
<profiles.server.port>
17002
</profiles.server.port>
<profiles.nginx.port>
11099
</profiles.nginx.port>
<profiles.server.gatewayport>
11078
</profiles.server.gatewayport>
<profiles.server.path>
/sampleform
</profiles.server.path>
<profiles.publish.path>
/home/publish
</profiles.publish.path>
<profiles.server.port>
18222
</profiles.server.port>
...
...
@@ -159,7 +145,7 @@
<profiles.redis.username></profiles.redis.username>
<profiles.redis.password>
hotel@2020
</profiles.redis.password>
<profiles.redis.database>
6
</profiles.redis.database>
<profiles.rabbitmq.host>
1
92.168.2.144
</profiles.rabbitmq.host>
<profiles.rabbitmq.host>
1
27.0.0.1
</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>
...
...
@@ -171,7 +157,6 @@
<profiles.log.path>
/home/mortals/app/logs
</profiles.log.path>
<profiles.filepath>
/home/mortals/app/data
</profiles.filepath>
<profiles.log.level>
INFO
</profiles.log.level>
<profiles.data.path>
/data
</profiles.data.path>
<profiles.nacos.server-addr>
127.0.0.1:8848
</profiles.nacos.server-addr>
<profiles.nacos.group>
DEFAULT_GROUP
</profiles.nacos.group>
<profiles.nacos.namespace>
smart-gov
</profiles.nacos.namespace>
...
...
device-manager/src/main/java/com/mortals/xhx/daemon/netty/server/controlserver/handler/NettyUDPServerHandler.java
View file @
b8ab7faa
package
com.mortals.xhx.daemon.netty.server.controlserver.handler
;
import
cn.hutool.core.util.URLUtil
;
import
cn.hutool.extra.spring.SpringUtil
;
import
com.alibaba.fastjson.JSON
;
import
com.alibaba.fastjson.JSONObject
;
import
com.mortals.framework.ap.GlobalSysInfo
;
...
...
@@ -18,8 +19,8 @@ import io.netty.util.CharsetUtil;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.util.ObjectUtils
;
import
java.net.
InetSocketAddress
;
import
java.
net.URL
;
import
java.net.
*
;
import
java.
util.Enumeration
;
import
static
com
.
mortals
.
xhx
.
common
.
key
.
Constant
.*;
...
...
@@ -35,13 +36,13 @@ public class NettyUDPServerHandler extends SimpleChannelInboundHandler<DatagramP
//@Value("${application.registerApiPath:/api/register}")
private
String
registerApiPath
=
"/api/register"
;
private
String
registerApiPath
=
"/api/register"
;
//@Value("${application.deviceInitApiPath:/api/deviceInit}")
private
String
deviceInitApiPath
=
"/api/deviceInit"
;
private
String
deviceInitApiPath
=
"/api/deviceInit"
;
//@Value("${application.deviceUpdateApiPath:/api/deviceUpdate}")
private
String
deviceUpdateApiPath
=
"/api/deviceUpdate"
;
private
String
deviceUpdateApiPath
=
"/api/deviceUpdate"
;
//@Value("${server.servlet.context-path:/m}")
private
String
servletPath
=
"/m"
;
private
String
servletPath
=
"/m"
;
private
DeviceService
deviceService
;
...
...
@@ -55,6 +56,8 @@ public class NettyUDPServerHandler extends SimpleChannelInboundHandler<DatagramP
@Override
protected
void
channelRead0
(
ChannelHandlerContext
ctx
,
DatagramPacket
packet
)
throws
Exception
{
String
serverPort
=
SpringUtil
.
getProperty
(
"server.port"
);
ByteBuf
byteBuf
=
packet
.
copy
().
content
();
byte
[]
bytes
=
new
byte
[
byteBuf
.
readableBytes
()];
byteBuf
.
readBytes
(
bytes
);
...
...
@@ -70,11 +73,19 @@ public class NettyUDPServerHandler extends SimpleChannelInboundHandler<DatagramP
String
action
=
jsonObject
.
getString
(
"action"
);
resp
.
setMsg
(
"获取服务端地址成功!"
);
if
(!
ObjectUtils
.
isEmpty
(
action
)
&&
"findserver"
.
equals
(
action
))
{
String
domain
=
GlobalSysInfo
.
getParamValue
(
PARAM_SERVER_HTTP_URL
,
"http://192.168.0.98:11091"
);
String
domain
=
""
;
String
currentIp
=
getInet4Address
();
if
(
currentIp
!=
null
)
{
domain
=
"http://"
+
currentIp
+
":"
+
serverPort
;
}
else
{
domain
=
GlobalSysInfo
.
getParamValue
(
PARAM_SERVER_HTTP_URL
,
"http://192.168.0.98:11091"
);
}
URL
url
=
URLUtil
.
url
(
domain
);
String
deviceRegisterApi
=
servletPath
+
registerApiPath
;
String
deviceInitApi
=
servletPath
+
deviceInitApiPath
;
log
.
info
(
"getServerUrl:"
+
domain
);
String
deviceRegisterApi
=
servletPath
+
registerApiPath
;
String
deviceInitApi
=
servletPath
+
deviceInitApiPath
;
String
deviceUpdateApi
=
servletPath
+
deviceUpdateApiPath
;
ServerInfo
serverInfo
=
new
ServerInfo
();
...
...
@@ -103,6 +114,48 @@ public class NettyUDPServerHandler extends SimpleChannelInboundHandler<DatagramP
}
}
public
static
InetAddress
getCurrentIp
()
{
try
{
Enumeration
<
NetworkInterface
>
networkInterfaces
=
NetworkInterface
.
getNetworkInterfaces
();
while
(
networkInterfaces
.
hasMoreElements
())
{
NetworkInterface
ni
=
(
NetworkInterface
)
networkInterfaces
.
nextElement
();
Enumeration
<
InetAddress
>
nias
=
ni
.
getInetAddresses
();
while
(
nias
.
hasMoreElements
())
{
InetAddress
ia
=
(
InetAddress
)
nias
.
nextElement
();
if
(!
ia
.
isLinkLocalAddress
()
&&
!
ia
.
isLoopbackAddress
()
&&
ia
instanceof
Inet4Address
)
{
return
ia
;
}
}
}
}
catch
(
SocketException
e
)
{
log
.
error
(
"获取ip异常"
,
e
);
}
return
null
;
}
public
static
String
getInet4Address
()
{
Enumeration
<
NetworkInterface
>
nis
;
String
ip
=
null
;
try
{
nis
=
NetworkInterface
.
getNetworkInterfaces
();
for
(;
nis
.
hasMoreElements
();
)
{
NetworkInterface
ni
=
nis
.
nextElement
();
Enumeration
<
InetAddress
>
ias
=
ni
.
getInetAddresses
();
for
(;
ias
.
hasMoreElements
();
)
{
InetAddress
ia
=
ias
.
nextElement
();
//ia instanceof Inet6Address && !ia.equals("")
if
(
ia
instanceof
Inet4Address
&&
!
ia
.
getHostAddress
().
equals
(
"127.0.0.1"
))
{
ip
=
ia
.
getHostAddress
();
}
}
}
}
catch
(
SocketException
e
)
{
// TODO Auto-generated catch block
e
.
printStackTrace
();
}
return
ip
;
}
@Override
public
void
exceptionCaught
(
ChannelHandlerContext
ctx
,
Throwable
cause
)
throws
Exception
{
// TODO Auto-generated method stub
...
...
@@ -112,9 +165,14 @@ public class NettyUDPServerHandler extends SimpleChannelInboundHandler<DatagramP
public
static
void
main
(
String
[]
args
)
{
String
domain
=
"http://192.168.0.98:11801"
;
/*
String domain="http://192.168.0.98:11801";
URL url = URLUtil.url(domain);
System.out.println(url.getHost());
System
.
out
.
println
(
url
.
getPort
());
System.out.println(url.getPort());*/
InetAddress
currentIp
=
NettyUDPServerHandler
.
getCurrentIp
();
System
.
out
.
println
(
currentIp
.
getHostName
());
System
.
out
.
println
(
getInet4Address
());
}
}
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