package com.crunii.micro.core;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure;
import com.crunii.micro.common.utils.JasyptEncryptorUtil;
import com.crunii.micro.common.utils.LogPathUtil;
import com.crunii.micro.config.ClientVersionRibbonConfiguration;
import org.apache.coyote.http11.Http11Nio2Protocol;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.boot.autoconfigure.data.redis.RedisReactiveAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.EnableMBeanExport;
import org.springframework.context.annotation.ImportResource;
import org.springframework.jmx.support.RegistrationPolicy;
import org.springframework.util.StringUtils;

/**
 * @author 田平 create 2023-02-14 11:21:23
 */
@EnableCaching
@EnableDiscoveryClient
@ImportResource({ "classpath:spring/*.xml" })
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
@SpringBootApplication(exclude = {
		RedisAutoConfiguration.class,
		RedisReactiveAutoConfiguration.class,
		TransactionAutoConfiguration.class,
		DataSourceAutoConfiguration.class,
		DruidDataSourceAutoConfigure.class })
@LoadBalancerClients(defaultConfiguration = ClientVersionRibbonConfiguration.class)
public class Application {

	public static void main(String[] args) {
		JasyptEncryptorUtil.setConfigClientProps();
		JasyptEncryptorUtil.setAdminClientProps();
		// 接netty重复设置的问题
		System.setProperty("es.set.netty.runtime.available.processors", "false");
		String env = System.getProperty("spring.profiles.active");
		if (!StringUtils.hasText(env)) {
			// local or dev or prod
			env = "local";
			System.setProperty("spring.profiles.active", env);
			// 设置nacos的日志目录
			System.setProperty("nacos.logging.path", LogPathUtil.getNacosLogPath());
		}
		SpringApplication.run(Application.class, args);
	}

	@Bean
	public TomcatServletWebServerFactory createTomcatServletWebServerFactory() {
		TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
		tomcat.setProtocol("org.apache.coyote.http11.Http11Nio2Protocol");
		tomcat.addConnectorCustomizers(connector -> {
			Http11Nio2Protocol protocol = (Http11Nio2Protocol) connector.getProtocolHandler();
			// 设置最大连接数
			protocol.setConnectionTimeout(60000);// 2分钟
			protocol.setMaxConnections(512);
			// 设置最大线程数
			protocol.setMaxThreads(200);
		});
		return tomcat;
	}

}
