main
黄海 11 months ago
parent b189125e9d
commit 80fec1e807

@ -1,4 +1,4 @@
package com.dsideal.gw.Index.Controller;
package com.dsideal.gw.Controller;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;

@ -3,7 +3,7 @@ package com.dsideal.gw;
import cn.hutool.core.io.FileUtil;
import com.cybermkd.mongo.plugin.MongoJFinalPlugin;
import com.dsideal.gw.Admin.Controller.LoginController;
import com.dsideal.gw.Index.Controller.IndexController;
import com.dsideal.gw.Controller.IndexController;
import com.dsideal.gw.Interceptor.*;
import com.dsideal.gw.Util.LogBackLogFactory;
import com.jfinal.config.*;

@ -0,0 +1,68 @@
package com.dsideal.gw.Controller;
import com.jfinal.core.Controller;
import com.jfinal.kit.PropKit;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import java.io.IOException;
public class ApiController extends Controller {
private OkHttpClient client = new OkHttpClient();
public void index() {
String path = getPara(); // 获取请求路径
System.out.println(path);
String serviceUrl = route(path); // 根据路径决定转发到哪个微服务
if (serviceUrl == null) {
renderJson("error", "服务未找到");
return;
}
try {
String method = getRequest().getMethod();
Request.Builder requestBuilder = new Request.Builder()
.url(serviceUrl)
.header("Authorization", getHeader("Authorization"));
if ("POST".equalsIgnoreCase(method)) {
String jsonPayload = getRawData(); // 获取请求体中的JSON数据
MediaType JSON = MediaType.get("application/json; charset=utf-8");
RequestBody body = RequestBody.create(JSON, jsonPayload);
requestBuilder.post(body);
}
Request request = requestBuilder.build();
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
renderJson(response.body().string());
} else {
renderJson("error", "服务请求失败");
}
} catch (IOException e) {
renderJson("error", "发生异常: " + e.getMessage());
}
}
private String route(String path) {
String[] prefixes = PropKit.getProp().getProperties().stringPropertyNames().stream()
.filter(key -> key.endsWith(".prefix"))
.toArray(String[]::new);
for (String prefixKey : prefixes) {
String prefix = PropKit.get(prefixKey);
if (path.startsWith(prefix)) {
String serviceKey = prefixKey.replace(".prefix", ".url");
return PropKit.get(serviceKey) + path;
}
}
return null;
}
}

@ -1,51 +0,0 @@
package com.dsideal.gw.Index.Controller;
import com.jfinal.core.Controller;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import java.io.IOException;
public class ApiController extends Controller {
private OkHttpClient client = new OkHttpClient();
public void index() {
String path = getPara(); // 获取请求路径
System.out.println(path);
String serviceUrl = route(path); // 根据路径决定转发到哪个微服务
if (serviceUrl == null) {
renderJson("error", "Service not found");
return;
}
try {
Request request = new Request.Builder()
.url(serviceUrl)
.header("Authorization", getHeader("Authorization"))
.build();
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
renderJson(response.body().string());
} else {
renderJson("error", "Service request failed");
}
} catch (IOException e) {
renderJson("error", "Exception occurred: " + e.getMessage());
}
}
private String route(String path) {
// 根据路径决定转发到哪个微服务
// 这里可以根据具体的路径或者配置文件来路由
if (path.startsWith("/service1")) {
return "http://localhost:8081" + path; // 微服务1的地址
} else if (path.startsWith("/service2")) {
return "http://localhost:8082" + path; // 微服务2的地址
}
return null;
}
}

@ -1,7 +1,7 @@
package com.dsideal.gw;
import cn.hutool.core.io.FileUtil;
import com.dsideal.gw.Index.Controller.ApiController;
import com.dsideal.gw.Controller.ApiController;
import com.dsideal.gw.Interceptor.AuthInterceptor;
import com.dsideal.gw.Util.LogBackLogFactory;
import com.jfinal.config.*;

@ -1,17 +0,0 @@
package com.dsideal.gw.Tools;
import cn.hutool.core.util.RandomUtil;
public class GenerateToken {
// 定义加密密钥
public static String key = RandomUtil.randomString("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", 10);
//public static String key = "BNNCba7Ikq";
public static void main(String[] args) {
System.out.println(key);
// YltModel ym=new YltModel();
//XDT 85 吉AF88888
//System.out.println("Token" + ym.getToken("XDT", 161, "吉AF88888",key));
}
}

@ -1,15 +1,5 @@
# 数据库信息
driverClassName=com.mysql.cj.jdbc.Driver
user=root
password=DsideaL147258369
jdbcUrl=jdbc:mysql://10.10.14.210:22066/yltcharge?useUnicode=true&characterEncoding=UTF-8&useSSL=false
service1.prefix=/service1
service1.url=http://localhost:8081
#mongodb
mongodbUri=mongodb://yltcharge:yltcharge@10.10.14.210:27017/yltcharge
#Minio 配置
minio_endpoint: http://10.10.14.212:9000
minio_accesskey: wJDQP0ZZpyLHIfnPXYPn
minio_secretkey: ZkmTmuoMK6f601rQF5OOQB2JlSvmBOjrHRhIz6bt
minio_bucketName:dsideal
minio_path_prefix=http://10.10.14.212:9000/dsideal/
service2.prefix=/service2
service2.url=http://localhost:8082

@ -1,15 +1,5 @@
# 数据库信息
driverClassName=com.mysql.cj.jdbc.Driver
user=root
password=DsideaL147258369
jdbcUrl=jdbc:mysql://10.10.14.210:22066/yltcharge?useUnicode=true&characterEncoding=UTF-8&useSSL=false
service1.prefix=/service1
service1.url=http://localhost:8081
#mongodb
mongodbUri=mongodb://yltcharge:yltcharge@10.10.14.210:27017/yltcharge
#Minio 配置
minio_endpoint: http://10.10.14.212:9000
minio_accesskey: wJDQP0ZZpyLHIfnPXYPn
minio_secretkey: ZkmTmuoMK6f601rQF5OOQB2JlSvmBOjrHRhIz6bt
minio_bucketName:dsideal
minio_path_prefix=http://10.10.14.212:9000/dsideal/
service2.prefix=/service2
service2.url=http://localhost:8082

Loading…
Cancel
Save