seata integrated consul configuration

Project components

Components related to seata:

  • Registration Center: consul
  • Gateway: zuul
  • Microservice call: feign
  • Database middleware: mybatis (single instance, no read-write separation)

Project requirements

Multiple microservices have collaborative operations, and transaction control is required to ensure data consistency

Configuration integration method

  1. Install seata: githubSeata does not have an agent, I provide attachments in the attached column
  2. Configure the registry: In registry.conf, select the registry as consul, and your own microservice name: microservice-seata-server (customizable)
  3. Configure seata database: In registry.conf, select file for configuration, name= "file.conf", select db for store attribute in file.conf, and configure database information in db
  4. Start seata to register to the registration center, connect to the database. I tested it here for the windows version. It is best to add the local ip when seata-server.bat is started. Otherwise, seata recognizes as the client address and will use the local area network (172.*), corresponding to the startup Add parameter -h ${ip}
  5. Microservice springboot project main class modification annotation (data source dynamic proxy)
  6. Write database config class

to pay

Data source dynamic configuration class

/**
 * 数据源代理
 */
@Configuration
public class DataSourceConfiguration {
    
    

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource druidDataSource(){
    
    
        DruidDataSource druidDataSource = new DruidDataSource();
        return druidDataSource;
    }

    @Primary
    @Bean("dataSource")
    public DataSourceProxy dataSource(DataSource druidDataSource){
    
    
        return new DataSourceProxy(druidDataSource);
    }

    @Bean
    public SqlSessionFactory sqlSessionFactory(DataSourceProxy dataSourceProxy)throws Exception{
    
    
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSourceProxy);
        sqlSessionFactoryBean.setMapperLocations(
            					new PathMatchingResourcePatternResolver()
        										.getResources("classpath*:/mapper/*.xml"));
        sqlSessionFactoryBean.setTransactionFactory(new SpringManagedTransactionFactory());
        return sqlSessionFactoryBean.getObject();
    }


Add the following annotation on the startup class
@Import(DataSourceConfig.class) //DataSourceConfig is the above configuration file and
at the same time exclude the automatic loading of the data source on the @SpringBootApplication annotation
@SpringBootApplication(exclude = (DataSourceAutoConfiguration.class))

pom dependency

 <!-- https://mvnrepository.com/artifact/io.seata/seata-spring-boot-starter -->
        <dependency>
            <groupId>io.seata</groupId>
            <artifactId>seata-spring-boot-starter</artifactId>
            <version>1.4.0</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
            <version>2.2.1.RELEASE</version>
            <exclusions>
                <exclusion>
                    <artifactId>io.seata</artifactId>
                    <groupId>seata-spring-boot-starter</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- api需至1.4.5版本 -->
        <dependency>
            <groupId>com.ecwid.consul</groupId>
            <artifactId>consul-api</artifactId>
            <version>1.4.5</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.3</version>
        </dependency>

application increase configuration

This part of the configuration can also be written in file.conf

seata:
  enabled: true
  application-id: applicationName
  tx-service-group: microservice-zgh-fescar-service-group
  enable-auto-data-source-proxy: false
  use-jdk-proxy: false
  registry:
    type: consul
    consul:
      cluster: microservice-seata-server
      server-addr: consul-server.zgh-dev.svc.cluster.local:8500
#  config:
#    file:
#      name: file.conf
  transport:
    # tcp, unix-domain-socket
    type: tcp
    #NIO, NATIVE
    server: nio
    #enable heartbeat
    heartbeat: true
    # the client batch send request enable
    enable-client-batch-send-request: false
    #thread factory for netty
    thread-factory:
      boss-thread-prefix: NettyBoss
      worker-thread-prefix: NettyServerNIOWorker
      server-executor-thread-prefix: NettyServerBizHandler
      share-boss-worker: false
      client-selector-thread-prefix: NettyClientSelector
      client-selector-thread-size: 1
      client-worker-thread-prefix: NettyClientWorkerThread
      # netty boss thread size
      boss-thread-size: 1
      #auto default pin or 8
      worker-thread-size: default
    # when destroy server, wait seconds
    shutdown:
      wait: 3
    serialization: seata
    compressor: none
  service:
    vgroup-mapping:
      microservice-zgh-fescar-service-group: default
    grouplist:
      zgh-dev-consul.sco.inossem.com:30820: default
    enable-degrade: true
    disable-global-transaction: false

`

Guess you like

Origin blog.csdn.net/gsh6022/article/details/110038447