SpringCloudAlibaba
Spring Cloud Alibaba基本概念和功能
Spring Cloud Alibaba是阿里巴巴提供的微服务开发一站式解决方案,是阿里巴巴开源中间件与Spring Cloud体系的融合。它提供了分布式应用服务开发的一站式解决方案,包含开发分布式应用微服务的必需组件,方便开发者通过Spring Cloud编程模型轻松使用这些组件来开发分布式应用服务。下面是Spring Cloud Alibaba的基本概念和功能:
- 服务注册与发现:Spring Cloud Alibaba提供了Nacos作为服务注册与发现组件,支持DNS-based和RPC-based的服务发现。
- 配置管理:Spring Cloud Alibaba提供了Nacos作为配置中心,支持动态配置管理,可以实现配置的实时更新。
- 流量控制:Spring Cloud Alibaba提供了Sentinel作为流量控制组件,可以实现流量控制、熔断降级、系统负载保护等功能。
- 分布式事务:Spring Cloud Alibaba提供了Seata作为分布式事务解决方案,支持AT、TCC、SAGA、XA等模式。
- 消息驱动:Spring Cloud Alibaba提供了RocketMQ作为消息驱动组件,支持异步消息处理、流量削峰、应用解耦等功能。
为什么要用Spring Cloud Alibaba?
使用Spring Cloud Alibaba有以下几个优点:
- 阿里巴巴生态:Spring Cloud Alibaba集成了阿里巴巴开源的中间件,经过了阿里巴巴大规模生产验证,稳定可靠。
- 中文文档:Spring Cloud Alibaba提供了完善的中文文档,方便国内开发者学习和使用。
- 社区活跃:Spring Cloud Alibaba有着活跃的社区支持,可以保证组件的更新和维护。
- 功能丰富:Spring Cloud Alibaba提供了丰富的微服务组件,可以满足各种业务场景的需求。
- 易于扩展:Spring Cloud Alibaba提供了丰富的扩展点,可以根据业务需求进行扩展和定制。
总之,使用Spring Cloud Alibaba可以帮助开发者快速构建微服务架构,提高开发效率,降低运维成本。
Spring Cloud Alibaba应用场景
Spring Cloud Alibaba主要应用于微服务架构、分布式系统、云原生应用等场景,适合构建大规模、高可用、可扩展的应用系统。
Spring Cloud Alibaba核心组件
Spring Cloud Alibaba的核心组件包括:
- Nacos:服务注册与发现、配置管理
- Sentinel:流量控制、熔断降级、系统负载保护
- Seata:分布式事务解决方案
- RocketMQ:消息驱动微服务
- Dubbo:高性能RPC框架
- Alibaba Cloud OSS:阿里云对象存储服务
- Alibaba Cloud SchedulerX:阿里云分布式任务调度
Spring Cloud Alibaba核心架构图
+------------------+ +------------------+ +------------------+
| Service | | Service | | Service |
| Provider A | | Provider B | | Provider C |
+------------------+ +------------------+ +------------------+
| | |
| Register | Register | Register
| | |
v v v
+------------------+ +------------------+ +------------------+
| Nacos | | Nacos | | Nacos |
| Server | | Server | | Server |
| (注册中心) | | (配置中心) | | (注册中心) |
+------------------+ +------------------+ +------------------+
^ ^ ^
| | |
| Discover | Discover | Discover
| | |
+------------------+ +------------------+ +------------------+
| Service | | Service | | Service |
| Consumer A | | Consumer B | | Consumer C |
+------------------+ +------------------+ +------------------+
| | |
| | |
| | |
+------------------+ +------------------+ +------------------+
| Sentinel | | Sentinel | | Sentinel |
| (流量控制) | | (熔断降级) | | (系统保护) |
+------------------+ +------------------+ +------------------+
| | |
| | |
| | |
+------------------+ +------------------+ +------------------+
| Seata | | Seata | | Seata |
| (分布式事务) | | (分布式事务) | | (分布式事务) |
+------------------+ +------------------+ +------------------+在上面的架构图中,展示了Spring Cloud Alibaba的核心组件之间的关系:服务提供者向Nacos注册服务,服务消费者从Nacos发现服务,通过Sentinel进行流量控制,通过Seata实现分布式事务。
Spring Cloud Alibaba快速入门
1、创建父工程
创建一个Maven父工程,添加Spring Cloud Alibaba依赖:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.0</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2023.0.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2023.0.0.0-RC1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>2、创建服务提供者
创建服务提供者模块:
添加依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>配置文件
server:
port: 8081
spring:
application:
name: service-provider
cloud:
nacos:
discovery:
server-addr: localhost:8848启动类
@SpringBootApplication
@EnableDiscoveryClient
public class ServiceProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceProviderApplication.class, args);
}
}Controller
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello() {
return "Hello from Service Provider!";
}
}3、创建服务消费者
创建服务消费者模块:
添加依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>配置文件
server:
port: 8082
spring:
application:
name: service-consumer
cloud:
nacos:
discovery:
server-addr: localhost:8848启动类
@SpringBootApplication
@EnableDiscoveryClient
public class ServiceConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceConsumerApplication.class, args);
}
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}Controller
@RestController
public class ConsumerController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/consume")
public String consume() {
return restTemplate.getForObject("http://service-provider/hello", String.class);
}
}Nacos配置管理
Nacos不仅提供服务注册与发现功能,还提供了配置管理功能。
1、添加依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>2、配置文件
创建bootstrap.yml:
spring:
application:
name: service-provider
cloud:
nacos:
config:
server-addr: localhost:8848
file-extension: yaml
group: DEFAULT_GROUP3、动态配置
在Nacos控制台创建配置:
- Data ID:service-provider.yaml
- Group:DEFAULT_GROUP
- 配置内容:
user:
name: 张三
age: 254、获取配置
@RestController
@RefreshScope
public class ConfigController {
@Value("${user.name}")
private String name;
@Value("${user.age}")
private Integer age;
@GetMapping("/config")
public String config() {
return "name: " + name + ", age: " + age;
}
}Sentinel流量控制
Sentinel是阿里巴巴开源的流量控制组件,可以实现流量控制、熔断降级、系统负载保护等功能。
1、添加依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>2、配置文件
spring:
cloud:
sentinel:
transport:
dashboard: localhost:8080
port: 87193、流量控制
@RestController
public class HelloController {
@GetMapping("/hello")
@SentinelResource(value = "hello", blockHandler = "handleException")
public String hello() {
return "Hello World!";
}
public String handleException(BlockException exception) {
return "系统繁忙,请稍后再试!";
}
}4、熔断降级
@RestController
public class HelloController {
@GetMapping("/hello")
@SentinelResource(value = "hello", fallback = "handleFallback")
public String hello() {
if (Math.random() > 0.5) {
throw new RuntimeException("发生异常");
}
return "Hello World!";
}
public String handleFallback(Throwable throwable) {
return "服务降级:" + throwable.getMessage();
}
}Seata分布式事务
Seata是阿里巴巴开源的分布式事务解决方案,支持AT、TCC、SAGA、XA等模式。
1、添加依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
</dependency>2、配置文件
seata:
enabled: true
application-id: service-provider
tx-service-group: my_test_tx_group
service:
vgroup-mapping:
my_test_tx_group: default
grouplist:
default: 127.0.0.1:80913、使用分布式事务
@Service
public class OrderService {
@Autowired
private OrderMapper orderMapper;
@Autowired
private StorageClient storageClient;
@Autowired
private AccountClient accountClient;
@GlobalTransactional
public void createOrder(Order order) {
orderMapper.insert(order);
storageClient.decrease(order.getProductId(), order.getCount());
accountClient.decrease(order.getUserId(), order.getMoney());
}
}RocketMQ消息驱动
RocketMQ是阿里巴巴开源的消息中间件,支持异步消息处理、流量削峰、应用解耦等功能。
1、添加依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rocketmq</artifactId>
</dependency>2、配置文件
spring:
cloud:
stream:
bindings:
output:
destination: test-topic
content-type: application/json
input:
destination: test-topic
content-type: application/json
group: test-group
rocketmq:
binder:
name-server: localhost:98763、发送消息
@Service
public class MessageService {
@Autowired
private Source source;
public void sendMessage(String message) {
source.output().send(MessageBuilder.withPayload(message).build());
}
}4、接收消息
@Service
public class MessageConsumer {
@StreamListener(Sink.INPUT)
public void receive(String message) {
System.out.println("Received: " + message);
}
}参考链接
https://spring-cloud-alibaba-group.github.io/
