Sentinel+Nacos+SpringCloud流量控制实现

环境说明:

Sentinel-1.6.0

Nacos-1.0.0

1.pom.xml配置


        org.springframework.boot
        spring-boot-starter-parent
        2.0.5.RELEASE
    
	
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                war
                import
            
            
	            org.springframework.cloud
	            spring-cloud-alibaba-dependencies
	            0.2.2.RELEASE
	             pom
                import
	        
        
    
        
	
		org.springframework.cloud
		spring-cloud-starter-alibaba-sentinel
	
	 
	
	    com.alibaba.csp
	    sentinel-datasource-nacos
	

2.controller配置

package com.zit.sentinel.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.csp.sentinel.annotation.SentinelResource;

@RestController
@RequestMapping("webservice/sentinel")
public class SentinelController {
	@GetMapping("/hello")
    @SentinelResource(value="hello")// 添加注解
    public String hi(@RequestParam(value = "name", defaultValue = "nacos", required = false) String name) {
        return "hello" + name;
    }
}

3.application.properties配置

## sentinel配置
# HttpServer地址
spring.cloud.sentinel.transport.port=8719
# SentinelDashBoard地址(通过jar启动)
spring.cloud.sentinel.transport.dashboard=192.168.182.1:8080

# sentinel datasource nacos
spring.cloud.sentinel.datasource.ds.nacos.server-addr=localhost:8848
spring.cloud.sentinel.datasource.ds.nacos.data-id=${spring.application.name}-sentinel
spring.cloud.sentinel.datasource.ds.nacos.group-id=DEFAULT_GROUP
spring.cloud.sentinel.datasource.ds.nacos.rule-type=flow

4.Nacos配置

Sentinel+Nacos+SpringCloud流量控制实现_第1张图片

5.启动Sentinel Dashboard

java -jar sentinel-dashboard-1.6.0.jar

Sentinel+Nacos+SpringCloud流量控制实现_第2张图片

6. hello>1次/秒

出现限流信息

Sentinel+Nacos+SpringCloud流量控制实现_第3张图片

代码见:https://github.com/o99o/CAC_SpringBoot_Cloud_Nacos_Producer_Sentinel.git

你可能感兴趣的:(Sentinel,SpringCloud)