SpringCloud Alibaba Nacos服务注册和配置中心
Nacos就是注册中心+配置中心的组合==等价于===eureka+config+Bus
Namespace+Group+Data (类比为Java中的工程、包、类)
其中的namespace是用来区分不同的部署环境的(例如dev、test、prod)
group类似于Java中的包,用来对不同类型的微服务配置文件进行分组管理
server: port: 3377 spring: application: name: nacos-config-client cloud: nacos: discovery: server-addr: localhost:8848 #Nacos服务注册中心地址 config: server-addr: localhost:8848 #Nacos作为配置中心地址 file-extension: yaml #指定yaml格式的配置 namespace: 7a901d46-e75e-4e6a-b186-5980cca4249b group:组名字 # ${spring.application.name}-${spring.profile.active}.${spring.cloud.nacos.config.file-extension} #nacos-config-client-dev.yaml
Nacos持久化配置解释
Nacos默认自带的嵌入式数据库derby
derby到mysql的切换
- nacos-server-1.1.4\nacos\conf目录下找到nacos-mysql.sqlsql脚本执行
- nacos-server-1.1.4\nacos\conf目录下找到application.properties
SpringCloud Alibaba Sentinel实现熔断与限流
下载:https://github.com/alibaba/Sentinel
sentinel采用的是懒加载(执行一次即可)
流控规则
流控模式
- 直接:快速失败
- 关联:当关联的资源达到阙值时,就限流自己
- 链路:多个请求调用了同一个微服务
流控效果
- 直接:快速失败
- 预热:阙值除以coldFactor(默认值为3),经过预热时长后才会达到阙值
- 排队等待:匀速排队,阙值必须设置为QPS
自定义流控配置
@Configuration public class GulimallProductSentinelConfig { public GulimallProductSentinelConfig() { WebCallbackManager.setUrlBlockHandler(new UrlBlockHandler() { @Override public void blocked(HttpServletRequest request, HttpServletResponse response, BlockException ex) throws IOException { R error = R.error(BizCodeEnum.TO_MANY_REQUEST.getCode(), BizCodeEnum.TO_MANY_REQUEST.getMessage()); response.setCharacterEncoding("UTF-8"); response.setContentType("application/json"); response.getWriter().write(JSON.toJSONString(error)); } }); } }
服务降级
热点key限流
@RequestMapping("/testHotKey") @SentinelResource(value = "testHotKey",blockHandler = "deal_testHotKet") public String testHotKey( @RequestParam(value = "p1",required = false) String p1, @RequestParam(value = "p2",required = false) String p2) { System.out.println("testHotKey 热点Key--测试"); int age = 10/0; return "testHotKey-------"; } public String deal_testHotKet(String p1, String p2, BlockException e) { return "----deal_testHotKet,------"; }
参数例外项(参数索引0代表第一个参数)
系统规则
相当于全局配置
@SentinelResource注解
blockhandler>fallback
搭配feign
1:引入sentinel和openfeign依赖
2:
3:
规则持久化
server: port: 8401 spring: application: name: cloudalibaba-sentinel-service cloud: nacos: discovery: server-addr: localhost:8848 sentinel: transport: dashboard: localhost:8080 port: 8719 datasource: ds1: nacos: server-addr: localhost:8848 dataId: ${spring.application.name} groupId: DEFAULT_GROUP data_type: json rule_type: flow management: endpoints: web: exposure: include: "*"
SpringCloud Alibaba Seata处理分布式事务
一次业务操作需要垮多个数据源或需要垮多个系统进行远程调用,就会产生分布式事务问题
分布式事务处理过程-ID+三组件模型
Transaction ID(XID):全局唯一的事务id
Transaction Coordinator(TC):事务协调器,维护全局事务的运行状态,负责协调并驱动全局事务的提交或回滚
Transaction Manager(TM):控制全局事务的边界,负责开启一个全局事务,并最终发起全局提交或全局回滚的决议
Resource Manager(RM):控制分支事务,负责分支注册、状态汇报,并接受事务协调的指令,驱动分支(本地)事务的提交和回滚
处理过程
seata-server的配置
1:修改conf目录下的file.conf配置文件
2:mysql数据库新建库seata
建表db_store.sql在seata-server-0.9.0\seata\conf目录里面(db_store.sql),新建表
-- the table to store GlobalSession data drop table if exists `global_table`; create table `global_table` ( `xid` varchar(128) not null, `transaction_id` bigint, `status` tinyint not null, `application_id` varchar(32), `transaction_service_group` varchar(32), `transaction_name` varchar(128), `timeout` int, `begin_time` bigint, `application_data` varchar(2000), `gmt_create` datetime, `gmt_modified` datetime, primary key (`xid`), key `idx_gmt_modified_status` (`gmt_modified`, `status`), key `idx_transaction_id` (`transaction_id`) ); -- the table to store BranchSession data drop table if exists `branch_table`; create table `branch_table` ( `branch_id` bigint not null, `xid` varchar(128) not null, `transaction_id` bigint , `resource_group_id` varchar(32), `resource_id` varchar(256) , `lock_key` varchar(128) , `branch_type` varchar(8) , `status` tinyint, `client_id` varchar(64), `application_data` varchar(2000), `gmt_create` datetime, `gmt_modified` datetime, primary key (`branch_id`), key `idx_xid` (`xid`) ); -- the table to store lock data drop table if exists `lock_table`; create table `lock_table` ( `row_key` varchar(128) not null, `xid` varchar(96), `transaction_id` long , `branch_id` long, `resource_id` varchar(256) , `table_name` varchar(32) , `pk` varchar(36) , `gmt_create` datetime , `gmt_modified` datetime, primary key(`row_key`) );
3:修改seata-server-0.9.0\seata\conf目录下的registry.conf目录下的registry.conf配置文件