Node.js 配置

下载地址:https://nodejs.org/en/download/

环境变量

全局目录&缓存

npm config set cache "D:\Program Files\nodejs\cache"
npm config set prefix "D:\Program Files\nodejs\global"

镜像配置

rem config方式
npm config set registry https://registry.npm.taobao.org 
npm info underscore

rem 指定
npm --registry https://registry.npm.taobao.org info underscore

Install GitLab with Docker

环境:CentOS release 6.9 (Final)

安装Docker

yum search docker
yum -y install docker-io
service docker start

# 查看版本
docker version

下载Gitlab

docker pull gitlab/gitlab-ce:latest

启动Gitlab

docker run --detach \
    --hostname gitlab.bcsytv.com \
    --publish 443:443 --publish 80:80 --publish 2222:22 \
    --name 'gitlab' \
    --restart always \
    --volume /srv/gitlab/config:/etc/gitlab \
    --volume /srv/gitlab/logs:/var/log/gitlab \
    --volume /srv/gitlab/data:/var/opt/gitlab \
    gitlab/gitlab-ce:latest

常用命令

# 列出所有容器 
docker ps -a

# 列出最近一次启动的container
docker ps -l 

# 显示一个运行的容器里面的进程信息
docker top Name/ID  

# 查看容器内部详情细节
docker inspect <id/container_name>

# 启动容器并启动bash(交互方式)
docker run -i -t <image_name/continar_id> /bin/bash

# 启动容器以后台方式运行(更通用的方式)
docker run -d -it  image_name

# 附着到正在运行的容器
docker attach <id、container_name>

# 进入正在运行的容器内部,同时运行bash(比attach更好用)
docker exec -t -i <id/container_name>  /bin/bash

# 查看容器日志
docker logs <id/container_name>

# 实时查看日志输出
docker logs -f <id/container_name> (类似 tail -f) (带上时间戳-t)

# 在容器中安装新的程序
docker run image_name apt-get install -y app_name  

# 从容器里面拷贝文件/目录到本地一个路径
docker cp Name:/container_path to_path  
docker cp ID:/container_path to_path

# 保存对容器的修改(commit) 当你对某一个容器做了修改之后(通过在容器中运行某一个命令),可以把对容器的修改保存下来,这样下次可以从保存后的最新状态运行该容器。
docker commit ID new_image_name  

# 删除单个容器
docker rm Name/ID 

# 删除所有容器
docker rm `docker ps -a -q`  

# 停止、启动、杀死、重启一个容器
docker stop Name/ID  
docker start Name/ID  
docker kill Name/ID  
docker restart name/ID

# 列出镜像
docker images

# 从dockerhub检索image
docker search image_name

# 下载image
docker pull image_name

# 删除一个或者多个镜像;
docker rmi image_name  

# 显示一个镜像的历史;
docker history image_name

# 发布docker镜像
docker push new_image_name

# ps:要发布到私有Registry中的镜像,在镜像命名中需要带上Registry的域名(如果非80端口,同时需要带上端口号)比如:
docker push dockerhub.yourdomain.com:443/hello.demo.kdemo:v1.0

# 查看docker0的网络(宿主机上操作)
ip a show docker0

# 查看容器的IP地址
docker inspect -f '{{ .NetworkSettings.IPAddress }}' <id、container_name>

# 附着到容器内部查看其内部ip:
ip a show eth0

纯JAVA ID序列生成

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;

public enum IdGenerator {

	INSTANCE;

	private long workerId;   			//用ip地址最后几个字节标示
	private long datacenterId = 0L; 	//可配置在properties中,启动时加载,此处默认先写成0
	private long sequence = 0L;
	private long workerIdBits = 8L; 	//节点ID长度
	private long sequenceBits = 12L; 	//序列号12位
	private long workerIdShift = sequenceBits; //机器节点左移12位
	private long datacenterIdShift = sequenceBits + workerIdBits; //数据中心节点左移14位
	private long sequenceMask = ~(-1L << sequenceBits); //4095
	private long lastTimestamp = -1L;

	IdGenerator() {
		workerId = 0x000000FF & getLastIP();
	}

	public synchronized String nextId() {
		long timestamp = timeGen(); //获取当前毫秒数
		//如果服务器时间有问题(时钟后退) 报错。
		if (timestamp < lastTimestamp) {
			throw new RuntimeException(String.format("Clock moved backwards.  Refusing to generate id for %d milliseconds", lastTimestamp - timestamp));
		}
		//如果上次生成时间和当前时间相同,在同一毫秒内
		if (lastTimestamp == timestamp) {
			//sequence自增,因为sequence只有12bit,所以和sequenceMask相与一下,去掉高位
			sequence = (sequence + 1) & sequenceMask;
			//判断是否溢出,也就是每毫秒内超过4095,当为4096时,与sequenceMask相与,sequence就等于0
			if (sequence == 0) {
				timestamp = tilNextMillis(lastTimestamp); //自旋等待到下一毫秒
			}
		} else {
			sequence = 0L; //如果和上次生成时间不同,重置sequence,就是下一毫秒开始,sequence计数重新从0开始累加
		}
		lastTimestamp = timestamp;

		long suffix = (datacenterId << datacenterIdShift) | (workerId << workerIdShift) | sequence;

		SimpleDateFormat sd = new SimpleDateFormat("yyyyMMddHHMMssSSS");
		String datePrefix = sd.format(timestamp);

		return datePrefix + suffix;
	}

	protected long tilNextMillis(long lastTimestamp) {
		long timestamp = timeGen();
		while (timestamp <= lastTimestamp) {
			timestamp = timeGen();
		}
		return timestamp;
	}

	protected long timeGen() {
		return System.currentTimeMillis();
	}

	private byte getLastIP() {
		byte lastip = 0;
		try {
			InetAddress ip = InetAddress.getLocalHost();
			byte[] ipByte = ip.getAddress();
			lastip = ipByte[ipByte.length - 1];
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		return lastip;
	}
}

测试

import org.junit.Assert;
import org.junit.Test;

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class IdGeneratorTest {

	@Test
	public void nextId() throws Exception {
		final IdGenerator idg = IdGenerator.INSTANCE;
		ExecutorService es = Executors.newFixedThreadPool(10);
		final HashSet idSet = new HashSet<String>();
		Collection collection = Collections.synchronizedCollection(idSet);
		long start = System.currentTimeMillis();
		System.out.println("***** start generate id ******");
		for (int i = 0; i < 10; i++)
			es.execute(() -> {
				for (int j = 0; j < 100000; j++) {
					String id = idg.nextId();
					synchronized (idSet) {
						idSet.add(id);
					}
				}
			});
		es.shutdown();
		es.awaitTermination(10, TimeUnit.SECONDS);
		long end = System.currentTimeMillis();
		System.out.println("***** end generate id *****");
		System.out.println("***** cost " + (end - start) + " ms!");
		Assert.assertEquals(10 * 100000, idSet.size());
		//idSet.forEach(o -> System.out.println(o));
	}
}

经测试,在1000000次的生成中未出现重复!!