From 2df02f0cd003797d46b3e91cc47a0b635b53b29f Mon Sep 17 00:00:00 2001 From: linwl <304115325@qq.com> Date: Fri, 15 Jan 2021 18:46:07 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0OkHttp=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E8=AF=BB=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DipperPositionApplication.java | 8 +- .../dipperposition/server/EphAsyncServer.java | 106 ++++++++++++++++++ .../server/TimeAsyncServer.java | 106 ++++++++++++++++++ 3 files changed, 217 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/telpo/dipperposition/server/EphAsyncServer.java create mode 100644 src/main/java/com/telpo/dipperposition/server/TimeAsyncServer.java diff --git a/src/main/java/com/telpo/dipperposition/DipperPositionApplication.java b/src/main/java/com/telpo/dipperposition/DipperPositionApplication.java index b7fe236..b76b1e2 100644 --- a/src/main/java/com/telpo/dipperposition/DipperPositionApplication.java +++ b/src/main/java/com/telpo/dipperposition/DipperPositionApplication.java @@ -6,6 +6,8 @@ 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.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; @@ -39,11 +41,11 @@ public class DipperPositionApplication { try { test(); //启动服务端 - DipperPositionServer nettyServer = new DipperPositionServer(); - nettyServer.startTimeAsnc(); + TimeAsyncServer nettyServer1 = new TimeAsyncServer(); + nettyServer1.startTimeAsnc(); DipperPositionServer nettyServer2 = new DipperPositionServer(); nettyServer2.startPosAsnc(); - DipperPositionServer nettyServer3 = new DipperPositionServer(); + EphAsyncServer nettyServer3 = new EphAsyncServer(); nettyServer3.startStarsAsnc(); } catch (Exception e) { log.error(e.getMessage()); diff --git a/src/main/java/com/telpo/dipperposition/server/EphAsyncServer.java b/src/main/java/com/telpo/dipperposition/server/EphAsyncServer.java new file mode 100644 index 0000000..28eca89 --- /dev/null +++ b/src/main/java/com/telpo/dipperposition/server/EphAsyncServer.java @@ -0,0 +1,106 @@ +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 new file mode 100644 index 0000000..110e470 --- /dev/null +++ b/src/main/java/com/telpo/dipperposition/server/TimeAsyncServer.java @@ -0,0 +1,106 @@ +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 TimeAsyncServer { + + // @Autowired PositionConfigInfo positionConfigInfo; + private String serverAddr; + private Integer timeAsycPort; + + 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); + + 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); + } + /* + * 时间同步进程线程 + */ + 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(); + } + } + +}