Project Description:

Taking the e-commerce project as an example, it is divided into order service, user service and inventory service. Each service is built by SpringBoot, registered between services by NACOS, called between services by Feign, demoted by Hystrix, and guaranteed by SeATA strong consistency of distributed transactions

Preparation:

  1. Code address: github.com/yunmengmeng…
  2. Create a new database and import the initial. SQL file under sD-order
  3. Gateway will be added as a gateway in the future

Start the nacos

Download version 2.0.3 of nacOS

Address: github.com/alibaba/nac…

Configuring the Database

After decompressing, go to the conf directory, import nacos-mysql. SQL into the database, and modify the database configuration of application.properties

Start the

Into the bin directory, execute. \ startup. CMD -m standalone, after the success of the start, enter the address http://localhost:8848/nacos, account password for nacos

The service registry

The introduction of the jar package

<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> . < version > 2.0.3 RELEASE < / version > < / dependency >Copy the code

Yml configuration

spring: application: name: sd-account cloud: nacos: discovery: server-addr: 127.0.0.1:8848 # Defaults to true. If the property is false, nacOS registerEnabled: true is not registeredCopy the code

Start the

After starting the project, you can see the service registered in the NACOS center

The other projects are registered with nacOS center in the following steps

Service configuration

The introduction of the jar package

<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> . < version > 2.0.3 RELEASE < / version > < / dependency >Copy the code

Yml configuration

Create a new bootstrap.yml and write the following configuration

Spring: cloud: nacos: config: server-addr: 127.0.0.1:8848 # file-extension: yaml # sd-accountCopy the code

Nacos configuration

Added the configuration in the NACOS configuration list

Project configuration

Create a new Person class and write the following code

After the project starts, you can see the console output Person(name= Zhangsan, age=14)

At this point, nacOS integration is complete

Feign and Hystrix

Feign

The introduction of the jar package

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
Copy the code

Enable Feign

Annotate @enableFeignClients in the startup or configuration class

Interservice invocation

This section uses the order service class to invoke the user service class as an example to add the AccountClient class

@feignClient (name = "sd-account") public interface AccountClient {@getMapping ("/ Decrease ") String decrease(@RequestParam Long userId, @RequestParam BigDecimal money); }Copy the code

Hystrix

Hystrix is used for fuses and downgrades, and Feign’s JAR contains Hystrix related parts without having to reference the JAR package

Enable Hystrix

feign:
  hystrix:
    enabled: true
Copy the code

Project configuration

Create a new Class AccountClientHystrix and write the following code

@Component public class AccountClientHystrix implements AccountClient { @Override public String decrease(Long userId, BigDecimal money) { return "invoke failed"; }}Copy the code

Specify AccountClientHystrix as the fuse class

@FeignClient(name = "sd-account", fallback = AccountClientHystrix.class)
Copy the code

When the call times out or the SD-Account service is not started, a fallback is triggered and the AccountClientHystrix-> Decrease () method is eventually called.

Seata

Start the Seata

Download version 1.4.0 of Seata-Server

Address: github.com/seata/seata…

After decompressing, go to the conf directory, open the registry. Conf file, change the type attribute to nacos, and change the application name to seata under nacOS configuration

Go to the bin directory and start the script

Configuring the Database

Execute seata-initial.sql in sD-order. This article uses only one library. If there are multiple databases, this file will be imported under each library

The introduction of the jar package

<! --> <dependency> <groupId>com.alibaba. Cloud </groupId> <artifactId> Spring-cloud-baba-seata </artifactId> <version>${spring-cloud-alibaba-seata.version}</version> </dependency> <! -- Connect to TC, <dependency> <groupId> IO. Seata </groupId> <artifactId>seata-all</artifactId> <version>${seata.version}</version> </dependency>Copy the code

Project configuration

  • Add the name of the transaction group in each project configuration file

  • Copy the Registry. Conf of Seata to the Resources directory of each project

  • Modify the file.conf file under each project. Note the vgroupmapping.my_test_tx_group = “default” property, where my_test_tx_group is configured in the yML file. Default is the cluster name of seata. Nacos

  • Creating distributed data sources

Distributed transaction

Declare distributed transactions

The OrderServiceImpl class method adds the @GlobalTransactional annotation

Call interface

Called http://localhost:8083/create? UserId = 1&productid =1&count=10&money=100. If the order table generates records and the number of accounts and storages decreases, the submission is successful

Note the self-increment of the primary key of the ORDER table

Release the line annotation of OrderServiceImpl, call the interface again, and return failure

SQL > select * from order table; SQL > select * from ORDER table

Database storage

By default, seata is stored as a file. If you want to store it as a database, you need to edit file.conf in the bin directory and set it to DB. At the same time, run seata-db.sql in the SD-order project