From 1c3164c096d405471ad7bb50fce86e321157d8a2 Mon Sep 17 00:00:00 2001 From: 15815213711 <zhaochongyi@isu6.com> Date: 星期一, 14 二月 2022 09:44:22 +0800 Subject: [PATCH] 初始化提交版本 --- src/main/java/com/xncoding/pos/handler/HeartBeatClientHandler.java | 86 ++++++ .gitignore | 14 + src/main/java/com/xncoding/pos/Application.java | 21 + src/main/java/com/xncoding/pos/config/NettyProperties.java | 36 ++ src/main/java/com/xncoding/pos/utils/RunShellUtils.java | 70 +++++ pom.xml | 98 +++++++ src/main/java/com/xncoding/pos/config/ShellProperties.java | 27 ++ src/main/java/com/xncoding/pos/utils/ApplicationContextUtils.java | 27 ++ run.sh | 72 +++++ src/main/java/com/xncoding/pos/config/SmsProperties.java | 82 ++++++ LICENSE | 20 + src/main/resources/application.yml | 52 ++++ src/main/java/com/xncoding/pos/handler/ClientStarter.java | 71 +++++ src/main/java/com/xncoding/pos/utils/SmsUtils.java | 71 +++++ 14 files changed, 747 insertions(+), 0 deletions(-) diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1e3c5bb --- /dev/null +++ b/.gitignore @@ -0,0 +1,14 @@ +# 姝や负娉ㄩ噴鈥� 灏嗚Git 蹇界暐 +# /缁撳熬琛ㄧず鏄洰褰曪紝蹇界暐鐩綍鍜岀洰褰曚笅鐨勬墍鏈変欢 +# /寮�澶磋〃绀烘牴鐩綍锛屽惁鍒欐槸.gitignore鐨勭浉瀵圭洰褰� +# !寮�澶磋〃绀哄弽閫� +.idea/ +target/ +*.iml +*.ipr +*.iws +*.log +.svn/ +.project +rebel.xml +.rebel-remote.xml.* diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..83cd47d --- /dev/null +++ b/LICENSE @@ -0,0 +1,20 @@ +The MIT License (MIT) + +Copyright (c) 2018 Xiong Neng + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..829bc1a --- /dev/null +++ b/pom.xml @@ -0,0 +1,98 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <groupId>com.xncoding</groupId> + <artifactId>HeartbeatServer</artifactId> + <version>1.0.0-RELEASE</version> + <packaging>jar</packaging> + + <name>HeartbeatServer</name> + <description>蹇冭烦妫�娴�</description> + + <parent> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-parent</artifactId> + <version>2.0.4.RELEASE</version> + </parent> + + <properties> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> + <java.version>1.8</java.version> + </properties> + + <dependencies> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter</artifactId> + </dependency> + <!-- https://mvnrepository.com/artifact/io.netty/netty-all --> + <dependency> + <groupId>io.netty</groupId> + <artifactId>netty-all</artifactId> + <version>4.1.73.Final</version> + </dependency> + <dependency> + <groupId>io.netty</groupId> + <artifactId>netty-handler</artifactId> + <version>4.1.72.Final</version> + </dependency> + <!-- hutool宸ュ叿 --> + <dependency> + <groupId>cn.hutool</groupId> + <artifactId>hutool-all</artifactId> + <version>5.7.20</version> + </dependency> + <!-- 闃块噷JSON瑙f瀽鍣� --> + <dependency> + <groupId>com.alibaba</groupId> + <artifactId>fastjson</artifactId> + <version>1.2.78</version> + </dependency> + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + <version>3.8.1</version> + <configuration> + <!--<proc>none</proc>--> + <source>1.8</source> + <target>1.8</target> + </configuration> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-surefire-plugin</artifactId> + <version>2.21.0</version> + <configuration> + <skip>true</skip> + </configuration> + </plugin> + <plugin> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-maven-plugin</artifactId> + <executions> + </executions> + </plugin> + </plugins> + + <resources> + <resource> + <directory>src/main/resources</directory> + </resource> + <resource> + <directory>src/main/java</directory> + <includes> + <include>**/*.xml</include> + </includes> + </resource> + </resources> + </build> + +</project> \ No newline at end of file diff --git a/run.sh b/run.sh new file mode 100644 index 0000000..955efb1 --- /dev/null +++ b/run.sh @@ -0,0 +1,72 @@ +#!/bin/bash +# 椤圭洰鑷姩鏇存柊鑴氭湰 +# 鍏坈lone鐩稿簲鐨勫垎鏀笅鏉ワ細 +# git clone ssh://git@120.24.173.142:7999/xxx.git +# 杩滅▼璋冭瘯鍚姩锛� +# nohup java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 -Xms512m -Xmx1024m -jar -Dspring.profiles.active=${profile} ${jarfile} >/dev/null 2>&1 & + +function start { + profile="$1" + echo "鍚姩鐜profile=${profile}" + jarfile=$(ls target/*.jar) + if [[ "$?" == "0" ]]; then + stop $profile $jarfile + fi + branch=$(git branch |awk '{print $2}') + git pull origin ${branch} + echo "鏇存柊瀹屼唬鐮佸紑濮嬮噸鏂版墦鍖�" + mvn clean && mvn clean && mvn package -DskipTests=true + if [[ "$?" != "0" ]]; then + echo "缂栬瘧鍑洪敊锛岄��鍑猴紒" + exit 1 + fi + echo "nohup java -Xms512m -Xmx1024m -jar -Dspring.profiles.active=${profile} ${jarfile} >/dev/null 2>&1 &" + nohup java -Xms512m -Xmx1024m -jar -Dspring.profiles.active=${profile} ${jarfile} >/dev/null 2>&1 & + echo "鍚姩搴旂敤涓紝璇锋煡鐪嬫棩蹇楁枃浠�..." +} + +function stop { + profile="$1" + jarfile="$2" + ps aux | grep "${jarfile}" | grep "spring.profiles.active=${profile}" | grep -v grep > /dev/null + if [[ "$?" == "0" ]]; then + echo "璇ュ簲鐢ㄨ繕鍦ㄨ窇锛屾垜鍏堝仠浜嗗畠" + pid=$(ps aux | grep "${jarfile}" | grep "spring.profiles.active=${profile}" | grep -v grep |awk '{print $2}') + if [[ "$pid" != "" ]]; then + kill -9 $pid + fi + echo "鍋滄搴旂敤鎴愬姛..." + fi +} + +if [[ "$1" == "start" ]]; then + if [[ "$#" < 2 ]]; then + echo "璇疯緭鍏ユ纭弬鏁帮細./epay.sh start {profile}" + exit 1 + fi + profile="$2" + if [[ "$profile" != "dev" && "$profile" != "test" && "$profile" != "show" && "$profile" != "production" ]]; then + echo "鍙傛暟閿欒锛岃杈撳叆姝g‘鐨刾rofile鍙傛暟锛屼娇鐢ㄦ柟娉曪細" + echo "./epay.sh start {profile} ==> 鍚姩搴旂敤锛寋profile}鍙栧�硷細dev|test|show|production" + exit 1 + fi + start "${profile}" +elif [[ "$1" == "stop" ]]; then + if [[ "$#" < 2 ]]; then + echo "璇疯緭鍏ユ纭弬鏁帮細./epay.sh stop {profile}" + exit 1 + fi + profile="$2" + if [[ "$profile" != "dev" && "$profile" != "test" && "$profile" != "show" && "$profile" != "production" ]]; then + echo "鍙傛暟閿欒锛岃杈撳叆姝g‘鐨刾rofile鍙傛暟锛屼娇鐢ㄦ柟娉曪細" + echo "./epay.sh stop {profile} ==> 鍋滄搴旂敤锛寋profile}鍙栧�硷細dev|test|show|production" + exit 1 + fi + jarfile=$(ls target/*.jar) + stop $profile $jarfile +else + echo "鍙傛暟閿欒锛屼娇鐢ㄦ柟娉曪細{}鍙傛暟鏄繀濉殑锛孾]鍙傛暟鍙��" + echo "./epay.sh start {profile} ==> 鍚姩搴旂敤锛寋profile}鍙栧�硷細dev|test|show|production" + echo "./epay.sh stop {profile} ==> 鍋滄搴旂敤锛寋profile}鍙栧�硷細dev|test|show|production" + exit 1 +fi diff --git a/src/main/java/com/xncoding/pos/Application.java b/src/main/java/com/xncoding/pos/Application.java new file mode 100644 index 0000000..ed05bb0 --- /dev/null +++ b/src/main/java/com/xncoding/pos/Application.java @@ -0,0 +1,21 @@ +package com.xncoding.pos; + +import com.xncoding.pos.handler.ClientStarter; +import io.netty.bootstrap.Bootstrap; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + private static final Logger logger = LoggerFactory.getLogger(Application.class); + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + logger.info("鍚姩鎵ц杩炴帴鏈嶅姟"); + ClientStarter starter = new ClientStarter(new Bootstrap()); + starter.connect(); + } + +} diff --git a/src/main/java/com/xncoding/pos/config/NettyProperties.java b/src/main/java/com/xncoding/pos/config/NettyProperties.java new file mode 100644 index 0000000..25ab946 --- /dev/null +++ b/src/main/java/com/xncoding/pos/config/NettyProperties.java @@ -0,0 +1,36 @@ +package com.xncoding.pos.config; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.stereotype.Component; + +@Component +@ConfigurationProperties("system.netty") +public class NettyProperties { + private int timeSeconds; + private String url; + private int port; + + public int getTimeSeconds() { + return timeSeconds; + } + + public void setTimeSeconds(int timeSeconds) { + this.timeSeconds = timeSeconds; + } + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + + public int getPort() { + return port; + } + + public void setPort(int port) { + this.port = port; + } +} diff --git a/src/main/java/com/xncoding/pos/config/ShellProperties.java b/src/main/java/com/xncoding/pos/config/ShellProperties.java new file mode 100644 index 0000000..8331cb9 --- /dev/null +++ b/src/main/java/com/xncoding/pos/config/ShellProperties.java @@ -0,0 +1,27 @@ +package com.xncoding.pos.config; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.stereotype.Component; + +@Component +@ConfigurationProperties("system.shell") +public class ShellProperties { + private int isRunShell; + private String url; + + public boolean isRunShell() { + return isRunShell == 0; + } + + public void setIsRunShell(int isRunShell) { + this.isRunShell = isRunShell; + } + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } +} diff --git a/src/main/java/com/xncoding/pos/config/SmsProperties.java b/src/main/java/com/xncoding/pos/config/SmsProperties.java new file mode 100644 index 0000000..82bc2d9 --- /dev/null +++ b/src/main/java/com/xncoding/pos/config/SmsProperties.java @@ -0,0 +1,82 @@ +package com.xncoding.pos.config; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.stereotype.Component; + +@Component +@ConfigurationProperties("system.sms") +public class SmsProperties { + private int isSendSms; + private String url; + private String userName; + private String pwd; + private String signature; + private String tpIdSuccess; + private String tpIdError; + private String mobiles; + + + public boolean isSendSms() { + return isSendSms == 0; + } + + public void setIsSendSms(int isSendSms) { + this.isSendSms = isSendSms; + } + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + + public String getUserName() { + return userName; + } + + public void setUserName(String userName) { + this.userName = userName; + } + + public String getPwd() { + return pwd; + } + + public void setPwd(String pwd) { + this.pwd = pwd; + } + + public String getSignature() { + return signature; + } + + public void setSignature(String signature) { + this.signature = signature; + } + + public String getTpIdSuccess() { + return tpIdSuccess; + } + + public void setTpIdSuccess(String tpIdSuccess) { + this.tpIdSuccess = tpIdSuccess; + } + + public String getTpIdError() { + return tpIdError; + } + + public void setTpIdError(String tpIdError) { + this.tpIdError = tpIdError; + } + + public String getMobiles() { + return mobiles; + } + + public void setMobiles(String mobiles) { + this.mobiles = mobiles; + } +} diff --git a/src/main/java/com/xncoding/pos/handler/ClientStarter.java b/src/main/java/com/xncoding/pos/handler/ClientStarter.java new file mode 100644 index 0000000..c2688c7 --- /dev/null +++ b/src/main/java/com/xncoding/pos/handler/ClientStarter.java @@ -0,0 +1,71 @@ +package com.xncoding.pos.handler; +import java.net.InetSocketAddress; + +import com.xncoding.pos.config.NettyProperties; +import com.xncoding.pos.utils.ApplicationContextUtils; +import com.xncoding.pos.utils.RunShellUtils; +import com.xncoding.pos.utils.SmsUtils; +import io.netty.bootstrap.Bootstrap; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.SocketChannel; +import io.netty.channel.socket.nio.NioSocketChannel; +import io.netty.handler.codec.string.StringDecoder; +import io.netty.handler.codec.string.StringEncoder; +import io.netty.handler.timeout.IdleStateHandler; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; + + +public class ClientStarter { + + private static final Logger logger = LoggerFactory.getLogger(ClientStarter.class); + + private static NettyProperties config; + + private final Bootstrap bootstrap; + + public ClientStarter(Bootstrap bootstrap) { + if(config == null){ + config = (NettyProperties)ApplicationContextUtils.getBean("nettyProperties"); + } + this.bootstrap = bootstrap; + ClientStarter clientStarter = this; + bootstrap.group(new NioEventLoopGroup()) + .channel(NioSocketChannel.class) + .handler(new ChannelInitializer<SocketChannel>() { + @Override + protected void initChannel(SocketChannel ch) throws Exception { + ch.pipeline().addLast(new StringEncoder()); + ch.pipeline().addLast(new StringDecoder()); + //鍙戦�佹秷鎭鐜囥�傚崟浣嶇銆傛璁剧疆鏄�5绉掑彂閫佷竴娆℃秷鎭� + ch.pipeline().addLast(new IdleStateHandler(config.getTimeSeconds(), config.getTimeSeconds(), config.getTimeSeconds())); + ch.pipeline().addLast(new HeartBeatClientHandler(clientStarter)); + } + }); + } + + public void connect() { + + ChannelFuture channelFuture = bootstrap.connect(new InetSocketAddress(config.getUrl(), config.getPort())); + channelFuture.addListener(future ->{ + if (future.isSuccess()) { + logger.info("杩炴帴鏈嶅姟鎴愬姛"); + } else { + logger.info("鏈嶅姟ping澶辫触"); + Thread.sleep(5000); + connect(); + } + }); + } + + + + + public static void main(String[] args) { + ClientStarter starter = new ClientStarter(new Bootstrap()); + starter.connect(); + } +} diff --git a/src/main/java/com/xncoding/pos/handler/HeartBeatClientHandler.java b/src/main/java/com/xncoding/pos/handler/HeartBeatClientHandler.java new file mode 100644 index 0000000..ae70696 --- /dev/null +++ b/src/main/java/com/xncoding/pos/handler/HeartBeatClientHandler.java @@ -0,0 +1,86 @@ +package com.xncoding.pos.handler; +import java.io.IOException; +import java.util.Date; +import java.util.concurrent.TimeUnit; + +import com.xncoding.pos.utils.RunShellUtils; +import com.xncoding.pos.utils.SmsUtils; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.EventLoop; +import io.netty.channel.SimpleChannelInboundHandler; +import io.netty.handler.timeout.IdleState; +import io.netty.handler.timeout.IdleStateEvent; +import io.netty.util.ReferenceCountUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; + +public class HeartBeatClientHandler extends SimpleChannelInboundHandler<String>{ + + + private final ClientStarter clientStarter; + private static final Logger logger = LoggerFactory.getLogger(HeartBeatClientHandler.class); + + public HeartBeatClientHandler(ClientStarter clientStarter) { + this.clientStarter = clientStarter; + } + + /** + * 瀹㈡埛绔洃鍚啓浜嬩欢銆備篃灏辨槸璁剧疆鏃堕棿鍐呮病鏈変笌鏈嶅姟绔氦浜掑垯鍙戦�乸ing 缁欐湇鍔$ + */ + @Override + public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { + if(evt instanceof IdleStateEvent) { + IdleState state = ((IdleStateEvent)evt).state(); + if(state == IdleState.ALL_IDLE) { + ctx.writeAndFlush("PING"); + logger.info("PING SUCCESS"); + } + } + super.userEventTriggered(ctx, evt); + } + /** + * channelInactive 琚Е鍙戜竴瀹氭槸鍜屾湇鍔″櫒鏂紑浜嗐�傚垎涓ょ鎯呭喌銆備竴绉嶆槸鏈嶅姟绔痗lose锛屼竴绉嶆槸瀹㈡埛绔痗lose銆� + */ + @Override + public void channelInactive(ChannelHandlerContext ctx) { + try { + super.channelInactive(ctx); + logger.error("瀹㈡埛绔笌鏈嶅姟绔柇寮�杩炴帴,鏂紑鐨勬椂闂翠负锛�"+(new Date()).toString()); + //1銆侀噸鍚湇鍔� + RunShellUtils runShellUtils = new RunShellUtils(); + boolean runShell = runShellUtils.runShell(); + logger.info("runShell{}",runShell); + //2銆� 鍙戦�佺煭淇$粰绯荤粺缁存姢浜哄憳 + SmsUtils smsUtils = new SmsUtils(); + smsUtils.sendSms(runShell); + //3銆佸畾鏃剁嚎绋� 鏂嚎閲嶈繛 + final EventLoop eventLoop = ctx.channel().eventLoop(); + //璁剧疆鏂紑杩炴帴鍚庨噸杩炴椂闂达紝姝よ缃槸鏂紑杩炴帴鍒嗛挓锛�120绉掞級鍚庨噸杩� + eventLoop.schedule(clientStarter::connect, 5, TimeUnit.SECONDS); + }catch (Exception e){ + e.printStackTrace(); + } + } + + /** + * 鍦ㄦ湇鍔″櫒绔笉浣跨敤蹇冭烦妫�娴嬬殑鎯呭喌涓嬶紝濡傛灉瀹㈡埛绔獊鐒舵嫈鎺夌綉绾挎柇缃戯紙娉ㄦ剰杩欓噷涓嶆槸瀹㈡埛搴︾▼搴忓叧闂紝鑰屼粎鏄紓甯告柇缃戯級 + */ + @Override + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { + if(cause instanceof IOException) { + System.out.println("server "+ctx.channel().remoteAddress()+"鍏抽棴杩炴帴"); + } + } + + /** + * 娑堟伅鐩戞帶锛岀洃鍚湇鍔$浼犳潵鐨勬秷鎭�(鍜宯etty鐗堟湰鏈夊叧锛屾湁鐨勭増鏈繖涓柟娉曞彨鍋歝lientRead0) + */ + @Override + protected void channelRead0(ChannelHandlerContext channelHandlerContext, String msg) throws Exception { + if ("PONG".equals(msg)) { + System.out.println("receive form server PONG"); + } + ReferenceCountUtil.release(msg); + } +} diff --git a/src/main/java/com/xncoding/pos/utils/ApplicationContextUtils.java b/src/main/java/com/xncoding/pos/utils/ApplicationContextUtils.java new file mode 100644 index 0000000..69adc96 --- /dev/null +++ b/src/main/java/com/xncoding/pos/utils/ApplicationContextUtils.java @@ -0,0 +1,27 @@ +package com.xncoding.pos.utils; + +import org.springframework.beans.BeansException; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.stereotype.Component; + +@Component +public class ApplicationContextUtils implements ApplicationContextAware { + + private static ApplicationContext context; + + + @Override + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + context = applicationContext; + } + + /** + * 鑾峰彇bean + * @param beanName + * @return + */ + public static Object getBean(String beanName) { + return context.getBean(beanName); + } +} \ No newline at end of file diff --git a/src/main/java/com/xncoding/pos/utils/RunShellUtils.java b/src/main/java/com/xncoding/pos/utils/RunShellUtils.java new file mode 100644 index 0000000..d482ffc --- /dev/null +++ b/src/main/java/com/xncoding/pos/utils/RunShellUtils.java @@ -0,0 +1,70 @@ +package com.xncoding.pos.utils; + +import com.xncoding.pos.config.ShellProperties; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.InputStreamReader; +import java.io.LineNumberReader; + +public class RunShellUtils { + private static final Logger logger = LoggerFactory.getLogger(RunShellUtils.class); + private static ShellProperties shellProperties; + public RunShellUtils(){ + if (shellProperties == null){ + shellProperties = (ShellProperties)ApplicationContextUtils.getBean("shellProperties"); + } + } + + + /** + * 鎵цshell + * @return 榛樿鎵ц鎴愬姛 true 鎵ц鎴愬姛 false 鎵ц澶辫触 + */ + public boolean runShell(){ + //榛樿鎵ц鎴愬姛 + boolean b = true; + if (shellProperties.isRunShell()) { + try { + // 1銆侀噸鍚剼鏈墽琛� + exec(shellProperties.getUrl()); + logger.info("閲嶅惎鑴氭湰鎵ц鎴愬姛锛�"); +// // 2銆佹墽琛屽惎鍔ㄨ剼鏈� +// exec(shellProperties.getRunUrl()); +// logger.info("鍚姩鑴氭湰鎵ц鎴愬姛锛�"); + }catch (Exception e){ + logger.error("鑴氭湰鎵ц澶辫触锛�"); + b = false; + e.printStackTrace(); + } + } + return b; + } + + private void rumShell(String url) throws Exception { + Process ps = Runtime.getRuntime().exec(url); + ps.waitFor(); + } + + + private void exec(String cmd) { + try { + logger.info("cmd:{}",cmd); + String[] cmdA = { "/bin/sh", "-c", cmd }; + Process process = Runtime.getRuntime().exec(cmdA); + LineNumberReader br = new LineNumberReader(new InputStreamReader( + process.getInputStream())); + StringBuffer sb = new StringBuffer(); + String line; + while ((line = br.readLine()) != null) { + logger.info(line); + sb.append(line).append("\n"); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + + +} diff --git a/src/main/java/com/xncoding/pos/utils/SmsUtils.java b/src/main/java/com/xncoding/pos/utils/SmsUtils.java new file mode 100644 index 0000000..76da29d --- /dev/null +++ b/src/main/java/com/xncoding/pos/utils/SmsUtils.java @@ -0,0 +1,71 @@ +package com.xncoding.pos.utils; +import cn.hutool.core.date.DateUtil; +import cn.hutool.crypto.SecureUtil; +import cn.hutool.http.HttpRequest; +import com.xncoding.pos.config.ShellProperties; +import com.xncoding.pos.config.SmsProperties; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; + +/** + * 鐭俊鏈嶅姟 + */ +public class SmsUtils { + private static final Logger logger = LoggerFactory.getLogger(SmsUtils.class); + private static SmsProperties smsProperties; + + public SmsUtils() { + if (smsProperties == null){ + smsProperties = (SmsProperties)ApplicationContextUtils.getBean("smsProperties"); + } + } + + public void sendSms( boolean isSuccess) { + logger.info("鏄惁鍙戦�佺煭淇}",smsProperties.isSendSms()); + if(!smsProperties.isSendSms()){return;} + //璇锋眰鍏ュ弬 + JSONObject requestJson = new JSONObject(); + //璐﹀彿 + requestJson.put("username", smsProperties.getUserName()); + //tKey + long tKey = System.currentTimeMillis() / 1000; + requestJson.put("tKey", tKey); + //鏄庢枃瀵嗙爜 + requestJson.put("password", SecureUtil.md5(SecureUtil.md5(smsProperties.getPwd()) + tKey)); + //妯℃澘ID + requestJson.put("tpId",isSuccess? smsProperties.getTpIdSuccess():smsProperties.getTpIdError()); + //绛惧悕 + requestJson.put("signature", smsProperties.getSignature()); + //鎵╁睍鍙� + requestJson.put("ext", ""); + //鑷畾涔夊弬鏁� + requestJson.put("extend", ""); + //鍙戦�佽褰曢泦鍚� + JSONArray records = new JSONArray(); + JSONObject record = new JSONObject(); + + String[] split = smsProperties.getMobiles().split(";"); + String now = DateUtil.now(); + for (String mobile : split) { + //鎵嬫満鍙� + record.put("mobile", mobile); + //鏇挎崲鍙橀噺 + JSONObject param = new JSONObject(); + param.put("date",now); + record.put("tpContent", param); + records.add(record); + + } + requestJson.put("records", records); + logger.info("鐭俊璇锋眰锛歿}", requestJson); + String result = HttpRequest.post(smsProperties.getUrl()) + .timeout(60000) + .body(requestJson.toJSONString(),"application/json;charset=UTF-8").execute().body(); + logger.info("鐭俊杩斿洖锛歿}", result); + + } + +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml new file mode 100644 index 0000000..546b7b2 --- /dev/null +++ b/src/main/resources/application.yml @@ -0,0 +1,52 @@ +spring: + profiles: + active: dev +# 寮�鍙戠幆澧冮厤缃� +server: + # 鏈嶅姟鍣ㄧ殑Http绔彛锛岄粯璁や负8090 + port: 8090 + servlet: + #搴旂敤鐨勮闂矾寰� + context-path: / + +logging: + level: + ROOT: INFO + com: + xncoding: DEBUG + file: /root/PubSvc/Logs/PubSvcAPI/HeartbeatServer.log + +system: + # 鐭俊 + sms: + # 鏄惁鍙戦�佺煭淇� 0 鍙戦�� 1涓嶅彂閫� + isSendSms: 1 + # 鐭俊鍦板潃 + url: https://101.132.90.14/v2/sendSmsTp + # 鐭俊璐︽埛 + userName: gdghhy + # 鐭俊瀵嗙爜 + pwd: Gdgh1234 + # 鍏徃鍚嶇О + signature: 銆愬箍涓滃啝姹囦俊鎭鎶�鏈夐檺鍏徃銆� + # 鑴氭湰鎵ц鎴愬姛妯$増Id + tpIdSuccess: 48319 + # 鑴氭湰鎵ц澶辫触妯$増Id + tpIdError: 48327 + # 鍙戦�佸埌鐩爣鎵嬫満鍙� 鍙互澶氫釜浠�;鍒嗗壊 + mobiles: "13510693034;15815213711;18520822537" + #mobiles: "15815213711;" + netty: + # 蹇冭烦鐩爣鏈嶅姟鍦板潃 + #url: '127.0.0.1' + url: '172.17.0.1' + # 蹇冭烦鐩爣鏈嶅姟绔彛 + #port: 7001 + port: 7000 + # 蹇冭烦妫�娴嬫椂闂撮棿闅� + time-seconds: 5 + shell: + # 鏄惁鍚姩鑷姩鎵ц鑴氭湰 0 鎵ц 1涓嶆墽琛� + isRunShell: 0 + # 鑴氭湰 + url: 'sh /root/PubSvc/Bin/restart-base.sh' -- Gitblit v1.8.0