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

整合项目

创建一个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
在这里插入图片描述
设置降级规则:
在这里插入图片描述
效果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jq1223

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值