温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Nginx如何部署SpringBoot项目

发布时间:2023-03-07 17:31:22 来源:亿速云 阅读:124 作者:iii 栏目:开发技术

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

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

# 端口号
server:
  port: 2001

2、编写一个Controler测试类

package com.example.demo1.controller;


import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Component
@RequestMapping("/v1")
public class HelloController {
    final static Logger log = LogManager.getLogger(HelloController.class);
    @Value("${server.port}")
    private int port ;

    @RequestMapping(value = "", method = RequestMethod.GET)
    public String test() {
        return "invoke url /,port="+port;
    }

    @RequestMapping(value = "/test1", method = RequestMethod.GET)
    public String test1() {
        return "invoke url /test1,port="+port;
    }

    @RequestMapping(value = "/test2", method = RequestMethod.GET)
    public String test2() {
        return "invoke url /test2,port="+port;
    }
}

3、编写一个启动类

package com.example.demo1;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Demo1Application {

    public static void main(String[] args) {
        SpringApplication.run(Demo1Application.class, args);
    }
}

4、我用到的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">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo1</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <log4j.version>2.19.0</log4j.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <!--            <exclusions>-->
            <!--                <exclusion>-->
            <!--                    <groupId>ch.qos.logback</groupId>-->
            <!--                    <artifactId>logback-classic</artifactId>-->
            <!--                </exclusion>-->

            <!--            </exclusions>-->
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <!-- <scope>test</scope> -->
        </dependency>
        <!--日志框架-->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <!--日志框架-->
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>

                <groupId>org.apache.maven.plugins</groupId>

                <artifactId>maven-assembly-plugin</artifactId>

                <version>2.5.5</version>

                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.example.demo1.Demo1Application</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</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文件

worker_processes  1;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    
    keepalive_timeout  65;

    server {
        listen       89;
        server_name  nginx_server;

        location / {
            proxy_pass http://server_ip:3001/v1;
        }
        location /edu {
            proxy_pass http://server_ip:3001/v1/test1;
        }
        location /ymd {
            proxy_pass http://server_ip:3002/v1/test2;
        }
    }

}

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

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

这里我都是10.161.20.10

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

浏览器中输入:

http://10.161.20.10:90/

invoke url /,port=3001

http://10.161.20.10:90/test1

invoke url /test1,port=3001

http://10.161.20.10:90/test2

invoke url /test2,port=3002

“Nginx如何部署SpringBoot项目”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI