Skip to content

feat: 优化feign配置 #205 #207

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

Merged
merged 1 commit into from
Jul 17, 2024
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
@@ -1,25 +1,23 @@
package com.tencent.devops.service

import com.tencent.devops.service.feign.CustomSpringMvcContract
import com.tencent.devops.service.feign.FeignGlobalConfiguration
import com.tencent.devops.service.feign.FeignFilterRequestMappingHandlerMapping
import feign.Contract
import org.springframework.boot.autoconfigure.AutoConfigureBefore
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration
import org.springframework.boot.autoconfigure.web.servlet.WebMvcRegistrations
import org.springframework.cloud.openfeign.AnnotatedParameterProcessor
import org.springframework.cloud.openfeign.FeignClientProperties
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Import
import org.springframework.context.annotation.PropertySource
import org.springframework.core.convert.ConversionService

/**
* Service自动化配置类
*/
@Configuration(proxyBeanMethods = false)
@PropertySource("classpath:common-service.properties")
@ConditionalOnWebApplication
@Import(FeignGlobalConfiguration::class)
@AutoConfigureBefore(WebMvcAutoConfiguration::class)
class ServiceServletAutoConfiguration {

Expand All @@ -29,14 +27,4 @@ class ServiceServletAutoConfiguration {
override fun getRequestMappingHandlerMapping() = FeignFilterRequestMappingHandlerMapping()
}
}

@Bean
fun feignContract(
feignClientProperties: FeignClientProperties?,
parameterProcessors: List<AnnotatedParameterProcessor>,
feignConversionService: ConversionService,
): Contract {
val decodeSlash = feignClientProperties == null || feignClientProperties.isDecodeSlash
return CustomSpringMvcContract(parameterProcessors, feignConversionService, decodeSlash)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.tencent.devops.service.feign

import feign.Contract
import org.springframework.cloud.openfeign.AnnotatedParameterProcessor
import org.springframework.cloud.openfeign.FeignClientProperties
import org.springframework.cloud.openfeign.FeignFormatterRegistrar
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.core.convert.ConversionService
import org.springframework.format.support.DefaultFormattingConversionService
import org.springframework.format.support.FormattingConversionService

/**
* feign的全局配置
* */
@Configuration
class FeignGlobalConfiguration {
@Bean
fun feignContract(
parameterProcessors: List<AnnotatedParameterProcessor>,
feignClientProperties: FeignClientProperties?,
feignConversionService: ConversionService,
): Contract {
val decodeSlash = feignClientProperties?.isDecodeSlash ?: true
return OldSpringMvcContract(parameterProcessors, feignConversionService, decodeSlash)
}

@Bean
fun feignConversionService(feignFormatterRegistrars: List<FeignFormatterRegistrar>): FormattingConversionService {
val conversionService: FormattingConversionService = DefaultFormattingConversionService()
for (feignFormatterRegistrar in feignFormatterRegistrars) {
feignFormatterRegistrar.registerFormatters(conversionService)
}
return conversionService
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,20 @@ import org.springframework.util.StringUtils
import org.springframework.web.bind.annotation.RequestMapping

/**
* 解决feign client不允许@RequestMapping注解的问题
* 新版本的Spring Cloud的@FeignClient出于安全考虑(不是十分理解),不允许使用@RequestMapping,
* 但这对现有的使用方式有很大的影响,需要修改所有的FeignClient,所以这里我们恢复之前的逻辑,允许在
* FeignClient上使用RequestMapping定义公共的路由前缀。
* */
class CustomSpringMvcContract(
class OldSpringMvcContract(
annotatedParameterProcessors: List<AnnotatedParameterProcessor>,
conversionService: ConversionService,
private val decodeSlash: Boolean,
) : SpringMvcContract(annotatedParameterProcessors, conversionService, decodeSlash) {
private val resourceLoader = DefaultResourceLoader()

/**
* 旧版本的SpringMvcContract处理逻辑
* */
override fun processAnnotationOnClass(data: MethodMetadata, clz: Class<*>) {
if (clz.interfaces.isEmpty()) {
val classAnnotation = findMergedAnnotation(clz, RequestMapping::class.java)
Expand Down
Loading