Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 网关新增强制路由集群 #4662 #4684

Merged
merged 2 commits into from
Jul 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,5 @@ const val AUTH_HEADER_DEVOPS_SIGN_INFO = "X-DEVOPS-SIGN-INFO"

const val AUTH_HEADER_PLATFORM = "X-DEVOPS-PLATFORM"
const val AUTH_HEADER_APP_VERSION = "X-DEVOPS-APP-VERSION"

const val AUTH_HEADER_GATEWAY_TAG = "X-GATEWAY-TAG"
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,18 @@ class Client @Autowired constructor(

// devnet区域的,只能直接通过ip访问
fun <T : Any> getScm(clz: KClass<T>): T {
return getGateway(clz, GatewayType.IDC_PROXY)
// 从网关访问去掉后缀,否则会变成 /process-devops/api/service/piplines 导致访问失败
val serviceName = findServiceName(clz).removeSuffix(serviceSuffix ?: "")
// 获取为feign定义的拦截器
val requestInterceptor = SpringContextUtil.getBeansWithClass(RequestInterceptor::class.java)
return Feign.builder()
.client(feignClient)
.errorDecoder(clientErrorDecoder)
.encoder(jacksonEncoder)
.decoder(jacksonDecoder)
.contract(jaxRsContract)
.requestInterceptors(requestInterceptor)
.target(clz.java, buildGatewayUrl(path = "/$serviceName/api", gatewayType = GatewayType.IDC_PROXY))
}

fun <T : Any> getImpl(clz: KClass<T>): T {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,35 @@
package com.tencent.devops.common.web

import com.tencent.devops.common.api.auth.AUTH_HEADER_DEVOPS_JWT_TOKEN
import com.tencent.devops.common.api.auth.AUTH_HEADER_GATEWAY_TAG
import com.tencent.devops.common.client.consul.ConsulConstants
import com.tencent.devops.common.client.consul.ConsulContent
import com.tencent.devops.common.security.jwt.JwtManager
import com.tencent.devops.common.service.trace.TraceTag
import feign.RequestInterceptor
import org.slf4j.LoggerFactory
import org.slf4j.MDC
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Value
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Primary
import org.springframework.web.context.request.RequestContextHolder
import org.springframework.web.context.request.ServletRequestAttributes

@Configuration
class FeignConfiguration {

@Value("\${spring.cloud.consul.discovery.tags:#{null}}")
private val tag: String? = null

private val logger = LoggerFactory.getLogger(FeignConfiguration::class.java)

/**
* feign调用拦截器
*/
@Bean
@Primary
fun requestInterceptor(@Autowired jwtManager: JwtManager): RequestInterceptor {
return RequestInterceptor { requestTemplate ->
val attributes =
Expand Down Expand Up @@ -87,4 +97,14 @@ class FeignConfiguration {
}
}
}

@Bean
fun gatewayTagRequestInterceptor(): RequestInterceptor {
return RequestInterceptor { requestTemplate ->
logger.info("add X-GATEWAY-TAG $tag")
if (!requestTemplate.headers().containsKey(AUTH_HEADER_GATEWAY_TAG)) {
requestTemplate.header(AUTH_HEADER_GATEWAY_TAG, tag)
}
}
}
}
4 changes: 2 additions & 2 deletions src/gateway/core/lua/util/tag_util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function _M:get_tag(ns_config)
local devops_project = ngx.var.project_id
local default_tag = ns_config.tag
local tag = default_tag
local header_tag = ngx.var.http_x_devops_tag
local header_tag = ngx.var.http_x_gateway_tag

if header_tag == nil then
if devops_project ~= nil and devops_project ~= '' then
Expand Down Expand Up @@ -91,7 +91,7 @@ function _M:get_tag(ns_config)
end

-- 设置tag到http请求头
ngx.header["X-DEVOPS-TAG"] = tag
ngx.header["X-GATEWAY-TAG"] = tag
else
tag = header_tag
end
Expand Down