linux

一、springboot 项目

1、新建一个springboot项目,对外提供一个访问接口

@RestController
public class TestController {
    @GetMapping("")
    public String test(){
        return "考试任务服务";
    }
}

2、在pom.xml中添加docker

   <properties>
        <java.version>1.8java.version>
        
        <docker.image.prefix>springbootdocker.image.prefix>
    properties>
 
            <plugin>
                <groupId>com.spotifygroupId>
                <artifactId>docker-maven-pluginartifactId>
                <version>1.0.0version>
                <configuration>
                    <imageName>${docker.image.prefix}/${project.artifactId}imageName>
                    <dockerDirectory>src/main/dockerdockerDirectory>
                    <resources>
                        <resource>
                            <targetPath>/targetPath>
                            <directory>${project.build.directory}directory>
                            <include>${project.build.finalName}.jarinclude>
                        resource>
                    resources>
                configuration>
            plugin>

完整的 pom.xml


<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.0modelVersion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.6.2version>
        <relativePath/> 
    parent>
    <groupId>com.sinuxgroupId>
    <artifactId>examtaskserviceartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <packaging>jarpackaging>
    <name>examtaskservicename>
    <description>Demo project for Spring Bootdescription>
    <properties>
        <java.version>1.8java.version>
        
        <docker.image.prefix>springbootdocker.image.prefix>
    properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
 
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-tomcatartifactId>
            <scope>providedscope>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-maven-pluginartifactId>
            <version>2.4.0version>
        dependency>
    dependencies>
 
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
 
 
 
 
 
            
            <plugin>
                <groupId>com.spotifygroupId>
                <artifactId>docker-maven-pluginartifactId>
                <version>1.0.0version>
                <configuration>
                    <imageName>${docker.image.prefix}/${project.artifactId}imageName>
                    <dockerDirectory>src/main/dockerdockerDirectory>
                    <resources>
                        <resource>
                            <targetPath>/targetPath>
                            <directory>${project.build.directory}directory>
                            <include>${project.build.finalName}.jarinclude>
                        resource>
                    resources>
                configuration>
            plugin>
 
        plugins>
    build>
 
project>

3、和pom.xml同级下创建Dockerfile

img

Ddockerfile内容如下:

FROM java:8
VOLUME /tmp
ADD examtaskservice-0.0.1-SNAPSHOT.jar app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

4、在application.yml 添加端口号

server:
  port: 8880

5、将项目跑起来 ,看能不能访问。成功后将项目打包。

二、Linux下配置运行环境(maven 、 jdk 、docker)

一:linux 安装jdk

1、检查系统中是否已安装JDK

# 查看JDK安装版本
java -version

2、检测系统JDK默认安装包

# 命令的意思是搜索java,查看安装包文件
rpm -qa | grep java

3、卸载OpenJDK

yum remove *openjdk*

之后再次输入命令 查看卸载情况:rpm -qa | grep java  或者  java -version

4、安装JDK

a、将下载好的jdk-8u151-linux-x64.tar.gz直接拖拽到 usr/java文件夹中。没有可以先创建 cd usr mkdir java。

b、解压 jdk-8u151-linux-x64.tar.gz。tar -xzvf jdk-8u321-linux-x64.tar.gz

c、配置Java 环境变量:

进入到文件中 vim /etc/profile ,在最后面添加下面配置。

JAVA_HOME=/usr/java/jdk1.8.0_151
CLASSPATH=$JAVA_HOME/lib/
PATH=$PATH:$JAVA_HOME/bin
export PATH JAVA_HOME CLASSPATH

source /etc/profile

d、查看版本:java -version

二:linux 安装maven

1、wget下载。当然也可以向上面jdk的方式

# 下载
wget https://archive.apache.org/dist/maven/maven-3/3.5.4/binaries/apache-maven-3.5.4-bin.tar.gz
# 解压
tar -xzvf apache-maven-3.5.4-bin.tar.gz
# 移动到/usr/local/maven3目录 
mv apache-maven-3.5.4 /usr/local/maven3

2、配置环境变量

进入到文件中 vim /etc/profile ,在最后面添加下面配置。

MAVEN_HOME=/usr/local/maven3
export MAVEN_HOME
export PATH=${PATH}:${MAVEN_HOME}/bin

3、刷新环境

source /etc/profile

4、查看版本 确保成功

mvn -version

你可能感兴趣的:(运维,linux,java,spring,boot)