springcloud整合分布式事務(wù)LCN
點(diǎn)擊上方藍(lán)色字體,選擇“標(biāo)星公眾號(hào)”
優(yōu)質(zhì)文章,第一時(shí)間送達(dá)
? 作者?|??Neil-King
來(lái)源 |? urlify.cn/ZRruey
66套java從入門(mén)到精通實(shí)戰(zhàn)課程分享
一、創(chuàng)建eureka注冊(cè)中心
?1?
?2?????????1.8
?3?????????Greenwich.SR2
?4?????
?5?
?6?????
?7?????????
?8?????????????org.springframework.boot
?9?????????????spring-boot-starter-web
10?????????
11?????????
12?????????????org.springframework.cloud
13?????????????spring-cloud-starter-netflix-eureka-server
14?????????
15?
16?????????
17?????????????org.springframework.boot
18?????????????spring-boot-starter-test
19?????????????test
20?????????
21?????
22?
23?????
24?????????
25?????????????
26?????????????????org.springframework.cloud
27?????????????????spring-cloud-dependencies
28?????????????????${spring-cloud.version}
29?????????????????<type>pomtype>
30?????????????????import
31?????????????
32?????????
33?????server.port=8761
?
#禁止將自己注冊(cè)到注冊(cè)中心
eureka.client.register-with-eureka=false
eureka.instance.hostname=localhost
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
eureka.server.enable-self-preservation=false
spring.cloud.config.discovery.enabled=true
@SpringBootApplication
@EnableEurekaServer
public?class?SpringcloudEurekaApplication?{
?
????public?static?void?main(String[]?args)?{
????????SpringApplication.run(SpringcloudEurekaApplication.class,?args);
????}
?
}
二、創(chuàng)建微服務(wù)A
?1?
?2?????????1.8
?3?????????Greenwich.SR2
?4?????
?5?
?6?????
?7?????????
?8?????????????org.springframework.boot
?9?????????????spring-boot-starter-data-jpa
10?????????
11?????????
12?????????????org.springframework.boot
13?????????????spring-boot-starter-web
14?????????
15?????????
16?????????????org.springframework.cloud
17?????????????spring-cloud-starter-netflix-eureka-client
18?????????
19?
20?????????
21?????????????mysql
22?????????????mysql-connector-java
23?????????????runtime
24?????????
25?????????
26?????????????org.springframework.boot
27?????????????spring-boot-starter-test
28?????????????test
29?????????
30?????????
31?????????????com.alibaba
32?????????????druid
33?????????????1.0.9
34?????????
35?????????
36?????????????org.springframework.cloud
37?????????????spring-cloud-starter-openfeign
38?????????
39?
spring.application.name=springcloud-aservice
server.port=8080
?
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
?
#spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://localhost:3306/yzh?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
?
#################jpa配置####################
spring.jpa.database=mysql
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
?1?@Entity
?2?public?class?Orders?implements?Serializable{
?3?
?4?????private?static?final?long?serialVersionUID?=?3295617400035010415L;
?5?
?6?????@Id
?7?????@GeneratedValue(strategy?=?GenerationType.IDENTITY)
?8?????private?Integer?orderId;
?9?
10?????private?Integer?itemId;
11?
12?????private?Integer?price;
13?
14?????public?Integer?getOrderId()?{
15?????????return?orderId;
16?????}
17?
18?????public?void?setOrderId(Integer?orderId)?{
19?????????this.orderId?=?orderId;
20?????}
21?
22?????public?Integer?getItemId()?{
23?????????return?itemId;
24?????}
25?
26?????public?void?setItemId(Integer?itemId)?{
27?????????this.itemId?=?itemId;
28?????}
29?
30?????public?Integer?getPrice()?{
31?????????return?price;
32?????}
33?
34?????public?void?setPrice(Integer?price)?{
35?????????this.price?=?price;
36?????}
37?}
public?interface?OrderDao?extends?JpaRepository?{
}
?1?@Service
?2?public?class?OrderService?{
?3?
?4?????@Autowired
?5?????private?OrderDao?orderDao;
?6?????@Autowired
?7?????private?InventoryService?inventoryService;
?8?
?9?????@Transactional
10?????public?Orders?addOrder(){
11?????????Orders?orders?=?new?Orders();
12?????????orders.setItemId(100);
13?????????orders.setPrice(2000);
14?
15?????????Orders?save?=?orderDao.save(orders);
16?
17?????????inventoryService.updateInventory(100,9);
18?
19?????????return?save;
20?????}
21?}
?1?@FeignClient(value?=?"springcloud-bservice",fallback?=?InventoryServiceFallback.class)
?2?public?interface?InventoryService?{
?3?
?4?????@GetMapping(value?=?"updateInventory")
?5??????Object?updateInventory(@RequestParam(value?=?"itemId")?Integer?itemId,@RequestParam(value?=?"itemNum")?Integer?itemNum);
?6?
?7?}
?8?
?9?//回調(diào)類(lèi)
10?public?class?InventoryServiceFallback?implements?InventoryService{
11?????@Override
12?????public?Object?updateInventory(Integer?itemId,?Integer?itemNum)?{
13?????????return?0;
14?????}
15?}
@RestController
public?class?OrdersController?{
?
????@Autowired
????private?OrderService?orderService;
?
????@RequestMapping("/addOrders")
????public?Object?addOrders(){
????????return?orderService.addOrder();
????}
}
三、創(chuàng)建微服務(wù)B
?1?
?2?????????1.8
?3?????????Greenwich.SR2
?4?????
?5?
?6?????
?7?????????
?8?????????????org.springframework.boot
?9?????????????spring-boot-starter-data-jpa
10?????????
11?????????
12?????????????org.springframework.boot
13?????????????spring-boot-starter-web
14?????????
15?????????
16?????????????org.springframework.cloud
17?????????????spring-cloud-starter-netflix-eureka-client
18?????????
19?
20?????????
21?????????????mysql
22?????????????mysql-connector-java
23?????????????runtime
24?????????
25?????????
26?????????????org.springframework.boot
27?????????????spring-boot-starter-test
28?????????????test
29?????????
30?
spring.application.name=springcloud-bservice
server.port=8081
?
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
?
#spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://localhost:3306/yzh?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
?
#################jpa配置####################
spring.jpa.database=mysql
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
?1?@Entity
?2?@Table(name?=?"tb_inventory")
?3?public?class?TbInventory?implements?Serializable{
?4?????private?static?final?long?serialVersionUID?=?4171468306443543867L;
?5?
?6?????@Id
?7?????@GeneratedValue(strategy?=?GenerationType.IDENTITY)
?8?????private?Integer?inventoryId;
?9?
10?????private?Integer?itemId;
11?
12?????private?Integer?itemnum;
13?
14?????public?Integer?getInventoryId()?{
15?????????return?inventoryId;
16?????}
17?
18?????public?void?setInventoryId(Integer?inventoryId)?{
19?????????this.inventoryId?=?inventoryId;
20?????}
21?
22?????public?Integer?getItemId()?{
23?????????return?itemId;
24?????}
25?
26?????public?void?setItemId(Integer?itemId)?{
27?????????this.itemId?=?itemId;
28?????}
29?
30?????public?Integer?getItemnum()?{
31?????????return?itemnum;
32?????}
33?
34?????public?void?setItemnum(Integer?itemnum)?{
35?????????this.itemnum?=?itemnum;
36?????}
37?}
public?interface?TbInventoryDao?extends?JpaRepository?{
}
?1?@Service
?2?public?class?TbInventoryService?{
?3?
?4?????@Autowired
?5?????private?TbInventoryDao?tbInventoryDao;
?6?
?7?
?8?????@Transactional
?9?????public?TbInventory?updateInventory(Integer?itemId,Integer?itemNum){
10?????????TbInventory?tbInventory?=?new?TbInventory();
11?????????tbInventory.setItemId(itemId);
12?????????tbInventory.setItemnum(itemNum);
13?????????TbInventory?save?=?tbInventoryDao.save(tbInventory);
14?????????System.err.println(1/0);
15?????????return??save;
16?
17?????}
18?}
@RestController
public?class?TbInventoryController?{
?
????@Autowired
????private?TbInventoryService?tbInventoryService;
?
????@GetMapping("updateInventory")
????public?Object?updateInventory(@RequestParam?Integer?itemId,?@RequestParam?Integer?itemNum){
????????return?tbInventoryService.updateInventory(itemId,itemNum);
????}
}
四、引入LCN
spring.application.name=tx-manager
server.port=7970
?
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/tx-manager?characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=root
?
mybatis.configuration.map-underscore-to-camel-case=true
mybatis.configuration.use-generated-keys=true
?
#tx-lcn.logger.enabled=true
#?TxManager?Host?Ip
#tx-lcn.manager.host=127.0.0.1
#?TxClient連接請(qǐng)求端口
#tx-lcn.manager.port=8070
#?心跳檢測(cè)時(shí)間(ms)
#tx-lcn.manager.heart-time=15000
#?分布式事務(wù)執(zhí)行總時(shí)間
#tx-lcn.manager.dtx-time=30000
#參數(shù)延遲刪除時(shí)間單位ms
#tx-lcn.message.netty.attr-delay-time=10000
#tx-lcn.manager.concurrent-level=128
#?開(kāi)啟日志
#tx-lcn.logger.enabled=true
#logging.level.com.codingapi=debug
#redis?主機(jī)
#spring.redis.host=127.0.0.1
#redis?端口
#spring.redis.port=6379
#redis?密碼
#spring.redis.password=
CREATE?TABLE?`t_tx_exception`??(
??`id`?bigint(20)?NOT?NULL?AUTO_INCREMENT,
??`group_id`?varchar(64)?CHARACTER?SET?utf8mb4?COLLATE?utf8mb4_general_ci?NULL?DEFAULT?NULL,
??`unit_id`?varchar(32)?CHARACTER?SET?utf8mb4?COLLATE?utf8mb4_general_ci?NULL?DEFAULT?NULL,
??`mod_id`?varchar(128)?CHARACTER?SET?utf8mb4?COLLATE?utf8mb4_general_ci?NULL?DEFAULT?NULL,
??`transaction_state`?tinyint(4)?NULL?DEFAULT?NULL,
??`registrar`?tinyint(4)?NULL?DEFAULT?NULL,
??`remark`?varchar(4096)?NULL?DEFAULT??NULL,
??`ex_state`?tinyint(4)?NULL?DEFAULT?NULL?COMMENT?'0?未解決?1已解決',
??`create_time`?datetime(0)?NULL?DEFAULT?NULL,
??PRIMARY?KEY?(`id`)?USING?BTREE
)?ENGINE?=?InnoDB?AUTO_INCREMENT?=?1?CHARACTER?SET?=?utf8mb4?COLLATE?=?utf8mb4_general_ci?ROW_FORMAT?=?Dynamic;
?1??
?2?????????1.8
?3?????????Finchley.RELEASE
?4?????????2.1.7.RELEASE
?5?????
?6?
?7?
?8?????????????org.springframework.boot
?9?????????????spring-boot-starter-data-redis
10?????????????${spring-boot.version}
11?????????
12?
13?????????
14?????????????org.springframework.boot
15?????????????spring-boot-starter-mail
16?????????????${spring-boot.version}
17?????????
18??
19?????????????org.springframework.boot
20?????????????spring-boot-starter-data-jpa
21?????????????${spring-boot.version}
22?????????
23?????????
24?????????????org.springframework.cloud
25?????????????spring-cloud-starter-netflix-eureka-server
26?????????
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
五、開(kāi)始整合
?1?
?2?????????????com.codingapi.txlcn
?3?????????????txlcn-tc
?4?????????????5.0.2.RELEASE
?5?????????????
?6?????????????????
?7?????????????????????org.springframework.boot
?8?????????????????????*
?9?????????????????
10?????????????
11?????????
12?
13?????????
14?????????????com.codingapi.txlcn
15?????????????txlcn-txmsg-netty
16?????????????5.0.2.RELEASE
17?????????????
18?????????????????
19?????????????????????org.springframework.boot
20?????????????????????*
21?????????????????
22?????????????
23?????????
六、啟動(dòng)、測(cè)試
粉絲福利:Java從入門(mén)到入土學(xué)習(xí)路線(xiàn)圖
???

?長(zhǎng)按上方微信二維碼?2 秒
感謝點(diǎn)贊支持下哈?
評(píng)論
圖片
表情




