プロジェクトコンポーネント
シータに関連するコンポーネント:
- 登録センター:領事
- ゲートウェイ:ズール
- マイクロサービスコール:偽物
- データベースミドルウェア:mybatis(単一インスタンス、読み取りと書き込みの分離なし)
プロジェクトの要件
複数のマイクロサービスには協調的な操作があり、データの整合性を確保するにはトランザクション制御が必要です
構成統合方法
- Seataをインストールします:githubSeataにはエージェントがありません。添付の列に添付ファイルを提供します
- レジストリを構成します。registry.confで、レジストリをconsulとして選択し、独自のマイクロサービス名:microservice-seata-server(カスタマイズ可能)
- Seataデータベースの構成:registry.confで、構成するファイルを選択し、name = "file.conf"、file.confのストア属性としてdbを選択し、dbでデータベース情報を構成します。
- Seataを起動して登録センターに登録し、データベースに接続します。ここでWindowsバージョンでテストしました。seata-server.batの起動時にローカルIPを追加することをお勧めします。そうしないと、seataはそれをクライアントアドレスとして認識します。起動時の追加パラメータ-h $ {ip}に対応するローカルエリアネットワーク(172. *)を使用します。
- マイクロサービスspringbootプロジェクトのメインクラス変更アノテーション(データソース動的プロキシ)
- データベース構成クラスを作成する
支払う
データソースの動的構成クラス
/**
* 数据源代理
*/
@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();
}
スタートアップクラスに次のアノテーションを追加します
@Import(DataSourceConfig.class)// DataSourceConfigは上記の構成ファイル
であると同時に、@ SpringBootApplicationアノテーションのデータソースの自動ロードを除外します
@SpringBootApplication(exclude =(DataSourceAutoConfiguration.class ))
pomの依存関係
<!-- 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>
アプリケーション増加構成
構成のこの部分は、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
`