Gitee:a7. (JiaBin1) - Gitee.com

1、安装Docker

#1.需要的安装包
yum install -y yum-utils

#2.设置镜像的仓库
yum-config-manager \
	--add-repo \
	http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo		#使用阿里云仓库
	
#更新yum软件包索引
yum makecache fast

#3.安装docker相关的内容
yum install docker-ce docker-ce-cli containerd.io

#4.启动docker
systemctl start docker

#5.查看当前docker版本
docker version

2、安装Docker-Compose

#1、下载
sudo curl -L "https://github.com/docker/compose/releases/download/1.26.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

# 这个可能快点!
curl -L https://get.daocloud.io/docker/compose/releases/download/1.25.5/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose

#2、授权
sudo chmod +x /usr/local/bin/docker-compose

3、部署SpringBoot + MySQL

1、创建SpringBoot项目,导入以下依赖

  • <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.2</version>
        </dependency>
    
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.76</version>
        </dependency>
    
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.48</version>
        </dependency>
    
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

2、搭建基础环境架构

【实体类】

  • /**
     * @Author 嘉宾
     * @Data 2022/3/21 14:54
     * @Version 1.0
     */
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class Person {
        private Integer pid;
        private String pname;
        private Integer psex;
    }

【接口】

  • /**
     * @Author 嘉宾
     * @Data 2022/3/21 14:55
     * @Version 1.0
     */
    @Mapper
    public interface PersonMapper {
        /**
         * 查询全部信息
          */
        List<Person> findAll();
    }

【mapper映射】

  • <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.jiabin.mapper.PersonMapper">
    
        <!--查询全部信息-->
        <select id="findAll" resultType="Person">
            select * from person
        </select>
    
    </mapper>

【service】

  • /**
     * @Author 嘉宾
     * @Data 2022/3/21 14:57
     * @Version 1.0
     */
    public interface PersonService {
        /**
         * 查询全部信息
         */
        List<Person> findAll();
    }

【impl】

  • /**
     * @Author 嘉宾
     * @Data 2022/3/21 14:57
     * @Version 1.0
     */
    @Service
    public class PersonServiceImpl implements PersonService {
    
        @Autowired
        private PersonMapper personMapper;
    
        @Override
        public List<Person> findAll() {
            return personMapper.findAll();
        }
    }

【controller】

  • /**
     * @Author 嘉宾
     * @Data 2022/3/21 14:58
     * @Version 1.0
     */
    @RestController
    @RequestMapping("/person")
    public class PersonController {
    
        @Autowired
        private PersonService personService;
    
        @GetMapping("/findAll")
        public String findAll(){
            List<Person> personList = personService.findAll();
            return personList.toString();
        }
    
        @GetMapping("/hello")
        public String hello(){
            return "<h1 color='red'>Hello.JiaBin!</hello>";
        }
    }

【配置文件】

  • spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    #这里填写服务名即可mysqldbserver,无需端口号
    spring.datasource.url=jdbc:mysql://mysqldbserver:3306/sys?useUnicode=true&characterEncoding=utf-8&useSSL=false
    spring.datasource.username=root
    spring.datasource.password=123456
    
    mybatis.mapper-locations=classpath:mapper/*.xml
    mybatis.type-aliases-package=com.jiabin.pojo

3、Dockerfile1

  • FROM java:8
    
    VOLUME /vol/development
    
    RUN mkdir -p /vol/development
    
    WORKDIR /vol/development
    
    COPY 这里需要改为自己的jar包.jar /vol/development/app.jar
    
    CMD ["java", "-Xmx200m", "-Djava.security.egd=file:/dev/./urandom","-jar","/vol/development/app.jar"]

4、Dockerfile

  • FROM mysql:5.7.17

5、docker-compose.yml

  • version : '3'
    services:
      #sql服务
      mysqldbserver:
      	#服务名称
        container_name: mysqldbserver
        #镜像
        image: mysql:5.7.17
        build:
          context: .
          dockerfile: Dockerfile
        #端口
        ports:
          - "3306:3306"
        command: [	#执行多条指令
            'mysqld',
            '--innodb-buffer-pool-size=20M',
            '--character-set-server=utf8',
            '--collation-server=utf8_general_ci',
            '--default-time-zone=+8:00',
            '--lower-case-table-names=1'
        ]
        environment:
          # 数据库
          MYSQL_DATABASE: sys
          # 设置root用户密码
          MYSQL_ROOT_PASSWORD: 123456
      #spring服务
      springappserver:
      	# 服务名称
        container_name: springappserver
        build:
          context: .
          dockerfile: Dockerfile1
        ports:	#端口
          - "8080:8080"
        volumes:	#数据卷
          - /vol/development
        depends_on:
          - mysqldbserver
        links:	#连接sql
          - mysqldbserver

6、将项目打包以及上述三个配置文件统一上传到我们的服务器

7、使用命令docker-compose up部署,通过ip+端口号访问项目即可!

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐