Docker安装
- sudo yum remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine
- sudo yum remove -y yum-utils
- sudo yum install -y yum-utils
- sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo![在这里插入图片描述](https://img-blog.csdnimg.cn/c0013681a78040f5a2e00deb0eb4a1e1.png) 如果这一步报错,说尝试其他镜像的话,执行下面步骤:
- sudo yum install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
- 启动 systemctl start docker
- 查看docker版本 docker -v
- 查看已经下载的镜像 docker images
- 设置重启后自启动
- 设置阿里云镜像库https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors (注意:分4步执行!) 10.1 sudo mkdir -p /etc/docker 10.2 sudo tee /etc/docker/daemon.json <<-‘EOF’ { “registry-mirrors”: [“https://71upgx1j.mirror.aliyuncs.com”] } EOF 10.3 sudo systemctl daemon-reload 10.4 sudo systemctl restart docker
Docker端口映射
安装MySQL
- 下载镜像:docker pull mysql:8.0.30
- 然后查看下载的镜像
- 虚拟机的话需要设置ipv4 云服务器不需要这么做 vim /usr/lib/sysctl.d/00-system.conf 重启网络和Docker
- 启动MySQL
docker run -p3306: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 -eMYSQL_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 -p6379: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 -p5671:5671 -p5672:5672 -p4369:4369 -p15671:15671 -p15672:15672 -p25672: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 -eMODE=standalone -eJVM_XMS=128m -eJVM_XMX=128m -eJVM_XMN=64m -eJVM_MS=64m -eJVM_MMS=64m -p8848: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.CREATETABLEIFNOTEXISTS`undo_log`(`branch_id`BIGINTNOTNULLCOMMENT'branch transaction id',`xid`VARCHAR(128)NOTNULLCOMMENT'global transaction id',`context`VARCHAR(128)NOTNULLCOMMENT'undo_log context,such as serialization',`rollback_info`LONGBLOBNOTNULLCOMMENT'rollback info',`log_status`INT(11)NOTNULLCOMMENT'0:normal status,1:defense status',`log_created`DATETIME(6)NOTNULLCOMMENT'create datetime',`log_modified`DATETIME(6)NOTNULLCOMMENT'modify datetime',UNIQUEKEY`ux_undo_log`(`xid`,`branch_id`))ENGINE=InnoDBAUTO_INCREMENT=1DEFAULTCHARSET= 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 dataCREATETABLEIFNOTEXISTS`global_table`(`xid`VARCHAR(128)NOTNULL,`transaction_id`BIGINT,`status`TINYINTNOTNULL,`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,PRIMARYKEY(`xid`),KEY`idx_status_gmt_modified`(`status`,`gmt_modified`),KEY`idx_transaction_id`(`transaction_id`))ENGINE=InnoDBDEFAULTCHARSET= utf8mb4;-- the table to store BranchSession dataCREATETABLEIFNOTEXISTS`branch_table`(`branch_id`BIGINTNOTNULL,`xid`VARCHAR(128)NOTNULL,`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),PRIMARYKEY(`branch_id`),KEY`idx_xid`(`xid`))ENGINE=InnoDBDEFAULTCHARSET= utf8mb4;-- the table to store lock dataCREATETABLEIFNOTEXISTS`lock_table`(`row_key`VARCHAR(128)NOTNULL,`xid`VARCHAR(128),`transaction_id`BIGINT,`branch_id`BIGINTNOTNULL,`resource_id`VARCHAR(256),`table_name`VARCHAR(32),`pk`VARCHAR(36),`status`TINYINTNOTNULLDEFAULT'0'COMMENT'0:locked ,1:rollbacking',`gmt_create`DATETIME,`gmt_modified`DATETIME,PRIMARYKEY(`row_key`),KEY`idx_status`(`status`),KEY`idx_branch_id`(`branch_id`),KEY`idx_xid_and_branch_id`(`xid`,`branch_id`))ENGINE=InnoDBDEFAULTCHARSET= utf8mb4;CREATETABLEIFNOTEXISTS`distributed_lock`(`lock_key`CHAR(20)NOTNULL,`lock_value`VARCHAR(20)NOTNULL,`expire`BIGINT,primarykey(`lock_key`))ENGINE=InnoDBDEFAULTCHARSET= utf8mb4;INSERTINTO`distributed_lock`(lock_key, lock_value, expire)VALUES('AsyncCommitting',' ',0);INSERTINTO`distributed_lock`(lock_key, lock_value, expire)VALUES('RetryCommitting',' ',0);INSERTINTO`distributed_lock`(lock_key, lock_value, expire)VALUES('RetryRollbacking',' ',0);INSERTINTO`distributed_lock`(lock_key, lock_value, expire)VALUES('TxTimeoutCheck',' ',0);
下载镜像:
docker pull seataio/seata-server:1.5.2
docker run --name seata-server -p8091:8091 -p7091:7091 -d seataio/seata-server:1.5.2
将容器内部resources文件夹拷贝出来并修改:
mkdir /home/seata/resources -pdockercp 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
dockerrm seata-server
docker run --name seata-server -p8091:8091 -p7091: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);}}}
或者使用切面来控制全局事务
packagecom.imooc.api;importio.seata.core.context.RootContext;importio.seata.core.exception.TransactionException;importio.seata.tm.api.GlobalTransaction;importio.seata.tm.api.GlobalTransactionContext;importlombok.extern.slf4j.Slf4j;importorg.apache.commons.lang3.StringUtils;importorg.aspectj.lang.JoinPoint;importorg.aspectj.lang.ProceedingJoinPoint;importorg.aspectj.lang.annotation.AfterThrowing;importorg.aspectj.lang.annotation.Around;importorg.aspectj.lang.annotation.Aspect;importorg.aspectj.lang.annotation.Before;importorg.springframework.stereotype.Component;@Slf4j@Component@AspectpublicclassSeataTransactionAspect{/**
* 调用service之前,手动加入或者创建全局事务
* @param joinPoint
* @throws TransactionException
*/@Before("execution(* com.imooc.service.impl..*.*(..))")publicvoidbeginTransaction(JoinPoint joinPoint)throwsTransactionException{
log.info("手动开启全局事务");// 手动开启全局事务GlobalTransaction gt =GlobalTransactionContext.getCurrentOrCreate();
gt.begin();}/**
* 捕获异常,则手动回滚全局事务
* @param throwable
* @throws Throwable
*/@AfterThrowing(
throwing ="throwable",
pointcut ="execution(* com.imooc.service.impl..*.*(..))")publicvoidseataRollback(Throwable throwable)throwsThrowable{
log.info("捕获到异常信息,则回滚,异常信息为:"+ throwable.getMessage());// 从当前线程获得xidString 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 -p9000:9000 -p9090: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客户端
版权归原作者 Sunny_yiyi 所有, 如有侵权,请联系我们删除。