docker-compose spring boot发布服务

2019-11-13  本文已影响0人  simians

spirngboot2+mysql8+redis+idea。
  首先这个一个分库分表的demo.所以引入的依赖会比较多,创建docker-compose时需要将该文件放到src文件同级的目录下如下

docker-compose
services:
  mysql:
#实例的名字
    container_name: mysql
#mysql最新镜像,如果没有会自动下载
    image: mysql:latest
    #ports 对外暴露端口号3306并映射到主机端口号3306
    ports:
      - 3306:3306
#暴露3306端口号给同一网络下的其他容器
    expose:
      - 3306
#设置环境变量,配置root 密码
    environment:
      MYSQL_ROOT_PASSWORD: "123456"
#时候总是启动,如果实例出现问题将会重启
  #  restart: always
#挂在数据卷
    volumes:
      #更具自己配置的路径修改 这是绝对路径,相对路径是docker-compose.yml 的当前路径
      #/e/docker/mysql 表示 E:/docker/mysql 这个地方自己定义
#将实例里的数据保存在主机上实现数据的备份防止实例停止造成数据的丢失
      - "/e/docker/mysql/db:/var/lib/mysql"
#自定义配置覆盖容器里的配置
      - "/e/docker/mysql/conf/my.cnf:/etc/my.cnf"
#初始化一下mysql 配置脚本可以放到该目录下
      - "/e/docker/mysql/init:/docker-entrypoint-initdb.d/"
#将连接各个实例可以使他们之间通信
    networks:
      - web1
  redis:
    container_name: redis
    image: redis:latest
    ports:
      - 6379:6379
    expose:
      - 6379
    command:
      --requirepass "123456" --appendonly yes
    volumes:
      - /e/docker/redis/data:/data
    networks:
      - web1

第三步 发布app

  web:
    container_name: shardingJdbcDemo
    image: openjdk:latest
    ports:
      - 8772:8772
    volumes:
      - "./target/shardingJdbcDemo.jar:/app.jar"
    command: "java -jar app.jar"
    networks:
      - web1
    depends_on:
      - mysql
      - redis
    expose:
      - 8772
networks:
  web1:
    driver: bridge

最后是networks

networks:
  web1:
    driver: bridge
需要注意的

1.   在发布web服务的时候需要发布mysql 与redis 特别是mysql 如果不在web 之前发布,app会报错这个地方通过depend_on来配置先后顺序
2.  注意数据源的配置,这个地方是个坑不注意的话会出现无法连接数据库,connection refuse的错误,需要修改成如下配置

url配置

其中我把ip 改为mysql (docker-compose里services 下的名字web,mysql,redis),这个原理不是很清楚,但是就是需要这样改。我能大概的明白他联的是mysql 这个实例的容器通过network
3.  mysql 的配置问题,我下载的是官方的镜像,在通过外部连接的时候回出现问题,root 无法连接,这时需要修改root的权限和加密方式。如下配置

#修改mysql8 的加密方式,不改的话无法登录会报错
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
ALTER USER 'root'@'%' IDENTIFIED BY '123456' PASSWORD EXPIRE NEVER;
FLUSH PRIVILEGES;
use mysql;
#创建用户
create user 'houhaoran'@'%' identified by '123456';
#用户授权  所有数据库.所有权限;
grant all privileges on *.* to 'houhaoran'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;



最后完整的docker-compose.yml配置
version: "3.1"
services:
  mysql:
    container_name: mysql
    image: mysql:latest
    ports:
      - 3306:3306
    expose:
      - 3306
    environment:
      MYSQL_ROOT_PASSWORD: "123456"
  #  restart: always
    volumes:
      #更具自己配置的路径修改 这是绝对路径,相对路径是docker-compose.yml 的当前路径
      #/e/docker/mysql 表示 E:/docker/mysql 这个地方自己定义
      - "/e/docker/mysql/db:/var/lib/mysql"
      - "/e/docker/mysql/conf/my.cnf:/etc/my.cnf"
      - "/e/docker/mysql/init:/docker-entrypoint-initdb.d/"
    networks:
      - web1
  redis:
    container_name: redis
    image: redis:latest
    ports:
      - 6379:6379
    expose:
      - 6379
    command:
      --requirepass "123456" --appendonly yes
    volumes:
      - /e/docker/redis/data:/data
    networks:
      - web1
  web:
    container_name: shardingJdbcDemo
    image: openjdk:latest
    ports:
      - 8772:8772
    volumes:
      - "./target/shardingJdbcDemo.jar:/app.jar"
    command: "java -jar app.jar"
    networks:
      - web1
    depends_on:
      - mysql
      - redis
    expose:
      - 8772
networks:
  web1:
    driver: bridge


maven完整的配置

<?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>demo</groupId>
    <artifactId>sharding-jdbc-demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.2.RELEASE</version>
        <relativePath />
    </parent>
    <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>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.4</version>
            <scope>provided</scope>
        </dependency>
        <!--sharding-jdbc 开始-->
        <!--sharding-jdbc -->
        <dependency>
            <groupId>io.shardingsphere</groupId>
            <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
            <version>3.1.0</version>
        </dependency>
        <!--sharding-jdbc结束-->
        <!--mysql 驱动开始-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!--mysql 结束-->
        <!--mybatis 依赖开始-->
        <!-- mybatis 依赖jar包-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.0</version>
        </dependency>
        <!--mybatis的自动生成器配置-->
        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.5</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/tk.mybatis/mapper-spring-boot-starter -->
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>2.1.0</version>
        </dependency>
        <!--mybatis 依赖结束-->
        <!--durid  阿里巴巴连接池-->
        <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.14</version>
        </dependency>
        <!--durid 结束-->
        <!--spring boot test 使用进行单元测试 开始-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <!--spring boot test 结束-->
        <!--spring boot 整合redis-->
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-redis -->

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
            <version>2.1.3.RELEASE</version>
        </dependency>
        <!--spring boot 整合reids 结束-->
        <!--spring boot z整合log4j2-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
            <version>2.1.3.RELEASE</version>
        </dependency>
        <!-- 加上这个才能辨认到log4j2.yml文件 --><!-- c.?-->
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-yaml</artifactId>
        </dependency>
        <!--jwt 使用开始-->
        <dependency>
            <groupId>com.auth0</groupId>
            <artifactId>java-jwt</artifactId>
            <version>3.4.0</version>
        </dependency>
        <!--jwt认证使用结束-->

        <!-- mybatis的分页插件 -->
        <!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.10</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
                <dependency>
                    <groupId>org.apache.commons</groupId>
                    <artifactId>commons-lang3</artifactId>
                    <version>3.9</version>
                </dependency>
        <!--临时使用-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.15</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>3.15</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.15</version>
        </dependency>
        <!--临时使用-->
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.5</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.58</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.6</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>shardingJdbcDemo</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <!--fork :  如果没有该项配置,肯呢个devtools不会起作用,即应用不会restart -->
                    <fork>true</fork>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
上一篇下一篇

猜你喜欢

热点阅读