昆明做網(wǎng)站哪家好佛山市人民政府門戶網(wǎng)站
目錄標(biāo)題
- 一、簡(jiǎn)介
- 二、SpringCloud Alibaba核心組件
- 2-1、Nacos (配置中心與服務(wù)注冊(cè)與發(fā)現(xiàn))
- 2-2、Sentinel (分布式流控)
- 2-3、RocketMQ (消息隊(duì)列)/RabbitMq/kafka
- 2-4、Seata (分布式事務(wù))
- 2-5、Dubbo (RPC)
- 三、為什么大家看好 Spring Cloud Alibaba
- 3-1、阿里巴巴強(qiáng)大的技術(shù)輸出能力
- 3-2、集成Dubbo,利用Dubbo在微服務(wù)領(lǐng)域的超高人氣
- 3-3、云原生趨勢(shì),集成阿里云商業(yè)化組件
- 四、創(chuàng)建簡(jiǎn)單項(xiàng)目
- 4-1、微服務(wù)環(huán)境搭建
- 4-2、版本依賴
- 4-3、組件版本關(guān)系
- 4-4、創(chuàng)建項(xiàng)目
- 4-4-1 導(dǎo)入springboot的jar包
- 4-4-2 創(chuàng)建maven以及導(dǎo)入jar包
- 4-4-3 運(yùn)行App
- 4-4-4 創(chuàng)建實(shí)例項(xiàng)目
- 五、創(chuàng)建公共類存放
- 5-1、創(chuàng)建sys-comm作為公共類
- 5-2、sys-order
- 5-3、sys-pro
一、簡(jiǎn)介
很多人可能會(huì)問,有了spring cloud這個(gè)微服務(wù)的框架,為什么又要使用spring cloud alibaba這個(gè)框架了?最重要的原因在于spring cloud中的幾乎所有的組件都使用Netflix公司的產(chǎn)品,然后在其基礎(chǔ)上做了一層封裝。然而Netflix的服務(wù)發(fā)現(xiàn)組件Eureka已經(jīng)停止更新;而其他的眾多組件在2020年停止維護(hù)。所以急需其他的一些替代產(chǎn)品,也就是spring cloud alibaba,SpringCloud Alibaba是依賴SpringCloud相關(guān)的標(biāo)準(zhǔn)實(shí)現(xiàn)的一套微服務(wù)的架構(gòu)。
作為 Spring Cloud 體系下的新實(shí)現(xiàn),Spring Cloud Alibaba 跟官方的組件或其它的第三方實(shí)現(xiàn)如 Netflix, Consul,Zookeeper 等對(duì)比,具備了更多的功能:
微服務(wù)架構(gòu)的常見問題 一旦采用微服務(wù)系統(tǒng)架構(gòu),就勢(shì)必會(huì)遇到這樣幾個(gè)問題:
? 這么多小服務(wù),如何管理他們?(服務(wù)治理 注冊(cè)中心[服務(wù)注冊(cè) 發(fā)現(xiàn) 剔除])
? 這么多小服務(wù),他們之間如何通訊?(restful rpc)
? 這么多小服務(wù),客戶端怎么訪問他們?(網(wǎng)關(guān) GateWay)
? 這么多小服務(wù),一旦出現(xiàn)問題了,應(yīng)該如何自處理?(容錯(cuò))
A–>B–>C–D—E sentinel
? 這么多小服務(wù),一旦出現(xiàn)問題了,應(yīng)該如何排錯(cuò)? (鏈路追蹤)
sluth
對(duì)于上面的問題,是任何一個(gè)微服務(wù)設(shè)計(jì)者都不能繞過去的,因此大部分的微服務(wù)產(chǎn)品都針對(duì)每一 個(gè)問題提供了相應(yīng)的組件來解決它們
二、SpringCloud Alibaba核心組件
配置中心 nacos : springcloud config
服務(wù)治理中心nacos : Eurke
2-1、Nacos (配置中心與服務(wù)注冊(cè)與發(fā)現(xiàn))
Nacos實(shí)現(xiàn)了服務(wù)的配置中心與服務(wù)注冊(cè)發(fā)現(xiàn)的功能,Nacos可以通過可視化的配置降低相關(guān)的學(xué)習(xí)與維護(hù)成本,實(shí)現(xiàn)動(dòng)態(tài)的配置管理與分環(huán)境的配置中心控制。 同時(shí)Nacos提供了基于http/RCP的服務(wù)注冊(cè)與發(fā)現(xiàn)功能。
2-2、Sentinel (分布式流控)
Sentinel是面向分布式微服務(wù)架構(gòu)的輕量級(jí)高可用的流控組件,以流量作為切入點(diǎn),從流量控制,熔斷降級(jí),系統(tǒng)負(fù)載保護(hù)等維度幫助用戶保證服務(wù)的穩(wěn)定性。常用與實(shí)現(xiàn)限流、熔斷降級(jí)等策略。
2-3、RocketMQ (消息隊(duì)列)/RabbitMq/kafka
RocketMQ基于Java的高性能、高吞吐量的消息隊(duì)列,在SpringCloud Alibaba生態(tài)用于實(shí)現(xiàn)消息驅(qū)動(dòng)的業(yè)務(wù)開發(fā),常見的消息隊(duì)列有Kafka、RocketMQ、RabbitMQ等,相關(guān)的比較文檔可以自行去翻閱。
2-4、Seata (分布式事務(wù))
既然是微服務(wù)的產(chǎn)品,那么肯定會(huì)用到分布式事物。Seata就是阿里巴巴開源的一個(gè)高性能分布式事物的解決方案。
2-5、Dubbo (RPC)
Dubbo已經(jīng)在圈內(nèi)很火了,SpringCloud Alibaba基于上面提到的Nacos服務(wù)注冊(cè)中心也同樣整合了Dubbo。
三、為什么大家看好 Spring Cloud Alibaba
3-1、阿里巴巴強(qiáng)大的技術(shù)輸出能力
阿里巴巴無疑是國(guó)內(nèi)開源技術(shù)領(lǐng)域的最有影響力的公司之一,已經(jīng)有Dubbo、Druid,FastJson等成功的開源組件,
再加上阿里不遺余力的推廣,社區(qū)發(fā)展也非???。
3-2、集成Dubbo,利用Dubbo在微服務(wù)領(lǐng)域的超高人氣
Dubbo是國(guó)內(nèi)應(yīng)用最廣的分布式服務(wù)框架之一,基于Dubbo改造的Dubbox等也有很多公司在使用,
Spring Cloud Alibaba對(duì)Dubbo做了比較好的集成,可以吸引不少使用Dubbo的開發(fā)者。
3-3、云原生趨勢(shì),集成阿里云商業(yè)化組件
云原生(Cloud Native)是今年技術(shù)領(lǐng)域特別熱門的一個(gè)詞,云原生是一種專門針對(duì)云上應(yīng)用而設(shè)計(jì)的方法,用于構(gòu)建和部署應(yīng)用,以充分發(fā)揮云計(jì)算的優(yōu)勢(shì)。
Spring Cloud Alibaba 集成了阿里云的商業(yè)化組件,可以說天然支持云原生特性。
四、創(chuàng)建簡(jiǎn)單項(xiàng)目
4-1、微服務(wù)環(huán)境搭建
微服務(wù)環(huán)境搭建
數(shù)據(jù)庫(kù):mysql
持久層:mybatis-plus
Springcloud:Hoxton.SR9
SpringcludAlibaba: 2.2.6.RELEASE
SpringBoot:2.3.2.RELEASE
nacos:1.4.2
最新的版本對(duì)應(yīng)關(guān)系可以參考網(wǎng)址:
https://github.com/alibaba/spring-cloud-alibaba
4-2、版本依賴
4-3、組件版本關(guān)系
每個(gè) Spring Cloud Alibaba 版本及其自身所適配的各組件對(duì)應(yīng)版本如下表所示(注意,Spring Cloud Dubbo 從 2021.0.1.0 起已被移除出主干,不再隨主干演進(jìn))
4-4、創(chuàng)建項(xiàng)目
- 先創(chuàng)建springboot
- 在springboot下創(chuàng)建maven
4-4-1 導(dǎo)入springboot的jar包
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><packaging>pom</packaging><modules><module>sys-order</module><module>sys-pro</module><module>sys-comm</module></modules><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.4.2</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>demo</artifactId><version>0.0.1-SNAPSHOT</version><name>cloudalibaba</name><description>Demo project for Spring Boot</description><!--設(shè)置一些屬性--><properties><java.version>1.8</java.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><spring-cloud.version>2020.0.1</spring-cloud.version><spring-cloud-alibaba.version>2021.1</spring-cloud-alibaba.version></properties>
<!-- <dependencies>-->
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter</artifactId>-->
<!-- </dependency>--><!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-test</artifactId>-->
<!-- <scope>test</scope>-->
<!-- </dependency>-->
<!-- </dependencies>--><!--版本管理,不引入jar 包--><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></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><image><builder>paketobuildpacks/builder-jammy-base:latest</builder></image></configuration></plugin></plugins></build></project>
4-4-2 創(chuàng)建maven以及導(dǎo)入jar包
<?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>demo</artifactId><groupId>com.example</groupId><version>0.0.1-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>sys-pro</artifactId><name>sys-pro</name><!-- FIXME change it to the project's website --><url>http://www.example.com</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.7</maven.compiler.source><maven.compiler.target>1.7</maven.compiler.target></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency></dependencies></project>
4-4-3 運(yùn)行App
package com.example;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;/*** Hello world!**/
@SpringBootApplication
public class ProApp
{public static void main( String[] args ){System.out.println( "Hello World!" );SpringApplication.run(ProApp.class );}
}
4-4-4 創(chuàng)建實(shí)例項(xiàng)目
entity
package com.example.entity;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;@Data
@AllArgsConstructor
@NoArgsConstructor
public class Product {private Integer id;private String name;
}
controller
package com.example.controller;import com.example.entity.Product;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;@RestController
public class Procontroller {@GetMapping("/pro/{id}")public Product getProById(@PathVariable Integer id) {return new Product(1,"test"+id);}
}
五、創(chuàng)建公共類存放
5-1、創(chuàng)建sys-comm作為公共類
<dependencies><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency></dependencies>
package com.example.entity;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;@Data
@AllArgsConstructor
@NoArgsConstructor
public class Product {private Integer id;private String name;
}
5-2、sys-order
pom.xml 需要引入sys-comm公共類
<dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><version>2.4.2</version></dependency>
<!-- 引用公共類--><dependency><groupId>com.example</groupId><artifactId>sys-comm</artifactId><version>0.0.1-SNAPSHOT</version></dependency></dependencies>
controller
package com.example.controller;import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;import javax.annotation.Resource;@RestController
public class OrderController {// 遠(yuǎn)程調(diào)用@Resourceprivate RestTemplate restTemplate;public Object addOrder(@PathVariable Integer pid) {KafkaProperties.Producer forObject= restTemplate.getForObject("http://localhost:8082/pro/2"+pid, KafkaProperties.Producer.class);return forObject;}
}
App
package com.example;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;/*** Hello world!**/
@SpringBootApplication
public class OrderApp
{public static void main( String[] args ){System.out.println( "Hello World!" );SpringApplication.run(OrderApp.class);}@Beanpublic RestTemplate getRestTemplate(){return new RestTemplate();}
}
5-3、sys-pro
pom.xml 需要引入sys-comm公共類
<dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><!-- 引用公共類--><dependency><groupId>com.example</groupId><artifactId>sys-comm</artifactId><version>0.0.1-SNAPSHOT</version></dependency></dependencies>
controller
package com.example.controller;import com.example.entity.Product;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;@RestController
public class Procontroller {@GetMapping("/pro/{id}")public Product getProById(@PathVariable Integer id) {return new Product(1,"test"+id);}
}
App
package com.example;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;/*** Hello world!**/
@SpringBootApplication
public class ProApp
{public static void main( String[] args ){System.out.println( "Hello World!" );SpringApplication.run(ProApp.class );}
}