本项目基于Spring Cloud Greenwich.RELEASE, Spring Cloud Alibaba 2.1.0, Nacos 1.1.3, SEATA 0.9.0 环境准备:
- Nacos Server参见官网,这里10.0.0.21:8848
- SEATA Server参见官网,这里10.0.0.22:8091
- 带有InnoDB引擎的MySQL
本部分借鉴了文章http://www.springcloud.cn/view/412
[
{
"id": "",
"uri": "http://www.baidu.com",
"order": 1111,
"filters": [
{
"name": "StripPrefix",
"args": {
"_genkey_0": "1"
}
}
],
"predicates": [
{
"name": "Path",
"args": {
"_genkey_0": "/baidu/**"
}
}
],
"description": "测试路由新增"
}
]原理:服务提供者增加元数据配置不同的版本号,服务消费者同样进行版本号配置,那么只需要判断当前的匹配规则,就可以实现有选择性的跳转到指定的服务提供者实例。 本示例实现版本流量控制,当访问端HTTP Header指定对应参数VERSION,后续经过网关,或使用远程Feign,或使用RestTemplate访问的接口都会进行流量过滤,只有当目的服务的Nacos版本元数据和请求头的版本数据匹配时,对应目标服务接口才会响应。流量控制是全链路的,也就是说当客户端访问服务A,而服务A再访问服务B,而服务B继续访问服务C直至响应回客户端,所有链路的服务都要做版本流量过滤。
<dependency>
<groupId>com.example</groupId>
<artifactId>sc-gateway-sidecar</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>ribbon:
filter:
metadata:
enabled: true com.example.scgatewaysidecar, com.example.scsidecar
<dependency>
<groupId>com.example</groupId>
<artifactId>sc-sidecar</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>ribbon:
filter:
metadata:
enabled: true com.example.scsidecar
| 实例 | 端口 | 版本元数据 |
|---|---|---|
| sc-consumer | 8846 | VERSION:1.0 |
| sc-consumer | 8847 | VERSION:2.0 |
| sc-gateway-server | 8080 | VERSION:1.0 |
| sc-gateway-server | 8081 | VERSION:2.0 |
| sc-product | 8844 | VERSION:1.0 |
| sc-product | 8845 | VERSION:2.0 |
通过sc-consumer使用RestTemplate访问sc-gateway-server的接口:
curl -X GET \
http://localhost:8080/v1/consumer/echo/app-name \
-H 'VERSION: 2.0'
通过sc-consumer使用FeignClient访问sc-gateway-server的接口:
curl -X GET \
http://localhost:8080/v1/consumer/get/gateway \
-H 'VERSION: 2.0'
通过sc-consumer使用FeignClient访问sc-product的接口:
curl -X GET \
http://localhost:8080/v1/consumer/get/product \
-H 'VERSION: 2.0'
custom:
test-value: nacos测试:
curl -X GET \
http://localhost:8080/v1/consumer/get/test-value \
-H 'VERSION: 1.0'
示例由官网示例演变而来,未尽事宜参见官网文档。
连接MySQL并执行sc-docs/db下面的SQL文件。 注意:实际上,示例用例中的3个服务应该有3个数据库。但是,我们只需创建一个数据库并配置3个数据源即可。
准备:
- 复制sc-account/src/main/resources/registry.conf, 覆盖conf/registry.conf
- 修改 conf/nacos-config.txt配置
删除
service.vgroup_mapping.my_test_tx_group=default
新增
service.vgroup_mapping.sc-storage-fescar-service-group=default
service.vgroup_mapping.sc-order-fescar-service-group=default
service.vgroup_mapping.sc-business-fescar-service-group=default
service.vgroup_mapping.sc-account-fescar-service-group=default
也可以在 Nacos 配置页面添加,data-id 为 service.vgroup_mapping.${YOUR_SERVICE_NAME}-fescar-service-group, group 为 SEATA_GROUP, 如果不添加该配置,启动后会提示no available server to connect 注意配置文件末尾有空行,需要删除。
- 同步配置数据到Nacos
cd conf
sh nacos-config.sh 10.0.0.21
成功后命令行有提示
init nacos config finished, please start seata-server
在Nacos管理页面应该可以看到有约47个Group为SEATA_GROUP的配置
- 启动SEATA
sh seata-server.sh -p 8091 -h 10.0.0.22 -m file &
启动后在 Nacos 的服务列表下面可以看到一个名为serverAddr的服务。
运行sc-account, sc-business, sc-gateway-server, sc-order, sc-storage. 注意:默认系统版本为1.0,测试需要时可以到Nacos动态配置实现灰度路由。
正常执行完成的方法:
curl -X GET \
http://localhost:8080/v1/business/purchase/commit \
-H 'VERSION: 2.0'
会发生异常并正常回滚的方法:
curl -X GET \
http://localhost:8080/v1/business/purchase/rollback \
-H 'VERSION: 2.0'