Sentinel生产环境实践

来自杨三超wiki

1、功能描述

API 限流

sentinel默认会探测所有API,但需要注意,Sentinel 会在客户端首次调用时候进行初始化,开始向控制台发送心跳包。因此需要确保客户端有访问量,才能在控制台上看到监控数据。

RPC限流粒度

可以是服务接口和服务方法两种粒度:

  • 服务接口:resourceName 为 接口全限定名,如 com.alibaba.csp.sentinel.demo.dubbo.FooService
  • 服务方法:resourceName 为 接口全限定名:方法签名,如 com.alibaba.csp.sentinel.demo.dubbo.FooService:sayHello(java.lang.String)

存在问题

1、规则持久化

支持基于 ZooKeeper / Nacos / Apollo 的数据源规则持久化,但需要改造;

2、统一异常处理;

需要自己改造;

3、监控数据持久化;

需要自己改造;

2、Dashboard改造部署

1、修改路由规则

第一步:修改dashboad.flowV1为dashboad.flow;

Sentinel生产环境实践_第1张图片

第二步:FlowServiceV1修改为FlowServiceV2;

Sentinel生产环境实践_第2张图片

2、支持规则持久化

sentinel支持限流、降级、热点、系统、授权五种规则,目前常用的功能是限流和降级、基于zookeeper实现持久化规则;

dashboad默认规则都是内存,同时需要修改对应的Controller;

Sentinel生产环境实践_第3张图片

Sentinel生产环境实践_第4张图片

 

3、监控日志持久化    

默认监控日志仅在内容保留5分钟

 

Sentinel生产环境实践_第5张图片

4、部署

  • 编译jar包:https://github.com/alibaba/Sentinel/releases
  • 启动:java -Dserver.port=8100 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.6.2.jar 
    • -Dserver.port=8080 用于指定 Sentinel 控制台端口为 8100

  • 扩展阅读 Sentinel 控制台: https://github.com/alibaba/Sentinel/wiki/%E6%8E%A7%E5%88%B6%E5%8F%B0

3、服务如何使用

1、添加相关pom

1.1 springboot2.x添加pom

<dependency>

    <groupId>com.alibaba.cloudgroupId>

    <artifactId>spring-cloud-starter-alibaba-sentinelartifactId>

    <version>2.1.0.RELEASEversion>

    <exclusions>

        <exclusion>

            <artifactId>sentinel-coreartifactId>

            <groupId>com.alibaba.cspgroupId>

        exclusion>

        <exclusion>

            <artifactId>fastjsonartifactId>

            <groupId>com.alibabagroupId>

        exclusion>

    exclusions>

dependency>

 

<dependency>

    <groupId>com.alibaba.cspgroupId>

    <artifactId>sentinel-datasource-zookeeperartifactId>

    <version>1.6.3version>

    <exclusions>

        <exclusion>

            <artifactId>sentinel-datasource-extensionartifactId>

            <groupId>com.alibaba.cspgroupId>

        exclusion>

        <exclusion>

            <artifactId>zookeeperartifactId>

            <groupId>org.apache.zookeepergroupId>

        exclusion>

    exclusions>

dependency>

<dependency>

    <groupId>com.alibaba.cspgroupId>

    <artifactId>sentinel-dubbo-adapterartifactId>

    <version>1.6.3version>

dependency>

 

<dependency>

    <groupId>org.apache.curatorgroupId>

    <artifactId>curator-frameworkartifactId>

    <version>2.11.0version>

dependency>

<dependency>

    <groupId>org.apache.curatorgroupId>

    <artifactId>curator-clientartifactId>

    <version>2.11.0version>

dependency>

<dependency>

    <groupId>org.apache.curatorgroupId>

    <artifactId>curator-recipesartifactId>

    <version>2.11.0version>

dependency>

 

 

1.2 springboot1.5.x添加pom

 折叠源码

<dependency>

    <groupId>com.alibaba.cloudgroupId>

    <artifactId>spring-cloud-starter-alibaba-sentinelartifactId>

    <version>1.5.1.RELEASEversion>

dependency>

<dependency>

    <groupId>com.alibaba.cspgroupId>

    <artifactId>sentinel-cluster-client-defaultartifactId>

    <version>1.5.1version>

dependency>

<dependency>

    <groupId>com.alibaba.cspgroupId>

    <artifactId>sentinel-cluster-common-defaultartifactId>

    <version>1.5.1version>

dependency>

<dependency>

    <groupId>com.alibaba.cspgroupId>

    <artifactId>sentinel-cluster-server-defaultartifactId>

    <version>1.5.1version>

dependency>

<dependency>

    <groupId>com.alibaba.cspgroupId>

    <artifactId>sentinel-datasource-extensionartifactId>

    <version>1.5.1version>

dependency>

<dependency>

    <groupId>com.alibaba.cspgroupId>

    <artifactId>sentinel-parameter-flow-controlartifactId>

    <version>1.5.1version>

dependency>

<dependency>

    <groupId>com.alibaba.cspgroupId>

    <artifactId>sentinel-transport-commonartifactId>

    <version>1.5.1version>

dependency>

<dependency>

    <groupId>com.alibaba.cspgroupId>

    <artifactId>sentinel-transport-simple-httpartifactId>

    <version>1.5.1version>

dependency>

<dependency>

    <groupId>com.alibaba.cspgroupId>

    <artifactId>sentinel-web-servletartifactId>

    <version>1.5.1version>

dependency>

<dependency>

    <groupId>com.alibaba.cspgroupId>

    <artifactId>sentinel-coreartifactId>

    <version>1.5.1version>

dependency>

<dependency>

    <groupId>com.alibaba.cspgroupId>

    <artifactId>sentinel-annotation-aspectjartifactId>

    <version>1.5.1version>

dependency>

 

<dependency>

    <groupId>com.alibaba.cspgroupId>

    <artifactId>sentinel-datasource-zookeeperartifactId>

    <version>1.5.1version>

    <exclusions>

        <exclusion>

            <artifactId>zookeeperartifactId>

            <groupId>org.apache.zookeepergroupId>

        exclusion>

    exclusions>

dependency>

 

<dependency>

    <groupId>com.alibaba.cspgroupId>

    <artifactId>sentinel-dubbo-adapterartifactId>

    <version>1.5.1version>

dependency>

 

2、添加配置

添加配置:

cloud:

  sentinel:

    transport:

      port: 8731

      dashboard: http://127.0.0.1:9001

    datasource:

      ds:

        zk:

          path: /

          serverAddr: zookeeper:2181

          groupId: SENTINEL_GROUP

          dataId: ${spring.application.name}-flow-rules

          ruleType: FLOW

      ds1:

        zk:

          path: /

          serverAddr: zookeeper:2181

          groupId: SENTINEL_GROUP

          dataId: ${spring.application.name}-degrade-rules

          ruleType: DEGRADE

3、添加统一异常

API统一异常

@Configuration

public class MyFallbackAPIConfig {

    /**

     * URL block handler.

     */

    private static volatile UrlBlockHandler urlBlockHandler = new DefaultUrlBlockHandler();

 

    static {

        WebCallbackManager.setUrlBlockHandler(urlBlockHandler);

    }

 

    public static class DefaultUrlBlockHandler implements UrlBlockHandler {

 

        private static void writeDefaultBlockedPage(HttpServletResponse response) throws IOException {

            response.setCharacterEncoding("UTF-8");

            response.setHeader("Content-Type""application/json; charset=utf-8");

 

            Map responseObject = new HashMap<>(3);

            responseObject.put("code",10001);

            responseObject.put("message","系统繁忙,请稍后再试!");

            responseObject.put("body",new Object());

            JSONObject responseJSONObject = (JSONObject)JSONObject.toJSON(responseObject);

 

            PrintWriter out = response.getWriter();

            out.write(responseJSONObject.toString());

            out.flush();

            out.close();

        }

 

        @Override

        public void blocked(HttpServletRequest request, HttpServletResponse response, BlockException ex)

                throws IOException {

            // Directly redirect to the default flow control (blocked) page or customized block page.

            blockRequest(request, response);

        }

 

        public static void blockRequest(HttpServletRequest request, HttpServletResponse response) throws IOException {

            StringBuffer url = request.getRequestURL();

 

            if ("GET".equals(request.getMethod()) && StringUtil.isNotBlank(request.getQueryString())) {

                url.append("?").append(request.getQueryString());

            }

 

            if (StringUtil.isBlank(WebServletConfig.getBlockPage())) {

                writeDefaultBlockedPage(response);

            else {

                String redirectUrl = WebServletConfig.getBlockPage() + "?http_referer=" + url.toString();

                // Redirect to the customized block page.

                response.sendRedirect(redirectUrl);

            }

        }

    }

}

 

dubbo统一异常

@Component

public class MyFallbackDubboConfig  {

    /**

     * URL block handler.

     */

    private static volatile DefaultDubboBlockHandler dubboFallback = new DefaultDubboBlockHandler();

 

    static {

        DubboFallbackRegistry.setConsumerFallback(dubboFallback);

    }

 

    public static class DefaultDubboBlockHandler implements DubboFallback {

 

        @Override

        public Result handle(Invoker invoker, Invocation invocation, BlockException e) {

            return new RpcResult(null);

        }

    }

 

}

你可能感兴趣的:(流量控制,熔断降级)