Talking about Spring Cloud Alibaba

1. What is Spring Cloud Alibaba

Spring Cloud Alibaba is a solution for microservice development. It contains the necessary components to develop distributed application microservices, and developers can easily use these components to develop distributed application services through the Spring Cloud programming model.

2. What it can do

It mainly provides the following functions:

  • Service current limiting and downgrading: By default, it supports the access of WebServlet, WebFlux, OpenFeign, RestTemplate, Spring Cloud Gateway, Dubbo and RocketMQ current limiting and downgrading functions. You can modify the current limiting and downgrading rules in real time through the console at runtime, and also supports viewing the current limiting and downgrading Metrics monitoring.
  • Service registration and discovery: Adapt to the Spring Cloud service registration and discovery standards, and the default integration corresponds to the adaptation of the load balancing components supported by the Spring Cloud version.
  • Distributed configuration management: supports external configuration in distributed systems, and automatically refreshes when configuration changes.
  • Message-driven capability: Build message-driven capabilities for microservice applications based on Spring Cloud Stream.
  • Distributed transactions: Use @GlobalTransactional annotation to solve distributed transaction problems efficiently and with zero intrusion to business.
  • Alibaba Cloud Object Storage: Massive, secure, low-cost, and highly reliable cloud storage services provided by Alibaba Cloud. Supports storing and accessing any type of data in any application, anytime, anywhere.
  • Distributed task scheduling: Provide second-level, accurate, highly reliable, and highly available timing (based on Cron expression) task scheduling services. At the same time, it provides a distributed task execution model, such as grid tasks. Grid tasks support the even distribution of sea quantum tasks to all Workers (schedulerx-client) for execution.
  • Alibaba Cloud SMS service: SMS service covering the world, friendly, efficient, and intelligent interconnected communication capabilities, helping enterprises quickly build customer access channels.

It can be seen that there are two "private goods" among them: Alibaba Cloud storage and Alibaba Cloud SMS. After all, it is understandable that they are provided by others. You can choose according to your own needs.

We rely on Spring Cloud Alibaba to quickly build our own microservice projects, and can access Alibaba middleware through configuration.

3. What are its components

The main components of Spring Cloud Alibaba include:

  • Sentinel: takes traffic as an entry point, and protects the stability of services from multiple dimensions such as traffic control, circuit breaker degradation, and system load protection.

  • Nacos: A dynamic service discovery, configuration management and service management platform that makes it easier to build cloud-native applications.

  • RocketMQ: An open source distributed messaging system, based on highly available distributed cluster technology, provides low-latency, highly reliable message publishing and subscription services.

  • Seata: Alibaba's open source product, an easy-to-use high-performance microservice distributed transaction solution.

  • Alibaba Cloud OSS: Alibaba Cloud Object Storage Service (OSS for short) 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, anywhere.

  • Alibaba Cloud SchedulerX: A distributed task scheduling product developed by the Alibaba middleware team, which provides second-level, accurate, highly reliable, and highly available scheduled (based on Cron expression) task scheduling services.

  • Alibaba Cloud SMS: SMS service covering the world, friendly, efficient, and intelligent interconnected communication capabilities, helping enterprises quickly build customer access channels.

4. How to use

environment:

The latest version of the environment requirements: JDK 17+, Spring Boot 3.0.x

After meeting the above requirements, add to the pom file:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-dependencies</artifactId>
            <version>2022.0.0.0-RC2</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

Take Sentinel as an example:

Sentinel is the traffic defense component of Alibaba's open source distributed system. Sentinel takes traffic as an entry point to protect service stability from multiple dimensions such as traffic control, circuit breaker downgrade, and system load protection.
OpenFeign is a declarative, templated HTTP client, Feign can help us call HTTP API more quickly and elegantly.

First we need to connect Feign to Sentinel.

First look at the service consumer:

1. Introduce dependencies

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

2. Define FeignClient

@FeignClient(name = "service-provider", fallbackFactory = EchoServiceFallbackFactory.class)
public interface EchoService {
    
    

    /**
     * 调用服务提供方的输出接口
     *
     * @param str 用户输入
     * @return
     */
    @GetMapping(value = "/echo/{str}")
    String echo(@PathVariable("str") String str);
}

3. Define the fallback factory and get exceptions

@Component
public class EchoServiceFallbackFactory implements FallbackFactory<EchoServiceFallback> {
    
    
    @Override
    public EchoServiceFallback create(Throwable throwable) {
    
    
        return new EchoServiceFallback(throwable);
    }
}

4. Define a specific fallback implementation

public class EchoServiceFallback implements EchoService {
    
    
    private Throwable throwable;

    EchoServiceFallback(Throwable throwable) {
    
    
        this.throwable = throwable;
    }

    @Override
    public String echo(String str) {
    
    
        return "consumer-fallback-default-str" + throwable.getMessage();
    }
}

Service Provider :

1. Define the service provider interface

@RestController
public class EchoController {
    
    

    @GetMapping("/echo/{str}")
    public String echo(@PathVariable String str) {
    
    
        return "provider-" + str;
    }

}

After the application starts, the detailed code can refer to spring cloud alibaba

Guess you like

Origin blog.csdn.net/qq_35241329/article/details/131007420