Sorry, it's been a long time. Maybe springCloud won't be updated again. Let's learn from springCloud Alibaba

Why not update spring cloud

With the iterative update of technology, the corresponding components of springCloud are also being updated and the update service is being stopped. Therefore, in order to address this situation, most domestic enterprises choose to prepare in advance, so springCloudAlibaba was born

Advantages of spring cloud Alibaba

Spring Cloud Alibaba is actually Alibaba's microservice solution. It is Alibaba's open-source microservice family bucket combined with its own microservice practice. It has been incubated into a sub project of Spring Cloud in the Spring Cloud project. Many components in the first generation Spring Cloud standard have been stopped, such as Eureak,zuul, etc. Therefore, Spring Cloud Alibaba is likely to become the standard implementation of the second generation of Spring Cloud, so many components are gradually used in the industry, and there are many successful cases. It is worth mentioning that Spring Cloud Alibaba is very compatible with Dubbo, and also provides some powerful functions, such as Sentinel flow control, Seata distributed transaction, Nacos service discovery and registration, etc.

Five components of spring cloud Alibaba

Sentinel: take the flow as the entry point to protect the stability of services from multiple dimensions such as flow control, fuse degradation and system load protection.
Nacos: a dynamic service discovery, configuration management and service management platform that is easier to build cloud native applications.
RocketMQ: an open source distributed messaging system, based on highly available distributed cluster technology, provides low latency and highly reliable message publishing and subscription services.
Dubbo: Apache Dubbo ™ Is a high-performance Java RPC framework.
Seata: Alibaba open source product, an easy-to-use high-performance microservice distributed transaction solution.
Alibaba Cloud OSS: Alibaba cloud Object Storage Service (OSS) is a massive, secure, low-cost and highly reliable cloud storage service provided by Alibaba cloud. You can store and access any type of data in any application, anytime and anywhere.
Alibaba cloud scheduler X: a distributed task scheduling product developed by Alibaba middleware team, which provides second level, accurate, highly reliable and highly available timed (based on Cron expression) task scheduling services.
Alibaba Cloud SMS: a global SMS service with friendly, efficient and intelligent interconnected communication capabilities to help enterprises quickly build customer access channels.

Next, let me talk about rocketMQ in spring cloud Alibaba

Firstly, rocketMQ is divided into server and client. The client sends messages to the server and the server processes messages. When it comes to this, many small partners will think of transactions. Because it is a distributed framework, there must be problems in transaction processing. At this time, the role of rocketMQ comes

		JSONObject jsonObject = new JSONObject();
        		   jsonObject.put("accountChange","Things that need business");
        Message<String> message = MessageBuilder.withPayload(jsonObject.toJSONString()).build();
        // Send a json message to the object to be processed by the rocketMQ server
        TransactionSendResult ta = rocketMQTemplate.sendMessageInTransaction("producer_group_txmsg_bank1","topic_txmsg",message,null);

Then we will now process the object json message in the service monitored by the rocketMQ server

@Component
@RocketMQMessageListener(consumerGroup = "consumer_group_txmsg_bank2" ,topic = "topic_txmsg")
public class receptionRocketMQ implements RocketMQListener<String> {
    @Override
    public void onMessage(String s) {
 		// TODO is used to process transactions and insert a transaction number information into the database
    }
}

After the server has processed differential transaction 1, we return to the rocketMQ client and process transaction 2 in the listener listening for sending information

Component
@RocketMQTransactionListener(txProducerGroup = "producer_group_txmsg_bank1")
public class ProducerTxmsgListener implements RocketMQLocalTransactionListener {
    // This method is called back after the message is sent successfully. This method executes local transactions
//    @Override
    public RocketMQLocalTransactionState executeLocalTransaction(Message message, Object arg) {
		// After updating the version, the checkLocalTransaction method will not work, so we uniformly execute, rollback and commit transactions in this method
		// TODO first sets the object String jsonString = new String((byte[]) message.getPayload());
		//              JSONObject jsonObject = JSONObject.parseObject(jsonString);
		// Take out the transaction number stored in the object, take out the query transaction number, and verify whether transaction 1 is successfully executed. If transaction 1 does not have a transaction number, it means that transaction 1 has been rolled back, and there is no need to carry out transaction 2. Directly return the status return rocketmqllocaltransactionstate.rollback
		// If transaction 1 is successful and the transaction number is valid, we will perform the relevant operations of transaction 2 here. If the transaction is completed without exception, rocketmqllocaltransactionstate.commit will be returned. Otherwise, rocketmqllocaltransactionstate.rollback will be returned and recorded in the database
        return RocketMQLocalTransactionState.COMMIT;
    }

    // This method views the service transaction execution status
    @Override
    public RocketMQLocalTransactionState checkLocalTransaction(Message message) {
        RocketMQLocalTransactionState state;
        final JSONObject jsonObject = JSON.parseObject(new String((byte[]) message.getPayload()));
        AccountChangeEvent accountChangeEvent = JSONObject.parseObject(jsonObject.getString("accountChange"),AccountChangeEvent.class);
        //Transaction id
        String txNo = accountChangeEvent.getTxNo();
        int isexistTx = accountInfoDao.isExistTx(txNo);
        log.info("Back check transaction number: {} result: {}", accountChangeEvent.getTxNo(),isexistTx);
        if(isexistTx>0){
            state= RocketMQLocalTransactionState.COMMIT;
        }else{
            state= RocketMQLocalTransactionState.UNKNOWN;
        }
        state= RocketMQLocalTransactionState.COMMIT;
        return state;

    }
}

Finally, check the status in the database, judge whether the total transaction is completed, and query its status to return
Well, the transaction module of spring cloud Alibaba ends here. See you next time

Keywords: Java

Added by Wales on Wed, 08 Sep 2021 07:21:44 +0300