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

org.zodiac.autoconfigure.eureka.EurekaAutoConfiguration Maven / Gradle / Ivy

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

import java.util.Map;
import java.util.Properties;

import org.springframework.aop.support.AopUtils;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.SearchStrategy;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.env.ConfigurableEnvironment;
import org.zodiac.autoconfigure.application.ApplicationInfoProperties;
import org.zodiac.autoconfigure.application.shutdown.AppShutdownConfigProperties;
import org.zodiac.autoconfigure.bootstrap.condition.ConditionalOnAppDiscoveryEnabled;
import org.zodiac.autoconfigure.bootstrap.condition.ConditionalOnSpringCloudBootstrapDisabled;
import org.zodiac.autoconfigure.bootstrap.registry.AutoAppRegistrationProperties;
import org.zodiac.autoconfigure.eureka.condition.ConditionalOnEurekaClientEnabled;
import org.zodiac.commons.constants.SystemPropertiesConstants;
import org.zodiac.commons.util.lang.Numbers;
import org.zodiac.commons.util.lang.Strings;
import org.zodiac.commons.util.spring.Springs;
import org.zodiac.core.application.AppInstance;
import org.zodiac.core.application.ApplicationInfoMetadata;
import org.zodiac.core.bootstrap.discovery.AppDiscoveryClient;
import org.zodiac.core.bootstrap.discovery.constants.DiscoveryConstants;

@SpringBootConfiguration
@ConditionalOnEurekaClientEnabled
@ConditionalOnAppDiscoveryEnabled
@ConditionalOnSpringCloudBootstrapDisabled
@ConditionalOnClass(value = {org.zodiac.eureka.client.EurekaAppDiscoveryClient.class, com.netflix.discovery.EurekaClient.class, com.netflix.config.ConfigurationManager.class})
public class EurekaAutoConfiguration {

    private ApplicationContext applicationContext;
    private ConfigurableEnvironment environment;
    private ApplicationInfoProperties applicationInfoProperties;
    private EurekaLocalProperties eurekaLocalProperties;
    private EurekaProperties eurekaProperties;

    public EurekaAutoConfiguration(ApplicationContext applicationContext, ConfigurableEnvironment environment,
        ApplicationInfoProperties applicationInfoProperties) {
        this.applicationContext = applicationContext;
        this.environment = environment;
        this.applicationInfoProperties = applicationInfoProperties;
    }

//    @Bean
//    @ConfigurationProperties(prefix = org.zodiac.eureka.base.constants.EurekaBaseSystemPropertiesConstants.EUREKA_LOCAL_PREFIX)
//    protected EurekaLocalProperties eurekaLocalProperties() {
//        Integer port = Numbers.getOrDefault(serverProperties.getPort(), Constants.Spring.DEFAULT_SERVER_PORT_NUMBER);
//        Integer securePort = serverProperties.getSsl() != null && serverProperties.getSsl().isEnabled() ? port : -1;
//        String urlPrefix = Springs.isServletWeb() ? serverProperties.getServlet().getContextPath() : "";
//        if (null == urlPrefix)
//            urlPrefix = "";
//        return new EurekaLocalProperties(port, securePort, urlPrefix);
//    }
//
//    @Bean
//    @ConfigurationProperties(prefix = org.zodiac.eureka.base.constants.EurekaBaseSystemPropertiesConstants.EUREKA_PREFIX)
//    protected EurekaProperties eurekaProperties() {
//        EurekaProperties eurekaProperties =
//            new EurekaProperties(applicationInfoProperties, appDiscoveryProperties, eurekaLocalProperties);
//        return eurekaProperties;
//    }

    @Bean
    @ConditionalOnMissingBean(value = com.netflix.appinfo.EurekaInstanceConfig.class, search = SearchStrategy.CURRENT)
    protected com.netflix.appinfo.EurekaInstanceConfig eurekaInstanceConfig() {
        Properties eurekaConfigPtopeties = eurekaProperties.toConfigPtopeties();
        /*载入配置。*/
        com.netflix.config.ConfigurationManager.loadProperties(eurekaConfigPtopeties);
        org.zodiac.eureka.base.instance.AppEurekaInstanceConfig instanceConfig =
            Strings.notBlank(eurekaLocalProperties.getNamespace())
                ? new org.zodiac.eureka.base.instance.AppEurekaInstanceConfig(eurekaLocalProperties.getNamespace(), applicationInfoProperties,
                    eurekaProperties.getInstance())
                : new org.zodiac.eureka.base.instance.AppEurekaInstanceConfig(applicationInfoProperties, eurekaProperties.getInstance());

        Integer managementServerPort = environment.getProperty(SystemPropertiesConstants.Spring.MANAGEMENT_SERVER_PORT, Integer.class);
        Integer managementPort = Numbers.getOrDefault(managementServerPort, eurekaProperties.getErurekaPort());
        if (null != managementPort)
            instanceConfig.addMetadata(SystemPropertiesConstants.Spring.MANAGEMENT_PORT, String.valueOf(managementPort));
        Integer jmxPort = environment.getProperty(SystemPropertiesConstants.JavaLang.COM_SUN_MANAGEMENT_JMXREMOTE_PORT,
                Integer.class);
        if (null != jmxPort)
            instanceConfig.addMetadata(SystemPropertiesConstants.Spring.JMX_PORT, String.valueOf(jmxPort), false);
        return instanceConfig;
    }

    @Bean
    @ConditionalOnMissingBean(value = com.netflix.discovery.EurekaClientConfig.class, search = SearchStrategy.CURRENT)
    protected com.netflix.discovery.EurekaClientConfig eurekaClientConfig(ConfigurableEnvironment env) {
        return new com.netflix.discovery.DefaultEurekaClientConfig();
    }

    @Bean
    @ConditionalOnMissingBean(value = com.netflix.appinfo.InstanceInfo.class, search = SearchStrategy.CURRENT)
    protected com.netflix.appinfo.InstanceInfo
        instanceInfo(com.netflix.appinfo.EurekaInstanceConfig eurekaInstanceConfig,
            com.netflix.discovery.EurekaClientConfig eurekaClientConfig) {
        com.netflix.appinfo.InstanceInfo instanceInfo =
            new com.netflix.appinfo.providers.EurekaConfigBasedInstanceInfoProvider(eurekaInstanceConfig).get();
        Map metadata = instanceInfo.getMetadata();
        metadata.put(ApplicationInfoMetadata.SERVICE_ID, applicationInfoProperties.getName());
        metadata.put(ApplicationInfoMetadata.WEIGHT, String.valueOf(applicationInfoProperties.getWeight()));
        metadata.put(ApplicationInfoMetadata.MODULE, applicationInfoProperties.getModule());
        metadata.put(ApplicationInfoMetadata.CLUSTER, applicationInfoProperties.getCluster());
        metadata.put(ApplicationInfoMetadata.ZONE, applicationInfoProperties.getZone());
        metadata.put(ApplicationInfoMetadata.REGION, applicationInfoProperties.getRegion());
        metadata.put(ApplicationInfoMetadata.ENV_TYPE, applicationInfoProperties.getEnvType());
        metadata.put(ApplicationInfoMetadata.INSTANCE_ID, instanceInfo.getInstanceId());
        metadata.put(ApplicationInfoMetadata.VERSION, applicationInfoProperties.getVersion());
        metadata.put(ApplicationInfoMetadata.HOST, eurekaInstanceConfig.getHostName(false));
        metadata.put(ApplicationInfoMetadata.IP_ADDRESS, eurekaInstanceConfig.getIpAddress());
        return instanceInfo;
    }

    @Bean
    @ConditionalOnMissingBean(value = com.netflix.appinfo.ApplicationInfoManager.class, search = SearchStrategy.CURRENT)
    protected com.netflix.appinfo.ApplicationInfoManager applicationInfoManager(
        com.netflix.appinfo.InstanceInfo instanceInfo, com.netflix.appinfo.EurekaInstanceConfig eurekaInstanceConfig) {
        com.netflix.appinfo.ApplicationInfoManager applicationInfoManager =
            new com.netflix.appinfo.ApplicationInfoManager(eurekaInstanceConfig, instanceInfo);
        return applicationInfoManager;
    }

    @Lazy
    @Bean(destroyMethod = "shutdown")
    @ConditionalOnMissingBean(value = com.netflix.discovery.EurekaClient.class, search = SearchStrategy.CURRENT)
    protected com.netflix.discovery.EurekaClient eurekaClient(
        com.netflix.appinfo.ApplicationInfoManager applicationInfoManager,
        com.netflix.discovery.EurekaClientConfig eurekaClientConfig) {
        com.netflix.appinfo.ApplicationInfoManager appManager = AopUtils.isAopProxy(applicationInfoManager)
            ? Springs.getTargetObject(applicationInfoManager) : applicationInfoManager;
        com.netflix.discovery.EurekaClient eurekaClient =
            new org.zodiac.eureka.client.AppEurekaClient(appManager, eurekaClientConfig, this.applicationContext);
        return eurekaClient;
    }

    @Bean(name = DiscoveryConstants.DISCOVERY_CLIENT_BEAN_NAME)
    protected AppDiscoveryClient discoveryClient(com.netflix.discovery.EurekaClient eurekaClient,
        com.netflix.discovery.EurekaClientConfig clientConfig,
        com.netflix.appinfo.EurekaInstanceConfig instanceConfig) {
        return new org.zodiac.eureka.client.EurekaAppDiscoveryClient(applicationInfoProperties, clientConfig, instanceConfig, eurekaClient);
    }

    @Bean
    protected org.zodiac.eureka.base.instance.listener.EurekaMetadataChangedListener
        eurekaMetadataChangedListener(com.netflix.appinfo.ApplicationInfoManager applicationInfoManager) {
        return new org.zodiac.eureka.base.instance.listener.EurekaMetadataChangedListener(applicationInfoManager);
    }

    @Bean(name = DiscoveryConstants.DISCOVERY_APP_INSTANCE_BEAN_NAME)
    protected org.zodiac.eureka.base.instance.EurekaAppInstance discoveryAppInstance(com.netflix.appinfo.InstanceInfo instanceInfo,
        com.netflix.discovery.EurekaClientConfig eurekaClientConfig, com.netflix.appinfo.EurekaInstanceConfig eurekaInstanceConfig) {
        return new org.zodiac.eureka.base.instance.EurekaAppInstance(applicationInfoProperties, instanceInfo, eurekaClientConfig, eurekaInstanceConfig);
    }

    @Bean
    protected org.zodiac.eureka.base.instance.EurekaAppContextLoader eurekaAppContextLoader(org.zodiac.eureka.base.instance.EurekaAppInstance eurekaAppInstance) {
        return new org.zodiac.eureka.base.instance.EurekaAppContextLoader(eurekaAppInstance);
    }

    @Bean
    @ConditionalOnBean(value = {AutoAppRegistrationProperties.class})
    @ConditionalOnProperty(value = SystemPropertiesConstants.Zodiac.SPRING_BOOTSTRAP_REGISTRY_AUTO_REGIS_ENABLED, matchIfMissing = true)
    protected org.zodiac.eureka.client.EurekaAppRegistration eurekaAppRegistration(org.zodiac.eureka.base.instance.AppEurekaInstanceConfig eurekaInstanceConfig,
        com.netflix.discovery.EurekaClientConfig eurekaClientConfig,
        com.netflix.discovery.EurekaClient eurekaClient,
        com.netflix.appinfo.ApplicationInfoManager applicationInfoManager,
        ObjectProvider healthCheckHandler,
        AppInstance appInstance) {
        return new org.zodiac.eureka.client.EurekaAppRegistration(eurekaInstanceConfig, eurekaClientConfig, eurekaClient, applicationInfoManager, healthCheckHandler.getIfAvailable(), appInstance);
    }

    @Bean
    protected org.zodiac.eureka.client.EurekaAppServiceRegistry eurekaServiceRegistry() {
        return new org.zodiac.eureka.client.EurekaAppServiceRegistry();
    }

    @Bean
    @ConditionalOnBean(AutoAppRegistrationProperties.class)
    @ConditionalOnProperty(value = SystemPropertiesConstants.Zodiac.SPRING_BOOTSTRAP_REGISTRY_AUTO_REGIS_ENABLED, matchIfMissing = true)
    protected org.zodiac.eureka.client.EurekaAutoAppRegistration eurekaAutoAppRegistration(
            ApplicationContext context, org.zodiac.eureka.client.EurekaAppServiceRegistry registry,
            org.zodiac.eureka.client.EurekaAppRegistration registration) {
        return new org.zodiac.eureka.client.EurekaAutoAppRegistration(context, registry, registration);
    }

    @Bean
    @ConditionalOnMissingBean
    protected org.zodiac.eureka.client.EurekaClientService eurekaClientService(com.netflix.appinfo.ApplicationInfoManager applicationInfoManager,
        org.zodiac.eureka.client.AppEurekaClient eurekaClient) {
        return new org.zodiac.eureka.client.DefaultEurekaClientService(applicationInfoManager, eurekaClient, eurekaClient.registrationEurekaHttpClient());
    }

    @Bean
    @ConditionalOnMissingBean
    protected org.zodiac.eureka.client.shutdown.EurekaClientShutdown eurekaClientShutdown(AppShutdownConfigProperties appShutdownConfigProperties,
        org.zodiac.eureka.client.EurekaClientService eurekaClientService) {
        return new org.zodiac.eureka.client.shutdown.EurekaClientShutdown(appShutdownConfigProperties, eurekaClientService);
    }

    @Bean
    @ConditionalOnMissingBean
    protected org.zodiac.eureka.base.instance.listener.EurekaMetadataChangedListener appClusterMetadataListener(com.netflix.appinfo.ApplicationInfoManager applicationInfoManager) {
        return new org.zodiac.eureka.base.instance.listener.EurekaMetadataChangedListener(applicationInfoManager);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy