Nginx如何部署SpringBoot项目

其他教程   发布日期:2023年08月03日   浏览次数:395

本篇内容介绍了“Nginx如何部署SpringBoot项目”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

1、新建一个yml文件 application.yml

  1. # 端口号
  2. server:
  3. port: 2001

2、编写一个Controler测试类

  1. package com.example.demo1.controller;
  2. import org.apache.logging.log4j.LogManager;
  3. import org.apache.logging.log4j.Logger;
  4. import org.springframework.beans.factory.annotation.Value;
  5. import org.springframework.stereotype.Component;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.RequestMethod;
  8. import org.springframework.web.bind.annotation.RestController;
  9. @RestController
  10. @Component
  11. @RequestMapping("/v1")
  12. public class HelloController {
  13. final static Logger log = LogManager.getLogger(HelloController.class);
  14. @Value("${server.port}")
  15. private int port ;
  16. @RequestMapping(value = "", method = RequestMethod.GET)
  17. public String test() {
  18. return "invoke url /,port="+port;
  19. }
  20. @RequestMapping(value = "/test1", method = RequestMethod.GET)
  21. public String test1() {
  22. return "invoke url /test1,port="+port;
  23. }
  24. @RequestMapping(value = "/test2", method = RequestMethod.GET)
  25. public String test2() {
  26. return "invoke url /test2,port="+port;
  27. }
  28. }

3、编写一个启动类

  1. package com.example.demo1;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. public class Demo1Application {
  6. public static void main(String[] args) {
  7. SpringApplication.run(Demo1Application.class, args);
  8. }
  9. }

4、我用到的pom文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.7.6</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.example</groupId>
  12. <artifactId>demo1</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>demo1</name>
  15. <description>Demo project for Spring Boot</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. <log4j.version>2.19.0</log4j.version>
  19. </properties>
  20. <dependencies>
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-starter</artifactId>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-starter-web</artifactId>
  28. <!-- <exclusions>-->
  29. <!-- <exclusion>-->
  30. <!-- <groupId>ch.qos.logback</groupId>-->
  31. <!-- <artifactId>logback-classic</artifactId>-->
  32. <!-- </exclusion>-->
  33. <!-- </exclusions>-->
  34. <exclusions>
  35. <exclusion>
  36. <groupId>org.springframework.boot</groupId>
  37. <artifactId>spring-boot-starter-logging</artifactId>
  38. </exclusion>
  39. </exclusions>
  40. </dependency>
  41. <dependency>
  42. <groupId>org.springframework.boot</groupId>
  43. <artifactId>spring-boot-starter-test</artifactId>
  44. <!-- <scope>test</scope> -->
  45. </dependency>
  46. <!--日志框架-->
  47. <dependency>
  48. <groupId>org.apache.logging.log4j</groupId>
  49. <artifactId>log4j-api</artifactId>
  50. <version>${log4j.version}</version>
  51. </dependency>
  52. <dependency>
  53. <groupId>org.apache.logging.log4j</groupId>
  54. <artifactId>log4j-core</artifactId>
  55. <version>${log4j.version}</version>
  56. </dependency>
  57. <!--日志框架-->
  58. </dependencies>
  59. <build>
  60. <plugins>
  61. <plugin>
  62. <groupId>org.apache.maven.plugins</groupId>
  63. <artifactId>maven-compiler-plugin</artifactId>
  64. <version>3.7.0</version>
  65. <configuration>
  66. <source>1.8</source>
  67. <target>1.8</target>
  68. <encoding>UTF-8</encoding>
  69. </configuration>
  70. </plugin>
  71. <plugin>
  72. <groupId>org.apache.maven.plugins</groupId>
  73. <artifactId>maven-assembly-plugin</artifactId>
  74. <version>2.5.5</version>
  75. <configuration>
  76. <archive>
  77. <manifest>
  78. <mainClass>com.example.demo1.Demo1Application</mainClass>
  79. </manifest>
  80. </archive>
  81. <descriptorRefs>
  82. <descriptorRef>jar-with-dependencies</descriptorRef>
  83. </descriptorRefs>
  84. </configuration>
  85. <executions>
  86. <execution>
  87. <id>make-assembly</id>
  88. <phase>package</phase>
  89. <goals>
  90. <goal>single</goal>
  91. </goals>
  92. </execution>
  93. </executions>
  94. </plugin>
  95. </plugins>
  96. </build>
  97. </project>

5、先在本地测试,启动项目,看到这个就说明启动成功了

. ____ _ __ _ _
/ / ___'_ __ _ _(_)_ __ __ _
( ( )\___ | '_ | '_| | '_ / _` |
/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/

6、测试,在浏览器中依次输入

http://127.0.0.1:3001/v1
http://127.0.0.1:3001/v1/test1
http://127.0.0.1:3001/v1/test2

在浏览器中能看到端口号的打印信息就说明成功了

7、maven编译打成jar包

8、修改nginx.conf文件

  1. worker_processes 1;
  2. events {
  3. worker_connections 1024;
  4. }
  5. http {
  6. include mime.types;
  7. default_type application/octet-stream;
  8. sendfile on;
  9. keepalive_timeout 65;
  10. server {
  11. listen 89;
  12. server_name nginx_server;
  13. location / {
  14. proxy_pass http://server_ip:3001/v1;
  15. }
  16. location /edu {
  17. proxy_pass http://server_ip:3001/v1/test1;
  18. }
  19. location /ymd {
  20. proxy_pass http://server_ip:3002/v1/test2;
  21. }
  22. }
  23. }

nginx_server:nginx所在的服务器的地址

server_ip:反向代理的服务器的地址

这里我都是10.161.20.10

7、测试,根据访问的路径跳转到不同的服务中

浏览器中输入:

http://10.161.20.10:90/

  1. invoke url /,port=3001

http://10.161.20.10:90/test1

  1. invoke url /test1,port=3001

http://10.161.20.10:90/test2

  1. invoke url /test2,port=3002

以上就是Nginx如何部署SpringBoot项目的详细内容,更多关于Nginx如何部署SpringBoot项目的资料请关注九品源码其它相关文章!