spring 注册表后处理器实现隐藏了prometheus系统和jvm指标

lf5gs5x2  于 11个月前  发布在  Spring
关注(0)|答案(2)|浏览(93)

我使用了一个实现BeanprocesstionRegistryPostProcessor的类,这样我就可以使用postProcessBeanprocesstionRegistry方法来示例化WebClient类型的bean,将它们添加到注册表中,然后通过Autowired和其他Class中的命名器(nameBean)使用它们。
一切正常,除了在prometheus指标中我没有看到任何jvm或系统类型指标。
我有一个类,它是通过re. test. pos. api. consumer. WebClientInstanceBuilderAutoptionation从META-INF/spring调用的。这个类调用WebClientInstanceBuilder类:

@Configuration
@EnableConfigurationProperties({ ConsumerProperties.class })
@ConditionalOnClass(WebClient.class)
@ConditionalOnBean(WebClient.Builder.class)
public class WebClientInstanceBuilderAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public WebClientInstanceBuilder webClientInstanceBuilder(Environment environment,
            WebClient.Builder webClientBuilder) {

        WebClientInstanceBuilder webClientInstanceBuilder = new WebClientInstanceBuilder(webClientBuilder);

        return webClientInstanceBuilder;
    }
}

字符串
我有一个类WebClientInstanceBuilder,它实现了BeanprocesstionRegistryPostProcessor,它的目标是在方法postProcessBeanprocesstionRegistry中示例化bean

public class WebClientInstanceBuilder implements BeanDefinitionRegistryPostProcessor {

    ConsumerProperties consumerProperties;
    WebClient.Builder webClientBuilder;

    public WebClientInstanceBuilder(WebClient.Builder webClientBuilder) {
        this.webClientBuilder   = webClientBuilder;
    }

    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {

            String beanKey = "accountManagementWebClient";

            WebClient client = webClientBuilder.clone().baseUrl("https://test.api:8443/test")
                    .codecs( codecs -> {codecs.defaultCodecs();})
                    .build();

            GenericBeanDefinition definition = new GenericBeanDefinition();
            definition.setBeanClass(WebClient.class);
            definition.setInstanceSupplier(() -> client);
            registry.registerBeanDefinition(beanKey, definition);
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    }

}


我有这样一个类,它使用bean accountManagementWebClient

@Component
public class AccountManager {
    @Autowired
    @Qualifier("accountManagementWebClient")
    private WebClient accountWebClient;

    public String getAccountByBillingAccountId(String billingAccountId, String tumUserId) {

        WebClient theAccountWebClient = accountWebClient;

        try {
            return null;
        } catch (Exception ex) {
            throw ex;
        }

    }
}


除了prometheus指标外,一切正常。当我查看指标prometheus时,我看不到JMV指标。我application.properties我有:

management.server.port=4242
management.endpoints.web.exposure.include=*
management.metrics.enable.all=true


如果我删除了BeanwiredtionRegistryPostProcessor的实现,并继续使用另一个解决方案创建WebClient Bean,我可以看到所有的指标prometheus,还有jvm和系统指标。但在这种情况下,我在使用@Autowired检索Bean时遇到了问题,因为不幸的是,一旦我删除了BeanwiredtionRegistryPostProcessor的实现,Autowired就会在我的bean示例化之前执行。
我尝试了很多方法,但都没有成功。
非常感谢

biswetbf

biswetbf1#

您不应该使用BeanDefinitionRegistryPostProcessor自定义WebClient,而是可以这样做:

@Bean
WebClient accountWebClient(WebClient.Builder builder) {
    return builder.baseUrl("https://httpbin.org/get").build();
}

字符串

g6baxovj

g6baxovj2#

另一个问题是:外部库使用BeanprocesstionRegistryPostProcessor的实现,因为我认为它可以覆盖方法postProcessBeanprocesstionRegistry(BeanprocesstionRegistry registry),因此使用注册表来存储bean。
有没有其他方法可以在不实现BeanfectionRegistryPostProcessor的情况下获得注册表?

相关问题