From 9ee68cd85ac6c5a63ffce687002bf856f21bf66a Mon Sep 17 00:00:00 2001 From: linwl <304115325@qq.com> Date: Mon, 18 Jan 2021 20:53:34 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=88=B0docker=E5=A4=96?= =?UTF-8?q?=E8=BF=90=E8=A1=8C=E7=8E=AF=E5=A2=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DipperPositionApplication.java | 13 +- .../handler/NettyServerHandler.java | 2 - .../server/DipperPositionServer.java | 127 ++++-------------- .../dipperposition/server/EphAsyncServer.java | 106 --------------- .../server/TimeAsyncServer.java | 75 ++++------- 5 files changed, 51 insertions(+), 272 deletions(-) delete mode 100644 src/main/java/com/telpo/dipperposition/server/EphAsyncServer.java diff --git a/src/main/java/com/telpo/dipperposition/DipperPositionApplication.java b/src/main/java/com/telpo/dipperposition/DipperPositionApplication.java index 055577f..d5fae42 100644 --- a/src/main/java/com/telpo/dipperposition/DipperPositionApplication.java +++ b/src/main/java/com/telpo/dipperposition/DipperPositionApplication.java @@ -1,28 +1,17 @@ package com.telpo.dipperposition; -import com.alibaba.nacos.api.NacosFactory; -import com.alibaba.nacos.api.config.ConfigService; -import com.alibaba.nacos.api.config.listener.Listener; -import com.alibaba.nacos.api.PropertyKeyConst; -import com.alibaba.nacos.api.exception.NacosException; -import com.telpo.dipperposition.co.PositionConfigInfo; import com.telpo.dipperposition.server.DipperPositionServer; -import com.telpo.dipperposition.server.EphAsyncServer; import com.telpo.dipperposition.server.TimeAsyncServer; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.properties.ConfigurationPropertiesScan; -import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableScheduling; -import java.util.Properties; -import java.util.concurrent.Executor; - /** * @program: gateway * @description: 网关启动类 @@ -44,6 +33,8 @@ public class DipperPositionApplication { log.info("北斗定位服务启动!"); //启动服务端 ConfigurableEnvironment environment = applicationContext.getEnvironment(); + TimeAsyncServer timeAsyncServer = new TimeAsyncServer(environment); + timeAsyncServer.start(); DipperPositionServer nettyServer = new DipperPositionServer(environment); nettyServer.start(); } catch (Exception e) { diff --git a/src/main/java/com/telpo/dipperposition/handler/NettyServerHandler.java b/src/main/java/com/telpo/dipperposition/handler/NettyServerHandler.java index 26f0b33..0edfe40 100644 --- a/src/main/java/com/telpo/dipperposition/handler/NettyServerHandler.java +++ b/src/main/java/com/telpo/dipperposition/handler/NettyServerHandler.java @@ -124,8 +124,6 @@ public class NettyServerHandler extends ChannelInboundHandlerAdapter { // if (Integer.parseInt(posAsycServerPort) == channel.localAddress().getPort()) { //if (AST_POS_CMD.equals(recvmg)) { - log.info("config info is : " + positionConfig.getCenterProvinceFilePath() + positionConfig.getCenterProvince() + positionConfig.getIpPositionRequestPath() + - positionConfig.getIpPositionRequestKey()); if (AST_POS_CMD.equals(msg)) { // channelAns = dipperAstPosAsyncTaskService.pushAstPos(ipAddress, // positionConfig.getCenterProvinceFilePath(), diff --git a/src/main/java/com/telpo/dipperposition/server/DipperPositionServer.java b/src/main/java/com/telpo/dipperposition/server/DipperPositionServer.java index d8e5ebc..0055fc2 100644 --- a/src/main/java/com/telpo/dipperposition/server/DipperPositionServer.java +++ b/src/main/java/com/telpo/dipperposition/server/DipperPositionServer.java @@ -69,101 +69,22 @@ public class DipperPositionServer { // ex.printStackTrace(); // } } -// /* -// * 时间同步进程线程 -// */ -// public void startTimeAsnc() { -// -// //new 一个主线程组 -// EventLoopGroup mainThreadGroup = new NioEventLoopGroup(1); -// //new 一个工作线程组 -// EventLoopGroup workThreadGroup = new NioEventLoopGroup(200); -// InetSocketAddress socketAddress = new InetSocketAddress(serverAddr, -// timeAsycPort); -// ServerBootstrap bootstrap = new ServerBootstrap() -// .group(mainThreadGroup, workThreadGroup) -// .channel(NioServerSocketChannel.class) -// .childHandler(new ServerChannelInitializer()) -// .localAddress(socketAddress) -// //设置队列大小 -// .option(ChannelOption.SO_BACKLOG, 1024) -// // 两小时内没有数据的通信时,TCP会自动发送一个活动探测数据报文 -// .childOption(ChannelOption.SO_KEEPALIVE, true); -// -// -// //绑定端口,开始接收进来的连接 -// try { -// ChannelFuture future = bootstrap.bind(socketAddress).sync(); -// log.info("服务器启动开始监听端口: {}", socketAddress.getPort()); -// -// -// future.channel().closeFuture().sync(); -// } catch (InterruptedException e) { -// e.printStackTrace(); -// } finally { -// //关闭主线程组 -// mainThreadGroup.shutdownGracefully(); -// //关闭工作线程组 -// workThreadGroup.shutdownGracefully(); -// } -// } - -// /* -// * 时间同步进程线程 -// */ -// public void startPosAsnc() { -// -// //new 一个主线程组 -// EventLoopGroup mainThreadGroup = new NioEventLoopGroup(1); -// //new 一个工作线程组 -// EventLoopGroup workThreadGroup = new NioEventLoopGroup(200); -// InetSocketAddress socketAddress = new InetSocketAddress(serverAddr, -// posAsycPort); -// ServerBootstrap bootstrap = new ServerBootstrap() -// .group(mainThreadGroup, workThreadGroup) -// .channel(NioServerSocketChannel.class) -// .childHandler(new ServerChannelInitializer()) -// .localAddress(socketAddress) -// //设置队列大小 -// .option(ChannelOption.SO_BACKLOG, 1024) -// // 两小时内没有数据的通信时,TCP会自动发送一个活动探测数据报文 -// .childOption(ChannelOption.SO_KEEPALIVE, true); -// -// -// //绑定端口,开始接收进来的连接 -// try { -// ChannelFuture future = bootstrap.bind(socketAddress).sync(); -// log.info("服务器启动开始监听端口: {}", socketAddress.getPort()); -// -// -// future.channel().closeFuture().sync(); -// } catch (InterruptedException e) { -// e.printStackTrace(); -// } finally { -// //关闭主线程组 -// mainThreadGroup.shutdownGracefully(); -// //关闭工作线程组 -// workThreadGroup.shutdownGracefully(); -// } -// } - /* * 星历同步进程线程 */ - //public void startStarsAsnc() { public void start() { //new 一个主线程组 EventLoopGroup mainThreadGroup = new NioEventLoopGroup(1); //new 一个工作线程组 EventLoopGroup workThreadGroup = new NioEventLoopGroup(200); - InetSocketAddress socketAddress = new InetSocketAddress(serverAddr,9011); + //InetSocketAddress socketAddress = new InetSocketAddress(serverAddr,9011); ServerBootstrap bootstrap = new ServerBootstrap() .group(mainThreadGroup, workThreadGroup) .channel(NioServerSocketChannel.class) .childHandler(new ServerChannelInitializer()) - .localAddress(socketAddress) + // .localAddress(socketAddress) //设置队列大小 .option(ChannelOption.SO_BACKLOG, 1024) // 两小时内没有数据的通信时,TCP会自动发送一个活动探测数据报文 @@ -172,28 +93,28 @@ public class DipperPositionServer { //绑定端口,开始接收进来的连接 try { - ChannelFuture channelFuture = bootstrap.bind(timeAsycPort).sync(); - log.info("时间辅助服务器启动开始监听端口: {}", timeAsycPort); - channelFuture.addListener(future -> { - if (future.isSuccess()){ - System.out.println("start success"); - }else{ - System.out.println("start failed"); - } - }); - channelFuture.channel().closeFuture().sync(); - - ChannelFuture channelFuture2 = bootstrap.bind(posAsycPort).sync(); - log.info("位置服务器启动开始监听端口: {}", posAsycPort); - channelFuture2.addListener(future -> { - if (future.isSuccess()){ - System.out.println("start success"); - }else{ - System.out.println("start failed"); - } - }); - channelFuture2.channel().closeFuture().sync(); - +// ChannelFuture channelFuture = bootstrap.bind(timeAsycPort).sync(); +// log.info("时间辅助服务器启动开始监听端口: {}", timeAsycPort); +// channelFuture.addListener(future -> { +// if (future.isSuccess()){ +// System.out.println("start success"); +// }else{ +// System.out.println("start failed"); +// } +// }); +// channelFuture.channel().closeFuture().sync(); +// +// ChannelFuture channelFuture2 = bootstrap.bind(posAsycPort).sync(); +// log.info("位置服务器启动开始监听端口: {}", posAsycPort); +// channelFuture2.addListener(future -> { +// if (future.isSuccess()){ +// System.out.println("start success"); +// }else{ +// System.out.println("start failed"); +// } +// }); +// channelFuture2.channel().closeFuture().sync(); +// ChannelFuture channelFuture3 = bootstrap.bind(starsAsycPort).sync(); log.info("星历服务器启动开始监听端口: {}", starsAsycPort); diff --git a/src/main/java/com/telpo/dipperposition/server/EphAsyncServer.java b/src/main/java/com/telpo/dipperposition/server/EphAsyncServer.java deleted file mode 100644 index 28eca89..0000000 --- a/src/main/java/com/telpo/dipperposition/server/EphAsyncServer.java +++ /dev/null @@ -1,106 +0,0 @@ -package com.telpo.dipperposition.server; - -import com.alibaba.nacos.api.NacosFactory; -import com.alibaba.nacos.api.PropertyKeyConst; -import com.alibaba.nacos.api.config.ConfigService; -import com.alibaba.nacos.api.exception.NacosException; -import com.telpo.dipperposition.handler.ServerChannelInitializer; -import io.netty.bootstrap.ServerBootstrap; -import io.netty.channel.ChannelFuture; -import io.netty.channel.ChannelOption; -import io.netty.channel.EventLoopGroup; -import io.netty.channel.nio.NioEventLoopGroup; -import io.netty.channel.socket.nio.NioServerSocketChannel; -import lombok.extern.slf4j.Slf4j; -import org.yaml.snakeyaml.Yaml; - -import java.io.ByteArrayInputStream; -import java.net.Inet4Address; -import java.net.InetAddress; -import java.net.InetSocketAddress; -import java.util.Map; -import java.util.Properties; - - -/** - * @program: DipperPositionServer - * @description: 北斗定位 - * @author: king - * @create: 2021-01-13 14:01 - */ -@Slf4j -public class EphAsyncServer { - - // @Autowired PositionConfigInfo positionConfigInfo; - private String serverAddr; - private Integer starsAsycPort; - - public EphAsyncServer() throws NacosException { - //String serverAddr = positionConfigInfo.getServerAddr(); - try { - try { - InetAddress ip4 = Inet4Address.getLocalHost(); - serverAddr = ip4.getHostAddress(); - } catch (Exception ex) { - serverAddr = "172.16.192.26"; - ex.printStackTrace(); - } - String dataId = "dipperposition-service"; - String group = "DEFAULT_GROUP"; - Properties properties = new Properties(); - properties.put(PropertyKeyConst.SERVER_ADDR, serverAddr); - ConfigService configService = NacosFactory.createConfigService(properties); - String content = configService.getConfig(dataId, group, 5000); - - ByteArrayInputStream tInputStringStream = new ByteArrayInputStream(content.getBytes()); - Yaml yaml = new Yaml(); - Map m1 = yaml.load(tInputStringStream); - Map m2 = (Map) m1.get("position-server"); - this.serverAddr = (String)m2.get("serverAddr"); - this.starsAsycPort = (Integer)m2.get("starsAsycPort"); - //log.info("Map server is:" + m2.get("serverAddr")); - } catch (Exception ex) { - ex.printStackTrace(); - } - //log.info("Config serverAddr is " + serverAddr); - } - - /* - * 星历同步进程线程 - */ - public void startStarsAsnc() { - - //new 一个主线程组 - EventLoopGroup mainThreadGroup = new NioEventLoopGroup(1); - //new 一个工作线程组 - EventLoopGroup workThreadGroup = new NioEventLoopGroup(200); - InetSocketAddress socketAddress = new InetSocketAddress(serverAddr, - starsAsycPort); - ServerBootstrap bootstrap = new ServerBootstrap() - .group(mainThreadGroup, workThreadGroup) - .channel(NioServerSocketChannel.class) - .childHandler(new ServerChannelInitializer()) - .localAddress(socketAddress) - //设置队列大小 - .option(ChannelOption.SO_BACKLOG, 1024) - // 两小时内没有数据的通信时,TCP会自动发送一个活动探测数据报文 - .childOption(ChannelOption.SO_KEEPALIVE, true); - - - //绑定端口,开始接收进来的连接 - try { - ChannelFuture future = bootstrap.bind(socketAddress).sync(); - log.info("服务器启动开始监听端口: {}", socketAddress.getPort()); - - - future.channel().closeFuture().sync(); - } catch (InterruptedException e) { - e.printStackTrace(); - } finally { - //关闭主线程组 - mainThreadGroup.shutdownGracefully(); - //关闭工作线程组 - workThreadGroup.shutdownGracefully(); - } - } -} diff --git a/src/main/java/com/telpo/dipperposition/server/TimeAsyncServer.java b/src/main/java/com/telpo/dipperposition/server/TimeAsyncServer.java index 110e470..c06344b 100644 --- a/src/main/java/com/telpo/dipperposition/server/TimeAsyncServer.java +++ b/src/main/java/com/telpo/dipperposition/server/TimeAsyncServer.java @@ -1,8 +1,5 @@ package com.telpo.dipperposition.server; -import com.alibaba.nacos.api.NacosFactory; -import com.alibaba.nacos.api.PropertyKeyConst; -import com.alibaba.nacos.api.config.ConfigService; import com.alibaba.nacos.api.exception.NacosException; import com.telpo.dipperposition.handler.ServerChannelInitializer; import io.netty.bootstrap.ServerBootstrap; @@ -12,18 +9,12 @@ import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioServerSocketChannel; import lombok.extern.slf4j.Slf4j; -import org.yaml.snakeyaml.Yaml; +import org.springframework.core.env.ConfigurableEnvironment; -import java.io.ByteArrayInputStream; -import java.net.Inet4Address; -import java.net.InetAddress; import java.net.InetSocketAddress; -import java.util.Map; -import java.util.Properties; - /** - * @program: DipperPositionServer + * @program: TimeAsyncServer * @description: 北斗定位 * @author: king * @create: 2021-01-13 14:01 @@ -33,53 +24,32 @@ public class TimeAsyncServer { // @Autowired PositionConfigInfo positionConfigInfo; private String serverAddr; + private Integer serverPort; private Integer timeAsycPort; + private Integer posAsycPort; + private Integer starsAsycPort; - public TimeAsyncServer() throws NacosException { - //String serverAddr = positionConfigInfo.getServerAddr(); - try { - try { - InetAddress ip4 = Inet4Address.getLocalHost(); - serverAddr = ip4.getHostAddress(); - } catch (Exception ex) { - serverAddr = "172.16.192.26"; - ex.printStackTrace(); - } - String dataId = "dipperposition-service"; - String group = "DEFAULT_GROUP"; - Properties properties = new Properties(); - properties.put(PropertyKeyConst.SERVER_ADDR, serverAddr); - ConfigService configService = NacosFactory.createConfigService(properties); - String content = configService.getConfig(dataId, group, 5000); + public TimeAsyncServer(ConfigurableEnvironment environment) throws NacosException { - ByteArrayInputStream tInputStringStream = new ByteArrayInputStream(content.getBytes()); - Yaml yaml = new Yaml(); - Map m1 = yaml.load(tInputStringStream); - Map m2 = (Map) m1.get("position-server"); - this.serverAddr = (String)m2.get("serverAddr"); - this.timeAsycPort = (Integer)m2.get("timeAsycPort"); - //log.info("Map server is:" + m2.get("serverAddr")); - } catch (Exception ex) { - ex.printStackTrace(); - } - //log.info("Config serverAddr is " + serverAddr); + this.serverAddr = environment.getProperty("position-server.serverAddr"); + this.serverPort = Integer.parseInt(environment.getProperty("server.port")); + this.timeAsycPort = Integer.parseInt(environment.getProperty("position-server.timeAsycPort")); + this.posAsycPort = Integer.parseInt(environment.getProperty("position-server.posAsycPort")); + this.starsAsycPort = Integer.parseInt(environment.getProperty("position-server.starsAsycPort")); } - /* - * 时间同步进程线程 - */ - public void startTimeAsnc() { + + public void start() { //new 一个主线程组 EventLoopGroup mainThreadGroup = new NioEventLoopGroup(1); //new 一个工作线程组 EventLoopGroup workThreadGroup = new NioEventLoopGroup(200); - InetSocketAddress socketAddress = new InetSocketAddress(serverAddr, - timeAsycPort); + //InetSocketAddress socketAddress = new InetSocketAddress(serverAddr,null); ServerBootstrap bootstrap = new ServerBootstrap() .group(mainThreadGroup, workThreadGroup) .channel(NioServerSocketChannel.class) .childHandler(new ServerChannelInitializer()) - .localAddress(socketAddress) + //.localAddress(socketAddress) //设置队列大小 .option(ChannelOption.SO_BACKLOG, 1024) // 两小时内没有数据的通信时,TCP会自动发送一个活动探测数据报文 @@ -88,11 +58,17 @@ public class TimeAsyncServer { //绑定端口,开始接收进来的连接 try { - ChannelFuture future = bootstrap.bind(socketAddress).sync(); - log.info("服务器启动开始监听端口: {}", socketAddress.getPort()); - + ChannelFuture channelFuture = bootstrap.bind(timeAsycPort).sync(); + log.info("时间辅助服务器启动开始监听端口: {}", timeAsycPort); + channelFuture.addListener(future -> { + if (future.isSuccess()){ + System.out.println("start success"); + }else{ + System.out.println("start failed"); + } + }); - future.channel().closeFuture().sync(); + channelFuture.channel().closeFuture().sync(); } catch (InterruptedException e) { e.printStackTrace(); } finally { @@ -102,5 +78,4 @@ public class TimeAsyncServer { workThreadGroup.shutdownGracefully(); } } - }