@Autowired
private RestTemplate restTemplate;
/**
-
@SentinelResource
-
blockHandler : 声明熔断时调用的降级方法
-
fallback : 抛出异常执行的降级方法
-
value : 自定义的资源名称
-
* 不设置:当前全类名.方法名
*/
@RequestMapping(value = “/buy/{id}”,method = RequestMethod.GET)
public Product findById(@PathVariable Long id) {
if(id != 1) {
throw new RuntimeException(“错误”);
}
return restTemplate.getForObject(“http://service-product/product/1”,Product.class);
}
}
(2)在order_service_rest当中的RestOrderApplication上测试
1)创建ExceptionUtils
package cn.itbluebox.order.exception;
import cn.itbluebox.order.entity.Product;
import com.alibaba.cloud.sentinel.rest.SentinelClientHttpResponse;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.fastjson.JSON;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
public class ExceptionUtils {
/**
-
静态方法
-
返回值: SentinelClientHttpResponse
-
参数 : request , byte[] , clientRquestExcetion , blockException
*/
//限流熔断业务逻辑
public static SentinelClientHttpResponse handleBlock(HttpRequest request, byte[] body,
ClientHttpRequestExecution execution,
BlockException ex) {
Product product = new Product();
product.setProductName(“限流熔断降级”);
return new SentinelClientHttpResponse(JSON.toJSONString(product));
}
//异常降级业务逻辑
public static SentinelClientHttpResponse handleFallback(HttpRequest request, byte[] body,
ClientHttpRequestExecution execution,
BlockException ex) {
Product product = new Product();
product.setProductName(“异常熔断降级”);
return new SentinelClientHttpResponse(JSON.toJSONString(product));
}
}
2)完善RestOrderApplication
package cn.itbluebox.order;
import cn.itbluebox.order.exception.ExceptionUtils;
import com.alibaba.cloud.sentinel.annotation.SentinelRestTemplate;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EntityScan(“cn.itbluebox.order.entity”)
public class RestOrderApplication {
/**
-
sentinel支持对restTemplate的服务调用使用sentinel方法.在构造
-
RestTemplate对象的时候,只需要加载@SentinelRestTemplate即可
-
资源名:
-
httpmethod:schema://host:port/path :协议、主机、端口和路径
-
httpmethod:schema://host:port :协议、主机和端口
-
@SentinelRestTemplate:
-
异常降级
-
fallback : 降级方法
-
fallbackClass : 降级配置类
-
限流熔断
-
blockHandler
-
blockHandlerClass
*/
@LoadBalanced
@Bean
@SentinelRestTemplate(fallbackClass = ExceptionUtils.class,fallback = “handleFallback”,
blockHandler = “handleBlock”,blockHandlerClass = ExceptionUtils.class)
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(RestOrderApplication.class,args);
}
}
3)运行测试
访问:http://localhost:9004/order/buy/1
访问:http://localhost:9004/order/buy/2
访问管理控制台:http://localhost:8080/#/dashboard/degrade/service-order-rest
访问测试:慢一些访问:http://localhost:9004/order/buy/1
快速多次访问
Sentinel 适配了 Feign 组件。如果想使用,除了引入 sentinel-starter 的依赖外还需要 2 个步骤:
-
配置文件打开 sentinel 对 feign 的支持: feign.sentinel.enabled=true
-
加入 openfeign starter 依赖使 sentinel starter 中的自动化配置类生效:
在order_service_feign当中的
(1)引入依赖
com.alibaba.cloud
spring-cloud-starter-alibaba-sentinel
org.springframework.cloud
spring-cloud-starter-openfeign
(2) 开启sentinel 支持
在工程的application.yml中添加sentinel 对 feign 的支持
激活sentinel的支持
feign:
sentinel:
enabled: true
(3)配置FeignClient
在之前我们已经设置好了熔断了直接启动运行即可
访问:http://localhost:9003/order/buy/1
访问:http://localhost:8080/#/dashboard/identity/service-order-feign
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新
如果你觉得这些内容对你有帮助,可以添加V获取:vip1024b (备注Java)
最后
分布式技术专题+面试解析+相关的手写和学习的笔记pdf
还有更多Java笔记分享如下:
一个人可以走的很快,但一群人才能走的更远。如果你从事以下工作或对以下感兴趣,欢迎戳这里加入程序员的圈子,让我们一起学习成长!
AI人工智能、Android移动开发、AIGC大模型、C C#、Go语言、Java、Linux运维、云计算、MySQL、PMP、网络安全、Python爬虫、UE5、UI设计、Unity3D、Web前端开发、产品经理、车载开发、大数据、鸿蒙、计算机网络、嵌入式物联网、软件测试、数据结构与算法、音视频开发、Flutter、IOS开发、PHP开发、.NET、安卓逆向、云计算
写和学习的笔记pdf
还有更多Java笔记分享如下:
[外链图片转存中…(img-1R5632af-1712248012948)]
一个人可以走的很快,但一群人才能走的更远。如果你从事以下工作或对以下感兴趣,欢迎戳这里加入程序员的圈子,让我们一起学习成长!
AI人工智能、Android移动开发、AIGC大模型、C C#、Go语言、Java、Linux运维、云计算、MySQL、PMP、网络安全、Python爬虫、UE5、UI设计、Unity3D、Web前端开发、产品经理、车载开发、大数据、鸿蒙、计算机网络、嵌入式物联网、软件测试、数据结构与算法、音视频开发、Flutter、IOS开发、PHP开发、.NET、安卓逆向、云计算