docker-compose部署springboot项目并使用mysql镜像

添加依赖

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>testdocker</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

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

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.3.1</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.33</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <!--打包lib-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.2.0</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>com.cyz.Application</mainClass> <!-- 替换为你的主类 -->
                        </manifest>
                        <manifestEntries>
                            <Class-Path>resources/</Class-Path>
                        </manifestEntries>
                    </archive>
                    <outputDirectory>${project.build.directory}</outputDirectory>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>3.1.1</version>
                <executions>
                    <execution>
                        <id>copy-dependency</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <!--打包resource-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.2.0</version>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/resources</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>src/main/resources</directory>
                                    <includes>
                                        <include>**/*</include>
                                    </includes>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

编写配置文件

server:
  port: 8001
spring:
  datasource:
    username: ${MYSQL_USER_NAME}
    #    username: root
    password: ${MYSQL_USER_PASSWORD}
    #    password: 123456
    driver-class-name: com.mysql.jdbc.Driver
    #    url: jdbc:mysql://192.168.208.148:3307/sys?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false
    #如果使用docker-compose部署,下面的port就是3306 是镜像原生的端口
    url: jdbc:mysql://${MYSQL_HOST}:${MYSQL_PORT}/sys

编写mapper文件

package com.cyz.controller.mapper;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @author cyz
 * @date 2023/8/28 16:43
 */
@Repository
@Mapper
public interface TestMapper {

    @Select("select  * from sys_config")
    List<Map> queryList();

}

编写Controller

package com.cyz.controller;

import com.cyz.controller.mapper.TestMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author cyz
 * @date 2023/8/28 16:03
 */
@RestController
public class TestController {

    @Autowired
    private TestMapper testMapper;
    @Value("${server.port}")
    private Integer port;

    @GetMapping("/get")
    public String get(){
        return testMapper.queryList().toString();
    }

    @GetMapping("/test")
    public String test(){
        return "success"+port;
    }
}

编写启动类

package com.cyz;

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

/**
 * @author cyz
 * @date 2023/8/28 16:01
 */
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

启动服务,访问两个接口是否有问题,没问题则进行下一步

编写Dockerfile

# 使用基于JDK 8的官方Java镜像作为基础镜像
FROM openjdk:8-jdk

# 将JAR包复制到容器中
COPY / /app/

# 设置工作目录
WORKDIR /app

# 运行JAR包
CMD ["java" , "-jar", "testdocker-1.0-SNAPSHOT.jar"]

编写docker-compose.yml  这边注意一下,项目连接的mysql的端口是3306,3307是外部访问的端口,这两个镜像相当于在一个环境中

version: '3.8'
services:
  mysql:
    image: mysql:8.0.34
    environment:
      MYSQL_ROOT_PASSWORD: 123456
    ports:
      - 3307:3306

  test-docker:
    build: .
    depends_on:
      - mysql
    environment:
      MYSQL_HOST: mysql
      MYSQL_PORT: 3306
      MYSQL_USER_NAME: root
      MYSQL_USER_PASSWORD: 123456
    ports:
      - 8001:8001

将项目打包,放到服务器上

执行docker-compose up -d 

访问

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值