自学内容网 自学内容网

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 

输入以下指令启动 

java -jar sentinel-dashboard.jar

启动后别把这个窗口关了  

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

<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配置文件里添加如下配置

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

 cloud:
     sentinel:
       transport:
        dashboard: localhost:8080

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

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

  

 

 

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

 

三.自定义流控响应

对第一个项目进行修改

1.添加过滤器SentinelFilterConfig 

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 

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文件也可以

feign.sentinel.enabled=true

 

2.添加feign接口实现类

 

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接口注解

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

4查看运行结果

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

五.熔断资源

1.try方式

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

 public Result setMessage(String message) {
        this.message = message;
        return this;
    }

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

   @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引包  

import com.alibaba.csp.sentinel.Entry;

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

再到sentinel网站设置try的阈值 

 

 

2.注解方式

再到sentinel网站设置annptation的阈值  

 

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


原文地址:https://blog.csdn.net/qq_67576286/article/details/142648554

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!