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

org.zodiac.autoconfigure.scc.client.SpringCloudClientDiscoveryBootstrapAutoConfiguration Maven / Gradle / Ivy

There is a newer version: 1.6.8
Show newest version
package org.zodiac.autoconfigure.scc.client;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.SmartApplicationListener;
import org.zodiac.autoconfigure.bootstrap.condition.ConditionalOnSpringCloudBootstrapDisabled;
import org.zodiac.autoconfigure.scc.client.condition.ConditionalOnSpringCloudAppConfigClientEnabled;
import org.zodiac.commons.util.Colls;

@SpringBootConfiguration
@ConditionalOnSpringCloudBootstrapDisabled
@ConditionalOnSpringCloudAppConfigClientEnabled
@org.springframework.cloud.client.discovery.EnableDiscoveryClient
@Import(value = {org.springframework.cloud.commons.util.UtilAutoConfiguration.class})
@ConditionalOnProperty(value = org.zodiac.scc.client.constants.SpringConfigClientSystemPropertiesConstants.SPRING_BOOTSTRAP_CONFIG_SPRING_CLOUD_DISCOVERY_ENABLED, havingValue = "true")
@ConditionalOnClass(value = {org.springframework.cloud.client.ServiceInstance.class, org.springframework.cloud.client.discovery.DiscoveryClient.class, org.zodiac.scc.client.SpringCloudConfigServerInstanceProvider.class})
public class SpringCloudClientDiscoveryBootstrapAutoConfiguration {

    private static Logger logger = LoggerFactory.getLogger(SpringCloudClientDiscoveryBootstrapAutoConfiguration.class);

    public SpringCloudClientDiscoveryBootstrapAutoConfiguration() {
    }

    @Bean
    protected org.zodiac.scc.client.SpringCloudConfigServerInstanceProvider configServerInstanceProvider(
        ObjectProvider function,
        ObjectProvider discoveryClient) {
        org.zodiac.scc.client.SpringCloudConfigServerInstanceProvider.Function fn = function.getIfAvailable();
        if (fn != null) {
            return new org.zodiac.scc.client.SpringCloudConfigServerInstanceProvider(fn);
        }
        org.springframework.cloud.client.discovery.DiscoveryClient client = discoveryClient.getIfAvailable();
        if (client == null) {
            throw new IllegalStateException("ConfigServerInstanceProvider reqiures a DiscoveryClient or Function");
        }
        return new org.zodiac.scc.client.SpringCloudConfigServerInstanceProvider(client);
    }

    @Bean
    protected SmartApplicationListener heartbeatListener(SpringCloudAppConfigClientProperties properties,
        org.zodiac.scc.client.SpringCloudConfigServerInstanceProvider provider) {
        return new HeartbeatListener(properties, provider);
    }

    private final static class HeartbeatListener implements SmartApplicationListener {

        private final SpringCloudAppConfigClientProperties springCloudAppConfigClientProperties;

        private final org.zodiac.scc.client.SpringCloudConfigServerInstanceProvider instanceProvider;

        private final org.springframework.cloud.client.discovery.event.HeartbeatMonitor monitor =
            new org.springframework.cloud.client.discovery.event.HeartbeatMonitor();

        private HeartbeatListener(SpringCloudAppConfigClientProperties springCloudAppConfigClientProperties,
            org.zodiac.scc.client.SpringCloudConfigServerInstanceProvider instanceProvider) {
            this.springCloudAppConfigClientProperties = springCloudAppConfigClientProperties;
            this.instanceProvider = instanceProvider;
        }

        @Override
        public boolean supportsEventType(Class eventType) {
            return ContextRefreshedEvent.class.isAssignableFrom(eventType)
                || org.springframework.cloud.client.discovery.event.HeartbeatEvent.class.isAssignableFrom(eventType);
        }

        @Override
        public void onApplicationEvent(ApplicationEvent event) {
            if (event instanceof ContextRefreshedEvent) {
                startup((ContextRefreshedEvent)event);
            } else if (event instanceof org.springframework.cloud.client.discovery.event.HeartbeatEvent) {
                heartbeat((org.springframework.cloud.client.discovery.event.HeartbeatEvent)event);
            }
        }

        public void startup(ContextRefreshedEvent event) {
            if (springCloudAppConfigClientProperties.isRefreshOnStartup())
                refresh();
        }

        public void heartbeat(org.springframework.cloud.client.discovery.event.HeartbeatEvent event) {
            if (this.monitor.update(event.getValue())) {
                if (springCloudAppConfigClientProperties.isRefreshOnHeartbeat())
                    refresh();
            }
        }

        private void refresh() {
            try {
                String serviceId = this.springCloudAppConfigClientProperties.getDiscovery().getServiceId();
                List listOfUrls = Colls.list();
                List serviceInstances =
                    this.instanceProvider.getConfigServerInstances(serviceId);

                for (int i = 0; i < serviceInstances.size(); i++) {

                    org.springframework.cloud.client.ServiceInstance server = serviceInstances.get(i);
                    String url = getHomePage(server);

                    if (server.getMetadata().containsKey("password")) {
                        String user = server.getMetadata().get("user");
                        user = user == null ? "user" : user;
                        this.springCloudAppConfigClientProperties.setUsername(user);
                        String password = server.getMetadata().get("password");
                        this.springCloudAppConfigClientProperties.setPassword(password);
                    }

                    if (server.getMetadata().containsKey("configPath")) {
                        String path = server.getMetadata().get("configPath");
                        if (url.endsWith("/") && path.startsWith("/")) {
                            url = url.substring(0, url.length() - 1);
                        }
                        url = url + path;
                    }

                    listOfUrls.add(url);
                }

                String[] uri = new String[listOfUrls.size()];
                uri = listOfUrls.toArray(uri);
                this.springCloudAppConfigClientProperties.setUri(uri);

            } catch (Exception ex) {
                if (this.springCloudAppConfigClientProperties.isFailFast()) {
                    throw ex;
                } else {
                    logger.warn("Could not locate configserver via discovery.", ex);
                }
            }
        }

        private String getHomePage(org.springframework.cloud.client.ServiceInstance server) {
            return server.getUri().toString() + "/";
        }

    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy