官网
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);
}
}