RestTemplate和Sentinel整合

这篇具有很好参考价值的文章主要介绍了RestTemplate和Sentinel整合。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

引入restTemplate

@EnableDiscoveryClient
@EnableFeignClients
@SpringBootApplication
public class OrderApplication {

    @Bean
    @LoadBalanced
    //添加注解@SentinelRestTemplate
    @SentinelRestTemplate
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }
    public static void main(String[] args) {
        SpringApplication.run(OrderApplication.class);
    }
}

控制器,通过RestTemplate 实现http请求

@RestController
public class RestTemplateController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/order/{id}")
    public String getUser(@PathVariable Integer id){
      String result =  restTemplate.getForObject("http://msb-user:10001/user/" + id, String.class);
        return result;
    }
}

服务提供者

@RestController
public class UserController {

    @GetMapping("/user/{userId}")
    public String getUserName(@PathVariable Integer userId){
        return "善缘老师";
    }
}

添加流控

注意选对流控请求
RestTemplate和Sentinel整合,sentinel,sentinel

正常请求
RestTemplate和Sentinel整合,sentinel,sentinel
频繁请求
RestTemplate和Sentinel整合,sentinel,sentinel

统一异常处理

注解添加属性
RestTemplate和Sentinel整合,sentinel,sentinel

   @Bean
    @LoadBalanced
    @SentinelRestTemplate(blockHandler = "handleException",
            blockHandlerClass= GlobalException.class
            ,fallback = "fallback",
            fallbackClass = GlobalException.class)
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }
    public static void main(String[] args) {
        SpringApplication.run(OrderApplication.class);
    }

异常类注意对象的引用,引用错误则启动报错

import com.alibaba.cloud.sentinel.rest.SentinelClientHttpResponse;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.msb.order.util.Result;
import org.springframework.http.HttpRequest;

import org.springframework.http.client.ClientHttpRequestExecution;

public class GlobalException {

    public static SentinelClientHttpResponse handleException(HttpRequest request,
                                                             byte[] body, ClientHttpRequestExecution execution, BlockException ex) {
        Result r = Result.error(-1, "===被限流啦===");
        System.out.println("近日来零三零三零是否");
        try {
            return new SentinelClientHttpResponse(new ObjectMapper().writeValueAsString(r));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static SentinelClientHttpResponse fallback(HttpRequest request,
                                                      byte[] body, ClientHttpRequestExecution execution, BlockException ex) {
        Result r = Result.error(-2, "===被异常降级啦===");
        System.out.println("近日来零三零三零是否2");
        try {
            return new SentinelClientHttpResponse(new ObjectMapper().writeValueAsString(r));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }
}

RestTemplate和Sentinel整合,sentinel,sentinel
异常请求
RestTemplate和Sentinel整合,sentinel,sentinel文章来源地址https://www.toymoban.com/news/detail-557614.html

到了这里,关于RestTemplate和Sentinel整合的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • SpringCloud Alibaba Sentinel整合GateWay

    在微服务系统中,网关提供了微服务系统的统一入口,所以我们在做限流的时候,肯定是要在网关层面做一个流量的控制,Sentinel 支持对 Spring Cloud Gateway、Zuul 等主流的 API Gateway 进行限流。 网关: Sentinel 1.6.0 引入了 Sentinel API Gateway Adapter Common 模块,此模块中包含网关限流的

    2024年02月16日
    浏览(31)
  • 强强联合:OpenFeign 整合 Sentinel

    书接前文: 微服务间的远程接口调用:OpenFeign 的使用 当项目中使用了  OpenFeign  后,可以很方便的进行远程服务调用,现在有个问题,假如远程服务出现故障了,调不了远程的接口,这边又着急等着返回结果,怎么办呢? 当然是使用  服务降级  ,本篇就使用  OpenFeign  进

    2023年04月11日
    浏览(32)
  • sentinel整合nacos配置中心持久化

    在网上找了很多的资料,发现sentinel整合nacos持久化的博文和视频大多数都只有改造限流部分的教程,并且都需要修改前端,略显麻烦,至于剩下的熔断、热点流控、授权的更是没有相关的改造教程,最后在知乎的看到一篇文章后让我大受启发 这位前辈讲到sentinel原来是把配置

    2024年02月04日
    浏览(40)
  • 【微服务】Feign 整合 Sentinel,深入探索 Sentinel 的隔离和熔断降级规则,以及授权规则和自定义异常返回结果

    在前文中,介绍了 Sentinel 的流控模式和流控效果,然而限流只是一种预防措施,虽然可以尽量避免因为并发问题而引起的服务故障,但服务仍然可能因其他因素而发生故障。为了将这些故障控制在一定范围内,以避免雪崩效应的发生,我们需要依赖线程隔离(舱壁模式)和熔

    2024年02月08日
    浏览(27)
  • Spring Cloud 之 Sentinel简介与GATEWAY整合实现

    随着微服务的流行,服务和服务之间的稳定性变得越来越重要。Sentinel 是面向分布式服务架构的流量控制组件,主要以流量为切入点,从限流、流量整形、熔断降级、系统负载保护、热点防护等多个维度来帮助开发者保障微服务的稳定性。 熔断 微服务架构的系统通常会包含

    2024年02月19日
    浏览(33)
  • SpringCloud之Gateway整合Sentinel服务降级和限流

    1.下载Sentinel.jar可以图形界面配置限流和降级规则 地址:可能需要翻墙 下载jar文件 2.引入maven依赖 3.写个自动注入Resource的过滤器类(可以不写注解直接使用) 4.写配置文件 application.properties 5.cmd命令行启动jar文件访问localhost:18080页面,自己设置QPS java -jar -server.port=18080 sentinel-dash

    2024年02月07日
    浏览(32)
  • Spring Cloud Gateway 整合 sentinel 实现流控熔断

            在微服务架构中,网关层可以屏蔽外部服务直接对内部服务进行调用,对内部服务起到隔离保护的作用,网关限流,顾名思义,就是通过网关层对服务进行限流,从而达到保护后端服务的作用。         Sentinel 从 1.6.0 版本开始就提供了 Spring Cloud Gateway 的适配

    2023年04月23日
    浏览(35)
  • 【springcloud 微服务】Spring Cloud Alibaba整合Sentinel详解

    目录 一、前言 二、环境准备 2.1 部署sentinel管控台 2.1.1 官网下载sentinel的jar包 2.1.2 启动控制台

    2023年04月09日
    浏览(39)
  • Spring Cloud Sentinel整合Nacos实现配置持久化

    sentinel配置相关配置后无法持久化,服务重启之后就没了,所以整合nacos,在nacos服务持久化,sentinel实时与nacos通信获取相关配置。 使用上一章节Feign消费者服务实现整合。 版本信息: nacos:1.4.1 Sentinel 控制台 1.7.2 spring-boot:2.3.3.RELEASE spring.cloud.version:Hoxton.SR8 spring.cloud.alibaba.v

    2024年02月08日
    浏览(44)
  • 【分布式微服务专题】从单体到分布式(四、SpringCloud整合Sentinel)

    相对来说,Sentinel的学习难度比之之前的Dubbo要低了不少。不过在学习过程中也遇到了一些认知局限带来的困难。比如,虽然还是学习了Sentinel,但是不知道生产环境该如何配置才是最佳的状态。 说到底,Sentinel还是没有提供很好的文档支持。比如:生产最佳实践方案。 官方也

    2024年01月23日
    浏览(37)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包