puzhibing
2023-05-18 53562814add61acfdc02d6b25dae6324f6fd5f92
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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.catalina.connector.Connector;
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.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
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 springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;
 
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
 
//@EnableHystrix
@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;
    }
 
    /*@Bean
    ServletWebServerFactory servletWebServerFactory() {
        TomcatServletWebServerFactory serverFactory = new TomcatServletWebServerFactory();
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setScheme("http");
        connector.setPort(80);
        connector.setSecure(false);
        connector.setRedirectPort(8443);
        serverFactory.addAdditionalTomcatConnectors(connector);
        return serverFactory;
    }*/
 
 
 
//    /**
//     * 向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"));
//            resource.add(swaggerResource("dispatch","/dispatch-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);
    }
}