Commit b8ab7faa authored by 赵啸非's avatar 赵啸非

修改udp获取ip方式

parent d0cc8dad
......@@ -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>192.168.2.144</profiles.rabbitmq.host>
<profiles.rabbitmq.host>127.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>
......
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());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment