[case16]springboot2 custom HttpTraceRepository

  springboot

Order

This paper mainly studies the springboot2 custom HttpTraceRepository

Configuration

@Configuration
@ConditionalOnWebApplication
@ConditionalOnProperty(prefix = "management.trace.http", name = "enabled", matchIfMissing = true)
@EnableConfigurationProperties(HttpTraceProperties.class)
@AutoConfigureBefore(HttpTraceAutoConfiguration.class)
public class TraceConfig {

    @Bean
    @ConditionalOnMissingBean(HttpTraceRepository.class)
    public RemoteHttpTraceRepository traceRepository() {
        return new RemoteHttpTraceRepository();
    }
}

A new configuration has been added here, and traceRepository has been customized before HttpTraceAutoConfiguration.

RemoteHttpTraceRepository

public class RemoteHttpTraceRepository implements HttpTraceRepository {

    private static final Logger LOGGER = LoggerFactory.getLogger(RemoteHttpTraceRepository.class);

    @Override
    public List<HttpTrace> findAll() {
        return Collections.emptyList();
    }

    @Override
    public void add(HttpTrace trace) {
        //send log to remote server or log center
        LOGGER.info(JSON.toJSONString(trace));
    }
}

Custom RemoteHttptraceRepository, you can output Trace to the log server by yourself in the add method

Summary

By customizing the HttpTraceRepository, it is very convenient for the webflux project to implement its own accesslog similar to tomcat.

doc