0%

轮询(默认)

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个停车位,小型车辆进入需要获取一个许可证,大型车辆进入需要获取多个许可证,车辆驶出时归还许可证.

配置application.yml

spring:
  datasource:
#   第一个数据源
    first:
      driver-class-name: com.mysql.jdbc.Driver
      url: jdbc:mysql://192.168.41.128:3306/first?useUnicode=true&characterEncoding=utf8&useSSL=false
      username: root
      password: 123456
    #   第二个数据源
    second:
      driver-class-name: com.mysql.jdbc.Driver
      url: jdbc:mysql://172.16.0.9:3306/second?useUnicode=true&characterEncoding=utf8&useSSL=false
      username: root
      password: 123456

获取DB配置

@Data
@Component
@ConfigurationProperties(prefix = "spring.datasource.first")
public class FirstDataBaseProperties {
    private String url;
    private String username;
    private String password;
    private String driverClassName;
}
@Data
@Component
@ConfigurationProperties(prefix = "spring.datasource.second")
public class SecondDataBaseProperties {
    private String url;
    private String username;
    private String password;
    private String driverClassName;
}

配置数据源

@Configuration
// 第一个数据源与Mapper接口绑定
@MapperScan(basePackages = "com.deri.task.dao.first",sqlSessionTemplateRef ="firstSqlSessionTemplate")
public class FirstDataSourceConfig {
    @Autowired
    FirstDataBaseProperties firstDataBaseProperties;

    @Bean(name = "firstDS")
    // 多数据源情况下需要指定主数据源
    @Primary
    public DataSource getFirstDataSource() {
        DataSource build =  DataSourceBuilder.create()
                .driverClassName(firstDataBaseProperties.getDriverClassName())
                .url(firstDataBaseProperties.getUrl())
                .username(firstDataBaseProperties.getUsername())
                .password(firstDataBaseProperties.getPassword())
                .build();
        return build;
    }

    @Bean(name = "firstSqlSessionFactory")
    @Primary
    public SqlSessionFactory firstSqlSessionFactory(@Qualifier("firstDS") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        return bean.getObject();
    }

    @Bean("firstTransactionManger")
    @Primary
    public DataSourceTransactionManager firstTransactionManger(@Qualifier("firstDS") DataSource dataSource){
        return new DataSourceTransactionManager(dataSource);
    }

    // 创建SqlSessionTemplate

    @Bean(name = "firstSqlSessionTemplate")
    @Primary
    public SqlSessionTemplate firstSqlSessionTemplate(@Qualifier("firstSqlSessionFactory") SqlSessionFactory sqlSessionFactory){
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
@Configuration
// 第二个数据源与Mapper接口绑定
@MapperScan(basePackages = "com.deri.task.dao.second",sqlSessionTemplateRef ="secondSqlSessionTemplate")
public class FirstDataSourceConfig {
    @Autowired
    SecondDataBaseProperties secondDataBaseProperties;

    @Bean(name = "secondDS")
    public DataSource getFirstDataSource() {
        DataSource build =  DataSourceBuilder.create()
                .driverClassName(secondDataBaseProperties.getDriverClassName())
                .url(secondDataBaseProperties.getUrl())
                .username(secondDataBaseProperties.getUsername())
                .password(secondDataBaseProperties.getPassword())
                .build();
        return build;
    }

    @Bean(name = "secondSqlSessionFactory")
    public SqlSessionFactory firstSqlSessionFactory(@Qualifier("secondDS") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        return bean.getObject();
    }

    @Bean("secondTransactionManger")
    public DataSourceTransactionManager firstTransactionManger(@Qualifier("secondDS") DataSource dataSource){
        return new DataSourceTransactionManager(dataSource);
    }

    // 创建SqlSessionTemplate

    @Bean(name = "secondSqlSessionTemplate")
    public SqlSessionTemplate firstSqlSessionTemplate(@Qualifier("secondSqlSessionFactory") SqlSessionFactory sqlSessionFactory){
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

参考链接

已知问题

  • mybatis驼峰命名转换失效问题
# 默认在application.yml中配置,但是在多数据源场景下该配置失效,需要在代码中显式指定.
mybatis:
  configuration:
    map-underscore-to-camel-case: true
@Bean(name = "secondSqlSessionFactory")
public SqlSessionFactory firstSqlSessionFactory(@Qualifier("secondDS") DataSource dataSource) throws Exception {
    SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
    bean.setDataSource(dataSource);
    // 指定驼峰转换
    bean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
    return bean.getObject();
}

另一种配置方法

不用创建FirstDataBasePropertiesSecondDataBaseProperties.其余保持一样.

    @Primary
    @Bean(name = "publicDataSourceProperties")
    @ConfigurationProperties(prefix = "spring.datasource.public")
    public DataSourceProperties publicDataSourceProperties() {
        return new DataSourceProperties();
    }
    @Bean(name = "publicDS")
    @Primary
    public DataSource getFirstDataSource(@Qualifier("publicDataSourceProperties") DataSourceProperties dataSourceProperties) {
        return dataSourceProperties.initializeDataSourceBuilder().build();
    }

命令

# 指定格式
ls -lSh `find /var/lib/docker -type f -name "*.log"` | head -n 5
# 所有文件格式按大小排序
find /var/lib/docker -type f -printf '%s %p\n' | sort -rn | head -10
# 查找大文件
find . -type f -size +800M  -print0 | xargs -0 du -h | sort -nr | head -10

find命令

  • type: b:块设备文档d:目录c:字符设备文档P:管道文档l:符号链接文档f:普通文档 .
  • name: 按文件名查找。支持*模糊匹配.
  • size: 文件大小。+表示大于,-表示小于。支持k,M,G单位.

xargs命令

给命令传递参数的一个过滤器

somecommand |xargs -item  command
  • xargs -0\0 作为定界符.
  • 删除指定格式文件
find . -type f -name "*.log" -print0 | xargs -0 rm -f
  • 统计文件行数
find . -type f -name "*.php" -print0 | xargs -0 wc -l
  • 下载文件中所有url链接
cat url-list.txt | xargs wget -c