What is? Spring Cloud Alibaba
We don't talk about Spring Cloud and Spring Cloud Alibaba The relationship between , You can see it on the official website !!
Spring Cloud Alibaba One stop solution for distributed application service development . The project contains the necessary components for developing distributed application services , It's convenient for developers to pass through Spring Cloud The programming model easily uses these components to develop distributed application services .
This is a sentence from the official website , We can see it by analyzing this sentence Spring Cloud Alibaba It's a distributed solution , It's not a framework , It's not architecture either , It's a solution . The purpose of the solution is to solve the problems encountered by distributed services .
For example, if we call between microservices ,Spring Cloud Alibaba The solution given is to use Nacos To do service registration and management , use Feign To make free calls between services . Alibaba cloud provides many similar components ,
Spring Cloud Alibaba Official website
Spring Cloud Alibaba Main components
- Sentinel: Take traffic as a starting point , Slave flow control 、 Fusing the drop 、 Multiple dimensions such as system load protection protect the stability of services .
- Nacos: A dynamic service discovery that is easier to build cloud native applications 、 Configuration management and service management platform .
- RocketMQ: An open source distributed message system , Based on highly available distributed cluster technology , Provide low latency 、 Highly reliable message publishing and subscription service .
- Dubbo:Apache Dubbo It's a high performance Java RPC frame .
- Seata: Alibaba open source products , An easy-to-use high-performance microservice distributed transaction solution .
- Alibaba Cloud OSS: Alibaba cloud object storage service (Object Storage Service, abbreviation OSS), It's a huge amount provided by Alibaba cloud 、 Security 、 Low cost 、 Highly reliable cloud storage services . You can use it in any application 、 Anytime 、 Any place to store and access any type of data .
- Alibaba Cloud SchedulerX: A distributed task scheduling product developed by Alibaba middleware team , Provide seconds 、 accurate 、 Highly reliable 、 High availability timing ( be based on Cron expression ) Task scheduling service .
- Alibaba Cloud SMS: SMS service covering the world , friendly 、 Efficient 、 Intelligent and interconnected communication capabilities , Help enterprises quickly build customer touch channel .
Components are divided into commercial and non-commercial and It can be simply understood as , Horse Dad Provides a basic distributed solution , Basic is free , Of course, the basic components are enough to meet the needs of small and medium-sized companies for the production environment .
however , In the development process of using related components , Version dependence is a real headache sometimes , I remember that there were countless unexplained problems in the development process , Finally, it was found from the official website , It's version dependency , And then all kinds of innumerable inner **** Fly past
Here are Spring cloud ,Spring Cloud Alibaba, Spring Boot Between version selection

In the version selection, we try to choose the stable version , That is to say Release After 3 To 4 A version , This is a stable version
Official document of version comparison Graduation version dependency ( Recommended )
Version selection should follow the official document as much as possible Otherwise, there will be incompatibility , We should be careful in choosing
Official account number :Java Architects Alliance , Update the technology every day , The backstage reply book can obtain the exquisite information
Let's take a look at building a simple springcloud What kind of preparation is needed
In fact, it's not hard to , Except for the simple idea Configuration , We just have one more Nacos As configuration center
What is Nacos


This is the introduction on the official website , Let's have a look , It's a good thing to know
First of all, download the installation package
Download address :nacos.io/zh-cn/docs/quick-start.html
Or from git download

Select the corresponding version to decompress ( Be careful Nacos After decompressing Is a complete run package , If you don't use it skillfully , Don't touch the configuration information inside )

After downloading, unzip and enter bin Catalog Run the command at the terminal
- MAC
sh startup.sh -m standalone
(standalone Stands for stand-alone mode operation , Later, I will explain how to build and start the cluster mode separately ) - Windows
cmd startup.cmd
After successful startup Default password nacos/nacos( Initial account password )

After logging in, you can see the Chinese and English switching in the upper right corner , Students who are not good at English You can switch to Chinese

- Configuration management is mainly used for project configuration , For example, configuration files can be used nacos To manage because nacos It's not just a service center , It's also a configuration center ( I'll talk about it later )
Our development project configuration generally has the following methods :1. Hard encoding -- As a form of class field , Lead to : Dynamic modification is difficult , No persistence 2. The configuration file ( properties、yml Documents, etc. )-- Lead to : Configuration changes dynamically , You may need to restart the application , Make the configuration work . Of course , You can also add a timed task to your code , Like each 10s Read profile content , Let the latest configuration take effect in the application in time , That way, you won't have to To restart the application “ Heavier ” Operation and maintenance operation of .3. DB Configuration table -- Lead to : Configuration changes dynamically , It may need to be solved through the exposure management interface .
Nacos Really separate the configuration from the application , Unified management , Elegant solution to dynamic configuration changes 、 Persistence 、 Operation and maintenance cost and other issues . The application itself does not need to add a management configuration interface , You don't need to implement the persistence of the configuration by yourself , There is no need to introduce “ Timing task ” In order to reduce operation and maintenance costs .Nacos Provides configuration management functions , Collapse all the logic related to the configuration , And it's easy to use SDK, So that the configuration of the application can be easily Nacos It's more than that in management ,Nacos Provide DNS-F function , It can be done with K8S、Spring Cloud and Dubbo And other open source products to integrate , Realize the registration function of the service .
- Service discovery is used to manage registration to nacos Microservices on , Can achieve services offline and other functions .
- The namespace is used to distinguish the service environment , When a project needs to be developed , test , Production and many different configurations , The namespace can be used for configuration isolation .
- Cluster management I'll talk about it later
Let's take a look at how to build a simple microservice
Project construction
1. First of all, let's use SpringBoot establish 2 A project , My name is :provider,consumer.








I've set up one here provider service , After that, I will build another consumer service , Because the procedure is the same as above , I won't repeat


I've built it up here 2 A service project , Click on apply, Click on ok, The project is completed

Service configuration
provider Service configuration
1.provider The code is as follows and the configuration file ( The default for new projects is application.properties, But more often used in work is yml Format , So here I put properties Changed to yml Format ) Here's the picture

2.ProviderController The code is as follows :
@RestController
public class ProviderController {
@Value("${providerName}")
private String name;
@GetMapping("send")
public String send(){
return name;
}
}

3.application.yml The configuration is as follows :
server:
port: 8080
# Custom parameters
providerName: provider
4.provider Sign up to nacos
Producers are registered with nacos Registry Center , step :
Add dependency :spring-cloud-starter-alibaba-nacos-discovery And springCloud

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Integrate Nacos -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>0.9.0.RELEASE</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
stay application.yml Middle configuration nacos Service address and application name
server:
port: 8080
spring:
application:
## Application name
name: provider
## nacos Service address
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
# Custom parameters
providerName: provider
Start the service , Then log in Nacos Service view , give the result as follows

consumer Service configuration
1. First of all, according to my introduction above maven rely on .
2. Since the configuration is basically the same as above, I will give an example of my code part
Code :
@RestController
public class ConsumerController {
@GetMapping("consumer")
public String send(){
return "Consumer";
}
}
yml To configure
server:
port: 8081
spring:
application:
## Application name
name: consumer
## nacos Service address
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
Start the service

Integrate Feign Implement remote call
1. First introduced feign Related dependencies
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
2. stay ConsumerApplication On the class to add @EnableFeignClients annotation :

3. To write feignClient:

@FeignClient("provider")
public interface ProviderFeign {
@RequestMapping("send")
String send();
}
stay Controller Use in feignClient:
@RestController
public class ConsumerController {
@Autowired
private ProviderFeign providerFeign;
@GetMapping("consumer")
public String send(){
String send = providerFeign.send();
return "Consumer" + send;
}
}
Test access :

summary
So far, a simple microservice project has been built . In fact, the operation is very simple , You can try it .
Official account number :Java Architects Alliance , Update the technology every day , The backstage reply book can obtain the exquisite information