【sentinel】【02】Springboot/SpringCloud 适配sentinel

官网

https://github.com/alibaba/Sentinel/wiki/%E4%B8%BB%E6%B5%81%E6%A1%86%E6%9E%B6%E7%9A%84%E9%80%82%E9%85%8D#spring-cloud

1.引入jar包

<!-- https://mvnrepository.com/artifact/com.alibaba.cloud/spring-cloud-starter-alibaba-sentinel -->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
    <version>1.5.1.RELEASE</version>
</dependency>

2.启动springboot应用

java  -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=user -jar user.jar 

2.1springboot 1.3.x启动会报错,sentinel最少需要springboot1.4.x

Caused by: java.lang.ClassNotFoundException: org.springframework.boot.web.servlet.FilterRegistrationBean
因为FilterRegistrationBean在1.3.x的位置是org.springframework.boot.context.embedded.FilterRegistrationBean
1.4.x之后开始FilterRegistrationBean位置修改为org.springframework.boot.web.servlet.FilterRegistrationBean

2.2选择springboot版本和springcloud版本

https://blog.csdn.net/qq32933432/article/details/89375630
1.4.7.RELEASE
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-parent -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.7.RELEASE</version>
    <type>pom</type>
</dependency>

Camden.RELEASE
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-dependencies -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-dependencies</artifactId>
    <version>Camden.RELEASE</version>
    <type>pom</type>
    <scope>runtime</scope>
</dependency>

3.使用@SentinelResource标签限制资源

例如
@Service
public class UserServiceImpl implements UserService{

	@Override
	@SentinelResource(value = "getUserService")
	public Map<String, Object> getUser() {
		 Map<String,Object> userMap = new HashMap<String,Object>();
		 userMap.put("id", "1");
		 userMap.put("name", "银真");
		 userMap.put("age", "31");
		 return userMap;
	}

}

4.@SentinelRestTemplate支持ribbon

例如
@SpringBootApplication
@EnableEurekaClient
public class OrderApplication {
	
	@Bean
    @LoadBalanced
    @SentinelRestTemplate
    RestTemplate restTemplate() {
        RestTemplate restTemplate = new RestTemplate();
        return restTemplate;
    }

	public static void main(String[] args) {
		 new SpringApplicationBuilder(OrderApplication.class).web(true).run(args);
	}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值