All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.zodiac.autoconfigure.bootstrap.breaker.routing.AppRoutingServerAutoConfiguration Maven / Gradle / Ivy

There is a newer version: 1.6.8
Show newest version
package org.zodiac.autoconfigure.bootstrap.breaker.routing;

import com.alibaba.ttl.TtlRunnable;
import com.alibaba.ttl.threadpool.agent.TtlAgent;

import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.task.TaskExecutorCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.zodiac.autoconfigure.bootstrap.condition.ConditionalOnBreakerRoutingEnabled;
import org.zodiac.autoconfigure.bootstrap.condition.ConditionalOnSpringCloudBootstrapDisabled;
import org.zodiac.commons.constants.SystemPropertiesConstants;
import org.zodiac.core.bootstrap.breaker.routing.service.AppRoutingClient;
import org.zodiac.core.bootstrap.breaker.routing.service.AppRoutingService;
import org.zodiac.core.bootstrap.breaker.routing.service.CompositeAppRoutingService;
import org.zodiac.core.bootstrap.breaker.routing.service.InternalAppRoutingService;
import org.zodiac.core.bootstrap.breaker.routing.service.NoopAppRoutingService;
import org.zodiac.core.bootstrap.breaker.routing.service.OrderedAppRoutingService;
import org.zodiac.core.context.config.annotation.AppRefreshScope;
import org.zodiac.core.internal.http.AppInternalHttpClient;

import java.util.List;
import java.util.Objects;

@AppRefreshScope
@SpringBootConfiguration
@ConditionalOnBreakerRoutingEnabled
@ConditionalOnSpringCloudBootstrapDisabled
public class AppRoutingServerAutoConfiguration {

    public AppRoutingServerAutoConfiguration() {
        super();
    }

    @Bean
    @ConditionalOnMissingBean
    protected TaskExecutorCustomizer taskExecutorCustomizer() {
        return taskExecutor -> taskExecutor.setTaskDecorator(runnable -> {
            if (!TtlAgent.isTtlAgentLoaded()) {
                return Objects.requireNonNull(TtlRunnable.get(runnable));
            }
            return runnable;
        });
    }

    @Bean
    protected NoopAppRoutingService noopAppRoutingService() {
        return new NoopAppRoutingService();
    }

    @SpringBootConfiguration
    @ConditionalOnWebApplication
    @ConditionalOnBreakerRoutingEnabled
    @EnableConfigurationProperties(value = {InternalAppRoutingProperties.class})
    protected static class OnWebAppRoutingAutoConfiguration {

        public OnWebAppRoutingAutoConfiguration() {
            super();
        }

        @Bean
        @ConditionalOnMissingBean
        @ConditionalOnProperty(value = SystemPropertiesConstants.Zodiac.SPRING_BOOTSTRAP_BREAKER_ROUTING_INTERNAL_ENABLED, havingValue = "true")
        protected AppRoutingClient routingClient(AppInternalHttpClient internalHttpClient) {
            return new AppRoutingClient(internalHttpClient);
        }

        @Bean
        @ConditionalOnMissingBean
        @ConditionalOnProperty(value = SystemPropertiesConstants.Zodiac.SPRING_BOOTSTRAP_BREAKER_ROUTING_INTERNAL_ENABLED, havingValue = "true")
        protected OrderedAppRoutingService internalRoutingService(
            InternalAppRoutingProperties internalRoutingProperties, AppRoutingClient routingClient) {
            return new InternalAppRoutingService(internalRoutingProperties, routingClient);
        }
    }

    @Bean
    @Primary
    protected AppRoutingService routingService(ObjectProvider> routingServiceList) {
        return new CompositeAppRoutingService(routingServiceList.getIfAvailable());
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy