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})
public void helloWorld() {

@ -18,7 +18,7 @@ import com.jfinal.template.Engine;
import java.io.File;
public class Start extends JFinalConfig {
public class baseApplication extends JFinalConfig {
//配置文件
public static void main(String[] args) {
@ -33,7 +33,7 @@ public class Start extends JFinalConfig {
}
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
public void onStart() {
//打印 启动Logo
String path = Start.class.getClassLoader().getResource("logo.txt").getPath();
String path = baseApplication.class.getClassLoader().getResource("logo.txt").getPath();
File file = new File(path);
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.contextPath=/ds-base
undertow.contextPath=/base
# 设定I/O线程数.
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;
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;
@ -13,7 +13,7 @@ public class AuthInterceptor implements Interceptor {
private final List<String> whitelist;
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");
}

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

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