SpringCloudAlibaba

前言

记录下RestTemplate整合Sentinel的方式
Sentinel的整合查看系列文章


环境

Spring Cloud Hoxton.SR9 + Spring Cloud Alibaba 2.2.6.RELEASE + Sentinel 1.8.1


具体实现

  • 实现内容中心使用RestTemplate调用用户中心接口限流

内容中心

  • 使用@SentinelRestTemplate注解为RestTemplate整合Sentinel
@Bean
@LoadBalanced
@SentinelRestTemplate
public RestTemplate restTemplate() {
    return new RestTemplate();
}
  • TestController.java
@RestController
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class TestController {
	private final RestTemplate restTemplate;

	/**
     * 为RestTemplate 整合Sentinel
     * @return
     */
    @GetMapping("test5")
    public String test5() {
        return restTemplate.getForObject(
                "http://user-center/test/{name}",
                String.class,
                "Coisini"
        );
    }

}

用户中心

  • TestController.java
@RestController
@Slf4j
public class TestController {

    @GetMapping("/test/{name}")
    public String test(@PathVariable String name) {
        log.info("请求...");
        return "hello " + name;
    }

}

测试

  • 接口调用

在这里插入图片描述


  • 添加一条QPS1的流控规则

在这里插入图片描述

  • 频繁访问接口被限流

在这里插入图片描述



关闭@SentinelRestTemplate注解

resttemplate:
  sentinel:
    enabled: false

- End -
白嫖有风险
点赞加收藏
以上为本篇文章的主要内容,希望大家多提意见,如果喜欢记得点个推荐哦
作者:Maggieq8324
本文版权归作者和博客园共有,欢迎转载,转载时保留原作者和文章地址即可。
原文地址:https://www.cnblogs.com/maggieq8324/p/15339593.html