其他分享
首页 > 其他分享> > SpringCloudAlibaba - RestTemplate 整合 Sentinel

SpringCloudAlibaba - RestTemplate 整合 Sentinel

作者:互联网

目录

前言

记录下RestTemplate整合Sentinel的方式
Sentinel的整合查看:SpringCloudAlibaba - 整合 Sentinel 实现服务限流


环境

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


具体实现


内容中心

@Bean
@LoadBalanced
@SentinelRestTemplate
public RestTemplate restTemplate() {
    return new RestTemplate();
}
@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"
        );
    }

}

用户中心

@RestController
@Slf4j
public class TestController {

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

}

测试

在这里插入图片描述


在这里插入图片描述

在这里插入图片描述



关闭@SentinelRestTemplate注解

resttemplate:
  sentinel:
    enabled: false

- End -
白嫖有风险
点赞加收藏

标签:return,name,RestTemplate,TestController,Sentinel,SpringCloudAlibaba,public
来源: https://www.cnblogs.com/maggieq8324/p/15339593.html