Linux卸载docker
查看docker状态
systemctl status docker
停止docker
systemctl stop docker
查看docker相关文件
yum list installed |grep docker
rpm -qa |grep docker
删除所有安装的docker文件包
yum -y remove containerd.io.x86_64
yum -y remove docker-ce.x86_64
yum -y remove docker-ce-18.06.3.ce-3.el7.x86_64
删除docker所有文件
rm -rf /var/lib/docker
至此,docker卸载完毕!
Docker安装
下载docker脚本
curl -fsSL get.docker.com -o get-docker.sh
使用阿里云镜像运行脚本
sh get-docker.sh --mirror Aliyun
启动docker
systemctl daemon-reload
systemctl restart docker
Docker常用命令
镜像操作
查看镜像列表 docker images
拉取镜像 docker pull nginx:alpine
删除镜像 docker rmi nginx:alpine
导出镜像 docker save -o nginx-alpine.tar nginx:alpine
导入镜像 docker load -i nginx-alpine.tar
使用docker镜像启动镜像仓库服务
docker run -d -p 5000:5000 --restart always --name registry registry:2
推送本地镜像到镜像仓库中
# 将nginx:alpine 打标签
docker tag nginx:alpine 192.168.150.100:5000/nginx:alpine
docker push 192.168.150.100:5000/nginx:alpine
报错:docker默认不允许向http的仓库地址推送,需要修改配置,跳过https校验
vi /etc/docker/daemon.json
"insecure-registries": [
"192.168.150.100:5000"
]
重启docker
systemctl restart docker
再push,就成功了
容器操作
查看容器列表
## 查看运行状态的容器列表
$ docker ps
## 查看全部状态的容器列表
$ docker ps -a
启动容器
## 后台启动
$ docker run --name nginx -d nginx:alpine
## 映射端口,把容器的端口映射到宿主机中,-p <host_port>:<container_port>
$ docker run --name nginx -d -p 8080:80 nginx:alpine
## 资源限制,最大可用内存500M
$ docker run --memory=500m nginx:alpine
查看容器日志
## 查看全部日志
$ docker logs nginx
## 实时查看最新日志
$ docker logs -f nginx
## 从最新的100条开始查看
$ docker logs --tail=100 -f nginx
容器数据持久化
## 挂载主机目录
$ docker run --name nginx -d -v /opt:/opt nginx:alpine
$ docker run --name mysql -e MYSQL_ROOT_PASSWORD=123456 -d -v /opt/mysql/:/var/lib/mysql mysql:5.7
进入容器或者执行容器内的命令
$ docker exec -ti <container_id_or_name> /bin/sh
$ docker exec <container_id_or_name> hostname
主机与容器之间拷贝数据
## 主机拷贝到容器
$ echo '123'>/tmp/test.txt
$ docker cp /tmp/test.txt nginx:/tmp
$ docker exec -ti nginx cat /tmp/test.txt
123
## 容器拷贝到主机
$ docker cp nginx:/tmp/test.txt ./
停止或者删除容器
## 停止运行中的容器
$ docker stop nginx
## 启动退出容器
$ docker start nginx
## 删除非运行中状态的容器
$ docker rm nginx
## 删除运行中的容器
$ docker rm -f nginx
查看容器或者镜像的明细
## 查看容器详细信息,包括容器IP地址等
$ docker inspect nginx
## 查看镜像的明细信息
$ docker inspect nginx:alpine
清掉所有容器
docker rm -f `docker ps -aq`
Docker端口映射
安装MySQL
- 下载镜像:docker pull mysql:8.0.30
- 然后查看下载的镜像
- 虚拟机的话需要设置ipv4 云服务器不需要这么做
vim /usr/lib/sysctl.d/00-system.conf
重启网络和Docker
- 启动MySQL
docker run -p 3306:3306 --name=mysql -v /home/mysql8/log:/var/log/mysql -v /home/mysql8/data:/var/lib/mysql -v /home/mysql8/mysql-files:/var/lib/mysql-files -e MYSQL_ROOT_PASSWORD=Admin~123456 -d mysql:8.0.30 --character-set-server=utf8mb4
查看docker运行的容器:docker ps
查看docker容器运行状态:docker stats (ctrl+c退出)
使用navicat可以连接了
如果docker ps没有运行的MySQL容器,那么执行docker ps -a表示查看所有容器(包括运行失败的容器,status是Exited 退出状态的)
那么这个时候需要看mysql容器日志:docker logs mysql
重新启动容器:docker start CONTAINER_ID
修改MySQL密码 进入docker bash命令
docker exec -it mysql bash
mysql -u root -p
设置远程连接
退出bash命令 Ctrl+D
让MySQL跟随系统重启的时候启动
docker update mysql --restart=always
安装Redis
docker pull redis:6.2.7
cd /home/
mkdir redis6/conf -p (-p表示递归创建文件夹)
mkdir redis6/data -p
cd /home/redis6/conf
touch redis.conf
docker run -p 6379:6379 --name=redis -v /home/redis6/data:/data -v /home/redis6/conf/redis.conf:/etc/redis/redis.conf -d redis:6.2.7 redis-server /etc/redis/redis.conf
docker ps (默认Redis没有密码)
进入bash命令使用Redis
让redis跟随系统重启的时候启动
docker update redis–restart=always
给Redis添加密码
cd /home/redis6/conf
vim redis.conf
docker restart redis
安装RabbitMQ
下载镜像:
docker pull rabbitmq:management
启动
docker run --name rabbitmq -p 5671:5671 -p 5672:5672 -p 4369:4369 -p 15671:15671 -p 15672:15672 -p 25672:25672 --restart always -d rabbitmq:management
访问登录界面
http://192.168.233.128:15672/
账号密码都是guest
安装Nacos
docker pull nacos/nacos-server:v2.1.1
启动nacos
docker run --name nacos -e MODE=standalone -e JVM_XMS=128m -e JVM_XMX=128m -e JVM_XMN=64m -e JVM_MS=64m -e JVM_MMS=64m -p 8848:8848 -d nacos/nacos-server:v2.1.1
安装Seata
使用docker安装seata前提,client端(业务端)需要新增表undo_log,具体根据数据库类型来
数据库类型
MySQL为:
-- for AT mode you must to init this sql for you business database. the seata server not need it.
CREATE TABLE IF NOT EXISTS `undo_log`
(
`branch_id` BIGINT NOT NULL COMMENT 'branch transaction id',
`xid` VARCHAR(128) NOT NULL COMMENT 'global transaction id',
`context` VARCHAR(128) NOT NULL COMMENT 'undo_log context,such as serialization',
`rollback_info` LONGBLOB NOT NULL COMMENT 'rollback info',
`log_status` INT(11) NOT NULL COMMENT '0:normal status,1:defense status',
`log_created` DATETIME(6) NOT NULL COMMENT 'create datetime',
`log_modified` DATETIME(6) NOT NULL COMMENT 'modify datetime',
UNIQUE KEY `ux_undo_log` (`xid`, `branch_id`)
) ENGINE = InnoDB
AUTO_INCREMENT = 1
DEFAULT CHARSET = utf8mb4 COMMENT ='AT transaction mode undo table';
seata-server端 需要新建数据库seata(这里名字要跟seata-server application.yml配置文件中store的db保持一致) 并创建下面4张表
根据数据库类型来
MySQL为:
-- -------------------------------- The script used when storeMode is 'db' --------------------------------
-- the table to store GlobalSession data
CREATE TABLE IF NOT EXISTS `global_table`
(
`xid` VARCHAR(128) NOT NULL,
`transaction_id` BIGINT,
`status` TINYINT NOT NULL,
`application_id` VARCHAR(32),
`transaction_service_group` VARCHAR(32),
`transaction_name` VARCHAR(128),
`timeout` INT,
`begin_time` BIGINT,
`application_data` VARCHAR(2000),
`gmt_create` DATETIME,
`gmt_modified` DATETIME,
PRIMARY KEY (`xid`),
KEY `idx_status_gmt_modified` (`status` , `gmt_modified`),
KEY `idx_transaction_id` (`transaction_id`)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4;
-- the table to store BranchSession data
CREATE TABLE IF NOT EXISTS `branch_table`
(
`branch_id` BIGINT NOT NULL,
`xid` VARCHAR(128) NOT NULL,
`transaction_id` BIGINT,
`resource_group_id` VARCHAR(32),
`resource_id` VARCHAR(256),
`branch_type` VARCHAR(8),
`status` TINYINT,
`client_id` VARCHAR(64),
`application_data` VARCHAR(2000),
`gmt_create` DATETIME(6),
`gmt_modified` DATETIME(6),
PRIMARY KEY (`branch_id`),
KEY `idx_xid` (`xid`)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4;
-- the table to store lock data
CREATE TABLE IF NOT EXISTS `lock_table`
(
`row_key` VARCHAR(128) NOT NULL,
`xid` VARCHAR(128),
`transaction_id` BIGINT,
`branch_id` BIGINT NOT NULL,
`resource_id` VARCHAR(256),
`table_name` VARCHAR(32),
`pk` VARCHAR(36),
`status` TINYINT NOT NULL DEFAULT '0' COMMENT '0:locked ,1:rollbacking',
`gmt_create` DATETIME,
`gmt_modified` DATETIME,
PRIMARY KEY (`row_key`),
KEY `idx_status` (`status`),
KEY `idx_branch_id` (`branch_id`),
KEY `idx_xid_and_branch_id` (`xid` , `branch_id`)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4;
CREATE TABLE IF NOT EXISTS `distributed_lock`
(
`lock_key` CHAR(20) NOT NULL,
`lock_value` VARCHAR(20) NOT NULL,
`expire` BIGINT,
primary key (`lock_key`)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4;
INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('AsyncCommitting', ' ', 0);
INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('RetryCommitting', ' ', 0);
INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('RetryRollbacking', ' ', 0);
INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('TxTimeoutCheck', ' ', 0);
下载镜像:
docker pull seataio/seata-server:1.5.2
docker run --name seata-server -p 8091:8091 -p 7091:7091 -d seataio/seata-server:1.5.2
将容器内部resources文件夹拷贝出来并修改:
mkdir /home/seata/resources -p
docker cp seata-server:/seata-server/resources /home/seata/
cd /home/seata/resources
vim application.yml
参考application.example.yml修改application.yml的 config,registry,store具体如下:
seata:
config:
# support: nacos, consul, apollo, zk, etcd3
type: nacos
nacos:
server-addr: 192.168.233.128:8848
namespace:
group: SEATA_GROUP
username: nacos
password: nacos
registry:
# support: nacos, eureka, redis, zk, consul, etcd3, sofa
type: nacos
nacos:
application: seata-server
server-addr: 192.168.233.128:8848
group: SEATA_GROUP
namespace:
cluster: default
username: nacos
password: nacos
store:
# support: file 、 db 、 redis
mode: db
db:
datasource: druid
db-type: mysql
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/seata?rewriteBatchedStatements=true
user: root
password: root
min-conn: 5
max-conn: 100
global-table: global_table
branch-table: branch_table
lock-table: lock_table
distributed-lock-table: distributed_lock
query-limit: 100
max-wait: 5000
重新挂载配置文件,需要先移除容器再使用挂在配置启动容器:
docker stop seata-server
docker rm seata-server
docker run --name seata-server -p 8091:8091 -p 7091:7091 -v /home/seata/resources:/seata-server/resources -d seataio/seata-server:1.5.2
查看seata启动控制台 docker logs seata-server
至此,nacos上就会注册seata服务
业务端使用seata控制全局事务
依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
<exclusions>
<exclusion>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
<version>1.5.2</version>
</dependency>
所有微服务配置文件需要添加
seata:
enabled: true
tx-service-group: imooc_tx_group
service:
vgroup-mapping:
imooc_tx_group: SEATA_GROUP
grouplist:
SEATA_GROUP: 192.168.233.128:8091
config:
nacos:
server-addr: 192.168.233.128:8848
username: nacos
password: nacos
registry:
nacos:
server-addr: 192.168.233.128:8848
username: nacos
password: nacos
调用方serviceImpl方法上添加 @GlobalTransactional,并且一旦远程调用失败,则手动回滚全局事务
// 发起远程调用,初始化用户简历,新增一条空记录
GraceJSONResult graceJSONResult = workMicroServiceFeign.init(user.getId());
if (graceJSONResult.getStatus() != 200) {
// 如果调用状态不是200,则手动回滚全局事务
String xid = RootContext.getXID();
if (StringUtils.isNotBlank(xid)) {
try {
GlobalTransactionContext.reload(xid).rollback();
} catch (TransactionException e) {
e.printStackTrace();
} finally {
GraceException.display(ResponseStatusEnum.USER_REGISTER_ERROR);
}
}
}
或者使用切面来控制全局事务
package com.imooc.api;
import io.seata.core.context.RootContext;
import io.seata.core.exception.TransactionException;
import io.seata.tm.api.GlobalTransaction;
import io.seata.tm.api.GlobalTransactionContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Slf4j
@Component
@Aspect
public class SeataTransactionAspect {
/**
* 调用service之前,手动加入或者创建全局事务
* @param joinPoint
* @throws TransactionException
*/
@Before("execution(* com.imooc.service.impl..*.*(..))")
public void beginTransaction(JoinPoint joinPoint) throws TransactionException {
log.info("手动开启全局事务");
// 手动开启全局事务
GlobalTransaction gt = GlobalTransactionContext.getCurrentOrCreate();
gt.begin();
}
/**
* 捕获异常,则手动回滚全局事务
* @param throwable
* @throws Throwable
*/
@AfterThrowing(
throwing = "throwable",
pointcut = "execution(* com.imooc.service.impl..*.*(..))"
)
public void seataRollback(Throwable throwable) throws Throwable {
log.info("捕获到异常信息,则回滚,异常信息为:" + throwable.getMessage());
// 从当前线程获得xid
String xid = RootContext.getXID();
if (StringUtils.isNotBlank(xid)) {
GlobalTransactionContext.reload(xid).rollback();
}
}
}
安装MINIO
docker pull minio/minio
mkdir /home/minio/data -p
cd /home/minio
docker run --name minio -p 9000:9000 -p 9090:9090 -d --restart=always -e "MINIO_ROOT_USER=imooc" -e "MINIO_ROOT_PASSWORD=imooc123456" -v /home/minio/data:/data minio/minio server /data --console-address ":9090" -address ":9000"
通过访问:192.168.233.128:9090 可以访问minio客户端