Spring cloud Alibaba from introduction to practice

Java architects Alliance 2020-11-06 23:24:01
spring cloud alibaba introduction practice


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

Spring Cloud Alibaba From the beginning to the practice, we can do everything in one net

 

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

Spring Cloud Alibaba From the beginning to the practice, we can do everything in one net

 

Spring Cloud Alibaba From the beginning to the practice, we can do everything in one net

 

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

Spring Cloud Alibaba From the beginning to the practice, we can do everything in one net

 


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 )

Spring Cloud Alibaba From the beginning to the practice, we can do everything in one net

 

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 )

Spring Cloud Alibaba From the beginning to the practice, we can do everything in one net

 

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

Spring Cloud Alibaba From the beginning to the practice, we can do everything in one net

 

  • 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.

Spring Cloud Alibaba From the beginning to the practice, we can do everything in one net

 

Spring Cloud Alibaba From the beginning to the practice, we can do everything in one net

 

Spring Cloud Alibaba From the beginning to the practice, we can do everything in one net

 

Spring Cloud Alibaba From the beginning to the practice, we can do everything in one net

 

Spring Cloud Alibaba From the beginning to the practice, we can do everything in one net

 

Spring Cloud Alibaba From the beginning to the practice, we can do everything in one net

 

Spring Cloud Alibaba From the beginning to the practice, we can do everything in one net

 

Spring Cloud Alibaba From the beginning to the practice, we can do everything in one net

 


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

Spring Cloud Alibaba From the beginning to the practice, we can do everything in one net

 

Spring Cloud Alibaba From the beginning to the practice, we can do everything in one net

 


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

Spring Cloud Alibaba From the beginning to the practice, we can do everything in one net

 

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

Spring Cloud Alibaba From the beginning to the practice, we can do everything in one net

 


2.ProviderController The code is as follows :

@RestController
public class ProviderController {
@Value("${providerName}")
private String name;
@GetMapping("send")
public String send(){
return name;
}
}
Spring Cloud Alibaba From the beginning to the practice, we can do everything in one net

 


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

Spring Cloud Alibaba From the beginning to the practice, we can do everything in one net

 

<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

Spring Cloud Alibaba From the beginning to the practice, we can do everything in one net

 

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

Spring Cloud Alibaba From the beginning to the practice, we can do everything in one net

 

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 :

Spring Cloud Alibaba From the beginning to the practice, we can do everything in one net

 


3. To write feignClient:

Spring Cloud Alibaba From the beginning to the practice, we can do everything in one net

 

@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 :

Spring Cloud Alibaba From the beginning to the practice, we can do everything in one net

 

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

版权声明
本文为[Java architects Alliance]所创,转载请带上原文链接,感谢

  1. 【计算机网络 12(1),尚学堂马士兵Java视频教程
  2. 【程序猿历程,史上最全的Java面试题集锦在这里
  3. 【程序猿历程(1),Javaweb视频教程百度云
  4. Notes on MySQL 45 lectures (1-7)
  5. [computer network 12 (1), Shang Xuetang Ma soldier java video tutorial
  6. The most complete collection of Java interview questions in history is here
  7. [process of program ape (1), JavaWeb video tutorial, baidu cloud
  8. Notes on MySQL 45 lectures (1-7)
  9. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  10. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  11. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  12. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  13. 【递归,Java传智播客笔记
  14. [recursion, Java intelligence podcast notes
  15. [adhere to painting for 386 days] the beginning of spring of 24 solar terms
  16. K8S系列第八篇(Service、EndPoints以及高可用kubeadm部署)
  17. K8s Series Part 8 (service, endpoints and high availability kubeadm deployment)
  18. 【重识 HTML (3),350道Java面试真题分享
  19. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  20. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  21. [re recognize HTML (3) and share 350 real Java interview questions
  22. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  23. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  24. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  25. RPC 1: how to develop RPC framework from scratch
  26. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  27. RPC 1: how to develop RPC framework from scratch
  28. 一次性捋清楚吧,对乱糟糟的,Spring事务扩展机制
  29. 一文彻底弄懂如何选择抽象类还是接口,连续四年百度Java岗必问面试题
  30. Redis常用命令
  31. 一双拖鞋引发的血案,狂神说Java系列笔记
  32. 一、mysql基础安装
  33. 一位程序员的独白:尽管我一生坎坷,Java框架面试基础
  34. Clear it all at once. For the messy, spring transaction extension mechanism
  35. A thorough understanding of how to choose abstract classes or interfaces, baidu Java post must ask interview questions for four consecutive years
  36. Redis common commands
  37. A pair of slippers triggered the murder, crazy God said java series notes
  38. 1、 MySQL basic installation
  39. Monologue of a programmer: despite my ups and downs in my life, Java framework is the foundation of interview
  40. 【大厂面试】三面三问Spring循环依赖,请一定要把这篇看完(建议收藏)
  41. 一线互联网企业中,springboot入门项目
  42. 一篇文带你入门SSM框架Spring开发,帮你快速拿Offer
  43. 【面试资料】Java全集、微服务、大数据、数据结构与算法、机器学习知识最全总结,283页pdf
  44. 【leetcode刷题】24.数组中重复的数字——Java版
  45. 【leetcode刷题】23.对称二叉树——Java版
  46. 【leetcode刷题】22.二叉树的中序遍历——Java版
  47. 【leetcode刷题】21.三数之和——Java版
  48. 【leetcode刷题】20.最长回文子串——Java版
  49. 【leetcode刷题】19.回文链表——Java版
  50. 【leetcode刷题】18.反转链表——Java版
  51. 【leetcode刷题】17.相交链表——Java&python版
  52. 【leetcode刷题】16.环形链表——Java版
  53. 【leetcode刷题】15.汉明距离——Java版
  54. 【leetcode刷题】14.找到所有数组中消失的数字——Java版
  55. 【leetcode刷题】13.比特位计数——Java版
  56. oracle控制用户权限命令
  57. 三年Java开发,继阿里,鲁班二期Java架构师
  58. Oracle必须要启动的服务
  59. 万字长文!深入剖析HashMap,Java基础笔试题大全带答案
  60. 一问Kafka就心慌?我却凭着这份,图灵学院vip课程百度云