Skip to content

Commit

Permalink
feat: 网关新增强制路由集群 TencentBlueKing#4662
Browse files Browse the repository at this point in the history
  • Loading branch information
mingshewhe committed Jul 14, 2021
1 parent 058d4c3 commit 9a2eb1d
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
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-HEAD-CONSUL-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

0 comments on commit 9a2eb1d

Please sign in to comment.