linux服务器发布springboot项目(无域名,只有服务器部署)

新建springboot项目

一个简单的hello项目
在这里插入图片描述

pom依赖

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
        <version>2.3.1.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

application配置文件

# 应用服务 WEB 访问端口
server.port=9000
# thymeleaf配置
spring.thymeleaf.cache= false
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.servlet.content-type=text/html

index.html页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>hello</title>
</head>
<body>
    <h1>hello 琛琛</h1>
</body>
</html>

测试页面接口DemoController

package com.example.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author lzh
 * @version 1.0
 * @description: TODO
 * @date 2021/1/24 15:44
 */

@Controller
@RequestMapping("/lzh")
public class DemoController {
    
    
    @GetMapping("/index")
    public String index() {
    
    
        return "index";
    }
}

打包springboot项目

在这里插入图片描述

上传到服务器

将刚刚打包的jar包放到相应的目录

在这里插入图片描述

阿里云开放端口9000(我上面自己写的9000)

在这里插入图片描述

跳转到刚刚放入jar包的目录中,命令行依次输入如下命令

firewall-cmd --zone=public --add-port=9000/tcp --permanent # 开端口
systemctl restart firewalld.service # 重启服务器
firewall-cmd --list-ports # 查看该端口是否开放
nohup java -jar demo-0.0.1-SNAPSHOT.jar >/dev/null 2>&1 & # 发布项目(名字为刚刚jar包的名字)

地址栏输入【ip地址+端口号+接口】查看

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_44635198/article/details/113127947