docker-compose 部署 Nginx、Postgres、redis、java应用

文件夹结构

C:\Users\Administrator\Desktop\lsheet>tree /f
│ 
│  docker-compose.yml   #部署文件
│
├─java-server
│      application-dev.yml           #项目配置
│      application.yml                  #项目配置 
│      web-lockysheet-server.jar   #项目
│
├─nginx
│  │  nginx.conf  #nginx配置
│  │
│  ├─html         #nginx静态文件夹
│  └─logs          #nginx日志文件夹
├─postgres
│  │  init.sql      #postgre初始化文件
│  │
│  └─data          #postgres数据文件夹
└─redis
    │  redis.conf   #redis配置文件
    │ 
    ├─data          #redis数据文件夹
    └─logs           #redis日志文件夹

准备工作

安装 docker

安装 docker-compose

安装 curl

yum install curl

 

1、进入文件夹

 2、设置redis/logs目录权限

chmod a+rwx ./redis/logs/

 3、开始构建生成镜像

docker-compose build

 4、后台启动容器

docker-compose up -d 

 5、查看镜像

docker ps

 6、验证

1)redis验证

 docker exec -it  容器ID  redis-cli -a '123456'

 2)postgres验证

#进入容器
docker exec -ti postgres /bin/bash
#登陆postgres
psql -U postgres
#列出全部数据库
\l
#切换数据库
\c luckysheetdb
#列出全部表名
\dt
#查看表数据
select * from luckysheet;

 3)验证java应用(使用测试url)

curl http://172.19.0.4:9004/luckysheet/test/constant?param=123

 4)验证nginx访问java应用

curl http://172.19.0.101/luckysheet/test/constant?param=123

 5)本示例是安装在云服务器上,通过浏览器测试

http://xx.100.104.9/luckysheet/test/constant?param=123

 7、配置文件

1)nginx配置

#运行用户
#user  nobody;

#开启进程数 <=CPU数
worker_processes  1;

#错误日志保存位置
error_log  /var/log/nginx/error.log;

#进程号保存文件
pid        /var/log/nginx/nginx.pid;

#等待事件
events {
    #Linux下打开提高性能
    #use epoll;
    #每个进程最大连接数(最大连接=连接数x进程数)
    worker_connections  1024;
}


http {
    #文件扩展名与文件类型映射表
    include       mime.types;
    
    #默认文件类型
    default_type  application/octet-stream;

    #日志文件输出格式 这个位置相于全局设置
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    #请求日志保存位置
    access_log  /var/log/nginx/access.log  main;

    #打开发送文件
    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #打开gzip压缩
    #gzip  on;
    gzip  on;
    gzip_min_length 1k;
    gzip_buffers 16 64k;
    gzip_http_version 1.0;
    gzip_comp_level 7;
    #DO NOT zip pics
    gzip_types text/plain application/x-javascript text/javascript application/x-httpd-php text/css text/xml text/jsp application/eot application/ttf application/otf application/svg application/woff;
    gzip_vary on;
    gzip_disable "MSIE [1-6].";

    #websocket
    map $http_upgrade $connection_upgrade {
        default upgrade;
        ''      close;
    }
    
    #设定负载均衡的服务器列表
    upstream luckysheetserver {
        server 172.19.0.4:9004 weight=1;  
    }
    
    #第一个虚拟主机
    server {
        #监听IP端口
        listen       80;
        
        #主机名
        server_name  localhost;
        
        #设置字符集
        #charset koi8-r;

        #本虚拟server的访问日志 相当于局部变量 
        #access_log  logs/host.access.log  main;
        
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        location /luckysheet/websocket/luckysheet {
            proxy_pass http://luckysheetserver/luckysheet/websocket/luckysheet;

            proxy_set_header Host $host;
            proxy_set_header X-real-ip $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_connect_timeout 1800s;
            proxy_read_timeout 600s;
            proxy_send_timeout 600s;
            #websocket
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
        }
        location /luckysheet/ {
            proxy_pass http://luckysheetserver;

            proxy_connect_timeout 1800;
            proxy_read_timeout 600;
        }
         
        location / {
            root   /usr/share/nginx/html/;
            index  index.html index.htm;            
            
            proxy_connect_timeout 1800;
            proxy_read_timeout 600;
            #websocket
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
        }

        #动静分离
        location ~ .*\.(html|js|css|jpg|txt)?$ {
           root  /usr/share/nginx/html/;
           #expires 3d;
        }
        
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html/;
        }

    }
}

2)redis配置

bind *
protected-mode no
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
supervised no
pidfile /usr/local/redis/redis.pid
loglevel notice
logfile /usr/local/redis/logs.log
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /usr/local/redis/data/
slave-serve-stale-data yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
maxmemory 500mb
maxmemory-policy noeviction
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
requirepass 123456

3)postgres初始化脚本

CREATE DATABASE luckysheetdb;
\c luckysheetdb;

DROP SEQUENCE IF EXISTS "public"."luckysheet_id_seq";
CREATE SEQUENCE "public"."luckysheet_id_seq"
INCREMENT 1
MINVALUE  1
MAXVALUE 9999999999999
START 1
CACHE 10;

DROP TABLE IF EXISTS "public"."luckysheet";
CREATE TABLE "luckysheet" (
  "id" int8 NOT NULL,
  "block_id" varchar(200) COLLATE "pg_catalog"."default" NOT NULL,
  "index" varchar(200) COLLATE "pg_catalog"."default" NOT NULL,
  "list_id" varchar(200) COLLATE "pg_catalog"."default" NOT NULL,
  "status" int2 NOT NULL,
  "json_data" jsonb,
  "order" int2,
  "is_delete" int2
);
CREATE INDEX "block_id" ON "public"."luckysheet" USING btree (
  "block_id" COLLATE "pg_catalog"."default" "pg_catalog"."text_ops" ASC NULLS LAST,
  "list_id" COLLATE "pg_catalog"."default" "pg_catalog"."text_ops" ASC NULLS LAST
);
CREATE INDEX "index" ON "public"."luckysheet" USING btree (
  "index" COLLATE "pg_catalog"."default" "pg_catalog"."text_ops" ASC NULLS LAST,
  "list_id" COLLATE "pg_catalog"."default" "pg_catalog"."text_ops" ASC NULLS LAST
);
CREATE INDEX "is_delete" ON "public"."luckysheet" USING btree (
  "is_delete" "pg_catalog"."int2_ops" ASC NULLS LAST
);
CREATE INDEX "list_id" ON "public"."luckysheet" USING btree (
  "list_id" COLLATE "pg_catalog"."default" "pg_catalog"."text_ops" ASC NULLS LAST
);
CREATE INDEX "order" ON "public"."luckysheet" USING btree (
  "list_id" COLLATE "pg_catalog"."default" "pg_catalog"."text_ops" ASC NULLS LAST,
  "order" "pg_catalog"."int2_ops" ASC NULLS LAST
);
CREATE INDEX "status" ON "public"."luckysheet" USING btree (
  "list_id" COLLATE "pg_catalog"."default" "pg_catalog"."text_ops" ASC NULLS LAST,
  "status" "pg_catalog"."int2_ops" ASC NULLS LAST
);
ALTER TABLE "public"."luckysheet" ADD CONSTRAINT "luckysheet_pkey" PRIMARY KEY ("id");

INSERT INTO "public"."luckysheet" VALUES (nextval('luckysheet_id_seq'), 'fblock', '1', '1079500#-8803#7c45f52b7d01486d88bc53cb17dcd2c3', 1, '{"row":84,"name":"Sheet1","chart":[],"color":"","index":"1","order":0,"column":60,"config":{},"status":0,"celldata":[],"ch_width":4748,"rowsplit":[],"rh_height":1790,"scrollTop":0,"scrollLeft":0,"visibledatarow":[],"visibledatacolumn":[],"jfgird_select_save":[],"jfgrid_selection_range":{}}', 0, 0);
INSERT INTO "public"."luckysheet" VALUES (nextval('luckysheet_id_seq'), 'fblock', '2', '1079500#-8803#7c45f52b7d01486d88bc53cb17dcd2c3', 0, '{"row":84,"name":"Sheet2","chart":[],"color":"","index":"2","order":1,"column":60,"config":{},"status":0,"celldata":[],"ch_width":4748,"rowsplit":[],"rh_height":1790,"scrollTop":0,"scrollLeft":0,"visibledatarow":[],"visibledatacolumn":[],"jfgird_select_save":[],"jfgrid_selection_range":{}}', 1, 0);
INSERT INTO "public"."luckysheet" VALUES (nextval('luckysheet_id_seq'), 'fblock', '3', '1079500#-8803#7c45f52b7d01486d88bc53cb17dcd2c3', 0, '{"row":84,"name":"Sheet3","chart":[],"color":"","index":"3","order":2,"column":60,"config":{},"status":0,"celldata":[],"ch_width":4748,"rowsplit":[],"rh_height":1790,"scrollTop":0,"scrollLeft":0,"visibledatarow":[],"visibledatacolumn":[],"jfgird_select_save":[],"jfgrid_selection_range":{}}', 2, 0);

4)docker-compose文件

version: "2"
services:

  nginx:
    image: nginx:latest
    #restart: always
    container_name: nginx
    environment:
      - TZ=Asia/Shanghai
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf
      - ./nginx/logs:/var/log/nginx/
      - ./nginx/html:/usr/share/nginx/html/
      - /etc/localtime:/etc/localtime
    networks:
      extnetwork:
        ipv4_address: 172.19.0.101
  
  
  redis:
    image: redis:latest
    container_name: redis
    #restart: always
    environment:
      - TZ=Asia/Shanghai
    command: redis-server /usr/local/etc/redis/redis.conf --requirepass 123456
    ports:
      - "6379:6379"
    volumes:
      - ./redis/data:/usr/local/redis/data/
      - ./redis/logs:/usr/local/redis/
      - ./redis/redis.conf:/usr/local/etc/redis/redis.conf
      - /etc/localtime:/etc/localtime
    networks:
      extnetwork:
        ipv4_address: 172.19.0.2
      

  postgres:
    image: postgres:12
    #restart: always
    privileged: true 
    container_name: postgres 
    ports:
      - 5432:5432
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: 123456
      PGDATA: /var/lib/postgresql/data/pgdata
    volumes:
      - ./postgres/data:/var/lib/postgresql/data/pgdata
      - ./postgres/init.sql:/docker-entrypoint-initdb.d/init.sql
      - /etc/localtime:/etc/localtime
    networks:
      extnetwork:
        ipv4_address: 172.19.0.3  
      

  web-server:
    image: java:8
    #restart: always
    privileged: true  
    ports:
      - 9004:9004
    volumes:
      - ./java-server/web-lockysheet-server.jar:/usr/local/luckysheet-server/app.jar
      - ./java-server/application.yml:/usr/local/luckysheet-server/application.yml
      - ./java-server/application-dev.yml:/usr/local/luckysheet-server/application-dev.yml
      - /etc/localtime:/etc/localtime
    command: [
      'java',
      '-Xmx200m',
      '-jar',
      '/usr/local/luckysheet-server/app.jar',
      '--spring.config.location=/usr/local/luckysheet-server/application.yml,/usr/local/luckysheet-server/application-dev.yml'
    ]  
    networks:
      extnetwork:
        ipv4_address: 172.19.0.4

networks:
   extnetwork:
      ipam:
         config:
         - subnet: 172.19.0.0/16
           gateway: 172.19.0.1    
            

 

注:docker-compose使用host部署(非固定IP)

1)docker-compose.yml

version: "2"
services:

  redis:
    image: redis:latest
    container_name: luckysheet-redis
    #restart: always
    environment:
      - TZ=Asia/Shanghai
    command: redis-server /usr/local/etc/redis/redis.conf --requirepass 123456
    ports:
      - "6379:6379"
    volumes:
      - ./redis/data:/usr/local/redis/data/
      - ./redis/logs:/usr/local/redis/
      - ./redis/redis.conf:/usr/local/etc/redis/redis.conf
      - /etc/localtime:/etc/localtime
    networks:
      - luckysheet-net

  postgres:
    image: postgres:12
    #restart: always
    privileged: true 
    container_name: luckysheet-postgres 
    ports:
      - 5432:5432
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: 123456
      PGDATA: /var/lib/postgresql/data/pgdata
    volumes:
      - ./postgres/data:/var/lib/postgresql/data/pgdata
      - ./postgres/init.sql:/docker-entrypoint-initdb.d/init.sql
      - /etc/localtime:/etc/localtime
    networks:
      - luckysheet-net
      

  web-server:
    image: java:8
    #restart: always
    privileged: true
    container_name: luckysheet-server  
    ports:
      - 9004:9004
    volumes:
      - ./java-server/web-luckysheet-server.jar:/usr/local/luckysheet-server/app.jar
      - ./java-server/application.yml:/usr/local/luckysheet-server/application.yml
      - ./java-server/application-dev.yml:/usr/local/luckysheet-server/application-dev.yml
      - /etc/localtime:/etc/localtime
    command: [
      'java',
      '-Xmx200m',
      '-jar',
      '/usr/local/luckysheet-server/app.jar',
      '--spring.config.location=/usr/local/luckysheet-server/application.yml,/usr/local/luckysheet-server/application-dev.yml'
    ]  
    networks:
      - luckysheet-net
    links:
      - redis
      - postgres

  nginx:
    image: nginx:latest
    #restart: always
    container_name: luckysheet-nginx
    environment:
      - TZ=Asia/Shanghai
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf
      - ./nginx/logs:/var/log/nginx/
      - ./nginx/html:/usr/share/nginx/html/
      - /etc/localtime:/etc/localtime
    networks:
      - luckysheet-net
    links:
      - web-server 

networks:
  luckysheet-net:
    external: false
            

2)nginx.conf

#运行用户
#user  nobody;

#开启进程数 <=CPU数
worker_processes  1;

#错误日志保存位置
error_log  /var/log/nginx/error.log;

#进程号保存文件
pid        /var/log/nginx/nginx.pid;

#等待事件
events {
    #Linux下打开提高性能
    #use epoll;
    #每个进程最大连接数(最大连接=连接数x进程数)
    worker_connections  1024;
}


http {
    #文件扩展名与文件类型映射表
    include       mime.types;
    
    #默认文件类型
    default_type  application/octet-stream;

    #日志文件输出格式 这个位置相于全局设置
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    #请求日志保存位置
    access_log  /var/log/nginx/access.log  main;

    #打开发送文件
    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #打开gzip压缩
    #gzip  on;
    gzip  on;
    gzip_min_length 1k;
    gzip_buffers 16 64k;
    gzip_http_version 1.0;
    gzip_comp_level 7;
    #DO NOT zip pics
    gzip_types text/plain application/x-javascript text/javascript application/x-httpd-php text/css text/xml text/jsp application/eot application/ttf application/otf application/svg application/woff;
    gzip_vary on;
    gzip_disable "MSIE [1-6].";

    #websocket
    map $http_upgrade $connection_upgrade {
        default upgrade;
        ''      close;
    }
    
    #设定负载均衡的服务器列表
    upstream luckysheetserver {
        server web-server:9004 weight=1;  
    }
    
    #第一个虚拟主机
    server {
        #监听IP端口
        listen       80;
        
        #主机名
        server_name  localhost;
        
        #设置字符集
        #charset koi8-r;

        #本虚拟server的访问日志 相当于局部变量 
        #access_log  logs/host.access.log  main;
        
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        location /luckysheet/websocket/luckysheet {
            proxy_pass http://luckysheetserver/luckysheet/websocket/luckysheet;

            proxy_set_header Host $host;
            proxy_set_header X-real-ip $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_connect_timeout 1800s;
            proxy_read_timeout 600s;
            proxy_send_timeout 600s;
            #websocket
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
        }
        location /luckysheet/ {
            proxy_pass http://luckysheetserver;

            proxy_connect_timeout 1800;
            proxy_read_timeout 600;
        }
         
        location / {
            root   /usr/share/nginx/html/;
            index  index.html index.htm;            
            
            proxy_connect_timeout 1800;
            proxy_read_timeout 600;
            #websocket
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
        }

        #动静分离
        location ~ .*\.(html|js|css|jpg|txt)?$ {
           root  /usr/share/nginx/html/;
           #expires 3d;
        }
        
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html/;
        }

    }
}

3)application-dev.yml

 

logging:
  config: classpath:logback-spring.xml

# 日志是否打印sql
showSql: true


spring:
  redis:
    host: redis
    port: 6379
    password: 123456
    timeout: 10000ms
    lettuce:
      pool:
        max-active: 8
        max-wait: -1ms
        max-idle: 8
        min-idle: 0
    database: 0

db:
  postgre:
    druid:
      url: jdbc:postgresql://postgres:5432/luckysheetdb?useSSL=false
      driverClassName: org.postgresql.Driver
      username: postgres
      password: 123456
      # 初始化大小,最小,最大
      initial-size: 8
      min-idle: 1
      max-active: 20
      # 配置获取连接等待超时的时间
      max-wait: 60000
      # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
      time-between-eviction-runsMillis: 60000
      # 配置一个连接在池中最小生存的时间,单位是毫秒
      min-evictable-idle-timeMillis: 300000
      validation-query: select 1
      test-while-idle: true
      test-on-borrow: false
      test-on-return: false
      # 打开PSCache,并且指定每个连接上PSCache的大小
      pool-prepared-statements: true
      max-open-prepared-statements: 20
      # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
      max-pool-prepared-statement-per-connection-size: 20
      filters: stat,wall
      # 通过connectProperties属性来打开mergeSql功能;慢SQL记录
      connection-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
      use-global-data-source-stat: true

 

注:

进入容器
docker exec -ti 容器ID /bin/bash
进入容器内redis
docker exec -it 容器ID  redis-cli -a '123456'
登陆docker查看日志
docker logs --tail 300 -f 容器ID
设置文件夹权限
chmod a+rwx 目标

 

posted @ 2020-11-30 22:08  桥头堡洗脚城  阅读(1925)  评论(1编辑  收藏  举报