SpringCloudAlibaba全網(wǎng)最全講解(建議收藏)
二、Spring Cloud
2.1、什么是SpringCloud
SpringCloud是一個(gè)含概多個(gè)子項(xiàng)目的開發(fā)工具集,集合了眾多的開源框架,他利用了Spring Boot開發(fā)的便利性實(shí)現(xiàn)了很多功能,如服務(wù)注冊(cè),服務(wù)注冊(cè)發(fā)現(xiàn),負(fù)載均衡等.SpringCloud在整合過程中主要是針對(duì)Netflix(奈飛)開源組件的封裝.SpringCloud的出現(xiàn)真正的簡化了分布式架構(gòu)的開發(fā)。
NetFlix 是美國的一個(gè)在線視頻網(wǎng)站,微服務(wù)業(yè)的翹楚,他是公認(rèn)的大規(guī)模生產(chǎn)級(jí)微服務(wù)的杰出實(shí)踐者,NetFlix的開源組件已經(jīng)在他大規(guī)模分布式微服務(wù)環(huán)境中經(jīng)過多年的生產(chǎn)實(shí)戰(zhàn)驗(yàn)證,因此Spring Cloud中很多組件都是基于NetFlix組件的封裝。
2.2、核心組件
eurekaserver、consul、nacos:服務(wù)注冊(cè)中心組件。
rabbion & openfeign:服務(wù)負(fù)載均衡 和 服務(wù)調(diào)用組件。
hystrix & hystrix dashboard:服務(wù)斷路器和服務(wù)監(jiān)控組件。
zuul、gateway:服務(wù)網(wǎng)關(guān)組件。
config:統(tǒng)一配置中心組件。
bus:消息總線組件。

2.3、版本命名
SpringCloud是一個(gè)由眾多獨(dú)立子項(xiàng)目組成的大型綜合項(xiàng)目,原則每個(gè)子項(xiàng)目上有不同的發(fā)布節(jié)奏,都維護(hù)自己發(fā)布版本號(hào)。為了更好的管理springcloud的版本,通過一個(gè)資源清單BOM(Bill of Materials),為避免與子項(xiàng)目的發(fā)布號(hào)混淆,所以沒有采用版本號(hào)的方式,而是通過命名的方式。這些名字是按字母順序排列的。如倫敦地鐵站的名稱(“天使”是第一個(gè)版本,“布里斯頓”是第二個(gè)版本,"卡姆登"是第三個(gè)版本)。當(dāng)單個(gè)項(xiàng)目的點(diǎn)發(fā)布累積到一個(gè)臨界量,或者其中一個(gè)項(xiàng)目中有一個(gè)關(guān)鍵缺陷需要每個(gè)人都可以使用時(shí),發(fā)布序列將推出名稱以“.SRX”結(jié)尾的“服務(wù)發(fā)布”,其中“X”是一個(gè)數(shù)字。
倫敦地鐵站的名字大致有如下:Angel、Brixton、Camden、Dalston、Edgware、Finchley、Greenwich、Hoxton。
2.4、版本選擇
由于SpringCloud的版本是必須和SpringBoot的版本對(duì)應(yīng)的,所以必須要根據(jù)SpringCloud版本來選擇SpringBoot的版本。

三、SpringCloud Alibaba
3.1、簡介
Spring Cloud Alibaba是Spring Cloud下的一個(gè)子項(xiàng)目,Spring Cloud Alibaba為分布式應(yīng)用程序開發(fā)提供了一站式解決方案,它包含開發(fā)分布式應(yīng)用程序所需的所有組件,使您可以輕松地使用Spring Cloud開發(fā)應(yīng)用程序,使用Spring Cloud Alibaba,您只需要添加一些注解和少量配置即可將Spring Cloud應(yīng)用程序連接到Alibaba的分布式解決方案,并使用Alibaba中間件構(gòu)建分布式應(yīng)用程序系統(tǒng)。Spring Cloud Alibaba 是阿里巴巴開源中間件跟 Spring Cloud 體系的融合。

3.2、主要功能
流量控制和服務(wù)降級(jí):默認(rèn)支持 WebServlet、WebFlux, OpenFeign、RestTemplate、Spring Cloud、Gateway, Zuul, Dubbo 和 RocketMQ 限流降級(jí)功能的接入,可以在運(yùn)行時(shí)通過控制臺(tái)實(shí)時(shí)修改限流降級(jí)規(guī)則,還支持查看限流降級(jí) Metrics 監(jiān)控。
服務(wù)注冊(cè)和發(fā)現(xiàn):實(shí)例可以在Alibaba Nacos上注冊(cè),客戶可以使用Spring管理的bean發(fā)現(xiàn)實(shí)例,通過Spring Cloud Netflix支持Ribbon客戶端負(fù)載均衡器。
分布式配置管理:支持分布式系統(tǒng)中的外部化配置,配置更改時(shí)自動(dòng)刷新。
消息驅(qū)動(dòng)能力:基于 Spring Cloud Stream 為微服務(wù)應(yīng)用構(gòu)建消息驅(qū)動(dòng)能力。
消息總線:使用Spring Cloud Bus RocketMQ鏈接分布式系統(tǒng)的節(jié)點(diǎn)。
分布式事務(wù):使用 @GlobalTransactional 注解, 高效并且對(duì)業(yè)務(wù)零侵入地解決分布式事務(wù)問題。
Dubbo RPC:通過Apache Dubbo RPC擴(kuò)展Spring Cloud服務(wù)到服務(wù)調(diào)用的通信協(xié)議。
分布式任務(wù)調(diào)度:提供秒級(jí)、精準(zhǔn)、高可靠、高可用的定時(shí)(基于 Cron 表達(dá)式)任務(wù)調(diào)度服務(wù)。同時(shí)提供分布式的任務(wù)執(zhí)行模型,如網(wǎng)格任務(wù)。網(wǎng)格任務(wù)支持海量子任務(wù)均勻分配到所有Worker(schedulerx-client)上執(zhí)行。
3.3、組件
Sentinel:把流量作為切入點(diǎn),從流量控制、熔斷降級(jí)、系統(tǒng)負(fù)載保護(hù)等多個(gè)維度保護(hù)服務(wù)的穩(wěn)定性。
Nacos:一個(gè)更易于構(gòu)建云原生應(yīng)用的動(dòng)態(tài)服務(wù)發(fā)現(xiàn)、配置管理和服務(wù)管理平臺(tái)。
RocketMQ:一款開源的分布式消息系統(tǒng),基于高可用分布式集群技術(shù),提供低延時(shí)的、高可靠 的消息發(fā)布與訂閱服務(wù)。
Dubbo:Apache Dubbo? 是一款高性能 Java RPC 框架。
Seata:阿里巴巴開源產(chǎn)品,一個(gè)易于使用的高性能微服務(wù)分布式事務(wù)解決方案。
Alibaba Cloud ACM:一款在分布式架構(gòu)環(huán)境中對(duì)應(yīng)用配置進(jìn)行集中管理和推送的應(yīng)用配置中心 產(chǎn)品。
Alibaba Cloud OSS: 阿里云對(duì)象存儲(chǔ)服務(wù)(Object Storage Service,簡稱 OSS),是阿里云提 供的海量、安全、低成本、高可靠的云存儲(chǔ)服務(wù)。您可以在任何應(yīng)用、任何時(shí)間、任何地點(diǎn)存儲(chǔ)和 訪問任意類型的數(shù)據(jù)。
Alibaba Cloud SchedulerX: 阿里中間件團(tuán)隊(duì)開發(fā)的一款分布式任務(wù)調(diào)度產(chǎn)品,提供秒級(jí)、精 準(zhǔn)、高可靠、高可用的定時(shí)(基于 Cron 表達(dá)式)任務(wù)調(diào)度服務(wù)。
Alibaba Cloud SMS: 覆蓋全球的短信服務(wù),友好、高效、智能的互聯(lián)化通訊能力,幫助企業(yè)迅速 搭建客戶觸達(dá)通道。

四、微服務(wù)項(xiàng)目搭建
4.1、技術(shù)選型
持久層:SpingData Jpa
數(shù)據(jù)庫: MySQL5.7
技術(shù)棧:SpringCloud Alibaba 技術(shù)棧
4.2、模塊設(shè)計(jì)
我們搭建一個(gè)微服務(wù)的項(xiàng)目,但是只有簡單的代碼,沒有任何業(yè)務(wù)邏輯。
shop-parent 父工程
shop-product-api:商品微服務(wù)api ,用于存放商品實(shí)體。
shop-product-server:商品微服務(wù),他的1端口是808x。
shop-order-api 訂單微服務(wù)api,用于存放訂單實(shí)體。
shop-order-server 訂單微服務(wù),他的端口是808x。
4.3、微服務(wù)的調(diào)用
;在微服務(wù)架構(gòu)中,最常見的場景就是微服務(wù)之間的相互調(diào)用。我們以電商系統(tǒng)中常見的用戶下單為例來演示微服務(wù)的調(diào)用:客戶向訂單微服務(wù)發(fā)起一個(gè)下單的請(qǐng)求,在進(jìn)行保存訂單之前需要調(diào)用商品微服務(wù)查詢商品的信息。
我們一般把服務(wù)的主動(dòng)調(diào)用方稱為服務(wù)消費(fèi)者,把服務(wù)的被調(diào)用方稱為服務(wù)提供者。

4.4、創(chuàng)建父工程
創(chuàng)建一個(gè)maven工程,然后在pom.xml文件中添加下面內(nèi)容
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<relativePath/>
</parent>
<groupId>cn.linstudy</groupId>
<artifactId>Shop-parent</artifactId>
<packaging>pom</packaging>
<version>1.0.0</version>
<modules>
<module>Shop-order-api</module>
<module>Shop-order-server</module>
<module>Shop-product-api</module>
<module>Shop-product-server</module>
</modules>
<properties>
<java.version>11</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-cloud.version>Hoxton.SR8</spring-cloud.version>
<spring-cloud-alibaba.version>2.2.3.RELEASE</spring-cloud-alibaba.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
復(fù)制代碼4.5、創(chuàng)建商品服務(wù)
4.5.1、書寫Shop-product-api的依賴
創(chuàng)建Shop-product-api項(xiàng)目,然后在pom.xml文件中添加下面內(nèi)容
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>Shop-parent</artifactId>
<groupId>cn.linstudy</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>Shop-product-api</artifactId>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>
復(fù)制代碼4.5.2、創(chuàng)建實(shí)體
//商品
@Entity(name = "t_shop_product")
@Data
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long pid;//主鍵
private String pname;//商品名稱
private Double pprice;//商品價(jià)格
private Integer stock;//庫存
}
復(fù)制代碼4.5.3、書寫Shop-product-server的依賴
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>Shop-parent</artifactId>
<groupId>cn.linstudy</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>Shop-order-server</artifactId>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.56</version>
</dependency>
<dependency>
<groupId>cn.linstudy</groupId>
<artifactId>Shop-order-api</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>cn.linstudy</groupId>
<artifactId>Shop-product-api</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</project>
復(fù)制代碼4.5.4、編寫application.yml
server:
port: 8081
spring:
application:
name: product-service
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql:///shop-product?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
username: root
password: admin
jpa:
properties:
hibernate:
hbm2ddl:
auto: update
dialect: org.hibernate.dialect.MySQL5InnoDBDialect
復(fù)制代碼4.5.5、創(chuàng)建數(shù)據(jù)庫
由于我們使用的是JPA,所以我們需要?jiǎng)?chuàng)建數(shù)據(jù)庫,但是不需要?jiǎng)?chuàng)建表,因?yàn)镴PA會(huì)在對(duì)應(yīng)的數(shù)據(jù)庫中自動(dòng)創(chuàng)建表。
4.5.6、創(chuàng)建DAO接口
// 第一個(gè)參數(shù)是實(shí)體類,第二個(gè)參數(shù)是實(shí)體類對(duì)象的主鍵的類型
public interface ProductDao extends JpaRepository<Product,Long> {
}
復(fù)制代碼4.5.7、創(chuàng)建Service接口及其實(shí)現(xiàn)類
public interface ProductService {
Product findById(Long productId);
}
復(fù)制代碼@Service
public class ProductServiceImpl implements ProductService {
@Autowired
ProductDao productDao;
@Override
public Product findById(Long productId) {
return productDao.findById(productId).get();
}
}
復(fù)制代碼4.5.8、書寫Controller
@RestController
@Slf4j
public class ProductController {
@Autowired
private ProductService productService;
//商品信息查詢
@RequestMapping("/product")
public Product findByPid(@RequestParam("pid") Long pid) {
Product product = productService.findByPid(pid);
return product;
}
}
復(fù)制代碼4.5.9、加入測(cè)試數(shù)據(jù)
我們?cè)趩?dòng)項(xiàng)目的時(shí)候,可以發(fā)現(xiàn)表已經(jīng)默認(rèn)幫我們自動(dòng)創(chuàng)建好了,我們需要導(dǎo)入測(cè)試數(shù)據(jù)。
INSERT INTO t_shop_product VALUE(NULL,'小米','1000','5000');
INSERT INTO t_shop_product VALUE(NULL,'華為','2000','5000');
INSERT INTO t_shop_product VALUE(NULL,'蘋果','3000','5000');
INSERT INTO t_shop_product VALUE(NULL,'OPPO','4000','5000');
復(fù)制代碼4.5.10、測(cè)試

4.6、創(chuàng)建訂單微服務(wù)
4.6.1、書寫Shop-order-api的依賴
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>Shop-parent</artifactId>
<groupId>cn.linstudy</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>Shop-order-api</artifactId>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
</dependencies>
</project>
復(fù)制代碼4.6.2、創(chuàng)建訂單實(shí)體
//訂單
@Entity(name = "t_shop_order")
@Data
@JsonIgnoreProperties(value = { "hibernateLazyInitializer"})
public class Order implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long oid;//訂單id
//用戶
private Long uid;//用戶id
private String username;//用戶名
//商品
private Long pid;//商品id
private String pname;//商品名稱
private Double pprice;//商品單價(jià)
//數(shù)量
private Integer number;//購買數(shù)量
}
復(fù)制代碼4.6.3、創(chuàng)建shop-order-server項(xiàng)目
在pom.xml中添加依賴。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>Shop-parent</artifactId>
<groupId>cn.linstudy</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>Shop-order-server</artifactId>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.56</version>
</dependency>
<dependency>
<groupId>cn.linstudy</groupId>
<artifactId>Shop-order-api</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>cn.linstudy</groupId>
<artifactId>Shop-product-api</artifactId>
<version>1.0.0</version>
</dependency>
</project>
復(fù)制代碼4.6.4、編寫application.yml
server:
port: 8082
spring:
application:
name: order-service
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql:///shop-product?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
username: root
password: 1101121833
jpa:
properties:
hibernate:
hbm2ddl:
auto: update
dialect: org.hibernate.dialect.MySQL5InnoDBDialect
復(fù)制代碼4.6.5、編寫Service及其實(shí)現(xiàn)類
我們寫的Service接口及其實(shí)現(xiàn)類不具備任何的業(yè)務(wù)邏輯,僅僅只是為了測(cè)試而用。
public interface OrderService {
Order getById(Long oid,Long pid);
}
復(fù)制代碼@Service
public class OrderServiceImpl implements OrderService {
@Autowired
OrderDao orderDao;
@Override
public Order getById(Long oid, Long pid) {
return orderDao.getOne(oid);
}
}
復(fù)制代碼4.6.6、創(chuàng)建Controller
@RestController
@Slf4j
public class OrderController {
@Autowired
private OrderService orderService;
@RequestMapping("getById")
public Order getById(Long oid,Long pid){
return orderService.getById(oid, pid);
}
復(fù)制代碼4.7、服務(wù)之間如何進(jìn)行調(diào)用
假設(shè)我們?cè)谟唵蔚姆?wù)里面需要調(diào)用到商品服務(wù),先查詢出id為1的商品,然后再查詢出他的訂單,這個(gè)1時(shí)候就涉及到服務(wù)之間的調(diào)用問題了。 服務(wù)之間的1調(diào)用本質(zhì)上是通過Java代碼去發(fā)起一個(gè)Http請(qǐng)求,我們可以使用RestTemplate來進(jìn)行調(diào)用。
4.7.1、在shop-order-server的啟動(dòng)類中添加注解
我們既然需要RestTemplate類,就需要將RestTemplate類注入到容器中。
@SpringBootApplication
public class OrderServer {
public static void main(String[] args) {
SpringApplication.run(OrderServer.class,args);
}
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
復(fù)制代碼4.7.2、修改Controller代碼
@RestController
public class OrderController {
@Autowired
RestTemplate restTemplate;
@Autowired
OrderService orderService;
@RequestMapping("getById")
public Order getById(Long oid,Long pid){
Product product = restTemplate.getForObject(
"http://localhost:8083/product?productId="+pid,Product.class); // 這里通過restTemplate去發(fā)起http請(qǐng)求,請(qǐng)求地址是http://localhost:8083/product,攜帶的參數(shù)是productId
Order order = orderService.getById(oid, product.getPid());
order.setUsername(product.getPname());
return order;
}
}
復(fù)制代碼這樣我們就完成了服務(wù)之間的互相調(diào)用。
4.8、存在的問題
雖然我們已經(jīng)可以實(shí)現(xiàn)微服務(wù)之間的調(diào)用。但是我們把服務(wù)提供者的網(wǎng)絡(luò)地址(ip,端口)等硬編碼到了代碼中,這種做法存在許多問題:
一旦服務(wù)提供者地址變化,就需要手工修改代碼。
一旦是多個(gè)服務(wù)提供者,無法實(shí)現(xiàn)負(fù)載均衡功能。
一旦服務(wù)變得越來越多,人工維護(hù)調(diào)用關(guān)系困難。
那么應(yīng)該怎么解決呢?這時(shí)候就需要通過注冊(cè)中心動(dòng)態(tài)的實(shí)現(xiàn)服務(wù)治理。
作者:XiaoLin_Java
鏈接:https://juejin.cn/post/7000225781137801229
來源:掘金
著作權(quán)歸作者所有。商業(yè)轉(zhuǎn)載請(qǐng)聯(lián)系作者獲得授權(quán),非商業(yè)轉(zhuǎn)載請(qǐng)注明出處。

