1.下载sentinel dashboard并启动

java -Dserver.port=9999 -Dcsp.sentinel.dashboard.server=localhost:9999 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.7.0.jar

2.在application.properties中增加sentinel和Eureka相关的配置

server.port=8813
spring.application.name=xiaozhang
eureka.client.serviceUrl.defaultZone=http://139.198.xx.xx:8761/eureka/,http://81.70.xx.xx:8761/eureka/
management.endpoints.web.exposure.include=*
spring.cloud.loadbalancer.ribbon.enabled=false
spring.cloud.sentinel.transport.dashboard=localhost:9999
feign.sentinel.enabled=true
resttemplate.sentinel.enabled=true

3.在pom.xml中增加相关依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<groupId>cn.edu.tju</groupId>
<artifactId>springcloudsentinelflowcontroll2</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>



<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.0.0.RELEASE</version>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-openfeign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

<!-- 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>2.0.4.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.0.4.RELEASE</version>

</dependency>


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>


</project>

4.在启动类上加上注解
@EnableEurekaClient
5.编写RestTemplate配置类及限流类

package cn.edu.tju.config;

import com.alibaba.cloud.sentinel.annotation.SentinelRestTemplate;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;


@Configuration
public class MyConfig {
@Bean
@LoadBalanced
@SentinelRestTemplate(fallback = "handleException", fallbackClass = ExceptionUtil.class, blockHandler="handleBlock",blockHandlerClass=ExceptionUtil.class)
public RestTemplate restTemplate() {
return new RestTemplate();
}

}
package cn.edu.tju.config;

import com.alibaba.cloud.sentinel.rest.SentinelClientHttpResponse;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;

public class ExceptionUtil {
public static SentinelClientHttpResponse handleException(HttpRequest request,
byte[] body, ClientHttpRequestExecution execution, BlockException ex) {
System.err.println("Exception: " + ex.getClass().getCanonicalName());
return new SentinelClientHttpResponse("异常发生了");
}

public static SentinelClientHttpResponse handleBlock(HttpRequest request,
byte[] body, ClientHttpRequestExecution execution, BlockException ex) {
System.err.println("flow limitation: " + ex.getClass().getCanonicalName());
return new SentinelClientHttpResponse("限流发生了");
}
}

6.编写controller类

package cn.edu.tju.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class RestTemplateController {
@Autowired
private RestTemplate restTemplate;
@RequestMapping("/rest")
public String rest(){
String result=restTemplate.getForObject("http://demoservice/test", String.class);

return result;
}

}

7.在sentinel控制台页面(http://localhost:9999,用户名密码sentinel)增加流控规则,主要是要配置资源名(请求类型加冒号加Feign请求的地址)和阈值

SpringCloud: sentinel在具体服务上限流(RestTemplate)_java


8.启动应用,快速访问控制器类中的接口地址(可快速点击刷新按钮),可以看到限流类中的方法被调用了

SpringCloud: sentinel在具体服务上限流(RestTemplate)_java_02