0%

version: '3'
services:
  nginx-route:
    image: hub.deri.org.cn/library/nginx
    container_name: nginx-route
    ports:
    - "8080:80"
    restart: always
    volumes:
    - "/etc/localtime:/etc/localtime"
    - "/wuzhiyong/nginx.conf:/etc/nginx/nginx.conf"
  authservice:
    image: wuzhiyong/authservice
    container_name: authservice
    restart: always
    volumes:
    - "/etc/localtime:/etc/localtime"
  dbcompare:
    image: wuzhiyong/dbcompare
    container_name: dbcompare
    restart: always
    volumes:
    - "/etc/localtime:/etc/localtime"
  graphcompare:
    image: wuzhiyong/graphcompare
    container_name: graphcompare
    restart: always
    volumes:
    - "/etc/localtime:/etc/localtime"
    - "/wuzhiyong/svg:/root/svg"
  hbasecompare:
    image: wuzhiyong/hbasecompare
    container_name: hbasecompare
    restart: always
    volumes:
    - "/etc/localtime:/etc/localtime"
    extra_hosts:
    - "hadoop1:172.16.0.7"
    - "hadoop2:172.16.0.8"
    - "hadoop3:172.16.0.9"
  taskservice:
    image: wuzhiyong/taskservice
    container_name: taskservice
    restart: always
    volumes:
    - "/etc/localtime:/etc/localtime"
networks:
  nwzb:
user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/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  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;

    upstream taskservice{
        server taskservice:28080;
    }

    upstream authservice{
        server authservice:25050;
    }

    upstream dbcompare{
        server dbcompare:28082;
    }

    upstream hbasecompare{
        server hbasecompare:26060;
    }

    upstream graphcompare{
        server graphcompare:29090;
    }

    server {
        listen    80 default_server;
        server_name _;
    location /ts {
        proxy_pass    http://taskservice;
    }
        location /auth {
            proxy_pass  http://authservice;
        }
        location /dbc {
            proxy_pass  http://dbcompare;
        }
        location /hbc {
            proxy_pass  http://hbasecompare;
        }
        location /graph {
            proxy_pass  http://graphcompare;
        }
    }
}

问题

容器时间与主机差8个小时,如果里面运行的是java程序,那么程序时间还是有8个小时时差.

解决

  • 容器内时间与宿主机时间不一样
#启动容器时增加
-v /etc/localtime:/etc/localtime
  • java时区主要从/etc/timezone获取
#dockerfile增加
RUN echo "Asia/shanghai" > /etc/timezone

#也也可以从宿主机映射,宿主机可能没有该文件,则需要先新增
-v /etc/timezone:/etc/timezone

#或者jvm参数传递
-Duser.timezone=GMT+08

轮询(默认)

upstream servername { 
    server 10.0.0.1:8080; 
    server 10.0.0.2:8080; 
}

权重

weight 默认为1,weight越大,负载的权重就越大

upstream servername { 
    server 10.0.0.1:8080 weight=5; 
    server 10.0.0.2:8080 weight=10; 
}

IP哈希

请求按访问ip的hash结果分配

upstream servername { 
    ip_hash; 
    server 10.0.0.1:8080; 
    server 10.0.0.2:8080; 
}

响应时间

按后端服务器的响应时间来分配请求,响应时间短的优先分配

upstream servername { 
    fair;
    server 10.0.0.1:8080; 
    server 10.0.0.2:8080; 
}

其它哈希

如:根据请求路径哈希,hash_method是使用的hash算法

upstream servername { 
    fair;
    server 10.0.0.1:8080; 
    server 10.0.0.2:8080;
    hash $request_uri; 
    hash_method crc32; 
}

down/backup

upstream servername { 
    // down 表示单前的server暂时不参与负载.
    server 10.0.0.1:8080 down; 
    server 10.0.0.2:8080; 
    server 10.0.0.3:8080; 
    // 其它所有的非backup机器down或者忙的时候请求backup机器.
    server 10.0.0.4:8080 backup; 
}

参考链接

查看swap状态

[root@ecs ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           7820        4825         133         896        2862        1818
Swap:             0           0           0
# 无返回
swapon -s
# 空
cat /proc/swaps

通过文件设置swap

  1. 新建swap交换文件
# count设置文件大小,4G
dd if=/dev/zero of=/home/swap bs=1024 count=4096000
  1. 制作为swap格式文件
mkswap /home/swap
  1. 挂载swap分区
swapon /home/swap

此时通过free命令已经可以看到swap分区了,但是重启失效.

  1. 设置持久生效
vi /etc/fstab
# 在文件最后一行加上
/home/swap swap swap default 0 0

删除文件swap分区

  1. 先停止swap分区
swapoff /home/swap
  1. 删除swap分区文件
rm -rf /home/swap
  1. 删除自动装载配置
vi /etc/fstab
# 删除刚刚 增加的配置

通过分区配置swap

参考链接

获取ngxin

docker pull hub.deri.org.cn/library/nginx

编写nginx.conf

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/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  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
    
    upstream taskservice{
        server 172.16.0.9:28080;
    }

    upstream authservice{
        server 172.16.0.9:25050;
    }

    upstream dbcservice{
        server 172.16.0.9:28082;
    }

    upstream hbcservice{
        server 172.16.0.9:26060;
    }

    upstream graphservice{
        server 172.16.0.9:29090;
    }

    server {
        listen	80 default_server;
        server_name _;
    location /ts {
        proxy_pass	http://taskservice;
    }
        location /auth {
            proxy_pass  http://authservice;
        }
        location /dbc {
            proxy_pass  http://dbcservice;
        }
        location /hbc {
            proxy_pass  http://hbcservice;
        }
        location /graph {
            proxy_pass  http://graphservice;
        }
    }
}

启动nginx容器

docker run -d --name nginx  -p 8080:80 -v /root/nginx/nginx.conf:/etc/nginx/nginx.conf  nginx

锁的说明

java中锁有SynchronizedReentrantLock,但是这两种锁一次只能允许一个线程访问资源,而信号量semaphore可以控制多个线程同时获取资源,实现更高级别的限流.

semaphore使用

// 创建信号量,permits表示许可证数量
Semaphore semaphore = new Semaphore(permits);
// 获取一个许可证
semaphore.acquire();
// 获取多个许可证
semaphore.acquire(2);

// 归还一个许可证
semaphore.release();
// 归还多个许可证
semaphore.release(2);

使用场景

如一个停车场有10个停车位,小型车辆进入需要获取一个许可证,大型车辆进入需要获取多个许可证,车辆驶出时归还许可证.