jfinal-4.2 通过commons-exec 调用frpc 客户端
[产生背景]
我的机器在内网,想要对外提供一个方位地址,通常的做法是在公网服务器上安装frp服务端,在本机的windows的安装一个frpc客户端,并启动,但是每次启动都要有1个黑窗口,看着很不舒服
有2中解决办法
1.将frpc做成widnows的服务,之前我也是怎么做的,配置过程也很方便
2.和jfinal项目整合在一起,frpc随着jfinal的启动而启动,随着jfinal的关闭而关闭
下面的是和final的整合过程
将frp客户端文件放到项目的lib目录下,并配置frpc.init
frpc.ini配置
[common] server_addr = xx.com server_port = xx #[001] #type = tcp #local_ip = 192.168.3.48 #local_port = 22 #remote_port = 8086 [litong-80] type = http local_port = 80 local_ip = 127.0.0.1 custom_domains = jfinal-4.2-web.litongjava.xx.com
frp启动工具类
package com.litong.utils.exec; import java.io.File; import lombok.extern.slf4j.Slf4j; /** * @author bill robot * @date 2020年8月17日_下午2:23:08 * @version 1.0 * @desc */ @Slf4j public class FrpUtil implements Runnable { public static void start() { File frpcExe = new File("lib/frp/frpc.exe"); File frpcIni = new File("lib/frp/frpc.ini"); String command = frpcExe.getAbsolutePath() + " -c " + frpcIni.getAbsolutePath(); log.info("启动frp:{}", command); String result = ExecUtil.execCmd(command); log.info("启动frp结果:{}", result); } @Override public void run() { FrpUtil.start(); } }
在启动类中添加一个启动方法,开启一个线程,并在main方法中调用启动方法
package com.litong.jfinal; import com.jfinal.config.Constants; import com.jfinal.config.Handlers; import com.jfinal.config.Interceptors; import com.jfinal.config.JFinalConfig; import com.jfinal.config.Plugins; import com.jfinal.config.Routes; import com.jfinal.server.undertow.UndertowConfig; import com.jfinal.server.undertow.UndertowServer; import com.jfinal.template.Engine; import com.litong.jfinal.route.AdminRoutes; import com.litong.jfinal.route.ApiRoutes; import com.litong.jfinal.route.FrontRoutes; import com.litong.jfinal.route.SystemRoutes; import com.litong.jfinal.utils.PropKitUtil; import com.litong.jfinal.utils.UndertowUtil; import com.litong.utils.dll.LibraryUtil; import com.litong.utils.exec.FrpUtil; import com.litong.utils.ip.IPUtil; public class Application extends JFinalConfig { private static String configFileName = PropKitUtil.configFileName; public static void main(String[] args) { long start = System.currentTimeMillis(); UndertowUtil.server = UndertowServer.create(Application.class, configFileName); // 启动Server全局共享 UndertowUtil.server.addSystemClassPrefix("com.litong.jfinal.utils.UndertowUtil"); UndertowUtil.server.start(); UndertowConfig undertowConfig = UndertowUtil.server.getUndertowConfig(); int port = undertowConfig.getPort(); String contextPath = undertowConfig.getContextPath(); long end = System.currentTimeMillis(); System.out.println(IPUtil.getThisUrl(port, contextPath)); System.out.println("启动完成,共使用了" + (end - start) + "ms"); startFrp(); } public void configConstant(Constants me) { me.setInjectDependency(true); me.setInjectSuperClass(true); } public void configRoute(Routes me) { me.setMappingSuperClass(true); me.add(new FrontRoutes()); // 前端路由 me.add(new AdminRoutes()); // 后端路由 me.add(new SystemRoutes()); // 系统路由 me.add(new ApiRoutes()); // API路由 } @Override public void configEngine(Engine me) { } @Override public void configPlugin(Plugins me) { } @Override public void configInterceptor(Interceptors me) { } @Override public void configHandler(Handlers me) { } /* * 使用onStart会拖慢jfinal启动速度 * afterJFinalStart 也会拖慢jfinal启动速度 */ public static void startFrp() { // 默认会阻塞主线程,所以新开一个线程启动 FrpUtil frpUtil = new FrpUtil(); Thread frpClientThread = new Thread(frpUtil, "frp client"); frpClientThread.run(); } }
因为是随着jfinal启动了,当jfinal关闭时frp也关闭了,
启动成功后的日志如下
Info: jfinal-undertow 1.6, undertow 2.0.19.Final, jvm 1.8.0_211 2020-08-17 14:52:43.186 INFO xnio.<clinit>:104 - XNIO version 3.3.8.Final 2020-08-17 14:52:43.199 INFO nio.<clinit>:55 - XNIO NIO Implementation Version 3.3.8.Final Starting Complete in 1.5 seconds. Welcome To The JFinal World (^_^) http://192.168.0.10:11023/jfinal-4.2-web 启动完成,共使用了1575ms 2020-08-17 14:52:44.718 INFO FrpUtil.start:19 - 启动frp:D:\dev_workspace\java\litong-project\jfinal-4.2-web\lib\frp\frpc.exe -c D:\dev_workspace\java\litong-project\jfinal-4.2-web\lib\frp\frpc.ini 2020-08-17 14:53:44.753 INFO FrpUtil.start:21 - 启动frp结果:2020/08/17 14:52:44 [I] [service.go:234] login to server success, get run id [6b2cb5b871512dd2], server udp port [0] 2020/08/17 14:52:45 [I] [proxy_manager.go:144] [6b2cb5b871512dd2] proxy added: [litong-80] 2020/08/17 14:52:45 [I] [control.go:153] [litong-80] start proxy success
缺点
使用java启动frpc,启动过程非常慢,而我在windwos控制台可以秒启动