| package com.sinata.zuul; | 
|   | 
| import com.sinata.zuul.util.applets.NettyServer0; | 
| import com.sinata.zuul.util.echo.NettyServer; | 
| import com.spring4all.swagger.EnableSwagger2Doc; | 
| import org.apache.http.client.HttpClient; | 
| import org.apache.http.config.SocketConfig; | 
| import org.apache.http.impl.client.HttpClientBuilder; | 
| import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; | 
| import org.springframework.boot.SpringApplication; | 
| import org.springframework.boot.autoconfigure.SpringBootApplication; | 
| import org.springframework.boot.builder.SpringApplicationBuilder; | 
| import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; | 
| import org.springframework.cloud.client.discovery.EnableDiscoveryClient; | 
| import org.springframework.cloud.client.loadbalancer.LoadBalanced; | 
| import org.springframework.cloud.netflix.zuul.EnableZuulProxy; | 
| import org.springframework.context.annotation.Bean; | 
| import org.springframework.context.annotation.Primary; | 
| import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; | 
| import org.springframework.http.converter.StringHttpMessageConverter; | 
| import org.springframework.stereotype.Component; | 
| import org.springframework.web.client.RestTemplate; | 
| import org.springframework.web.cors.CorsConfiguration; | 
| import org.springframework.web.cors.UrlBasedCorsConfigurationSource; | 
| import org.springframework.web.filter.CorsFilter; | 
| import springfox.documentation.swagger.web.SwaggerResource; | 
| import springfox.documentation.swagger.web.SwaggerResourcesProvider; | 
|   | 
| import java.nio.charset.StandardCharsets; | 
| import java.util.ArrayList; | 
| import java.util.List; | 
|   | 
|   | 
| @EnableSwagger2Doc | 
| @EnableZuulProxy//开启网关服务 | 
| @EnableDiscoveryClient//开启eureka客户端的消费者 | 
| @SpringBootApplication | 
| public class ZuulApplication extends SpringBootServletInitializer { | 
|   | 
|     public static void main(String[] args) { | 
|         SpringApplication.run(ZuulApplication.class, args); | 
| //        NettyServer nettyServer = new NettyServer(); | 
| //        nettyServer.bind(); | 
| //        NettyServer0 nettyServer0 = new NettyServer0(); | 
| //        nettyServer0.bind(); | 
|     } | 
|   | 
|   | 
|     @Bean //SpringCloud内部服务质检使用服务名调用 | 
|     @LoadBalanced | 
|     public RestTemplate internalRestTemplate() { | 
|         PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(); | 
|         connectionManager.setDefaultSocketConfig(SocketConfig.custom().setTcpNoDelay(true).build()); | 
|         connectionManager.setDefaultMaxPerRoute(100);//最大并发连接 | 
|         connectionManager.setMaxTotal(200); // 总的最大连接数 | 
|         HttpClient httpClient = HttpClientBuilder.create().setConnectionManager(connectionManager).build(); | 
|         HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory(httpClient); | 
|         httpRequestFactory.setConnectionRequestTimeout(30 * 1000); | 
|         httpRequestFactory.setConnectTimeout(30 * 3000); | 
|         httpRequestFactory.setReadTimeout(30 * 3000); | 
|         RestTemplate restTemplate = new RestTemplate(httpRequestFactory); | 
|         restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8)); | 
|         return restTemplate; | 
|     } | 
|   | 
|   | 
|   | 
| //    /** | 
| //     * 向Spring容器中定义RestTemplate对象 | 
| //     * @return | 
| //     */ | 
| //    @Bean //必须new 一个RestTemplate并放入spring容器当中,否则启动时报错 | 
| //    public RestTemplate restTemplate() { | 
| //        PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(); | 
| //        connectionManager.setDefaultSocketConfig(SocketConfig.custom().setTcpNoDelay(true).build()); | 
| //        connectionManager.setDefaultMaxPerRoute(100);//最大并发连接 | 
| //        connectionManager.setMaxTotal(200); // 总的最大连接数 | 
| //        HttpClient httpClient = HttpClientBuilder.create().setConnectionManager(connectionManager).build(); | 
| //        HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory(httpClient); | 
| //        httpRequestFactory.setConnectionRequestTimeout(30 * 1000); | 
| //        httpRequestFactory.setConnectTimeout(30 * 3000); | 
| //        httpRequestFactory.setReadTimeout(30 * 3000); | 
| //        RestTemplate restTemplate = new RestTemplate(httpRequestFactory); | 
| //        restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8)); | 
| //        return restTemplate; | 
| //    } | 
|   | 
|   | 
|   | 
|     /** | 
|      * 配置Swagger | 
|      */ | 
|     @Component | 
|     @Primary | 
|     class DocumentationConfig implements SwaggerResourcesProvider { | 
|         @Override | 
|         public List<SwaggerResource> get() { | 
|             List resource=new ArrayList<>(); | 
|             //name可以随便写,location前缀要与zuul配置的path一致。zuul开了token验证,要加上token,否则不用加?token=1 | 
|             resource.add(swaggerResource("user","/user-server/v2/api-docs","1.0")); | 
|             resource.add(swaggerResource("driver","/driver-server/v2/api-docs","1.0")); | 
|             return resource; | 
|         } | 
|   | 
|         //name可以随便写,location前缀要与zuul配置的path一致 | 
|         private SwaggerResource swaggerResource(String name, String location, String version){ | 
|             SwaggerResource swaggerResource=new SwaggerResource(); | 
|             swaggerResource.setName(name); | 
|             swaggerResource.setLocation(location); | 
|             swaggerResource.setSwaggerVersion(version); | 
|             return swaggerResource; | 
|         } | 
|     } | 
|   | 
|   | 
|     @Override | 
|     protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { | 
|         return builder.sources(ZuulApplication.class); | 
|     } | 
| } |