springcloud整合seata、nacos

官网下载 选择版本1.5.2
https://github.com/seata/seata/releases
修改seata/conf/application.yml文件内容:
server:
  port: 7091
 
spring:
  application:
    name: seata-server
 
logging:
  config: classpath:logback-spring.xml
  file:
    path: ./logs/seata
#  extend:
#    logstash-appender:
#      destination: 127.0.0.1:4560
#    kafka-appender:
#      bootstrap-servers: 127.0.0.1:9092
#      topic: logback_to_logstash
 
console:
  user:
    username: seata
    password: seata
 
seata:
  config:
    # support: nacos 、 consul 、 apollo 、 zk  、 etcd3
    type: nacos
    nacos:
      # nacos ip地址
      server-addr: 123.249.xx.xxx:8848
      group: dev
      namespace: dev
      username:
      password:
      dataId: seata-server.properties
  registry:
    # support: nacos 、 eureka 、 redis 、 zk  、 consul 、 etcd3 、 sofa
    type: nacos
    nacos:
      application: seata-server
      # nacos ip地址
      server-addr: 123.249.xx.xxx:8848
      group: dev
      namespace: dev
      cluster: default
      username:
      password:

  security:
    secretKey: SeataSecretKey0c382ef121d778043159209298fd40bf3850a017
    tokenValidityInMilliseconds: 1800000
    ignore:
      urls: /,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/api/v1/auth/login
创建seata数据库

seata库创建表 三张

CREATE TABLE `branch_table` (
  `branch_id` bigint(20) NOT NULL,
  `xid` varchar(128) COLLATE utf8mb4_hungarian_ci NOT NULL,
  `transaction_id` bigint(20) DEFAULT NULL,
  `resource_group_id` varchar(32) COLLATE utf8mb4_hungarian_ci DEFAULT NULL,
  `resource_id` varchar(256) COLLATE utf8mb4_hungarian_ci DEFAULT NULL,
  `lock_key` varchar(128) COLLATE utf8mb4_hungarian_ci DEFAULT NULL,
  `branch_type` varchar(8) COLLATE utf8mb4_hungarian_ci DEFAULT NULL,
  `status` tinyint(4) DEFAULT NULL,
  `client_id` varchar(64) COLLATE utf8mb4_hungarian_ci DEFAULT NULL,
  `application_data` varchar(2000) COLLATE utf8mb4_hungarian_ci DEFAULT NULL,
  `gmt_create` datetime DEFAULT NULL,
  `gmt_modified` datetime DEFAULT NULL,
  PRIMARY KEY (`branch_id`),
  KEY `idx_xid` (`xid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_hungarian_ci;

CREATE TABLE `global_table` (
  `xid` varchar(128) COLLATE utf8mb4_hungarian_ci NOT NULL,
  `transaction_id` bigint(20) DEFAULT NULL,
  `status` tinyint(4) NOT NULL,
  `application_id` varchar(32) COLLATE utf8mb4_hungarian_ci DEFAULT NULL,
  `transaction_service_group` varchar(32) COLLATE utf8mb4_hungarian_ci DEFAULT NULL,
  `transaction_name` varchar(128) COLLATE utf8mb4_hungarian_ci DEFAULT NULL,
  `timeout` int(11) DEFAULT NULL,
  `begin_time` bigint(20) DEFAULT NULL,
  `application_data` varchar(2000) COLLATE utf8mb4_hungarian_ci DEFAULT NULL,
  `gmt_create` datetime DEFAULT NULL,
  `gmt_modified` datetime DEFAULT NULL,
  PRIMARY KEY (`xid`),
  KEY `idx_gmt_modified_status` (`gmt_modified`,`status`),
  KEY `idx_transaction_id` (`transaction_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_hungarian_ci;

CREATE TABLE `lock_table` (
  `row_key` varchar(128) COLLATE utf8mb4_hungarian_ci NOT NULL,
  `xid` varchar(96) COLLATE utf8mb4_hungarian_ci DEFAULT NULL,
  `transaction_id` mediumtext COLLATE utf8mb4_hungarian_ci,
  `branch_id` mediumtext COLLATE utf8mb4_hungarian_ci,
  `resource_id` varchar(256) COLLATE utf8mb4_hungarian_ci DEFAULT NULL,
  `table_name` varchar(32) COLLATE utf8mb4_hungarian_ci DEFAULT NULL,
  `pk` varchar(36) COLLATE utf8mb4_hungarian_ci DEFAULT NULL,
  `gmt_create` datetime DEFAULT NULL,
  `gmt_modified` datetime DEFAULT NULL,
  PRIMARY KEY (`row_key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_hungarian_ci;
sc-order 业务库创建日志表 undo_log
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`),
  UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
seata-server.properties nacos配置文件
store.mode=db
store.lock.mode=db
store.session.mode=db
 
store.db.datasource=druid
store.db.dbType=mysql
store.db.driverClassName=com.mysql.cj.jdbc.Driver
store.db.url=jdbc:mysql://49.7.xx.xxx:20006/sc_dev_seata?useUnicode=true&rewriteBatchedStatements=true
store.db.user=sx-admin
store.db.password=xxxxxx
store.db.minConn=5
store.db.maxConn=30
store.db.globalTable=global_table
store.db.branchTable=branch_table
store.db.distributedLockTable=distributed_lock
store.db.queryLimit=100
store.db.lockTable=lock_table
store.db.maxWait=5000
 
service.vgroupMapping.default_tx_group=default
启动seata服务:

==如果报错/无法启动 : - 必须bin目录 ==

./seata-server.sh -h 123.249.xx.xxx
客户端添加seata依赖
<!--移除alibaba-seata自带的seata-spring-boot-starter,因为自带的版本太低 -->
<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>
<!--添加seata-spring-boot-starter依赖,版本需要与服务端版本相对应,此版本使用1.5.2 -->
<dependency>
    <groupId>io.seata</groupId>
    <artifactId>seata-spring-boot-starter</artifactId>
    <version>1.5.2</version>
</dependency>
客户端配置文件 -nacos配置文件
seata:
  # 默认关闭,如需启用spring.datasource.dynami.seata需要同时开启
  enabled: true
  # Seata 应用编号,默认为 ${spring.application.name}
  application-id: ${spring.application.name}
  # Seata 事务组编号,用于 TC 集群名。该配置需要与服务端提到的group相对应,也需要与下面的相对应 sc-order-web-group
  tx-service-group: default_tx_group  #  ${spring.application.name}-group
  # 关闭自动代理
  enable-auto-data-source-proxy: false
  # 服务配置项
  service:
    # 虚拟组和分组的映射
    vgroupMapping:
      default_tx_group: default    #该配置需要与服务端提到的group相对应
    grouplist:
      default: 123.249.xx.xxx:8091
  config:
    # support: nacos, consul, apollo, zk, etcd3
    type: nacos
    nacos:
      serverAddr: 123.249.xx.xxx:8848
      namespace: dev # 需要与服务端添加的配置文件相同
      group: dev
      username: nacos
      password: nacos
      dataId: seata-server.properties
  registry:
    # support: nacos, eureka, redis, zk, consul, etcd3, sofa
    type: nacos
    nacos:
      serverAddr: 123.249.xx.xxx:8848
      namespace: dev  # 需要与服务端添加的配置文件相同
      group: dev   # 需要与服务端添加的配置文件相同
      username: nacos
      password: nacos
      dataId: seata-server.properties
      
mybatis-plus:
  global-config:
    banner: false
  configuration:
    #jdbc-type-for-null: null
    map-underscore-to-camel-case: true
    auto-mapping-behavior: full
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.deltaphone.sc.material.domain.entity
手动代理 - 添加
@Configuration
@Data
public class DataSourceProxyConfig {

    @Value("${spring.datasource.url}")
    private String url;
    @Value("${spring.datasource.username}")
    private String userName;
    @Value("${spring.datasource.password}")
    private String password;
    @Value("${spring.datasource.driver-class-name}")
    private String driveClassName;

    @Bean("dataSource")
    public DataSource druidDataSource() {
        HikariDataSource hikariDataSource = new HikariDataSource();
        hikariDataSource.setJdbcUrl(url);
        hikariDataSource.setUsername(userName);
        hikariDataSource.setPassword(password);
        hikariDataSource.setDriverClassName(driveClassName);

        return hikariDataSource;
    }

    @Bean
    @Primary
    public DataSourceProxy dataSourceProxy(DataSource dataSource) {
        return new DataSourceProxy(dataSource);
    }
}
结果

1.服务端客户端分别在nacos有一个配置文件
在这里插入图片描述

2.需要这个配置
在这里插入图片描述
3.docker运行需要2个端口
7091 8091

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Cloud Alibaba是Spring Cloud的一个子项目,它是阿里巴巴微服务生态的重要组成部分,提供了一系列基于Spring Boot和Spring Cloud的微服务组件,如服务注册与发现、配置管理、负载均衡、服务调用、熔断器等。而Nacos作为一个新兴的服务发现和配置中心,可以方便地进行服务治理。 Spring Cloud Alibaba整合Nacos的过程相对简单,只需要引入相关依赖,并在代码中使用对应的注解进行配置即可。 首先,在pom.xml文件中添加以下依赖: ```xml <!-- Nacos Discovery --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <!-- Nacos Config --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency> ``` 然后,在启动类上使用@EnableDiscoveryClient注解启用Nacos服务注册与发现功能: ```java @SpringBootApplication @EnableDiscoveryClient public class DemoApplication{ public static void main(String[] args){ SpringApplication.run(DemoApplication.class,args); } } ``` 接下来就可以使用@NacosValue注解注入配置信息: ```java @RestController public class DemoController{ @NacosValue(value="${config.key}",autoRefreshed=true) private String configValue; @GetMapping("/config") public String getConfig(){ return configValue; } } ``` 这样就可以通过Nacos Config来动态修改配置信息了。另外,Spring Cloud Alibaba还提供了一些其他有用的组件,如Sentinel、Seata等,可以方便地进行服务治理和分布式事务管理。 总的来说,Spring Cloud Alibaba整合Nacos是一个极为方便且实用的方式,它可以大大简化微服务应用的开发和部署,提高了系统的可靠性和可维护性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值