springCloud集成Sentinel进行限流

1、首先创建一个ParentMoudle的java pom文件项目进行服务项目管理,其中要注意的是需要将配置成如下的dependencyManagement格式,否则子项目会报错;

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

2、依次创建eurekaServer、provider、feignClient(主要是对外暴露),每个项目的parent设置成如下格式

   <parent>
        <groupId>com.yangfan</groupId>
        <artifactId>springcloud_feign_parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
  • 创建eurekaserver项目

        1⃣️pom文件中引入spring-cloud-starter-netflix-eureka-server

<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
            <version>2.1.0.RELEASE</version>
        </dependency>
</dependencies>

       2⃣️ 配置yml文件:核心是设置eurekaClient的注册中心的地址

server:
  port: 9010

spring:
  application:
    name: eureka-server

eureka:
  client:
    service-url:
      #eureka 服务地址,如果是集群的话;需要指定其他集群eureka地址
      defaultZone: http://127.0.0.1:9010/eureka
      # 不注册自己
    registry-with-eureka: false
    #不拉服务
    fetch-registry: false

3⃣️在启动类中开启@EnableEurekaServer注解,开启eurekaServer服务

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }

}
  • 创建provider项目

1⃣️项目引入spring-boot-starter-web项目、spring-cloud-starter-netfix-eureka-client依赖

 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--eureka客户端依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <version>2.1.0.RELEASE</version>
        </dependency>
    </dependencies>

2⃣️设置eurekaServer的注册地址为:http://127.0.0.1:9010/eureka

server:
  port: 9011

spring:
  application:
    name: sentinel-feign-provider

eureka:
  client:
    service-url:
      #eureka 服务地址,如果是集群的话;需要指定其他集群eureka地址
      defaultZone: http://127.0.0.1:9010/eureka #需要注册到注册中心的地址
      # 不注册自己

3⃣️在启动类中开启@EnableEurekaClient注解,表示开启Eureka客户端发现功能

@EnableEurekaClient //开启Eureka客户端发现功能
@SpringBootApplication
public class SentinelFeignProviderApplication {

    public static void main(String[] args) {
        SpringApplication.run(SentinelFeignProviderApplication.class, args);
    }

}
  • 创将feign项目

1⃣️项目中引入spring-cloud-starter-netfix-eureka-client依赖、feign的起步依赖spring-cloud-starter-openfeign、sentinel依赖spring-cloud-starter-alibaba-sentinel

 <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--eureka客户端依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <version>2.1.0.RELEASE</version>
        </dependency>

        <!--feign的起步依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <version>2.1.0.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
            <version>2.1.0.RELEASE</version>
        </dependency>
    </dependencies>

2⃣️yml文件配置:开启sentinel对feign的支持、设置sentinel本地的dashboard地址、向eureka注册中心注册feign服务

server:
  port: 9012

spring:
  application:
    name: sentinel-feign-client
  cloud:
    sentinel:
      transport:
        dashboard: localhost:9000 #开启 sentinel对feign的支持
#激活sentinel的支持
feign:
  sentinel:
    enabled: true

eureka:
  client:
    service-url:
      #eureka 服务地址,如果是集群的话;需要指定其他集群eureka地址
      defaultZone: http://127.0.0.1:9010/eureka #需要注册到注册中心的地址
      # 不注册自己

3⃣️开启@EnableEurekaClient 注解表示向注册中心注册自己@EnableFeignClients表示开启FeignClient服务

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class SentinelFeignClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(SentinelFeignClientApplication.class, args);
    }

}

 4⃣️创建一个FallbackService实现自FeignAgent并实现hello方法

@Component
public class FallbackService implements FeignAgent {
    @Override
    public String hello() {
        return "系统繁忙请稍后!";
    }
}

5⃣️在Feign中新建一个Controller,并引入FeignClient,在hello方法中调用feign的hello方法

@RestController
public class TestController {

    @Autowired
    private FeignAgent feignAgent;

    @GetMapping("/hello")
    public String hello(){
        return feignAgent.hello();
    }
}

6⃣️配置sentinel dashboard的流控规则,流控规则的编写形式为:http:请求方式:协议://服务名/请求路径跟参数 (GET:http://sentinel-feign-provider/hello)

最终效果如下

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Cloud集成Sentinel非常简单,只需要添加SentinelSentinel Dashboard的依赖,然后在启动类上加上`@EnableCircuitBreaker`注解即可。下面我们来详细介绍一下整个过程。 1. 添加依赖 在pom.xml文件中添加如下依赖: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-sentinel-datasource-nacos</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-nacos-discovery</artifactId> </dependency> ``` 其中,`spring-cloud-starter-alibaba-sentinel`是Sentinel的依赖,`spring-cloud-alibaba-sentinel-datasource-nacos`是Sentinel使用Nacos作为数据源的依赖,`spring-cloud-alibaba-nacos-discovery`是Nacos的依赖。 2. 启用Sentinel 在启动类上添加`@EnableCircuitBreaker`注解,启用Sentinel: ```java @SpringBootApplication @EnableDiscoveryClient @EnableCircuitBreaker public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 3. 配置Sentinel 在application.yml文件中添加如下配置: ```yaml spring: application: name: service-provider cloud: sentinel: transport: dashboard: localhost:8080 datasource: ds1: nacos: server-addr: localhost:8848 dataId: ${spring.application.name}-flow-rules groupId: DEFAULT_GROUP rule-type: flow ``` 其中,`transport.dashboard`配置Sentinel Dashboard的地址,`datasource`配置Sentinel使用Nacos作为数据源,`nacos.server-addr`配置Nacos的地址,`dataId`指定流控规则的Data ID,`groupId`指定Nacos的Group ID,`rule-type`指定规则类型。 4. 配置Sentinel Dashboard 下载Sentinel Dashboard,启动Sentinel Dashboard: ```bash java -jar sentinel-dashboard-1.8.1.jar ``` 访问http://localhost:8080即可查看Sentinel Dashboard。 5. 配置流控规则 在Nacos中添加流控规则。以service-provider服务为例,添加Data ID为service-provider-flow-rules的配置项,内容为: ```json [ { "resource": "hello", "count": 3.0, "grade": 1, "limitApp": "default", "strategy": 0, "controlBehavior": 0, "clusterMode": false } ] ``` 其中,`resource`指定资源名称,`count`指定阈值,`grade`指定流控模式,`limitApp`指定流控针对的调用来源,`strategy`指定流控策略,`controlBehavior`指定流控效果,`clusterMode`指定是否为集群模式。 6. 测试 启动服务提供者和服务消费者,访问http://localhost:8081/hello,即可触发流控规则,Sentinel会阻止请求并返回限流信息。 以上就是Spring Cloud集成Sentinel的整个过程,希望对你有所帮助。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值