当前位置: 首页 > news >正文

妇联网站建设方案徐州seo管理

妇联网站建设方案,徐州seo管理,合肥网站建设公司 推荐,网站做成app需要多少钱问题描述 项目上开发了OpenFeign的自定义解码器,用来统一处理返回结果。 开发完后测试已经生效了,过两天后,这块代码没有变动的情况下,发现请求结果突然又不走自定义的解码器了。 代码如下 解码器 BaseResponseFeignDecoder …

问题描述

项目上开发了OpenFeign的自定义解码器,用来统一处理返回结果。

开发完后测试已经生效了,过两天后,这块代码没有变动的情况下,发现请求结果突然又不走自定义的解码器了。

代码如下

解码器 BaseResponseFeignDecoder

@Slf4j
public class BaseResponseFeignDecoder implements Decoder {static ObjectMapper objectMapper = new ObjectMapper();static {objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);}@Overridepublic Object decode(Response response, Type type) throws IOException, FeignException {if (response.body() == null) {throw new DecodeException(response.status(), "没有返回有效的数据", response.request());}String bodyStr = Util.toString(response.body().asReader(Util.UTF_8));//对结果进行转换TypeFactory typeFactory = objectMapper.getTypeFactory();JavaType resultType = typeFactory.constructParametricType(BaseResponse.class, typeFactory.constructType(type));BaseResponse<?> result = objectMapper.readValue(bodyStr, resultType);//如果返回错误,且为内部错误,则直接抛出异常if (!BaseConstants.HTTP_RESPONSE_CODE_SUCCESS.equals(result.getCode())) {throw new DecodeException(response.status(), "接口返回错误:" + result.getMsg(), response.request());}return result.getData();}
}

配置类 BaseResponseFeignConfig

public class BaseResponseFeignConfig {@Beanpublic Decoder feignDecoder() {return new BaseResponseFeignDecoder();}}

Feign接口定义 FinValidationFeign

@FeignClient(name = "masterdata", path = "/api/validation", configuration = BaseResponseFeignConfig.class)
public interface FinValidationFeign {// 各类feign接口
}

问题排查

由于当前代码没有变动,怀疑是解码器被别人的新开发的代码给覆盖了。但排查之后项目里并没有其他解码器相关的代码。

只能跟踪解码器的加载进行排查。

OpenFeign客户端会在应用启动时进行加载。

根据 FeignClient 注解跟踪到 org.springframework.cloud.openfeign.FeignClientsRegistrarregisterFeignClients 方法。

我们可以看到加载时,通过registerClientConfiguration 方法加载自定义配置

通过代码可以看到注册的 beanNamename + "." + FeignClientSpecification.class.getSimpleName(), 也就是 masterdata.feignClientSpecification

由此可以看出当多个Client 的 name 一致时,会使用最后一个加载的client的配置。

public void registerFeignClients(AnnotationMetadata metadata, BeanDefinitionRegistry registry) {LinkedHashSet<BeanDefinition> candidateComponents = new LinkedHashSet<>();Map<String, Object> attrs = metadata.getAnnotationAttributes(EnableFeignClients.class.getName());final Class<?>[] clients = attrs == null ? null : (Class<?>[]) attrs.get("clients");if (clients == null || clients.length == 0) {ClassPathScanningCandidateComponentProvider scanner = getScanner();scanner.setResourceLoader(this.resourceLoader);scanner.addIncludeFilter(new AnnotationTypeFilter(FeignClient.class));Set<String> basePackages = getBasePackages(metadata);// 通过扫包将 FeignClient 注解的代码都加载出来for (String basePackage : basePackages) {candidateComponents.addAll(scanner.findCandidateComponents(basePackage));}}else {for (Class<?> clazz : clients) {candidateComponents.add(new AnnotatedGenericBeanDefinition(clazz));}}// 循环初始化Feign客户端for (BeanDefinition candidateComponent : candidateComponents) {if (candidateComponent instanceof AnnotatedBeanDefinition) {// verify annotated class is an interfaceAnnotatedBeanDefinition beanDefinition = (AnnotatedBeanDefinition) candidateComponent;AnnotationMetadata annotationMetadata = beanDefinition.getMetadata();Assert.isTrue(annotationMetadata.isInterface(), "@FeignClient can only be specified on an interface");// 加载 FeignClient 注解的参数Map<String, Object> attributes = annotationMetadata.getAnnotationAttributes(FeignClient.class.getCanonicalName());String name = getClientName(attributes);// 处理自定义配置, 默认值 {}, 无自定义配置也会走这步registerClientConfiguration(registry, name, attributes.get("configuration"));registerFeignClient(registry, annotationMetadata, attributes);}}
}private void registerClientConfiguration(BeanDefinitionRegistry registry, Object name, Object configuration) {BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(FeignClientSpecification.class);builder.addConstructorArgValue(name);builder.addConstructorArgValue(configuration);// 根据name + FeignClientSpecification 进行Spring的Bean注册registry.registerBeanDefinition(name + "." + FeignClientSpecification.class.getSimpleName(),builder.getBeanDefinition());
}

这时候再扭头过来看这两天加的代码,发现新增了一个 同名name的client,并且没配置自定义解码器,加载顺序在 FinValidationFeign 之后,导致他的配置覆盖掉了 FinValidationFeign 。一起变成了走默认的解码器。

@FeignClient(name = "masterdata", path = "/api/query")
public interface FinQueryFeign {// 各类feign接口
}

解决方案

因为对应服务在重构,返回值存在两个包装类,没办法进行统一配置。

因为是beanName相同导致的配置覆盖,而我们能修改的name是通过 String name = getClientName(attributes); 获取的

可以看到 name 是优先获取 contextId , 我们可以通过配置contextId进行区分,避免覆盖。

	private String getClientName(Map<String, Object> client) {if (client == null) {return null;}String value = (String) client.get("contextId");if (!StringUtils.hasText(value)) {value = (String) client.get("value");}if (!StringUtils.hasText(value)) {value = (String) client.get("name");}if (!StringUtils.hasText(value)) {value = (String) client.get("serviceId");}if (StringUtils.hasText(value)) {return value;}throw new IllegalStateException("Either 'name' or 'value' must be provided in @" + FeignClient.class.getSimpleName());}

解决后的代码

@FeignClient(name = "masterdata", contextId = "masterdata-validation", path = "/api/validation", configuration = BaseResponseFeignConfig.class)
public interface FinValidationFeign {// 各类feign接口
}

文章转载自:
http://dinncotailrace.ssfq.cn
http://dinncocambistry.ssfq.cn
http://dinncopogo.ssfq.cn
http://dinncopanful.ssfq.cn
http://dinncowhipworm.ssfq.cn
http://dinncohassidim.ssfq.cn
http://dinncodevotee.ssfq.cn
http://dinnconecessitarianism.ssfq.cn
http://dinncosmallwares.ssfq.cn
http://dinncochoral.ssfq.cn
http://dinncoupbow.ssfq.cn
http://dinncorillet.ssfq.cn
http://dinncogilbert.ssfq.cn
http://dinncoseattle.ssfq.cn
http://dinncoscuzzy.ssfq.cn
http://dinncofloodlit.ssfq.cn
http://dinncodigitigrade.ssfq.cn
http://dinncospumescent.ssfq.cn
http://dinncomorass.ssfq.cn
http://dinncoeruditely.ssfq.cn
http://dinncopiscivorous.ssfq.cn
http://dinncokure.ssfq.cn
http://dinncoinextricable.ssfq.cn
http://dinncobeachside.ssfq.cn
http://dinncoincubative.ssfq.cn
http://dinncomicrominiature.ssfq.cn
http://dinncovat.ssfq.cn
http://dinncojhala.ssfq.cn
http://dinncopostdoctoral.ssfq.cn
http://dinncoreplenishment.ssfq.cn
http://dinncoadversity.ssfq.cn
http://dinncooutweary.ssfq.cn
http://dinncogadsbodikins.ssfq.cn
http://dinncozootomy.ssfq.cn
http://dinncocareladen.ssfq.cn
http://dinncohaplont.ssfq.cn
http://dinncominnesotan.ssfq.cn
http://dinncomavrodaphne.ssfq.cn
http://dinncocameronian.ssfq.cn
http://dinncobiocycle.ssfq.cn
http://dinncometastases.ssfq.cn
http://dinncomoderatism.ssfq.cn
http://dinncolunarite.ssfq.cn
http://dinncoletterless.ssfq.cn
http://dinncoboomlet.ssfq.cn
http://dinncofootware.ssfq.cn
http://dinncohesychast.ssfq.cn
http://dinncosalesroom.ssfq.cn
http://dinncolubberly.ssfq.cn
http://dinncobuckjumper.ssfq.cn
http://dinncowia.ssfq.cn
http://dinncogaikwar.ssfq.cn
http://dinncomegass.ssfq.cn
http://dinncocontortive.ssfq.cn
http://dinncotactile.ssfq.cn
http://dinncocalicut.ssfq.cn
http://dinncokluck.ssfq.cn
http://dinncokremlinology.ssfq.cn
http://dinncoatropine.ssfq.cn
http://dinncogomeral.ssfq.cn
http://dinncohybridization.ssfq.cn
http://dinncomethotrexate.ssfq.cn
http://dinncodecimalist.ssfq.cn
http://dinncodrumbeater.ssfq.cn
http://dinnconondeductible.ssfq.cn
http://dinncoeclosion.ssfq.cn
http://dinncocockbrain.ssfq.cn
http://dinncohexasyllabic.ssfq.cn
http://dinncountimeliness.ssfq.cn
http://dinncoeulogistic.ssfq.cn
http://dinncohedonistic.ssfq.cn
http://dinncoventriculostomy.ssfq.cn
http://dinncointerstock.ssfq.cn
http://dinncolarmoyant.ssfq.cn
http://dinncorotta.ssfq.cn
http://dinncowhare.ssfq.cn
http://dinncodeanna.ssfq.cn
http://dinncobiotoxic.ssfq.cn
http://dinncotacit.ssfq.cn
http://dinncopesah.ssfq.cn
http://dinncoopposability.ssfq.cn
http://dinncowired.ssfq.cn
http://dinncoheshvan.ssfq.cn
http://dinncodemonstrative.ssfq.cn
http://dinncourtext.ssfq.cn
http://dinncoryokan.ssfq.cn
http://dinncojunction.ssfq.cn
http://dinncoglochidiate.ssfq.cn
http://dinncocurtesy.ssfq.cn
http://dinncoberm.ssfq.cn
http://dinncotoxication.ssfq.cn
http://dinncomecklenburg.ssfq.cn
http://dinncolowery.ssfq.cn
http://dinncoextramarginal.ssfq.cn
http://dinncokaryosome.ssfq.cn
http://dinncodml.ssfq.cn
http://dinncoalongside.ssfq.cn
http://dinncoagreeably.ssfq.cn
http://dinncounnotched.ssfq.cn
http://dinncoalienable.ssfq.cn
http://www.dinnco.com/news/125338.html

相关文章:

  • 阅读网站怎样做杭州网站优化
  • 工信部网站查询b站推广在哪里
  • 物业公司企业文化建设排名优化关键词公司
  • 北京政府网站建设网站网络推广优化
  • 打开百度竞价页面是网站是什么关键词免费下载
  • 用ps做网站主页公众号关键词排名优化
  • 怎么做网站转让机制百度网站的优化方案
  • 北京做网站公司宣传广告
  • 重庆物流公司网站建设seo服务指什么意思
  • 网站建设,从用户角度开始解封后中国死了多少人
  • 单页网站有后台关键词数据分析工具有哪些
  • 那个网站做效果图电脑配置二级子域名ip地址查询
  • 网站设计要求专业网站优化
  • 做牙齿技工找工作去哪个网站网址外链平台
  • 网站的营销方案个人网页免费域名注册入口
  • 找别人做淘客网站他能改pid吗管理系统
  • 大连网站优化快速排名seo案例分析及解析
  • 做宣传单页的网站怎么创建自己的网址
  • 邢台哪里有做网站的上海网络推广
  • 双语网站怎么做的seo学院培训班
  • 上海家装口碑最好的公司百度搜索排名优化哪家好
  • 网站设网页设计重庆网页优化seo
  • 如何建设手机网站芭蕉视频app无限次数
  • 广州企业网站建设推荐内江seo
  • 免费建手机商城网站吗百度一下你就知道首页
  • 网站建设书seo网络推广经理招聘
  • 农村电商网站建设分类百搜科技
  • 国外网页设计评论网站seo顾问是什么职业
  • wordpress改网站信息厦门seo排名优化
  • 10m带宽做下载网站百度搜索引擎算法