整合项目

创建一个SpringBoot项目 sentinel

引入子相关依赖

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.jq</groupId>
<artifactId>test</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.jq</groupId>
<artifactId>sentinel</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sentinel</name>
<description>Demo project for Spring Boot</description>

<dependencies>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>

<!-- cloud-dubbo依赖 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>


<!-- 引入provider-api -->
<dependency>
<groupId>com.jq</groupId>
<artifactId>server-dubbo-provider-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>

</dependencies>

</project>

application.yml配置

server:
port: 10005

spring:
application:
name: sentinel
cloud:
nacos:
discovery:
server-addr: 192.168.230.137:8848
#连接sentinel
sentinel:
transport:
port: 10005
dashboard: http://192.168.230.137:8858

dubbo:
scan:
base-packages: com.jq.service
protocol:
name: dubbo
port: 19005
registry:
address: spring-cloud:// 192.168.230.137:8848

启动类:加@EnableDiscoveryClient

package com.jq;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableDiscoveryClient
public class SentinelApplication {

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

}

**创建控制层 **

package com.jq.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.jq.config.MyBlockHandlerClass;
import com.jq.service.IDubboProviderService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
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 java.util.concurrent.TimeUnit;

@RequestMapping("Sentine")
@RestController
public class SentineController {



/***
*
* 方式一
*
*
* */


@org.apache.dubbo.config.annotation.Reference
//@org.apache.dubbo.config.annotation.Reference:生成代理对象,就像调用本地方法一样调用远程业务方法
private IDubboProviderService iDubboProviderService ;


/**
* dubbo 远程调用
* @param
* @return
*/
@GetMapping("/getDubboRemoteInfo")
@SentinelResource(value = "getDubboRemoteInfo",
blockHandlerClass = CustomerBlockHandler.class, blockHandler = "handlerException2")
public String getDubboRemoteInfo(){
return iDubboProviderService.getRemoteDubboInfo();
}

/***
*
* 方式二
*
*
* */


@Autowired
private RestTemplate restTemplate;

@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}

@GetMapping("/consumer")
@SentinelResource(value = "consumer",
blockHandlerClass = CustomerBlockHandler.class, blockHandler = "handlerException2")
public String test() {
//server 服务名称
//hell控制层
//getDubboRemoteInfo 方法
return restTemplate.getForObject("http://server/hell/getDubboRemoteInfo",String.class);
}




}

自定义处理类

package com.jq.config;

import com.alibaba.csp.sentinel.slots.block.BlockException;

public class CustomerBlockHandler {


public static String handlerException(BlockException exception) {
return "按照客户自定义的Glogal 全局异常处理 ---- 1";
}

public static String handlerException2(BlockException exception) {
return "按照客户自定义的Glogal 全局异常处理 ---2";
}

}

浏览器访问访问消息提供者:http://localhost:10005/Sentine/getDubboRemoteInfo

SpringCloud + SpringBoot +Dubbo +Sentinel 整合 (4)_spring


设置降级规则:

SpringCloud + SpringBoot +Dubbo +Sentinel 整合 (4)_ide_02


效果:

SpringCloud + SpringBoot +Dubbo +Sentinel 整合 (4)_ide_03