sentinel微服务部署

一.启动nacos和redis

1.查看是否有nacos和redis

复制代码
docker ps -a

2.启动nacos和redis

复制代码
docker start nacos
docker start redis-6379
docker ps   

二.使用openfeign项目

这里看我另一个博客OpenFeign微服务部署-CSDN博客,我把SpringSessiondemo复制后改为sentinel1 ,SpringSessiondemo1复制后改为sentinel2

1.下载sentinel

下载地址:Releases · alibaba/Sentinel · GitHub

注意版本号对应

2.启动sentinel

输入以下指令启动

XML 复制代码
java -jar sentinel-dashboard.jar

启动后别把这个窗口关了

3.给两个项目都添加如下依赖

XML 复制代码
<dependency>
  <groupId>com.alibaba.cloud</groupId>
  <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>

<dependency>
  <groupId>com.alibaba.csp</groupId>
  <artifactId>sentinel-web-servlet</artifactId>
</dependency>

4.登录sentinel

登录后的界面

5.给两个项目的application.yml配置文件里添加如下配置

这里建议是不要有黄条,有黄条可能出错

XML 复制代码
 cloud:
     sentinel:
       transport:
        dashboard: localhost:8080

6.启动两个项目并访问项目

7.访问sentinel网站,设置限流

一下一下点击访问的时候,还可以看见正常响应

三.自定义流控响应

对第一个项目进行修改

1.添加过滤器SentinelFilterConfig

java 复制代码
package com.jr.config;

import com.alibaba.csp.sentinel.adapter.servlet.CommonFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;

@Configuration
public class SentinelFilterConfig {

    @Bean
    public FilterRegistrationBean<Filter> filterFilterRegistrationBean(){
        FilterRegistrationBean<Filter> result = new FilterRegistrationBean<>(new CommonFilter());
        result.addUrlPatterns("/*");
        return result;
    }
}

2.添加配置类SentinelConfig

java 复制代码
package com.jr.config;

import com.alibaba.csp.sentinel.adapter.servlet.callback.WebCallbackManager;
import com.alibaba.fastjson.JSON;
import com.jr.util.Result;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SentinelConfig {

    public SentinelConfig() {
        WebCallbackManager.setUrlBlockHandler((request, response, e) -> {
            Result error = Result.error();
            error.setMessage("被限流了!");
            response.setCharacterEncoding("UTF-8");
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().write(JSON.toJSONString(error));
        });
    }
}

3.启动运行

你重新启动要重新在sentinel网站重新设置一下阈值

四.熔断feign(第一个项目sentinel1)

1.在第一个项目sentinel1工程里 开启feign的sentinel,写properties文件中可以,写在yaml文件也可以

XML 复制代码
feign.sentinel.enabled=true

2.添加feign接口实现类

java 复制代码
package com.jr.feign.impl;

import com.jr.entry.Score;
import com.jr.entry.UserDto;
import com.jr.feign.ScoreFeign;
import com.jr.util.Result;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ScoreFeignImpl implements ScoreFeign {
    @Override
    public Result info() {
        List<Score> list = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            Score score = new Score();
            score.setName("name" + i);
            score.setScore(99.99);
            list.add(score);
        }
        return  Result.ok().put("data",list);
    }

    @Override
    public Result id(String id) {
        return null;
    }

    @Override
    public Result add(UserDto user) {
        return null;
    }
}

3.修改feign接口注解

java 复制代码
@FeignClient(value = "demo-sentinel-s", fallback = ScoreFeignImpl.class) //fallback 一旦出现熔断,要走哪个类。

4查看运行结果

关掉第二个sentinel2工程,模拟宕机效果。在使用sentinel1工程去访问sentinel2工程,就可以看见熔断处理了。当遇到宕机的时候,就访问了自己工程里的feign实现类方法。

五.熔断资源

1.try方式

在第一个工程sentinel1工程的Result工具类里写以下代码

java 复制代码
 public Result setMessage(String message) {
        this.message = message;
        return this;
    }

在第一个工程sentinel1工程的UserController里,添加如下方法

java 复制代码
   @GetMapping("/try")
    public Result trySources(){
        String sourcesName = "testTry";
        try(Entry entry = SphU.entry(sourcesName)) { //SphU.entry方法通过传入资源名称和其他参数来获取访问令牌。如果获取到令牌,则可以访问目标资源;如果没有获取到令牌,则无法访问对应资源。
            return Result.ok();
        } catch (BlockException e) {
            return Result.error().setMessage("被限流了!");
        }
    }

注意Entry引包

java 复制代码
import com.alibaba.csp.sentinel.Entry;

重启两个项目,运行一下看效果,记得在snetinel网站重新设置一下阈值。就是自己写的方法,也被限流了。

再到sentinel网站设置try的阈值

2.注解方式

再到sentinel网站设置annptation的阈值

到此结束,希望能帮到大家

相关推荐
咩咩啃树皮6 小时前
第40篇:Vue3组件化开发精讲——组件拆分、复用、父子通信、工程化架构
java·前端·架构
鱟鲥鳚7 小时前
Spring Boot 集成 LangChain4j:从模型调用到 Tool Calling(Demo版)
java·spring boot
大模型码小白8 小时前
【Python零基础教程】继承、多态与魔法函数:面向对象编程三大核心特性详解
java·大数据·开发语言·人工智能·python·ai编程
CS创新实验室9 小时前
量子计算机的操作系统:原理、实践与经典架构之对比
架构·操作系统·量子计算
腾渊信息科技公司9 小时前
Spring Boot对接MES实战:视觉检测数据自动同步方案
java·人工智能·spring boot·后端·计算机视觉·ai·软件需求
人间凡尔赛9 小时前
WAIC 2026 落幕,AI Agent 开发已进入 Harness 架构时代
人工智能·架构
极客侃科技10 小时前
制造企业 MES/APS 选型:SAP PP/DS 集成、ERP-MES 边界划分与一体化架构要点
运维·架构·制造
晏宁科技YaningAI10 小时前
VoIP系统的工程实现模型:从信令控制到媒体传输的完整架构解析
网络·人工智能·架构·系统架构·信息与通信
爱笑的源码基地10 小时前
高并发 Redis 缓存门诊HIS系统源码,含财务统计药房进销存
java·程序·门诊系统·诊所系统·云诊所源码
wuqingshun31415910 小时前
TCP超时重传机制是为了解决什么问题?
java