Unverified Commit ccf3e330 authored by Licho's avatar Licho Committed by GitHub

refactor: flink web with iframe and process with css and proxy (#2309)

* fix: flink web depdendencies * refactor: flink web with iframe css * Spotless Apply * refactor: flink web with iframe css * refactor: flink web with iframe css * Spotless Apply * refactor: flink web hidden * Spotless Apply * refactor: margin Signed-off-by: 's avatarLicho <lecho.sun@gmail.com> * refactor: back proxy * refactor: construct url * fix * refactor: target address * feat: url custom server * feat: log * refactor: * Spotless Apply * refactor: * fix Signed-off-by: 's avatarLicho <lecho.sun@gmail.com> * fix Signed-off-by: 's avatarLicho <lecho.sun@gmail.com> * Spotless Apply --------- Signed-off-by: 's avatarLicho <lecho.sun@gmail.com> Co-authored-by: 's avatarleechor <leechor@users.noreply.github.com>
parent be0ff515
...@@ -58,3 +58,4 @@ docs/yarn.lock ...@@ -58,3 +58,4 @@ docs/yarn.lock
docs/*.log docs/*.log
tmp/* tmp/*
extends/* extends/*
/.run/
...@@ -37,6 +37,10 @@ ...@@ -37,6 +37,10 @@
</properties> </properties>
<dependencies> <dependencies>
<dependency>
<groupId>org.mitre.dsmiley.httpproxy</groupId>
<artifactId>smiley-http-proxy-servlet</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.jasypt</groupId> <groupId>org.jasypt</groupId>
<artifactId>jasypt</artifactId> <artifactId>jasypt</artifactId>
......
/*
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.dinky.configure;
import org.dinky.utils.FlinkWebURITemplateProxyServlet;
import org.mitre.dsmiley.httpproxy.ProxyServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
@Configuration
public class FlinkWebProxyServletConfiguration implements EnvironmentAware {
private static final String TARGET_URL = "http://{_authority}/#/job/running/{_jid}/overview";
@Bean
public ServletRegistrationBean servletRegistrationBean() {
ServletRegistrationBean servletRegistrationBean =
new ServletRegistrationBean(new FlinkWebURITemplateProxyServlet());
servletRegistrationBean.addUrlMappings("/api/flink_web/proxy/*", "/api/flink_web/*");
servletRegistrationBean.addInitParameter(ProxyServlet.P_TARGET_URI, TARGET_URL);
servletRegistrationBean.addInitParameter(ProxyServlet.P_LOG, "false");
return servletRegistrationBean;
}
@Override
public void setEnvironment(Environment environment) {}
}
/*
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.dinky.utils;
import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URIUtils;
import org.apache.http.client.utils.URLEncodedUtils;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.mitre.dsmiley.httpproxy.ProxyServlet;
import org.mitre.dsmiley.httpproxy.URITemplateProxyServlet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class FlinkWebURITemplateProxyServlet extends ProxyServlet {
protected static final Logger logger = LoggerFactory.getLogger(FlinkWebURITemplateProxyServlet.class);
public static final String FLINK_WEB_PROXY = "/api/flink_web/proxy";
private static volatile String AUTHORITY;
protected static final Pattern TEMPLATE_PATTERN = Pattern.compile("\\{(.+?)\\}");
private static final String ATTR_QUERY_STRING = URITemplateProxyServlet.class.getSimpleName() + ".queryString";
protected String targetUriTemplate; // has {name} parts
@Override
protected void initTarget() throws ServletException {
targetUriTemplate = getConfigParam(P_TARGET_URI);
if (targetUriTemplate == null) throw new ServletException(P_TARGET_URI + " is required.");
// leave this.target* null to prevent accidental mis-use
}
@Override
protected synchronized void service(HttpServletRequest servletRequest, HttpServletResponse servletResponse)
throws ServletException, IOException {
if (!servletRequest.getRequestURI().contains(FLINK_WEB_PROXY)) {
// iframe inner request
int i = 0;
while (AUTHORITY == null && i++ < 10) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
if (AUTHORITY != null) {
service(servletRequest, servletResponse, AUTHORITY);
} else {
logger.debug("AUTHORITY is null, skip service");
}
return;
}
if (Objects.equals(servletRequest.getRequestURI(), FLINK_WEB_PROXY)) {
String queryString = getQueryString(servletRequest);
Map<String, String> params = getParams(queryString);
String newTargetUri = getNewTargetUri(targetUriTemplate, params);
try {
URI target = new URI(newTargetUri);
AUTHORITY = URIUtils.extractHost(target).toString();
} catch (URISyntaxException e) {
throw new ServletException("Rewritten targetUri is invalid: " + newTargetUri, e);
}
}
service(servletRequest, servletResponse, targetUriTemplate);
}
private void service(
HttpServletRequest servletRequest, HttpServletResponse servletResponse, String targetUriTemplateCurrent)
throws ServletException, IOException {
String queryString = getQueryString(servletRequest);
Map<String, String> params = getParams(queryString);
String newTargetUri = getNewTargetUri(targetUriTemplateCurrent, params);
servletRequest.setAttribute(ATTR_TARGET_URI, newTargetUri);
URI targetUriObj;
try {
targetUriObj = new URI(newTargetUri);
} catch (Exception e) {
throw new ServletException("Rewritten targetUri is invalid: " + newTargetUri, e);
}
servletRequest.setAttribute(ATTR_TARGET_HOST, URIUtils.extractHost(targetUriObj));
// Determine the new query string based on removing the used names
StringBuilder newQueryBuf = new StringBuilder(queryString.length());
for (Map.Entry<String, String> nameVal : params.entrySet()) {
if (newQueryBuf.length() > 0) newQueryBuf.append('&');
newQueryBuf.append(nameVal.getKey()).append('=');
if (nameVal.getValue() != null) newQueryBuf.append(URLEncoder.encode(nameVal.getValue(), "UTF-8"));
}
servletRequest.setAttribute(ATTR_QUERY_STRING, newQueryBuf.toString());
try {
super.service(servletRequest, servletResponse);
} catch (Exception ex) {
logger.warn(String.format(
"%s origin url:%s params:%s",
ex.getMessage(), servletRequest.getRequestURL(), servletRequest.getQueryString()));
}
}
private static String getNewTargetUri(String targetUriTemplateCurrent, Map<String, String> params)
throws ServletException {
// Now rewrite the URL
StringBuffer urlBuf = new StringBuffer(); // note: StringBuilder isn't supported by Matcher
Matcher matcher = TEMPLATE_PATTERN.matcher(targetUriTemplateCurrent);
while (matcher.find()) {
String arg = matcher.group(1);
String replacement = params.remove(arg); // note we remove
if (replacement == null) {
throw new ServletException("Missing HTTP parameter " + arg + " to fill the template");
}
matcher.appendReplacement(urlBuf, replacement);
}
matcher.appendTail(urlBuf);
String newTargetUri = urlBuf.toString();
return newTargetUri;
}
private Map<String, String> getParams(String queryString) throws ServletException {
List<NameValuePair> pairs;
try {
// note: HttpClient 4.2 lets you parse the string without building the URI
pairs = URLEncodedUtils.parse(new URI(queryString), "UTF-8");
} catch (URISyntaxException e) {
throw new ServletException("Unexpected URI parsing error on " + queryString, e);
}
LinkedHashMap<String, String> params = new LinkedHashMap<String, String>();
for (NameValuePair pair : pairs) {
params.put(pair.getName(), pair.getValue());
}
return params;
}
private static String getQueryString(HttpServletRequest servletRequest) {
String requestQueryString = servletRequest.getQueryString();
String queryString = "";
if (requestQueryString != null) {
queryString = "?" + requestQueryString; // no "?" but might have "#"
}
int hash = queryString.indexOf('#');
if (hash >= 0) {
queryString = queryString.substring(0, hash);
}
return queryString;
}
@Override
protected String rewriteQueryStringFromRequest(HttpServletRequest servletRequest, String queryString) {
return (String) servletRequest.getAttribute(ATTR_QUERY_STRING);
}
}
...@@ -139,6 +139,11 @@ ...@@ -139,6 +139,11 @@
<artifactId>flink-doris-connector-1.16</artifactId> <artifactId>flink-doris-connector-1.16</artifactId>
<version>1.3.0</version> <version>1.3.0</version>
</dependency> </dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-runtime-web</artifactId>
<version>${flink.version}</version>
</dependency>
</dependencies> </dependencies>
<repositories> <repositories>
......
...@@ -132,5 +132,10 @@ ...@@ -132,5 +132,10 @@
<artifactId>flink-doris-connector-1.16</artifactId> <artifactId>flink-doris-connector-1.16</artifactId>
<version>1.3.0</version> <version>1.3.0</version>
</dependency> </dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-runtime-web</artifactId>
<version>${flink.version}</version>
</dependency>
</dependencies> </dependencies>
</project> </project>
...@@ -16,7 +16,7 @@ const operatorType = { ...@@ -16,7 +16,7 @@ const operatorType = {
}; };
const JobOperator = (props: JobProps) => { const JobOperator = (props: JobProps) => {
const { jobDetail } = props; const { jobDetail } = props;
const webUri = `/api/flink/${jobDetail?.history?.jobManagerAddress}/#/job/${jobDetail?.instance?.jid}/overview`; const webUri = `/api/flink/${jobDetail?.history?.jobManagerAddress}/#/job/running/${jobDetail?.instance?.jid}/overview`;
const handleJobOperator = (key: string) => { const handleJobOperator = (key: string) => {
Modal.confirm({ Modal.confirm({
......
...@@ -2,13 +2,58 @@ import { JobProps } from '@/pages/DevOps/JobDetail/data'; ...@@ -2,13 +2,58 @@ import { JobProps } from '@/pages/DevOps/JobDetail/data';
const JobOperatorGraph = (props: JobProps) => { const JobOperatorGraph = (props: JobProps) => {
const { jobDetail } = props; const { jobDetail } = props;
const url = `${window.location.origin}/api/flink_web/proxy?_authority=${jobDetail?.history?.jobManagerAddress}&_jid=${jobDetail?.instance?.jid}`;
const onLoad = () => {
const iframe = document.getElementById('iframe-id') as HTMLIFrameElement;
if (!iframe) return;
const innerDoc = iframe.contentWindow?.document;
if (!innerDoc) return;
innerDoc.body.style.visibility = 'hidden';
const flinkJob = innerDoc?.querySelector('flink-root > nz-layout > nz-layout');
if (!flinkJob) return;
// @ts-ignore
const style = flinkJob.style;
style.visibility = 'visible';
style.top = '0';
style.left = '0';
style.zIndex = 999;
style.marginLeft = '0px';
style.marginTop = '0px';
// style.overflow = 'hidden';
style.position = 'absolute';
style.width = iframe.offsetWidth + 'px';
style.height = iframe.offsetHeight + 'px';
};
return ( return (
<div
style={{
border: '0px solid',
overflow: 'hidden',
width: '100%',
height: '100%'
}}
>
<iframe <iframe
width='100%' id='iframe-id'
height={window.innerHeight - 200} scrolling='no'
style={{ border: 0 }} referrerPolicy={'no-referrer'}
src={`/api/flink/web/${jobDetail?.history?.jobManagerAddress}/#/job/running/${jobDetail?.instance?.jid}/overview`} src={url}
onLoad={() => onLoad()}
sandbox='allow-same-origin allow-scripts allow-popups allow-forms allow-downloads allow-downloads-without-user-activation allow-modals '
style={{
overflow: 'hidden',
border: '0px none',
width: '100%',
height: window.innerHeight
// marginLeft: '-260px',
// marginRight: '-220px'
}}
/> />
</div>
); );
}; };
......
...@@ -85,7 +85,6 @@ ...@@ -85,7 +85,6 @@
<maven-enforcer-plugin.version>3.0.0</maven-enforcer-plugin.version> <maven-enforcer-plugin.version>3.0.0</maven-enforcer-plugin.version>
<maven-help-plugin.version>3.4.0</maven-help-plugin.version> <maven-help-plugin.version>3.4.0</maven-help-plugin.version>
<maven-jar-plugin.version>3.3.0</maven-jar-plugin.version> <maven-jar-plugin.version>3.3.0</maven-jar-plugin.version>
<maven-jar-plugin.version>3.3.0</maven-jar-plugin.version>
<maven-shade-plugin.version>3.4.1</maven-shade-plugin.version> <maven-shade-plugin.version>3.4.1</maven-shade-plugin.version>
<maven-surefire-plugin.version>3.0.0-M5</maven-surefire-plugin.version> <maven-surefire-plugin.version>3.0.0-M5</maven-surefire-plugin.version>
<maven.compiler.source>${target.java.version}</maven.compiler.source> <maven.compiler.source>${target.java.version}</maven.compiler.source>
...@@ -108,6 +107,7 @@ ...@@ -108,6 +107,7 @@
<scala.version>2.12.10</scala.version> <scala.version>2.12.10</scala.version>
<!-- `provided` for product environment ,`compile` for dev environment --> <!-- `provided` for product environment ,`compile` for dev environment -->
<scope.runtime>provided</scope.runtime> <scope.runtime>provided</scope.runtime>
<smily.version>1.12.1</smily.version>
<snakeyaml.version>1.33</snakeyaml.version> <snakeyaml.version>1.33</snakeyaml.version>
<spotless.ratchetFrom>origin/dev</spotless.ratchetFrom> <spotless.ratchetFrom>origin/dev</spotless.ratchetFrom>
<spotless.version>2.27.1</spotless.version> <spotless.version>2.27.1</spotless.version>
...@@ -119,6 +119,11 @@ ...@@ -119,6 +119,11 @@
<dependencyManagement> <dependencyManagement>
<dependencies> <dependencies>
<dependency>
<groupId>org.mitre.dsmiley.httpproxy</groupId>
<artifactId>smiley-http-proxy-servlet</artifactId>
<version>${smily.version}</version>
</dependency>
<!--easy-rules 执行引擎--> <!--easy-rules 执行引擎-->
<dependency> <dependency>
<groupId>org.jeasy</groupId> <groupId>org.jeasy</groupId>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment