dubbo集成sentinel springboot Controller集成sentine

  • 1.首先项目确认版本 如下配置  

    也可以看下http://www.apple-dina.com/view/135 这里的推荐依赖关系,要是自己配置的话可能会有各种jar包的问题


dubbo是2.7.8 

sentinel 是1.8.0  

springcloud 是Hoxton.SR9 

springboot 是2.3.2.RELEASE

springcloudalibaba 是 2.2.5 RELEASE

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.3.2.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Hoxton.SR9</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-dependencies</artifactId>
            <version>2.2.5.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
        </dependency>
    </dependencies>
</dependencyManagement>
  • 2.两种项目配置

一、dubbo项目 -- 针对dubboservice

pom.xml依赖添加

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-apache-dubbo-adapter</artifactId> 
</dependency>
spring-cloud-starter-alibaba-sentinel sentinel集成
sentinel-apache-dubbo-adapter 推送dubbo信息

application.yml配置 

spring:
  application:
    name: sdnro-stock
  profiles:
    include: service
  cloud:
    sentinel:
      transport:
        dashboard: 192.168.0.81:18000 指定dashboard服务
        heartbeat-interval-ms: 500
     eager: true


二、web项目 -- 针对controller


pom.xml

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-transport-simple-http</artifactId>
</dependency>
<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-annotation-aspectj</artifactId>
</dependency>
sentinel-transport-simple-http 普通http请求发送dashboard
sentinel-annotation-aspectj 注解支持

添加aop

@Configuration
public class SentinelAspectConfig {
    @Bean
    public SentinelResourceAspect sentinelResourceAspect() {
        return new SentinelResourceAspect();
    }
}


application.yml

spring:
  application:
    name: sdnro-web
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.0.80:8848
    sentinel:
      transport:
        dashboard: 192.168.0.81:18000
        heartbeat-interval-ms: 500
        port: 8719  -- 与dashboard通信接口 这里可以不指定默认8719
      eager: true


controller上 添加注解 

如下,也会注册自定义资源到sentinel上,并且使用fallback可以指定熔断与限流的处理

当然此时sentinel 上会显示两条

一个是test 一个是/create  会重复

@SentinelResource(value = "test",fallback = "xxx")
@PostMapping("/create")
public String createOrder() {
。。。。。
}

此篇文章就不多说了,还可以根据sentinel抛出的blockException做各种处理



乐享:知识积累,快乐无限。