forked from apache/linkis
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fa3011f
commit 7935cde
Showing
9 changed files
with
253 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
linkis-commons/linkis-rpc/src/main/java/org/apache/linkis/rpc/conf/DynamicFeignClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/* | ||
* 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.apache.linkis.rpc.conf; | ||
|
||
import org.apache.linkis.DataWorkCloudApplication; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
|
||
import org.springframework.cloud.openfeign.FeignClientBuilder; | ||
import org.springframework.cloud.openfeign.FeignClientFactoryBean; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
@Component | ||
public class DynamicFeignClient<T> { | ||
|
||
private FeignClientBuilder feignClientBuilder; | ||
|
||
private final ConcurrentHashMap<String, T> CACHE_BEAN = new ConcurrentHashMap(); | ||
|
||
public DynamicFeignClient() { | ||
this.feignClientBuilder = | ||
new FeignClientBuilder(DataWorkCloudApplication.getApplicationContext()); | ||
} | ||
|
||
public T getFeignClient(final Class<T> type, final String serviceName) { | ||
return getFeignClient(type, serviceName, null); | ||
} | ||
|
||
public T getFeignClient( | ||
final Class<T> type, final Class<?> fallbackFactory, final String serviceName) { | ||
return getFeignClient(type, fallbackFactory, serviceName, null); | ||
} | ||
|
||
public T getFeignClient( | ||
final Class<T> type, | ||
final FeignClientFactoryBean clientFactoryBean, | ||
final String serviceName) { | ||
return getFeignClient(type, clientFactoryBean, serviceName, null); | ||
} | ||
|
||
public T getFeignClient(final Class<T> type, String serviceName, final String serviceUrl) { | ||
String k = serviceName; | ||
if (StringUtils.isNotEmpty(serviceUrl)) { | ||
k = serviceUrl; | ||
} | ||
return CACHE_BEAN.computeIfAbsent( | ||
k, | ||
(t) -> { | ||
FeignClientBuilder.Builder<T> builder = | ||
this.feignClientBuilder.forType(type, serviceName); | ||
if (StringUtils.isNotEmpty(serviceUrl)) { | ||
builder.url(serviceUrl); | ||
} | ||
return builder.build(); | ||
}); | ||
} | ||
|
||
public T getFeignClient( | ||
final Class<T> type, | ||
final Class<?> fallbackFactory, | ||
final String serviceName, | ||
final String serviceUrl) { | ||
String k = serviceName; | ||
if (StringUtils.isNotEmpty(serviceUrl)) { | ||
k = serviceUrl; | ||
} | ||
return CACHE_BEAN.computeIfAbsent( | ||
k, | ||
(t) -> { | ||
FeignClientFactoryBean feignClientFactoryBean = new FeignClientFactoryBean(); | ||
feignClientFactoryBean.setFallbackFactory(fallbackFactory); | ||
FeignClientBuilder.Builder<T> builder = | ||
this.feignClientBuilder.forType(type, feignClientFactoryBean, serviceName); | ||
if (StringUtils.isNotEmpty(serviceUrl)) { | ||
builder.url(serviceUrl); | ||
} | ||
return builder.build(); | ||
}); | ||
} | ||
|
||
public T getFeignClient( | ||
final Class<T> type, | ||
final FeignClientFactoryBean clientFactoryBean, | ||
final String serviceName, | ||
final String serviceUrl) { | ||
String k = serviceName; | ||
if (StringUtils.isNotEmpty(serviceUrl)) { | ||
k = serviceUrl; | ||
} | ||
return CACHE_BEAN.computeIfAbsent( | ||
k, | ||
(t) -> { | ||
FeignClientBuilder.Builder<T> builder = | ||
this.feignClientBuilder.forType(type, clientFactoryBean, serviceName); | ||
if (StringUtils.isNotEmpty(serviceUrl)) { | ||
builder.url(serviceUrl); | ||
} | ||
return builder.build(); | ||
}); | ||
} | ||
|
||
private T getFromCache(final String serviceName, final String serviceUrl) { | ||
if (StringUtils.isNotEmpty(serviceUrl)) { | ||
return CACHE_BEAN.get(serviceUrl); | ||
} else { | ||
return CACHE_BEAN.get(serviceName); | ||
} | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
linkis-commons/linkis-rpc/src/main/java/org/apache/linkis/rpc/conf/FeignConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* 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.apache.linkis.rpc.conf; | ||
|
||
import org.apache.linkis.rpc.constant.RpcConstant; | ||
import org.apache.linkis.server.security.SSOUtils$; | ||
import org.apache.linkis.server.security.SecurityFilter$; | ||
|
||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.context.request.RequestContextHolder; | ||
import org.springframework.web.context.request.ServletRequestAttributes; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
|
||
import java.util.Enumeration; | ||
|
||
import scala.Tuple2; | ||
|
||
import feign.RequestInterceptor; | ||
import feign.RequestTemplate; | ||
|
||
@Configuration | ||
public class FeignConfig implements RequestInterceptor { | ||
|
||
@Override | ||
public void apply(RequestTemplate requestTemplate) { | ||
ServletRequestAttributes attributes = | ||
(ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); | ||
if (null != attributes) { | ||
HttpServletRequest request = attributes.getRequest(); | ||
Enumeration<String> headerNames = request.getHeaderNames(); | ||
while (headerNames.hasMoreElements()) { | ||
String name = headerNames.nextElement(); | ||
String value = request.getHeader(name); | ||
if (name.equalsIgnoreCase("content-length")) { | ||
continue; | ||
} | ||
requestTemplate.header(name, value); | ||
} | ||
requestTemplate.header( | ||
RpcConstant.LINKIS_LOAD_BALANCER_TYPE, RpcConstant.LINKIS_LOAD_BALANCER_TYPE_RPC); | ||
Tuple2<String, String> userTicketKV = | ||
SSOUtils$.MODULE$.getUserTicketKV(SecurityFilter$.MODULE$.OTHER_SYSTEM_IGNORE_UM_USER()); | ||
requestTemplate.header(userTicketKV._1, userTicketKV._2); | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
linkis-commons/linkis-rpc/src/main/java/org/apache/linkis/rpc/constant/RpcConstant.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* 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.apache.linkis.rpc.constant; | ||
|
||
public class RpcConstant { | ||
|
||
public static final String LINKIS_LOAD_BALANCER_TYPE = "LinkisLoadBalancerType"; | ||
|
||
public static final String LINKIS_LOAD_BALANCER_TYPE_RPC = "RPC"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters