SpringBoot/SpringCloud初探二(反向代理)
反向代理采用Nginx,部署容器采用tomcat。
一、Nginx配置
upstream testserver{
server 127.0.0.1:8790 weight=1;
server 127.0.0.1:8791 weight=1;
}
server {
listen 8088;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://testserver;
root html;
index index.html index.htm;
}
...
}
二、Tomcat配置
(一)webapps/
(二)配置脚本
serverl.xml
...
<Connector port="8790" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
...
三、Tomcat部署的代码适配
(一)启动类
@SpringBootApplication
public class GatewayWebapp extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder){
return builder.sources(GatewayWebapp.class);
}
public static void main(String[] args) {
SpringApplication.run(GatewayWebapp.class, args);
}
}
(二)POM依赖适配
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
...
<packaging>war</packaging>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
...
</project>
上一篇:
IDEA上Java项目控制台中文乱码
