欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

基于springboot部署jar包通过nginx负载均衡

程序员文章站 2022-07-03 11:10:47
...

概括

1、请求示意图;
2、springboot打包;
3、springboot部署发布;
4、配置nginx的nginx.conf;
5、防火墙开放端口;
6、测试运行;

1、请求示意图

基于springboot部署jar包通过nginx负载均衡

2、springboot打包

基于springboot部署jar包通过nginx负载均衡
基于springboot部署jar包通过nginx负载均衡

3、springboot部署发布

controller

@Slf4j
@RestController
@RequestMapping("/index")
public class IndexController {

    @Value("${server.port}")
    private String port;

    @GetMapping("/getMsg/{name}")
    public Object getMsg(@PathVariable("name") String name) {
        log.info(" getMsg ======> param: name->{}", name);
        log.info(" getMsg ======> param: port->{}", port);
        return name +"  :  "+ port + "  ok";
    }
}

本地访问
基于springboot部署jar包通过nginx负载均衡
部署发布

[aaa@qq.com application]# pwd
/usr/local/application
[aaa@qq.com application]# ls
demo1.jar  demo2.jar 
[aaa@qq.com application]# nohup java -jar demo1.jar --server.port=9090 >> demo1.log &
[aaa@qq.com application]# nohup java -jar demo2.jar --server.port=9091 >> demo2.log &
[aaa@qq.com sbin]# ps -ef|grep java
root       1856      1  0 10:44 tty1     00:00:06 /opt/jdk8/jdk1.8.0_161/bin/java -Djava.util.logging.config.file=/opt/tomcat8/apache-tomcat-8.5.53/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dorg.apache.catalina.security.SecurityListener.UMASK=0027 -Dignore.endorsed.dirs= -classpath /opt/tomcat8/apache-tomcat-8.5.53/bin/bootstrap.jar:/opt/tomcat8/apache-tomcat-8.5.53/bin/tomcat-juli.jar -Dcatalina.base=/opt/tomcat8/apache-tomcat-8.5.53 -Dcatalina.home=/opt/tomcat8/apache-tomcat-8.5.53 -Djava.io.tmpdir=/opt/tomcat8/apache-
tomcat-8.5.53/temp org.apache.catalina.startup.Bootstrap start
root       1967   1893  1 10:47 pts/0    00:00:16 java -jar demo1.jar --server.port=9090
root       2086   1893  1 10:55 pts/0    00:00:14 java -jar demo2.jar --server.port=9091
root       2879   1893  0 11:10 pts/0    00:00:00 grep --color=auto java
[aaa@qq.com application]# tail -f demo1.log 

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::       (v2.1.16.RELEASE)

2020-08-08 10:47:49.582  INFO 1967 --- [           main] com.example.demo.DemoApplication         : Starting DemoApplication v0.0.1-SNAPSHOT on sentos with PID 1967 (/usr/local/application/demo1.jar started by root in /usr/local/application)
2020-08-08 10:47:49.592  INFO 1967 --- [           main] com.example.demo.DemoApplication         : No active profile set, falling back to default profiles: default
2020-08-08 10:47:51.278  INFO 1967 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 9090 (http)
2020-08-08 10:47:51.328  INFO 1967 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-08-08 10:47:51.328  INFO 1967 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.37]
2020-08-08 10:47:51.450  INFO 1967 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-08-08 10:47:51.450  INFO 1967 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1758 ms
2020-08-08 10:47:51.755  INFO 1967 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-08-08 10:47:52.220  INFO 1967 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 9090 (http) with context path ''
2020-08-08 10:47:52.223  INFO 1967 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 3.598 seconds (JVM running for 4.355)
[aaa@qq.com application]# tail -f demo2.log
2020-08-08 10:55:03.963  INFO 2086 --- [           main] com.example.demo.DemoApplication         : Starting DemoApplication v0.0.1-SNAPSHOT on sentos with PID 2086 (/usr/local/application/demo2.jar started by root in /usr/local/application)
2020-08-08 10:55:03.966  INFO 2086 --- [           main] com.example.demo.DemoApplication         : No active profile set, falling back to default profiles: default
2020-08-08 10:55:05.386  INFO 2086 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 9091 (http)
2020-08-08 10:55:05.430  INFO 2086 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-08-08 10:55:05.430  INFO 2086 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.37]
2020-08-08 10:55:05.551  INFO 2086 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-08-08 10:55:05.551  INFO 2086 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1504 ms
2020-08-08 10:55:05.836  INFO 2086 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-08-08 10:55:06.149  INFO 2086 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 9091 (http) with context path ''
2020-08-08 10:55:06.153  INFO 2086 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 2.779 seconds (JVM running for 3.335)

4、配置nginx的nginx.conf

	keepalive_timeout  65;
    #gzip  on;
	
	# 负载均衡策略
    upstream servers{
		#每3个请求192.168.0.104:9090处理1个,192.168.0.104:9091处理2个
		server 192.168.0.104:9090 weight=1;
		server 192.168.0.104:9091 weight=2;
    }
	
    server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
			proxy_pass http://servers;
    }

启动nginx

[aaa@qq.com sbin]# ./nginx 
[aaa@qq.com sbin]# ps -ef |grep nginx 
root       2507      1  0 11:02 ?        00:00:00 nginx: master process ./nginx
nobody     2508   2507  0 11:02 ?        00:00:00 nginx: worker process
nobody     2509   2507  0 11:02 ?        00:00:00 nginx: worker process
root       2514   1893  0 11:02 pts/0    00:00:00 grep --color=auto nginx

5、防火墙开放端口

[aaa@qq.com application]# firewall-cmd --zone=public --add-port=9090/tcp --permanent
success
[aaa@qq.com application]# firewall-cmd --zone=public --add-port=9091/tcp --permanent
success
[aaa@qq.com application]# firewall-cmd --zone=public --add-port=80/tcp --permanent
success
[aaa@qq.com application]# firewall-cmd --reload
success
[aaa@qq.com application]# firewall-cmd --list-ports
3306/tcp 80/tcp 6379/tcp 9090/tcp 9091/tcp

6、测试运行

基于springboot部署jar包通过nginx负载均衡
基于springboot部署jar包通过nginx负载均衡

相关标签: springboot nginx