codecentric / spring-boot-admin

Admin UI for administration of spring boot applications
Apache License 2.0
12.39k stars 3.08k forks source link

SpringBootAdminClientAutoConfiguration required a single bean, but 2 were found #3312

Closed ffroliva closed 6 months ago

ffroliva commented 6 months ago

Spring Boot Admin Server information

Client information

Description

I need to customize RegistrationClient and for that, I have created a Configuration classes as follows:

import de.codecentric.boot.admin.client.config.ClientProperties;
import de.codecentric.boot.admin.client.registration.BlockingRegistrationClient;
import de.codecentric.boot.admin.client.registration.RegistrationClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.ssl.NoSuchSslBundleException;
import org.springframework.boot.ssl.SslBundle;
import org.springframework.boot.ssl.SslBundles;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.boot.web.client.RestTemplateCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Primary;
import org.springframework.web.client.RestTemplate;

@Slf4j
@Configuration
@Import({ClientProperties.class})
public class RestTemplateConfiguration {

    public static final String MY_SSL_BUNDLE = "my_ssl_bundle";

    RestTemplateBuilder customRestTemplateBuilder(RestTemplateBuilder restTemplateBuilder, SslBundles sslBundles) {
        if (sslBundles != null) {
            try {
                SslBundle sslBundle = sslBundles.getBundle(MY_SSL_BUNDLE);
                restTemplateBuilder.setSslBundle(sslBundle);
                log.info("Found 'spring.ssl.bundle.jks.my_ssl_bundle.*' bundle, setting it to RestTemplateBuilder");
            } catch (NoSuchSslBundleException ex) {
                log.info("SSL not enabled. 'spring.ssl.bundle.jks.my_ssl_bundle.*' not provided on the configurations.");
                log.info("Creating RestTemplate with no SSL configurations.");
            }
        }
        return restTemplateBuilder;
    }

    @Bean
    @Primary
    public RegistrationClient registrationClient(@Qualifier("de.codecentric.boot.admin.client.config.ClientProperties") ClientProperties client, SslBundles sslBundles) {
        RestTemplateBuilder builder = (new RestTemplateBuilder(new RestTemplateCustomizer[0])).setConnectTimeout(client.getConnectTimeout());
        builder.setReadTimeout(client.getReadTimeout());
        if (client.getUsername() != null && client.getPassword() != null) {
            builder = builder.basicAuthentication(client.getUsername(), client.getPassword());
        }

        RestTemplate build = customRestTemplateBuilder(builder, sslBundles).build();
        return new BlockingRegistrationClient(build);
    }

    /**
     * @return a new customized ResTemplate instance
     */
    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder, SslBundles sslBundles) {
        return customRestTemplateBuilder(restTemplateBuilder, sslBundles).build();
    }
}

When I start the application I get the following error:

Parameter 1 of method registrator in de.codecentric.boot.admin.client.config.SpringBootAdminClientAutoConfiguration required a single bean, but 2 were found:

hzpz commented 6 months ago

Hey @ffroliva, this is a Spring related issue (not Spring Boot Admin). Spring Boot Admin already provides an instance of ClientProperties (see @EnableConfigurationProperties in SpringBootAdminClientAutoConfiguration). If you @Import ClientProperties again, there will be another instance of this class and this is what the error message is about.

Just remove the line @Import({ClientProperties.class}) and it should work. Please let us know if that helps.

ffroliva commented 6 months ago

Dear Timo,

I have added the @.***and removed the @Import({ClientProperties.class})`. It worked.

I wonder if the approach I am taking is the best approach to customize the RegistrationClient with SSL configuration. Do you suggest any alternative solution?

Thank you in advance.

Regards,

Flavio Oliva

Em sex., 19 de abr. de 2024 às 07:35, Timo @.***> escreveu:

Hey @ffroliva https://github.com/ffroliva, this is a Spring related issue (not Spring Boot Admin). Spring Boot Admin already provides an instance of ClientProperties (see @EnableConfigurationProperties in SpringBootAdminClientAutoConfiguration). If you @Import ClientProperties again, there will be another instance of this class and this is what the error message is about.

Just remove the line @Import({ClientProperties.class}) and it should work. Please let us know if that helps.

— Reply to this email directly, view it on GitHub https://github.com/codecentric/spring-boot-admin/issues/3312#issuecomment-2065855069, or unsubscribe https://github.com/notifications/unsubscribe-auth/AF67VGZNFH24DFVT4IFERQLY6C3MRAVCNFSM6AAAAABGLMINJ2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDANRVHA2TKMBWHE . You are receiving this because you were mentioned.Message ID: @.***>

hzpz commented 6 months ago

Hey @ffroliva, right now it seems that this is the only way to customize the RegistrationClient. We are considering changing our auto-configuration so that it'll be easier to customize and/or implement a property to customize the SSL bundle.

ffroliva commented 6 months ago

Thank you @Timo

Looking forward to this enhancement.

BTW, spring boot admin is a great project. Well done!

You may close this thread if you understand so.

Regards,

Flavio

Em sex., 19 de abr. de 2024 às 12:51, Timo @.***> escreveu:

Hey @ffroliva https://github.com/ffroliva, right now it seems that this is the only way to customize the RegistrationClient. We are considering changing our auto-configuration so that it'll be easier to customize and/or implement a property to customize the SSL bundle.

— Reply to this email directly, view it on GitHub https://github.com/codecentric/spring-boot-admin/issues/3312#issuecomment-2066410988, or unsubscribe https://github.com/notifications/unsubscribe-auth/AF67VG65VHHHBBB4VYEWTG3Y6EAN5AVCNFSM6AAAAABGLMINJ2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDANRWGQYTAOJYHA . You are receiving this because you were mentioned.Message ID: @.***>

hzpz commented 6 months ago

Thanks! We will mention this issue if we implement any of the enhancements.