Spring Cloud Alibaba
Spring Cloud Alibaba
概述
Spring Cloud Alibaba是阿里巴巴开源的微服务解决方案,为Spring Cloud生态提供了一套完整的微服务开发工具。它包含了服务发现、配置管理、消息队列、分布式事务等组件,特别适合在阿里云环境中使用。
核心组件
Nacos - 服务发现与配置管理
服务发现
java
@SpringBootApplication
@EnableDiscoveryClient
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}
@SpringBootApplication
@EnableDiscoveryClient
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}
yaml
spring:
application:
name: service-provider
cloud:
nacos:
discovery:
server-addr: localhost:8848
spring:
application:
name: service-provider
cloud:
nacos:
discovery:
server-addr: localhost:8848
配置管理
yaml
spring:
application:
name: service-provider
cloud:
nacos:
config:
server-addr: localhost:8848
file-extension: yaml
group: DEFAULT_GROUP
spring:
application:
name: service-provider
cloud:
nacos:
config:
server-addr: localhost:8848
file-extension: yaml
group: DEFAULT_GROUP
java
@RestController
@RefreshScope
public class ConfigController {
@Value("${user.name:unknown}")
private String userName;
@GetMapping("/config")
public String getConfig() {
return "User name: " + userName;
}
}
@RestController
@RefreshScope
public class ConfigController {
@Value("${user.name:unknown}")
private String userName;
@GetMapping("/config")
public String getConfig() {
return "User name: " + userName;
}
}
Sentinel - 流量控制与熔断降级
基本使用
xml
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
java
@RestController
public class TestController {
@GetMapping("/hello")
@SentinelResource(value = "hello", fallback = "helloFallback")
public String hello() {
return "Hello Sentinel";
}
public String helloFallback() {
return "Hello Fallback";
}
}
@RestController
public class TestController {
@GetMapping("/hello")
@SentinelResource(value = "hello", fallback = "helloFallback")
public String hello() {
return "Hello Sentinel";
}
public String helloFallback() {
return "Hello Fallback";
}
}
流控规则配置
java
@PostConstruct
public void initFlowRules() {
List<FlowRule> rules = new ArrayList<>();
FlowRule rule = new FlowRule();
rule.setResource("hello");
rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
rule.setCount(20);
rules.add(rule);
FlowRuleManager.loadRules(rules);
}
@PostConstruct
public void initFlowRules() {
List<FlowRule> rules = new ArrayList<>();
FlowRule rule = new FlowRule();
rule.setResource("hello");
rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
rule.setCount(20);
rules.add(rule);
FlowRuleManager.loadRules(rules);
}
RocketMQ - 消息队列
xml
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rocketmq</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rocketmq</artifactId>
</dependency>
消息生产者
java
@Component
public class MessageProducer {
@Autowired
private RocketMQTemplate rocketMQTemplate;
public void sendMessage(String topic, Object message) {
rocketMQTemplate.convertAndSend(topic, message);
}
}
@Component
public class MessageProducer {
@Autowired
private RocketMQTemplate rocketMQTemplate;
public void sendMessage(String topic, Object message) {
rocketMQTemplate.convertAndSend(topic, message);
}
}
消息消费者
java
@Component
@RocketMQMessageListener(topic = "test-topic", consumerGroup = "test-group")
public class MessageConsumer implements RocketMQListener<String> {
@Override
public void onMessage(String message) {
System.out.println("Received message: " + message);
}
}
@Component
@RocketMQMessageListener(topic = "test-topic", consumerGroup = "test-group")
public class MessageConsumer implements RocketMQListener<String> {
@Override
public void onMessage(String message) {
System.out.println("Received message: " + message);
}
}
Seata - 分布式事务
xml
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
</dependency>
java
@Service
public class BusinessService {
@Autowired
private StorageService storageService;
@Autowired
private OrderService orderService;
@GlobalTransactional
public void purchase(String userId, String commodityCode, int orderCount) {
storageService.deduct(commodityCode, orderCount);
orderService.create(userId, commodityCode, orderCount);
}
}
@Service
public class BusinessService {
@Autowired
private StorageService storageService;
@Autowired
private OrderService orderService;
@GlobalTransactional
public void purchase(String userId, String commodityCode, int orderCount) {
storageService.deduct(commodityCode, orderCount);
orderService.create(userId, commodityCode, orderCount);
}
}
与Spring Cloud对比
功能 | Spring Cloud | Spring Cloud Alibaba |
---|---|---|
服务发现 | Eureka, Consul | Nacos |
配置管理 | Config Server | Nacos Config |
负载均衡 | Ribbon, LoadBalancer | Ribbon, LoadBalancer |
熔断降级 | Hystrix, Resilience4j | Sentinel |
网关 | Gateway, Zuul | Gateway |
消息队列 | - | RocketMQ |
分布式事务 | - | Seata |
部署架构
单机部署
bash
# 启动Nacos
sh startup.sh -m standalone
# 启动Sentinel Dashboard
java -jar sentinel-dashboard.jar
# 启动Seata Server
sh seata-server.sh
# 启动Nacos
sh startup.sh -m standalone
# 启动Sentinel Dashboard
java -jar sentinel-dashboard.jar
# 启动Seata Server
sh seata-server.sh
集群部署
yaml
# Nacos集群配置
spring:
cloud:
nacos:
discovery:
server-addr: nacos1:8848,nacos2:8848,nacos3:8848
# Nacos集群配置
spring:
cloud:
nacos:
discovery:
server-addr: nacos1:8848,nacos2:8848,nacos3:8848
最佳实践
- 服务拆分:合理设计服务边界
- 配置管理:使用Nacos统一管理配置
- 流量控制:通过Sentinel保护系统稳定性
- 消息解耦:使用RocketMQ实现服务间异步通信
- 事务管理:合理使用Seata处理分布式事务
- 监控告警:集成监控系统,及时发现问题
总结
Spring Cloud Alibaba提供了完整的微服务解决方案,特别适合在阿里云环境中使用。它的组件相比Netflix组件更加活跃,功能也更加丰富,是构建微服务架构的优秀选择。