<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          springcloud整合分布式事務(wù)LCN

          共 11708字,需瀏覽 24分鐘

           ·

          2020-12-24 00:48

          點(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è)中心

          a、pom文件

          ?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?????

          b、properties文件

          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

          c、在啟動(dòng)類(lèi)上加上@EnableEurekaServer注解

          @SpringBootApplication
          @EnableEurekaServer
          public?class?SpringcloudEurekaApplication?{
          ?
          ????public?static?void?main(String[]?args)?{
          ????????SpringApplication.run(SpringcloudEurekaApplication.class,?args);
          ????}
          ?
          }

          d、啟動(dòng)該服務(wù)

          二、創(chuàng)建微服務(wù)A

          a、pom文件

          ?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?

          b、properties文件

          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

          c、在啟動(dòng)類(lèi)上添加@EnableEurekaClient注解

          d、創(chuàng)建entity

          ?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?}

          e、創(chuàng)建dao

          public?interface?OrderDao?extends?JpaRepository?{
          }


          f、創(chuàng)建service

          ?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?}

          g、創(chuàng)建feign調(diào)用

          ?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?}

          h、在啟動(dòng)類(lèi)上添加@EnableFeignClients注解

          i、創(chuàng)建web

          @RestController
          public?class?OrdersController?{
          ?
          ????@Autowired
          ????private?OrderService?orderService;
          ?
          ????@RequestMapping("/addOrders")
          ????public?Object?addOrders(){
          ????????return?orderService.addOrder();
          ????}
          }

          三、創(chuàng)建微服務(wù)B

          a、pom文件

          ?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?

          b、properties文件

          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

          c、在啟動(dòng)類(lèi)上添加@EnableEurekaClient注解

          d、創(chuàng)建entity

          ?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?}

          e、創(chuàng)建dao

          public?interface?TbInventoryDao?extends?JpaRepository?{
          }


          f、創(chuàng)建service

          ?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?}

          g、創(chuàng)建web

          @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

          a、下載地址:https://github.com/codingapi/tx-lcn/releases

          (此處下載的是版本5.0.2.RELEASE)

          ?b、將項(xiàng)目txlcn-tm導(dǎo)入idea,并修改properties文件

          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=

            • #?給出信息都是默認(rèn)值
              關(guān)于詳細(xì)配置說(shuō)明見(jiàn)TM配置

            • application.properties 加載順序如下:
              0、命令行啟動(dòng)參數(shù)指定
              1、file:./config/(當(dāng)前jar目錄下的config目錄)
              2、file:./(當(dāng)前jar目錄)
              3、classpath:/config/(classpath下的config目錄)
              4、classpath:/(classpath根目錄)
              發(fā)布的二進(jìn)制可執(zhí)行Jar包含一個(gè)默認(rèn)配置文件(也就是4),可按需要覆蓋默認(rèn)配置

          c、創(chuàng)建MySQL數(shù)據(jù)庫(kù), 名稱(chēng)為: tx-manager、然后創(chuàng)建數(shù)據(jù)表

          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;

          d、將項(xiàng)目改造成springcloud項(xiàng)目,并注冊(cè)到eureka中

          ?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?????????

          注意:為了防止版本沖突需要知道springboot的版本為2.1.x,并且設(shè)置springcloud的版本為Finchley.RELEASE,還要在properties文件中新增配置:spring.cloud.compatibility-verifier.enabled=false

          c、將項(xiàng)目注冊(cè)到eureka中

          eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

          d、在啟動(dòng)類(lèi)上添加@EnableEurekaClient注解

          五、開(kāi)始整合

          a、在服務(wù)A、B的pom文件中引入坐標(biāo)

          ?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?????????

          注意:為了防止springboot的jar包沖突,需要將里面關(guān)于springboot的所有jar包排除掉。

          b、在服務(wù)A、B的啟動(dòng)類(lèi)上添加注解@EnableDistributedTransaction啟動(dòng)分布式事務(wù)

          c、在服務(wù)A中的需要分布式事務(wù)方法上面添加@LcnTransaction,服務(wù)B中添加@TxcTransaction注解。

          六、啟動(dòng)、測(cè)試

          a、依次啟動(dòng)eureka注冊(cè)中心,txlcn-tm,服務(wù)A、B

          ?

          ?b、調(diào)用服務(wù)




          粉絲福利:Java從入門(mén)到入土學(xué)習(xí)路線(xiàn)圖

          ???

          ?長(zhǎng)按上方微信二維碼?2 秒


          感謝點(diǎn)贊支持下哈?

          瀏覽 39
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  欧美色图中文字幕 | www.操屄 | 免费在线观看黄色视频网站 | 日本影片一区 | 日本黄色A片免费看 |