main
黄海 11 months ago
parent daeee59941
commit fc282bdebc

@ -12,7 +12,7 @@ public class LoginController extends Controller {
/** /**
* http://10.10.21.20:9001/ds-base/login/helloWorld * http://10.10.21.20:9001/base/login/helloWorld
*/ */
@Before({GET.class}) @Before({GET.class})
public void helloWorld() { public void helloWorld() {

@ -18,7 +18,7 @@ import com.jfinal.template.Engine;
import java.io.File; import java.io.File;
public class Start extends JFinalConfig { public class baseApplication extends JFinalConfig {
//配置文件 //配置文件
public static void main(String[] args) { public static void main(String[] args) {
@ -33,7 +33,7 @@ public class Start extends JFinalConfig {
} }
PropKit.use(configFile); PropKit.use(configFile);
UndertowServer.create(Start.class, "undertow.properties").start(); UndertowServer.create(baseApplication.class, "undertow.properties").start();
} }
/** /**
@ -200,7 +200,7 @@ public class Start extends JFinalConfig {
@Override @Override
public void onStart() { public void onStart() {
//打印 启动Logo //打印 启动Logo
String path = Start.class.getClassLoader().getResource("logo.txt").getPath(); String path = baseApplication.class.getClassLoader().getResource("logo.txt").getPath();
File file = new File(path); File file = new File(path);
System.out.println(FileUtil.readUtf8String(file)); System.out.println(FileUtil.readUtf8String(file));
} }

@ -1,20 +0,0 @@
{
"host": "10.10.14.212",
"user": "root",
"pwd": "dsideal",
"port": 22,
"project": [
{
"projectName": "gw-charge",
"workingPath": "D:/dsWork/YltProject/Ylt/gw-charge",
"remotePath": "/usr/local/gw-charge",
"localLibPath": ""
},
{
"projectName": "zhu-que",
"workingPath": "D:/dsWork/YltProject/ZhuQue",
"remotePath": "/usr/local/zhu-que",
"localLibPath": "D:/dsWork/YltProject/ZhuQue/lib/"
}
]
}

@ -6,7 +6,7 @@ undertow.host=0.0.0.0
undertow.resourcePath=D:/dsWork/QingLong/WebRoot,classpath:static undertow.resourcePath=D:/dsWork/QingLong/WebRoot,classpath:static
# 目录名称 # 目录名称
undertow.contextPath=/ds-base undertow.contextPath=/base
# 设定I/O线程数. # 设定I/O线程数.
server.undertow.io-threads=8 server.undertow.io-threads=8

@ -0,0 +1,9 @@
package com.dsideal.gw.Controller;
import com.jfinal.core.Controller;
public class IndexController extends Controller {
public void index() {
renderText("Welcome to JFinal Gateway.");
}
}

@ -1,6 +1,6 @@
package com.dsideal.gw.Interceptor; package com.dsideal.gw.Interceptor;
import com.dsideal.gw.Start; import com.dsideal.gw.gwApplication;
import com.jfinal.aop.Interceptor; import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation; import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller; import com.jfinal.core.Controller;
@ -13,7 +13,7 @@ public class AuthInterceptor implements Interceptor {
private final List<String> whitelist; private final List<String> whitelist;
public AuthInterceptor() { public AuthInterceptor() {
Map<String, Object> jfinalConfig = (Map<String, Object>) Start.yamlConfig.get("jfinal"); Map<String, Object> jfinalConfig = (Map<String, Object>) gwApplication.yamlConfig.get("jfinal");
whitelist = (List<String>) jfinalConfig.get("whitelist"); whitelist = (List<String>) jfinalConfig.get("whitelist");
} }

@ -1,51 +1,67 @@
package com.dsideal.gw.Controller; package com.dsideal.gw.Interceptor;
import com.dsideal.gw.Start; import com.dsideal.gw.gwApplication;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller; import com.jfinal.core.Controller;
import okhttp3.MediaType; import okhttp3.*;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
public class ApiController extends Controller { public class RuteInterceptor implements Interceptor {
private final OkHttpClient client = new OkHttpClient(); private final OkHttpClient client = new OkHttpClient();
private final List<Map<String, String>> routes; private final List<Map<String, String>> routes;
public ApiController() { public RuteInterceptor() {
Map<String, Object> jfinalConfig = (Map<String, Object>) Start.yamlConfig.get("jfinal"); Map<String, Object> jfinalConfig = (Map<String, Object>) gwApplication.yamlConfig.get("jfinal");
routes = (List<Map<String, String>>) jfinalConfig.get("routes"); routes = (List<Map<String, String>>) jfinalConfig.get("routes");
}
/**
*
*
* @param path
* @return
*/
private String route(String path) {
if (routes != null) {
for (Map<String, String> route : routes) {
String prefix = route.get("prefix");
if (path.startsWith(prefix)) {
return route.get("url") + path;
}
}
}
return null;
} }
public void index() { @Override
String path = getRequest().getRequestURI(); // 获取请求路径 public void intercept(Invocation inv) {
if (path.equals("/")) { Controller controller = inv.getController();
renderText("Welcome to JFinal Gateway."); String path = controller.getRequest().getRequestURI();
if(path.equals("/")){
inv.invoke();
return; return;
} }
String serviceUrl = route(path); // 根据路径决定转发到哪个微服务 String serviceUrl = route(path); // 根据路径决定转发到哪个微服务
if (serviceUrl == null) { if (serviceUrl == null) {
renderJson("error", "服务未找到"); controller.renderJson("error", "服务未找到");
return; return;
} }
try { try {
//方法类型 //方法类型
String method = getRequest().getMethod(); String method = controller.getRequest().getMethod();
//传递jwt token //传递jwt token
Request.Builder requestBuilder = new Request.Builder() Request.Builder requestBuilder = new Request.Builder()
.url(serviceUrl) .url(serviceUrl)
.header("Authorization", getHeader("Authorization")); .header("Authorization", controller.getHeader("Authorization"));
if ("POST".equalsIgnoreCase(method)) { if ("POST".equalsIgnoreCase(method)) {
String jsonPayload = getRawData(); // 获取请求体中的JSON数据 String jsonPayload = controller.getRawData(); // 获取请求体中的JSON数据
MediaType JSON = MediaType.get("application/json; charset=utf-8"); MediaType JSON = MediaType.get("application/json; charset=utf-8");
RequestBody body = RequestBody.create(JSON, jsonPayload); RequestBody body = RequestBody.create(JSON, jsonPayload);
requestBuilder.post(body); requestBuilder.post(body);
@ -56,32 +72,15 @@ public class ApiController extends Controller {
if (response.isSuccessful()) { if (response.isSuccessful()) {
if (response.body() != null) { if (response.body() != null) {
renderJson(response.body().string()); controller.renderJson(response.body().string());
} }
} else { } else {
renderJson("error", "服务请求失败"); controller.renderJson("error", "服务请求失败");
return;
} }
} catch (IOException e) { } catch (IOException e) {
renderJson("error", "发生异常: " + e.getMessage()); throw new RuntimeException(e);
} }
} inv.invoke();
/**
*
*
* @param path
* @return
*/
private String route(String path) {
if (routes != null) {
for (Map<String, String> route : routes) {
String prefix = route.get("prefix");
if (path.startsWith(prefix)) {
return route.get("url") + path;
}
}
}
return null;
} }
} }

@ -1,8 +1,9 @@
package com.dsideal.gw; package com.dsideal.gw;
import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.FileUtil;
import com.dsideal.gw.Controller.ApiController; import com.dsideal.gw.Controller.IndexController;
import com.dsideal.gw.Interceptor.AuthInterceptor; import com.dsideal.gw.Interceptor.AuthInterceptor;
import com.dsideal.gw.Interceptor.RuteInterceptor;
import com.dsideal.gw.Util.LogBackLogFactory; import com.dsideal.gw.Util.LogBackLogFactory;
import com.jfinal.config.*; import com.jfinal.config.*;
import com.jfinal.server.undertow.UndertowServer; import com.jfinal.server.undertow.UndertowServer;
@ -14,11 +15,11 @@ import java.util.Map;
import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.Yaml;
public class Start extends JFinalConfig { public class gwApplication extends JFinalConfig {
public static Map<String, Object> yamlConfig; public static Map<String, Object> yamlConfig;
//通过构造函数读取配置文件 //通过构造函数读取配置文件
public Start() { public gwApplication() {
//配置文件 //配置文件
String configFile = "application_dev.yaml"; String configFile = "application_dev.yaml";
String myEnvVar = System.getenv("WORKING_ENV"); String myEnvVar = System.getenv("WORKING_ENV");
@ -39,7 +40,7 @@ public class Start extends JFinalConfig {
//配置文件 //配置文件
public static void main(String[] args) { public static void main(String[] args) {
UndertowServer.create(Start.class, "undertow.properties").start(); UndertowServer.create(gwApplication.class, "undertow.properties").start();
} }
/** /**
@ -61,8 +62,7 @@ public class Start extends JFinalConfig {
*/ */
@Override @Override
public void configRoute(Routes me) { public void configRoute(Routes me) {
//默认页面 me.add("/", IndexController.class);
me.add("/", ApiController.class);
} }
@Override @Override
@ -82,6 +82,7 @@ public class Start extends JFinalConfig {
@Override @Override
public void configInterceptor(Interceptors me) { public void configInterceptor(Interceptors me) {
me.addGlobalActionInterceptor(new AuthInterceptor()); me.addGlobalActionInterceptor(new AuthInterceptor());
me.addGlobalActionInterceptor(new RuteInterceptor());
} }
/** /**
@ -97,7 +98,7 @@ public class Start extends JFinalConfig {
@Override @Override
public void onStart() { public void onStart() {
//打印 启动Logo //打印 启动Logo
String path = Start.class.getClassLoader().getResource("logo.txt").getPath(); String path = gwApplication.class.getClassLoader().getResource("logo.txt").getPath();
File file = new File(path); File file = new File(path);
System.out.println(FileUtil.readUtf8String(file)); System.out.println(FileUtil.readUtf8String(file));
} }
Loading…
Cancel
Save