|
|
|
@ -1,40 +1,34 @@
|
|
|
|
|
package com.dsideal.gw.Controller;
|
|
|
|
|
|
|
|
|
|
import com.dsideal.gw.Start;
|
|
|
|
|
import com.jfinal.core.Controller;
|
|
|
|
|
import okhttp3.MediaType;
|
|
|
|
|
import okhttp3.OkHttpClient;
|
|
|
|
|
import okhttp3.Request;
|
|
|
|
|
import okhttp3.RequestBody;
|
|
|
|
|
import okhttp3.Response;
|
|
|
|
|
import org.yaml.snakeyaml.Yaml;
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
public class ApiController extends Controller {
|
|
|
|
|
|
|
|
|
|
private final OkHttpClient client = new OkHttpClient();
|
|
|
|
|
private List<Map<String, String>> routes;
|
|
|
|
|
private final List<Map<String, String>> routes;
|
|
|
|
|
|
|
|
|
|
public ApiController() {
|
|
|
|
|
Yaml yaml = new Yaml();
|
|
|
|
|
try (InputStream input = getClass().getClassLoader().getResourceAsStream("application_dev.yaml")) {
|
|
|
|
|
if (input == null) {
|
|
|
|
|
System.out.println("Sorry, unable to find routes.yaml");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
Map<String, List<Map<String, String>>> yamlData = yaml.load(input);
|
|
|
|
|
routes = yamlData.get("routes");
|
|
|
|
|
} catch (IOException ex) {
|
|
|
|
|
ex.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
Map<String, Object> jfinalConfig = (Map<String, Object>) Start.yamlConfig.get("jfinal");
|
|
|
|
|
routes = (List<Map<String, String>>) jfinalConfig.get("routes");
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void index() {
|
|
|
|
|
String path = getPara(); // 获取请求路径
|
|
|
|
|
System.out.println(path);
|
|
|
|
|
String path = getRequest().getRequestURI(); // 获取请求路径
|
|
|
|
|
if (path.equals("/")) {
|
|
|
|
|
renderText("Welcome to JFinal Gateway.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
String serviceUrl = route(path); // 根据路径决定转发到哪个微服务
|
|
|
|
|
|
|
|
|
|
if (serviceUrl == null) {
|
|
|
|
@ -43,7 +37,9 @@ public class ApiController extends Controller {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
//方法类型
|
|
|
|
|
String method = getRequest().getMethod();
|
|
|
|
|
//传递jwt token
|
|
|
|
|
Request.Builder requestBuilder = new Request.Builder()
|
|
|
|
|
.url(serviceUrl)
|
|
|
|
|
.header("Authorization", getHeader("Authorization"));
|
|
|
|
@ -59,7 +55,9 @@ public class ApiController extends Controller {
|
|
|
|
|
Response response = client.newCall(request).execute();
|
|
|
|
|
|
|
|
|
|
if (response.isSuccessful()) {
|
|
|
|
|
renderJson(response.body().string());
|
|
|
|
|
if (response.body() != null) {
|
|
|
|
|
renderJson(response.body().string());
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
renderJson("error", "服务请求失败");
|
|
|
|
|
}
|
|
|
|
@ -68,6 +66,13 @@ public class ApiController extends Controller {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:路由
|
|
|
|
|
*
|
|
|
|
|
* @param path
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
private String route(String path) {
|
|
|
|
|
if (routes != null) {
|
|
|
|
|
for (Map<String, String> route : routes) {
|
|
|
|
|