1、linux下nacos安装
1-1、下载安装包
官网(https://nacos.io/zh-cn/docs/quick-start.html)
下载地址(https://github.com/alibaba/nacos/releases/)
下载示例1-2、解压安装包
将压缩包放到相应的目录下进行解压,解压:
解压完成后的nacos文件tar -zxvf nacos-server-2.0.0.tar.gz
1-3、编辑相应配置
vim /nacos/conf/application.properties
1-4、初始化数据库
我是用Navicat客户端连接数据库,执行/nacos/conf目录下nacos-mysql.sql文件,进行数据库初始化
进入bin目录,cd /nacos/bin
启动nacos命令(-m standalone 指定启动模式为单机版):
sh startup.sh -m standalone
查看日志
tail -500f /nacos/logs/start.out
默认端口为8848,在application.aproperties中可以配置。
centos7给防火墙开发8848端口:
firewall-cmd --zone=public --add-port=8848/tcp --permanent
重新载入防火墙:
firewall-cmd --reload
访问nacos控制台,http://192.168.232.128:8848/nacos ( http://ip:8848/nacos),用户名密码均为:nacos
关闭nacos命令,bin目录下执行
./shutdown.sh
2、seata安装
2-1、软件下载
下载地址 https://github.com/seata/seata/releases
tar -xvf seata-server-1.4.1.tar.gz
2-2、修改目录下 conf/registry.conf 配置
vim conf/registry.conf
registry {
# file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
type = "nacos" // 这里修改成nacos
loadBalance = "RandomLoadBalance"
loadBalanceVirtualNodes = 10
nacos {
application = "seata-server" // nacos注册时的服务名
serverAddr = "192.168.232.128" // nacos地址
group = "SEATA_GROUP"
namespace = "" // 默认public
cluster = "default"
username = "nacos"
password = "nacos"
}
}
config {
# file、nacos 、apollo、zk、consul、etcd3
type = "nacos"
nacos {
serverAddr = "192.168.232.128"
namespace = ""
group = "SEATA_GROUP"
username = "nacos"
password = "nacos"
}
}
2-3、修改conf目录下file.conf配置
vim conf/file.conf
2-4、修改conf目录下registry.conf配置
vim conf/registry.conf
2-5、初始化表
创建seata数据库
-- -------------------------------- 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_gmt_modified_status` (`gmt_modified`, `status`),
KEY `idx_transaction_id` (`transaction_id`)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8;
-- 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 = utf8;
-- the table to store lock data
CREATE TABLE IF NOT EXISTS `lock_table`
(
`row_key` VARCHAR(128) NOT NULL,
`xid` VARCHAR(96),
`transaction_id` BIGINT,
`branch_id` BIGINT NOT NULL,
`resource_id` VARCHAR(256),
`table_name` VARCHAR(32),
`pk` VARCHAR(36),
`gmt_create` DATETIME,
`gmt_modified` DATETIME,
PRIMARY KEY (`row_key`),
KEY `idx_branch_id` (`branch_id`)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8;
CREATE TABLE `undo_log` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`branch_id` bigint(20) NOT NULL,
`xid` varchar(100) NOT NULL,
`context` varchar(128) NOT NULL,
`rollback_info` longblob NOT NULL,
`log_status` int(11) NOT NULL,
`log_created` datetime NOT NULL,
`log_modified` datetime NOT NULL,
`ext` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE,
UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;
2-6、在seata目录下创建config.txt文件
touch config.txt
注意 service.vgroup_mapping.${your-service-gruop}=default,中间的 ${your-service-gruop} 为自己定义的服务组名称,SpringBlade默认为service-name-group的格式。
若服务名为blade-demo,则这里配置的 ${your-service-gruop} 为blade-demo-group
比如截图中的两个服务,分别是gateway-fescar-service和api-admin-fescar-service,他们对应的配置如下
config.txt内容根据seata版本获取,地址:https://github.com/seata/seata/tree/1.4.1/script/config-center
2-7、创建导入脚本nacos-config.sh
在seata目录下创建script文件夹
mkdir script
创建导入脚本nacos-config.sh
cd script
vim nacos-config.sh
nacos-config.sh脚本内容,根据seata版本自行下载,地址: https://github.com/seata/seata/tree/1.4.1/script/config-center/nacos
给脚本赋予执行权
chmod +x /nacos-config.sh
执行脚本
sh nacos-config.sh
执行成功可以看到配置列表
注意:
执行.sh脚本时出现$’\r’: 未找到命令,
原因
是因为命令直接从windows 复制过来导致的
解决
yum install dos2unix
dos2unix **.sh 进行转换
再次执行即可
2-8、启动seata
cd /seata/bin
sh seata-server.sh -h 192.168.232.128 -p 8091 &
注:ip地址为127.0.0.1时默认在nacos注入内网ip,导致外网无法访问
启动成功如图所示
3、springcloud配置nacos+seata
3-1、创建项目
pom.xml加入以赖
<properties>
<java.version>1.8</java.version>
<nacos.version>2.1.3.RELEASE</nacos.version>
<spring.cloud.seata.version>2.1.0.RELEASE</spring.cloud.seata.version>
<seata-all.version>1.4.1</seata-all.version>
<druid.version>1.1.10</druid.version>
</properties>
</dependencyManagement>
</dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>${nacos.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>${nacos.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-seata</artifactId>
<version>${spring.cloud.seata.version}</version>
</dependency>
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-all</artifactId>
<version>${seata-all.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
配置项目application.yml
spring:
cloud:
alibaba:
seata:
tx-service-group: apiadmin-service-group
nacos:
discovery:
server-addr: 192.168.232.128:8848
group: SEATA_GROUP
ip: 192.168.232.128
nacos:
group: SEATA_GROUP
namespace: public
# 配置中心地址
server-addr: 127.0.0.1:8848
seata:
application: seata-server
tx-service-group: my_test_tx_group
seata:
enabled: true
application-id: ${spring.application.name} // 当前应用模块名称
tx-service-group: ${nacos.seata.tx-service-group}
enable-auto-data-source-proxy: true
config:
# 指明类型
type: nacos
nacos:
server-addr: ${nacos.server-addr}
namespace: ${nacos.namespace}
group: ${nacos.group}
username: "nacos"
password: "nacos"
registry:
type: nacos
nacos:
application: ${nacos.seata.application}
server-addr: ${nacos.server-addr}
namespace: ${nacos.namespace}
group: ${nacos.group}
username: "nacos"
password: "nacos"
4、docker-compose运行springcloud
4-1、docker-compose安装
Linux
Linux 上我们可以从 Github 上下载它的二进制包来使用,最新发行的版本地址:https://github.com/docker/compose/releases。
运行以下命令以下载 Docker Compose 的当前稳定版本:
sudo curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
要安装其他版本的 Compose,请替换 1.24.1。
将可执行权限应用于二进制文件:
sudo chmod +x /usr/local/bin/docker-compose
创建软链:
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
测试是否安装成功:
docker-compose --version
cker-compose version 1.24.1, build 4667896b
4-2、Dockerfile定义
FROM java:8
RUNmkdir/microservice
WORKDIR /microservice
ADD /register-center-1.0.jar /microservice/
EXPOSE8761
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/microservice/register-center-1.0.jar"]
说明:
FROM java:8
FROM 指定基于的基础镜像。必须在第一行,本例中是基于Java 8作为基础镜像的。
RUN mkdir /microservice
RUN 执行shell命令。本例中是用于创建microservice目录。
WORKDIR /microservice
WORKDIR 为后续的RUN、CMD、ENTRYPOINT指令配置工作目录。
ADD /register-center-1.0.jar /microservice/
ADD 复制指定位置的程序包到容器的指定位置。本例中是将位于该Dockerfile同一目录下的springCloudEureka-1.0.jar复制到容器的/microservice/目录下。
EXPOSE 8761
EXPOSE 暴露容器的端口,供外部使用。本例中是只注册中心服务springCloudEureka的端口,并确保对外能够访问。
ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", ……]
ENTRYPOINT 配置容器启动后只需的命令。本例中是启动Java程序。
4-3、docker-compose.yml定义
这个脚本中定义了一次性启停上述三个服务,并实现服务间的通信,脚本内容如下:
version: '3'
services:
#注册中心
#自定义的服务名称
register-center:
#镜像名称
image: register-center:v1
#容器名称
container_name: register-center
#容器down掉后,会自动重启
restart: always
#挂载一个目录或者一个已存在的数据卷容器。格式为宿主机目录:容器内目录
volumes:
- $PWD/register-center/register-center-1.0.jar:/microservice/
#映射端口。格式为宿主机端口:容器端口
ports:
- "8761:8761"
#容器启动后,执行的命令
command: java -Djava.security.egd=file:/dev/./urandom -jar /microservice/register-center-1.0.jar
#配置中心
config-center:
image: config-center:v1
container_name: config-center
restart: always
volumes:
- $PWD/config-center/config-center-1.0.jar:/microservice/
ports:
- "8888:8888"
command: java -Djava.security.egd=file:/dev/./urandom -jar /microservice/config-center-1.0.jar
depends_on:
- register-center
links:
- register-center:register-center
#网关服务
gateway:
image: gateway:v1
container_name: gateway
restart: always
volumes:
- $PWD/gateway/gateway-1.0.jar:/microservice/
ports:
- "8111:8111"
command: java -Djava.security.egd=file:/dev/./urandom -jar /microservice/gateway-1.0.jar
depends_on:
- register-center
- config-center
links:
- register-center:register-center
- config-center:config-center
4-4、编译、运行
将各个打包好的服务jar包 、Dockerfile脚本、 docker-compose.yml上传至docker服务器上,我是存放在如下结构的目录下,具体依个人习惯而定。
编译镜像
[docker@docker register-center]$ docker build -t gateway:v1 . // 注意最后的 . 一定要加上
Sending build context to Docker daemon 44.31MB
Step 1/6 : FROM java:8
---> d23bdf5b1b1b
Step 2/6 : RUN mkdir /microservice
---> Running in fcf4af033ac5
---> c0cf16047e86
Removing intermediate container fcf4af033ac5
Step 3/6 : WORKDIR /microservice
---> cc1fae4f23dc
Removing intermediate container d6fd78d760a9
Step 4/6 : ADD /register-center-1.0.jar /microservice/
---> ee8d0db56843
Removing intermediate container dd5b20c5e72c
Step 5/6 : EXPOSE 8761
---> Running in 46ae28623bd1
---> d02692f9c49c
Removing intermediate container 46ae28623bd1
Step 6/6 : ENTRYPOINT java -Djava.security.egd=file:/dev/./urandom -jar /microservice/register-center-1.0.jar
---> Running in cf63f2ca01af
---> 51431eb470e5
Removing intermediate container cf63f2ca01af
Successfully built 51431eb470e5
Successfully tagged register-center:v1
运行
在docker-compose.yml目录下,执行docker-compose up -d命令后台启动。
[root@localhost server-bomeiyi]# ll
总用量 4
-rw-r--r--. 1 root root 749 4月 16 17:41 docker-compose.yml
drwxrwxrwx. 4 root root 57 4月 16 17:59 images
[root@localhost server-bomeiyi]# docker-compose up -d
docker-compose 命令
启停命令
docker-compose up -d 全部后台启动
docker-compose stop xxx 停止某一个服务
docker-compose stop 停止所有服务
docker-compose start xxx启动某一个服务
docker-compose restart xxx 重启某一个服务
docker-compose stop xxx && docker-compose up -d --build xxx // 停止某个服务并重新打包某个镜像重新启动某个服务
其他命令
docker-compose config 查看当前yml文件中的所有配置
docker-compose rm xxx 删除某一个服务(容器)
docker-compose rm 删除所有服务(容器)
网友评论