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.aop.Before;
|
||||||
import com.jfinal.core.Controller;
|
import com.jfinal.core.Controller;
|
@ -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,15 +1,5 @@
|
|||||||
# 数据库信息
|
service1.prefix=/service1
|
||||||
driverClassName=com.mysql.cj.jdbc.Driver
|
service1.url=http://localhost:8081
|
||||||
user=root
|
|
||||||
password=DsideaL147258369
|
|
||||||
jdbcUrl=jdbc:mysql://10.10.14.210:22066/yltcharge?useUnicode=true&characterEncoding=UTF-8&useSSL=false
|
|
||||||
|
|
||||||
#mongodb
|
service2.prefix=/service2
|
||||||
mongodbUri=mongodb://yltcharge:yltcharge@10.10.14.210:27017/yltcharge
|
service2.url=http://localhost:8082
|
||||||
|
|
||||||
#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/
|
|
||||||
|
@ -1,15 +1,5 @@
|
|||||||
# 数据库信息
|
service1.prefix=/service1
|
||||||
driverClassName=com.mysql.cj.jdbc.Driver
|
service1.url=http://localhost:8081
|
||||||
user=root
|
|
||||||
password=DsideaL147258369
|
|
||||||
jdbcUrl=jdbc:mysql://10.10.14.210:22066/yltcharge?useUnicode=true&characterEncoding=UTF-8&useSSL=false
|
|
||||||
|
|
||||||
#mongodb
|
service2.prefix=/service2
|
||||||
mongodbUri=mongodb://yltcharge:yltcharge@10.10.14.210:27017/yltcharge
|
service2.url=http://localhost:8082
|
||||||
|
|
||||||
#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/
|
|
||||||
|
Loading…
Reference in new issue