一 介绍说明
1.1 说明
1.1.1 消费者8081
1.1.2 openfegin接口
1.1.3 提供者9091
9091微服务满足:
1 openfegin 配置fallback逻辑,作为统一fallback服务降级处理。
2.sentinel访问触发了自定义的限流配置,在注解@sentinelResource里面配置blockhandler方法。
二 实操案例
2.1 consumer案例配置
2.1.1 pom配置
<!--alibaba-sentinel--><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-sentinel</artifactId></dependency><!--nacos-config--><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId></dependency><!-- 引入自己定义的api通用包 --><dependency><groupId>com.jurf.ms.api</groupId><artifactId>ms-api-commons</artifactId><version>1.0-SNAPSHOT</version></dependency>
2.1.2 业务代码
@Resourceprivate PayFeignSentinelApi payFeignSentinelApi;
// @Autowired
// private OpenFeginApi openFeingApi;@GetMapping(value = "/consumer/pay/nacos/get/{orderNo}")public ResultData getPayByOrderNo(@PathVariable("orderNo") String orderNo){return payFeignSentinelApi.getPayByOrderNo(orderNo);// return openFeingApi.getPayByOrderNo(orderNo);}
2.1.3 配置文件
2.1.4 启动配置
2.2 openfegin-api的配置
2.2.1 pom文件
<!--openfeign--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency><!--alibaba-sentinel--><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-sentinel</artifactId></dependency>
2.2.2 业务代码
1.代码接口: 注意这里要配置成访问服务的服务名称,和nacos的服务名一致。
/*** @auther zzyy* @create 2024-01-05 13:05*/
@FeignClient(value = "ms-alibaba-provider9091",contextId = "ms-alibaba-provider9091-1",fallback = PayFeignSentinelApiFallBack.class)
public interface PayFeignSentinelApi
{@GetMapping(value = "/pay/nacos/get/{orderNo}")public ResultData getPayByOrderNo(@PathVariable("orderNo") String orderNo);
}
2.fallback实现类
@Component
public class PayFeignSentinelApiFallBack implements PayFeignSentinelApi
{@Overridepublic ResultData getPayByOrderNo(String orderNo){return ResultData.fail(ReturnCodeEnum.RC500.getCode(),"对方服务宕机或不可用,FallBack服务降级o(╥﹏╥)o");}
}
2.3 提供配置
2.3.1 pom配置
<!--openfeign--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency><!--alibaba-sentinel--><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-sentinel</artifactId></dependency>
2.3.2 业务实现
//openfeign+sentinel进行服务降级和流量监控的整合处理case@GetMapping(value = "/pay/nacos/get/{orderNo}")@SentinelResource(value = "getPayByOrderNo",blockHandler = "handlerBlockHandler")public ResultData getPayByOrderNo(@PathVariable("orderNo") String orderNo){//模拟从数据库查询出数据并赋值给DTOPayDTO payDTO = new PayDTO();payDTO.setId(1024);payDTO.setOrderNo(orderNo);payDTO.setAmount(BigDecimal.valueOf(9.9));payDTO.setPayNo("pay:"+ IdUtil.fastUUID());payDTO.setUserId(1);return ResultData.success("查询返回值:"+payDTO);}public ResultData handlerBlockHandler(@PathVariable("orderNo") String orderNo, BlockException exception){return ResultData.fail(ReturnCodeEnum.RC500.getCode(),"getPayByOrderNo服务不可用," +"触发sentinel流控配置规则"+"\t"+"o(╥﹏╥)o");}
2.3.3 配置
2.4 服务启动
1.nacos启动
2.sentinel启动
3.应用服务启动
2.5 验证访问
1.消费者: http://localhost:8081/consumer/pay/nacos/get/999
2.提供者:http://localhost:9091/pay/nacos/get/222
3.验证blockhandler
a) sentinel配置
b)频繁刷新访问: 提示sentinel流控规则访问
4.验证fallback
关闭9091服务提供者
再次访问:提示fallback异常提示。