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=xiaowei
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>springcloudsentinelflowcontroll</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.在启动类上加上注解
@EnableFeignClients
@EnableEurekaClient
5.编写FeinClient接口及限流类、限流配置类
package cn.edu.tju.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
//调用echo方法时,会去Eureka中去查找demoservice的地址,最终请求类似于RestTemplate的http://demoservice/test
@FeignClient(name = "demoservice", fallback = EchoServiceFallback.class, configuration = FeignConfiguration.class)
public interface EchoService {
@GetMapping(value = "/test")
String echo( String str);
}
//配置限流实现类
class FeignConfiguration {
@Bean
public EchoServiceFallback echoServiceFallback() {
return new EchoServiceFallback();
}
}
//用来限流的实现类,要实现EchoService中的方法,也就是echo方法
class EchoServiceFallback implements EchoService {
@Override
public String echo( String str) {
return "限流发生了,请求字符串为: "+str;
}
}
6.编写controller类
package cn.edu.tju.controller;
import cn.edu.tju.service.EchoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@Autowired
private EchoService echoService;
@RequestMapping("/hi/{str}")
public String hi(@PathVariable String str){
return echoService.echo(str);
}
}
7.打开sentinel控制台页面(http://localhost:9999,用户名密码sentinel),编写流量控制规则,主要是要配置资源名(请求类型加冒号加Feign请求的地址)和阈值
8.启动应用,快速访问控制器类中的接口地址(可快速点击刷新按钮),可以看到限流类中的方法被调用了