完整报错
2020-10-28 17:28:18.716 ERROR 20924 --- [eoutChecker_1_1] i.s.c.rty.NettyClientChannelManager :
no available service 'default' found, please make sure registry config correct
原因及解决方案:参考seata官方FAQ(Frequently asked questions) :https://seata.io/zh-cn/docs/overview/faq.html
参考:https://blog.csdn/sinat_38670641/article/details/108237920
https://seata.io/zh-cn/docs/overview/what-is-seata.html
注:
- 将客户端先搭建起来, 我demo用的user服务跟order服务
- 只做seata服务端启动流程,不做解释,因为我也刚入门。。。只是搭建了好久才搭建成功,记录一下
- windows上搭建
我一直没解决的问题是:nacos-server-1.3.0相关配置有问题跟启动流程有问题
nacos1.3.0有文件默认是没有的
get start?
seata、nacos、springcloud、feign、sentinel 项目中使用到的组件,其中sentinel跟feign可以忽略,主要是seata整合nacos
一、数据库准备
客户端的数据库执行:
-- 注意此处0.7.0+ 增加字段 context
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,
PRIMARY KEY (`id`),
UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
服务端需要使用到的数据库执行:
-- the table to store GlobalSession data
drop table if exists `global_table`;
create table `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`)
);
-- the table to store BranchSession data
drop table if exists `branch_table`;
create table `branch_table` (
`branch_id` bigint not null,
`xid` varchar(128) not null,
`transaction_id` bigint ,
`resource_group_id` varchar(32),
`resource_id` varchar(256) ,
`lock_key` varchar(128) ,
`branch_type` varchar(8) ,
`status` tinyint,
`client_id` varchar(64),
`application_data` varchar(2000),
`gmt_create` datetime,
`gmt_modified` datetime,
primary key (`branch_id`),
key `idx_xid` (`xid`)
);
-- the table to store lock data
drop table if exists `lock_table`;
create table `lock_table` (
`row_key` varchar(128) not null,
`xid` varchar(96),
`transaction_id` long ,
`branch_id` long,
`resource_id` varchar(256) ,
`table_name` varchar(32) ,
`pk` varchar(36) ,
`gmt_create` datetime ,
`gmt_modified` datetime,
primary key(`row_key`)
);
最终数据库结构如下图:
二、修改file.conf和registry.conf配置
file.conf
主要是将store.mode改为由file改为db,然后进行相关store.db的修改,也就是数据库的配置
## transaction log store, only used in seata-server
store {
## store mode: file、db、redis
mode = "db"
## file store property
file {
## store location dir
dir = "sessionStore"
# branch session size , if exceeded first try compress lockkey, still exceeded throws exceptions
maxBranchSessionSize = 16384
# globe session size , if exceeded throws exceptions
maxGlobalSessionSize = 512
# file buffer size , if exceeded allocate new buffer
fileWriteBufferCacheSize = 16384
# when recover batch read size
sessionReloadReadSize = 100
# async, sync
flushDiskMode = async
}
## database store property
db {
## the implement of javax.sql.DataSource, such as DruidDataSource(druid)/BasicDataSource(dbcp)/HikariDataSource(hikari) etc.
datasource = "druid"
## mysql/oracle/postgresql/h2/oceanbase etc.
dbType = "mysql"
driverClassName = "com.mysql.jdbc.Driver"
url = "jdbc:mysql://127.0.0.1:3306/seata"
user = "root"
password = "123456"
minConn = 5
maxConn = 30
globalTable = "global_table"
branchTable = "branch_table"
lockTable = "lock_table"
queryLimit = 100
maxWait = 5000
}
## redis store property
redis {
host = "127.0.0.1"
port = "6379"
password = ""
database = "0"
minConn = 1
maxConn = 10
queryLimit = 100
}
}
registry.conf
主要是修改服务注册和服务发现type为nacos,然后修改相应的nacos的配置
registry {
# file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
type = "nacos"
nacos {
application = "seata-server"
serverAddr = "127.0.0.1:8848"
group = "SEATA_GROUP"
namespace = ""
cluster = "default"
username = ""
password = ""
}
eureka {
serviceUrl = "http://localhost:8761/eureka"
application = "default"
weight = "1"
}
redis {
serverAddr = "localhost:6379"
db = 0
password = ""
cluster = "default"
timeout = 0
}
zk {
cluster = "default"
serverAddr = "127.0.0.1:2181"
sessionTimeout = 6000
connectTimeout = 2000
username = ""
password = ""
}
consul {
cluster = "default"
serverAddr = "127.0.0.1:8500"
}
etcd3 {
cluster = "default"
serverAddr = "http://localhost:2379"
}
sofa {
serverAddr = "127.0.0.1:9603"
application = "default"
region = "DEFAULT_ZONE"
datacenter = "DefaultDataCenter"
cluster = "default"
group = "SEATA_GROUP"
addressWaitTime = "3000"
}
file {
name = "file.conf"
}
}
config {
# file、nacos 、apollo、zk、consul、etcd3
type = "nacos"
nacos {
serverAddr = "127.0.0.1:8848"
namespace = ""
group = "SEATA_GROUP"
username = ""
password = ""
}
consul {
serverAddr = "127.0.0.1:8500"
}
apollo {
appId = "seata-server"
apolloMeta = "http://192.168.1.204:8801"
namespace = "application"
}
zk {
serverAddr = "127.0.0.1:2181"
sessionTimeout = 6000
connectTimeout = 2000
username = ""
password = ""
}
etcd3 {
serverAddr = "http://localhost:2379"
}
file {
name = "file.conf"
}
}
三、进行nacos作为配置中心的配置
config.txt中是要执行的naocs配置项:
附在文件里了,下面有整合完成项目的github链接
执行nacos-config.sh,执行命令:
sh nacos-config.sh 127.0.0.1
执行结果:
然后可以看到在nacos中有了相应的配置:
四、服务端启动
启动就双击就行
五、客户端使用
1.引入依赖:
官方推荐引入依赖方式:
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<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>
2.yml配置
order服务的配置,user差不多
注:关注点主要是seata打头的配置
server:
port: 9000
spring:
cloud:
service-registry:
auto-registration:
failFast: false
enabled: true
register-management: true
sentinel:
transport:
port: 8719
dashboard: localhost:8080
eager: true
datasource:
ds2:
nacos:
data-id: order-sentinel
data-type: json
group-id: DEFAULT_GROUP
rule-type: flow
server-addr: localhost:8848
nacos:
discovery:
###服务注册地址
server-addr: 127.0.0.1:8848
enabled: true
config:
server-addr: 127.0.0.1:8848
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/alibaba_cloud_order?useSSL=false&useUnicode=true&characterEncoding=utf8&autoReconnect=true&rewriteBatchedStatements=TRUE&serverTimezone=Asia/Shanghai
username: root
password: 123456
type: com.alibaba.druid.pool.DruidDataSource
application:
name: server-order
seata:
application-id: ${spring.application.name}
tx-service-group: my_test_tx_group
config:
type: nacos
nacos:
#需要和server在同一个注册中心下
serverAddr: 127.0.0.1:8848
#需要server端(registry和config)、nacos配置client端(registry和config)保持一致
group: SEATA_GROUP
registry:
type: nacos
nacos:
#需要和server端保持一致,即server在nacos中的名称,默认为seata-server
application: seata-server
serverAddr: 127.0.0.1:8848
#需要server端(registry和config)、nacos配置client端(registry和config)保持一致
group: SEATA_GROUP
feign:
sentinel:
enabled: true
# spring-cloud-starter-alibaba-nacos-discovery 在实现的时候提供了一个EndPoint,
# EndPoint的访问地址为 http://ip:port/actuator/nacos-discovery。 EndPoint 的信息主要提供了两类:
#1、subscribe: 显示了当前有哪些服务订阅者
#2、NacosDiscoveryProperties: 显示了当前服务实例关于 Nacos 的基础配置
management:
endpoints:
web:
exposure:
include: '*'
3.在需要使用到分布式事务的地方加注解@GlobalTransactional
@GlobalTransactional
public String createOrder(Integer age) {
OrderInfo order = new OrderInfo().setAddress("address").setOrderNo("orderNo").setPhone("phone");
orderMapper.insert(order);
SysUser user = new SysUser().setPassword("password").setUserName("username");
userFeign.andUser(user);
int i = 1 / age;
return "create order success";
}
以上
注:只需要在创建订单添加@GlobalTransactional就可以了,user服务不需要
github:https://github/xiaxin07/xiaxin-spring-cloud-alibaba-sentinel-demo.git
更多推荐
【seata1.3.0】no available service ‘default‘ found, please make sure registry conf
发布评论