org.zodiac.autoconfigure.server.http.NettyWebServletServerAutoConfiguration Maven / Gradle / Ivy
package org.zodiac.autoconfigure.server.http;
//import java.util.Collection;
//import java.util.function.Supplier;
//
//import org.springframework.beans.factory.BeanFactory;
//import org.springframework.boot.SpringBootConfiguration;
//import org.springframework.boot.autoconfigure.AutoConfigureAfter;
//import org.springframework.boot.autoconfigure.AutoConfigureOrder;
//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.ConditionalOnWebApplication;
//import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
//import org.springframework.boot.autoconfigure.condition.SearchStrategy;
//import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Import;
//import org.springframework.core.Ordered;
//import org.zodiac.autoconfigure.netty.condition.ConditionalOnNettyServerEnabled;
//import org.zodiac.commons.constants.SystemPropertiesConstants;
//
///**
// * Servlet 容器工厂配置类:配置内置Netty容器Bean。 最早是直接将NettyTcpSpringServletServerFactory加@Component注解,这样集成在任何环境中都会加载,可能引起端口冲突。
// * 所以通过这个配置类,配置在当前上下文缺少 EmbeddedServletContainerFactory接口实现类时(即缺少内置Servlet容器), 加载EmbeddedNettyFactory
// * 这样SpringBoot项目在引入这个maven依赖,并且排除了内置tomcat依赖、且没引入其他servlet容器(如jetty)时, 就可以通过工厂类加载并启动netty容器了。
// */
//@SpringBootConfiguration
//@ConditionalOnNettyServerEnabled
//@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
//// @ConditionalOnWebApplication(type = Type.SERVLET)
////@ConditionalOnProperty(prefix = SystemPropertiesConstants.Zodiac.SERVER_NETTY_PREFIX, value = "enabled", havingValue = "true")
//@AutoConfigureAfter(value = {org.zodiac.autoconfigure.netty.NettyServletServerAutoConfiguration.class})
//@Import(value = {NettyWebServletServerAutoConfiguration.SimpleServletWebServerFactoryConfigutation.class, NettyWebServletServerAutoConfiguration.RemoteServletWebServerFactoryConfigutation.class})
//@ConditionalOnClass(value = {javax.servlet.Servlet.class, org.zodiac.server.http.servlet.simple.SimpleWebServer.class, org.zodiac.netty.base.api.ProtocolHandler.class, org.zodiac.netty.springboot.server.servlet.NettyTcpSpringServletServerFactory.class})
//public class NettyWebServletServerAutoConfiguration {
//
//// @Bean
//// @ConditionalOnMissingBean
//// @ConfigurationProperties(prefix = SystemPropertiesConstants.Zodiac.SERVER_NETTY_PREFIX, ignoreInvalidFields = true)
//// protected NettyServerProperties nettyServerProperties(ObjectProvider serverPropertiesProvider,
//// ObjectProvider multipartPropertiesProvider) {
//// final NettyServerProperties properties = new NettyServerProperties();
//// Optional.ofNullable(serverPropertiesProvider.getIfAvailable()).ifPresent(p -> {
//// if (null != p.getPort())
//// properties.getListen().setServerPort(p.getPort());
//// boolean secure = null != p.getSsl() && p.getSsl().isEnabled();
//// properties.getHandler().setEnableSsl(secure);
//// properties.getTls().setEnabled(secure);
//// properties.getHttp().getHttp20().setEnabled(null != p.getHttp2() && p.getHttp2().isEnabled());
//// });
//// Optional.ofNullable(multipartPropertiesProvider.getIfAvailable()).ifPresent(p -> {
//// properties.getHttp().getMultipart().setEnabled(p.getEnabled())
//// .setFileSizeThreshold(p.getFileSizeThreshold()).setLocation(p.getLocation())
//// .setMaxFileSize(p.getMaxFileSize()).setMaxRequestSize(p.getMaxFileSize())
//// .setResolveLazily(p.isResolveLazily());
//// });
//// return properties;
//// }
//
//// @Bean
//// @ConfigurationProperties(prefix = SystemPropertiesConstants.Zodiac.SERVER_SIMPLE_PREFIX, ignoreInvalidFields = true)
//// protected SimpleHttpServerProperties simpleHttpServerProperties(ObjectProvider multipartPropertiesProvider) {
//// SimpleHttpServerProperties properties = new SimpleHttpServerProperties();
//// Optional.ofNullable(multipartPropertiesProvider.getIfAvailable()).ifPresent(p -> {
//// properties.getMultipart().setEnabled(p.getEnabled())
//// .setFileSizeThreshold(p.getFileSizeThreshold()).setLocation(p.getLocation())
//// .setMaxFileSize(p.getMaxFileSize()).setMaxRequestSize(p.getMaxFileSize())
//// .setResolveLazily(p.isResolveLazily());
//// });
//// return properties;
//// }
//
// /**
// * Netty的Bootstrap类必须在classloader中存在,才能启动Netty容器。
// *
// */
// @SpringBootConfiguration
//// @ConditionalOnPlatformTenantEnabled
// @ConditionalOnWebApplication(type = Type.SERVLET)
// @ConditionalOnMissingBean(value = ServletWebServerFactory.class, search = SearchStrategy.CURRENT)
// @ConditionalOnProperty(name = SystemPropertiesConstants.Zodiac.SERVER_SIMPLE_ENABLED, havingValue = "true")
// @ConditionalOnClass(value = {io.netty.bootstrap.Bootstrap.class, javax.servlet.Servlet.class, org.zodiac.server.http.servlet.simple.SimpleWebServer.class,
// org.zodiac.netty.base.api.ProtocolHandler.class, org.zodiac.netty.springboot.server.servlet.NettyTcpSpringServletServerFactory.class})
// protected static class SimpleServletWebServerFactoryConfigutation {
//
// private org.zodiac.netty.springboot.NettyServerProperties nettyServerProperties;
// private SimpleHttpServerProperties simpleHttpServerProperties;
//
// public SimpleServletWebServerFactoryConfigutation(org.zodiac.netty.springboot.NettyServerProperties nettyServerProperties,
// SimpleHttpServerProperties simpleHttpServerProperties) {
// super();
// this.nettyServerProperties = nettyServerProperties;
// this.simpleHttpServerProperties = simpleHttpServerProperties;
// }
//
// /*上述条件注解成立的场景,使用此NettyWebServer容器工厂*/
// @Bean
// @ConditionalOnMissingBean(value = {ServletWebServerFactory.class})
// protected org.zodiac.server.http.servlet.simple.spring.SimpleServletWebSpringServerFactory simpleServletWebSpringServerFactory() {
// return new org.zodiac.server.http.servlet.simple.spring.SimpleServletWebSpringServerFactory(nettyServerProperties, simpleHttpServerProperties);
// }
// }
//
// /**
// * Netty的Bootstrap类必须在classloader中存在,才能启动Netty容器。
// *
// */
// @SpringBootConfiguration
//// @ConditionalOnHttpCacheEnabled
// @ConditionalOnWebApplication(type = Type.SERVLET)
// @ConditionalOnMissingBean(value = ServletWebServerFactory.class, search = SearchStrategy.CURRENT)
// @ConditionalOnClass(value = {io.netty.bootstrap.Bootstrap.class, org.zodiac.netty.base.api.ProtocolHandler.class, org.zodiac.netty.springboot.server.servlet.NettyTcpSpringServletServerFactory.class})
// protected static class RemoteServletWebServerFactoryConfigutation {
//
// private org.zodiac.netty.springboot.NettyServerProperties nettyServerProperties;
//
// public RemoteServletWebServerFactoryConfigutation(org.zodiac.netty.springboot.NettyServerProperties nettyServerProperties) {
// this.nettyServerProperties = nettyServerProperties;
// }
//
// /**
// * Add a TCP service factory
// *
// * @param protocolHandlers protocolHandlers
// * @param serverListeners serverListeners
// * @param beanFactory beanFactory
// * @return NettyTcpServerFactory
// */
// @Bean(name = org.zodiac.netty.base.constants.NettyConstants.NETTY_TCP_SERVER_FACTORY_BEAN_NAME)
// //@ConditionalOnMissingBean
// protected org.zodiac.netty.springboot.server.servlet.NettyTcpSpringServletServerFactory nettyTcpSpringServletServerFactory(Collection protocolHandlers,
// Collection serverListeners, BeanFactory beanFactory) {
// Supplier handlerSupplier = () -> {
// Class extends org.zodiac.netty.base.protocol.DynamicProtocolChannelHandler> type = nettyServerProperties.getChannelHandler();
// return type == org.zodiac.netty.base.protocol.DynamicProtocolChannelHandler.class ? new org.zodiac.netty.base.protocol.DynamicProtocolChannelHandler()
// : beanFactory.getBean(type);
// };
// org.zodiac.netty.springboot.server.servlet.NettyTcpSpringServletServerFactory tcpServerFactory =
// new org.zodiac.netty.springboot.server.servlet.NettyTcpSpringServletServerFactory(nettyServerProperties, handlerSupplier);
// tcpServerFactory.getProtocolHandlers().addAll(protocolHandlers);
// tcpServerFactory.getServerListeners().addAll(serverListeners);
// return tcpServerFactory;
// }
// }
//}