Docker安装
-
sudo yum remove docker
docker–client
docker–client–latest
docker–common
docker–latest
docker–latest–logrotate
docker–logrotate
docker–engine
-
sudo yum–config–manager —add–repo https://download.docker.com/linux/centos/docker–ce.repo
如果这一步报错,说尝试其他镜像的话,执行下面步骤:
-
sudo yum install docker–ce docker-ce–cli containerd.io docker-buildx–plugin docker-compose–plugin
-
设置阿里云镜像库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 -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
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客户端
原文地址:https://blog.csdn.net/ZHUSHANGLIN/article/details/130986395
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_50276.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!